chg: [user-settings:edit] Prevent assigning a setting to another user
parent
d46143a37f
commit
08d2e193dd
|
@ -118,13 +118,19 @@ class UserSettingsController extends AppController
|
||||||
} else {
|
} else {
|
||||||
$validUsers = $this->Users->find('list')->select(['id', 'username'])->order(['username' => 'asc'])->all()->toArray();
|
$validUsers = $this->Users->find('list')->select(['id', 'username'])->order(['username' => 'asc'])->all()->toArray();
|
||||||
}
|
}
|
||||||
|
$dropdownData = [
|
||||||
|
'user' => [$entity->user_id => $validUsers[$entity->user_id]],
|
||||||
|
];
|
||||||
|
|
||||||
$entity = $this->CRUD->edit($id, [
|
$entity = $this->CRUD->edit($id, [
|
||||||
'redirect' => ['action' => 'index', $entity->user_id],
|
'redirect' => ['action' => 'index', $entity->user_id],
|
||||||
'beforeSave' => function ($data) use ($validUsers) {
|
'beforeSave' => function ($data) use ($validUsers, $entity) {
|
||||||
if (!in_array($data['user_id'], array_keys($validUsers))) {
|
if (!in_array($data['user_id'], array_keys($validUsers))) {
|
||||||
throw new MethodNotAllowedException(__('You cannot edit the given user.'));
|
throw new MethodNotAllowedException(__('You cannot edit the given user.'));
|
||||||
}
|
}
|
||||||
|
if ($data['user_id'] != $entity->user_id) {
|
||||||
|
throw new MethodNotAllowedException(__('You cannot assign the setting to a different user.'));
|
||||||
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
@ -132,11 +138,9 @@ class UserSettingsController extends AppController
|
||||||
if (!empty($responsePayload)) {
|
if (!empty($responsePayload)) {
|
||||||
return $responsePayload;
|
return $responsePayload;
|
||||||
}
|
}
|
||||||
$dropdownData = [
|
|
||||||
'user' => $validUsers,
|
|
||||||
];
|
|
||||||
$this->set(compact('dropdownData'));
|
$this->set(compact('dropdownData'));
|
||||||
$this->set('user_id', $this->entity->user_id);
|
$this->set('user_id', $this->entity->user_id);
|
||||||
|
$this->set('is_edit', true);
|
||||||
$this->render('add');
|
$this->render('add');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
'label' => __('User'),
|
'label' => __('User'),
|
||||||
'options' => $dropdownData['user'],
|
'options' => $dropdownData['user'],
|
||||||
'value' => !is_null($user_id) ? $user_id : '',
|
'value' => !is_null($user_id) ? $user_id : '',
|
||||||
|
'disabled' => !empty($is_edit),
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'field' => 'name',
|
'field' => 'name',
|
||||||
|
|
Loading…
Reference in New Issue