From 6d5684f597eeb1d31339806b2ef3c026c6ffe59d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Parient=C3=A9?= <41431456+arthurpar06@users.noreply.github.com> Date: Wed, 15 May 2024 17:23:20 +0200 Subject: [PATCH] Add internal user fields (#1805) * Add internal user fields * Apply fixes from StyleCI --------- Co-authored-by: arthurpar06 --- ..._15_144813_add_internal_to_user_fields.php | 27 +++++++++++++++++++ .../Controllers/Admin/UserFieldController.php | 17 +++++++++++- .../Controllers/Auth/RegisterController.php | 5 ++-- .../Frontend/ProfileController.php | 4 +-- app/Models/UserField.php | 2 ++ app/Repositories/UserRepository.php | 17 +++++++----- 6 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 app/Database/migrations/2024_05_15_144813_add_internal_to_user_fields.php diff --git a/app/Database/migrations/2024_05_15_144813_add_internal_to_user_fields.php b/app/Database/migrations/2024_05_15_144813_add_internal_to_user_fields.php new file mode 100644 index 000000000..1f8390177 --- /dev/null +++ b/app/Database/migrations/2024_05_15_144813_add_internal_to_user_fields.php @@ -0,0 +1,27 @@ +boolean('internal')->after('private')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('user_fields', function (Blueprint $table) { + $table->dropColumn('internal'); + }); + } +}; diff --git a/app/Http/Controllers/Admin/UserFieldController.php b/app/Http/Controllers/Admin/UserFieldController.php index 814462096..3f07363dd 100644 --- a/app/Http/Controllers/Admin/UserFieldController.php +++ b/app/Http/Controllers/Admin/UserFieldController.php @@ -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]); } @@ -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]); } @@ -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.'); @@ -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')); diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 6275e50f7..52a3bef1c 100755 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -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' => [], @@ -124,6 +124,7 @@ protected function validator(array $data): Validator $userFields = UserField::where([ 'show_on_registration' => true, 'required' => true, + 'internal' => false, 'active' => true, ])->get(); @@ -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([ diff --git a/app/Http/Controllers/Frontend/ProfileController.php b/app/Http/Controllers/Frontend/ProfileController.php index 9b940fa36..ad0dd3d30 100644 --- a/app/Http/Controllers/Frontend/ProfileController.php +++ b/app/Http/Controllers/Frontend/ProfileController.php @@ -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'; } @@ -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([ diff --git a/app/Models/UserField.php b/app/Models/UserField.php index 098a42649..226c30547 100644 --- a/app/Models/UserField.php +++ b/app/Models/UserField.php @@ -23,6 +23,7 @@ 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', ]; @@ -30,6 +31,7 @@ class UserField extends Model 'show_on_registration' => 'boolean', 'required' => 'boolean', 'private' => 'boolean', + 'internal' => 'boolean', 'active' => 'boolean', ]; diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 86aa20ab1..decbb3b45 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -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) {