Skip to content

Commit

Permalink
Add internal user fields (nabeelio#1805)
Browse files Browse the repository at this point in the history
* Add internal user fields

* Apply fixes from StyleCI

---------

Co-authored-by: arthurpar06 <arthurpar06@users.noreply.github.com>
  • Loading branch information
arthurpar06 and arthurpar06 authored May 15, 2024
1 parent 7c0eb7d commit 6d5684f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('user_fields', function (Blueprint $table) {
$table->boolean('internal')->after('private')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_fields', function (Blueprint $table) {
$table->dropColumn('internal');
});
}
};
17 changes: 16 additions & 1 deletion app/Http/Controllers/Admin/UserFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
public function index(Request $request): View
{
$this->userFieldRepo->pushCriteria(new RequestCriteria($request));
$fields = $this->userFieldRepo->all();
$fields = $this->userFieldRepo->where('internal', false)->get();

return view('admin.userfields.index', ['fields' => $fields]);
}
Expand Down Expand Up @@ -99,6 +99,11 @@ public function edit(int $id): RedirectResponse|View
return redirect(route('admin.userfields.index'));
}

if ($field->internal) {
Flash::error('You cannot edit an internal user field');
return redirect(route('admin.userfields.index'));
}

return view('admin.userfields.edit', ['field' => $field]);
}

Expand All @@ -121,6 +126,11 @@ public function update(int $id, Request $request): RedirectResponse
return redirect(route('admin.userfields.index'));
}

if ($field->internal) {
Flash::error('You cannot edit an internal user field');
return redirect(route('admin.userfields.index'));
}

$this->userFieldRepo->update($request->all(), $id);

Flash::success('Field updated successfully.');
Expand All @@ -142,6 +152,11 @@ public function destroy(int $id): RedirectResponse
return redirect(route('admin.userfields.index'));
}

if ($field->internal) {
Flash::error('You cannot delete an internal user field');
return redirect(route('admin.userfields.index'));
}

if ($this->userFieldRepo->isInUse($id)) {
Flash::error('This field cannot be deleted, it is in use. Deactivate it instead');
return redirect(route('admin.userfields.index'));
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function showRegistrationForm(Request $request): View
}

$airlines = $this->airlineRepo->selectBoxList();
$userFields = UserField::where(['show_on_registration' => true, 'active' => true])->get();
$userFields = UserField::where(['show_on_registration' => true, 'active' => true, 'internal' => false])->get();

return view('auth.register', [
'airports' => [],
Expand Down Expand Up @@ -124,6 +124,7 @@ protected function validator(array $data): Validator
$userFields = UserField::where([
'show_on_registration' => true,
'required' => true,
'internal' => false,
'active' => true,
])->get();

Expand Down Expand Up @@ -215,7 +216,7 @@ protected function create(Request $request): User

Log::info('User registered: ', $user->toArray());

$userFields = UserField::where(['show_on_registration' => true, 'active' => true])->get();
$userFields = UserField::where(['show_on_registration' => true, 'active' => true, 'internal' => false])->get();
foreach ($userFields as $field) {
$field_name = 'field_'.$field->slug;
UserFieldValue::updateOrCreate([
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Frontend/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function update(Request $request): RedirectResponse
'avatar' => 'nullable|mimes:jpeg,png,jpg',
];

$userFields = UserField::where(['show_on_registration' => true, 'required' => true])->get();
$userFields = UserField::where(['show_on_registration' => true, 'required' => true, 'internal' => false])->get();
foreach ($userFields as $field) {
$rules['field_'.$field->slug] = 'required';
}
Expand Down Expand Up @@ -217,7 +217,7 @@ public function update(Request $request): RedirectResponse
}

// Save all of the user fields
$userFields = UserField::all();
$userFields = UserField::where('internal', false)->get();
foreach ($userFields as $field) {
$field_name = 'field_'.$field->slug;
UserFieldValue::updateOrCreate([
Expand Down
2 changes: 2 additions & 0 deletions app/Models/UserField.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class UserField extends Model
'show_on_registration', // Show on the registration form?
'required', // Required to be filled out in registration?
'private', // Whether this is shown on the user's public profile
'internal', // Whether this field is for internal use only (e.g. modules)
'active',
];

protected $casts = [
'show_on_registration' => 'boolean',
'required' => 'boolean',
'private' => 'boolean',
'internal' => 'boolean',
'active' => 'boolean',
];

Expand Down
17 changes: 11 additions & 6 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ public function model()
/**
* Get all of the fields which has the mapped values
*
* @param User $user
* @param bool $only_public_fields Only include the user's public fields
* @param User $user
* @param bool $only_public_fields Only include the user's public fields
* @param mixed $with_internal_fields
*
* @return \App\Models\UserField[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getUserFields(User $user, $only_public_fields = null): Collection
public function getUserFields(User $user, $only_public_fields = null, $with_internal_fields = false): Collection
{
$fields = UserField::when(!$with_internal_fields, function ($query) {
return $query->where('internal', false);
});

if (is_bool($only_public_fields)) {
$fields = UserField::where(['private' => !$only_public_fields])->get();
} else {
$fields = UserField::get();
$fields = $fields->where(['private' => !$only_public_fields]);
}

$fields = $fields->get();

return $fields->map(function ($field, $_) use ($user) {
foreach ($user->fields as $userFieldValue) {
if ($userFieldValue->field->slug === $field->slug) {
Expand Down

0 comments on commit 6d5684f

Please sign in to comment.