fix: [userSettings:add] Aded check to avoid duplicated setting for the same user

refacto/CRUDComponent
Sami Mokaddem 2023-09-05 10:46:25 +02:00
parent a3c23f46d5
commit 883f0eb44a
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 6 additions and 0 deletions

View File

@ -70,6 +70,12 @@ class UserSettingsController extends AppController
$this->CRUD->add([
'redirect' => ['action' => 'index', $user_id],
'beforeSave' => function ($data) use ($currentUser) {
$fakeUser = new \stdClass();
$fakeUser->id = $data['user_id'];
$existingSetting = $this->UserSettings->getSettingByName($fakeUser, $data['name']);
if (!empty($existingSetting)) {
throw new MethodNotAllowedException(__('You cannot create a setting that already exists for the given user.'));
}
if (empty($currentUser['role']['perm_admin'])) {
$data['user_id'] = $currentUser->id;
}