fix: [roles] setting default should be exclusive

- added aftersave action to remove default from other roles
cli-modification-summary
iglocska 2022-01-27 22:06:26 +01:00
parent 1ca0f21b86
commit cf67c3d1f0
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 18 additions and 2 deletions

View File

@ -31,7 +31,15 @@ class RolesController extends AppController
public function add() public function add()
{ {
$this->CRUD->add(); $rolesModel = $this->Roles;
$this->CRUD->add([
'afterSave' => function ($data) use ($rolesModel) {
if ($data['is_default']) {
$rolesModel->query()->update()->set(['is_default' => false])->where(['id !=' => $data->id])->execute();
}
return true;
}
]);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) { if (!empty($responsePayload)) {
return $responsePayload; return $responsePayload;
@ -51,7 +59,15 @@ class RolesController extends AppController
public function edit($id) public function edit($id)
{ {
$this->CRUD->edit($id); $rolesModel = $this->Roles;
$this->CRUD->edit($id, [
'afterSave' => function ($data) use ($rolesModel) {
if ($data['is_default']) {
$rolesModel->query()->update()->set(['is_default' => false])->where(['id !=' => $data->id])->execute();
}
return true;
}
]);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) { if (!empty($responsePayload)) {
return $responsePayload; return $responsePayload;