Merge branch 'main' into develop

cli-modification-summary
iglocska 2022-01-25 15:59:31 +01:00
commit acc9c94baa
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
4 changed files with 107 additions and 20 deletions

View File

@ -157,9 +157,6 @@ class CRUDComponent extends Component
{ {
$this->getMetaTemplates(); $this->getMetaTemplates();
$data = $this->Table->newEmptyEntity(); $data = $this->Table->newEmptyEntity();
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
if ($this->request->is('post')) { if ($this->request->is('post')) {
$patchEntityParams = [ $patchEntityParams = [
'associated' => [], 'associated' => [],
@ -223,6 +220,9 @@ class CRUDComponent extends Component
} }
} }
} }
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
$this->Controller->entity = $data; $this->Controller->entity = $data;
$this->Controller->set('entity', $data); $this->Controller->set('entity', $data);
} }
@ -295,21 +295,18 @@ class CRUDComponent extends Component
$data->where($params['conditions']); $data->where($params['conditions']);
} }
$data = $data->first(); $data = $data->first();
if (isset($params['afterFind'])) {
$data = $params['afterFind']($data, $params);
}
if (empty($data)) { if (empty($data)) {
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias)); throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
} }
$data = $this->getMetaFields($id, $data); $data = $this->getMetaFields($id, $data);
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
if ($this->request->is(['post', 'put'])) { if ($this->request->is(['post', 'put'])) {
$patchEntityParams = [ $patchEntityParams = [
'associated' => [] 'associated' => []
]; ];
$input = $this->__massageInput($params); $input = $this->__massageInput($params);
if (!empty($params['fields'])) {
$patchEntityParams['fields'] = $params['fields'];
}
$data = $this->Table->patchEntity($data, $input, $patchEntityParams); $data = $this->Table->patchEntity($data, $input, $patchEntityParams);
if (isset($params['beforeSave'])) { if (isset($params['beforeSave'])) {
$data = $params['beforeSave']($data); $data = $params['beforeSave']($data);
@ -352,6 +349,9 @@ class CRUDComponent extends Component
} }
} }
} }
if (!empty($params['fields'])) {
$this->Controller->set('fields', $params['fields']);
}
$this->Controller->entity = $data; $this->Controller->entity = $data;
$this->Controller->set('entity', $data); $this->Controller->set('entity', $data);
} }
@ -469,7 +469,11 @@ class CRUDComponent extends Component
} }
$data = $data->first(); $data = $data->first();
if (isset($params['beforeSave'])) { if (isset($params['beforeSave'])) {
$data = $params['beforeSave']($data); try {
$data = $params['beforeSave']($data);
} catch (Exception $e) {
$data = false;
}
} }
if (!empty($data)) { if (!empty($data)) {
$success = $this->Table->delete($data); $success = $this->Table->delete($data);

View File

@ -33,16 +33,30 @@ class UsersController extends AppController
if (!empty($responsePayload)) { if (!empty($responsePayload)) {
return $responsePayload; return $responsePayload;
} }
$this->set(
'validRoles',
$this->Users->Roles->find('list')->select(['id', 'name'])->order(['name' => 'asc'])->where(['perm_admin' => 0])->all()->toArray()
);
$this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate'); $this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate');
} }
public function add() public function add()
{ {
$currentUser = $this->ACL->getUser(); $currentUser = $this->ACL->getUser();
$validRoles = [];
if (!$currentUser['role']['perm_admin']) {
$validRoles = $this->Users->Roles->find('list')->select(['id', 'name'])->order(['name' => 'asc'])->where(['perm_admin' => 0])->all()->toArray();
} else {
$validRoles = $this->Users->Roles->find('list')->order(['name' => 'asc'])->all()->toArray();
}
$this->CRUD->add([ $this->CRUD->add([
'beforeSave' => function($data) use ($currentUser) { 'beforeSave' => function($data) use ($currentUser, $validRoles) {
if (!$currentUser['role']['perm_admin']) { if (!$currentUser['role']['perm_admin']) {
$data['organisation_id'] = $currentUser['organisation_id']; $data['organisation_id'] = $currentUser['organisation_id'];
if (!in_array($data['role_id'], array_keys($validRoles))) {
throw new MethodNotAllowedException(__('You do not have permission to assign that role.'));
}
} }
$this->Users->enrollUserRouter($data); $this->Users->enrollUserRouter($data);
return $data; return $data;
@ -65,9 +79,7 @@ class UsersController extends AppController
$org_conditions = ['id' => $currentUser['organisation_id']]; $org_conditions = ['id' => $currentUser['organisation_id']];
} }
$dropdownData = [ $dropdownData = [
'role' => $this->Users->Roles->find('list', [ 'role' => $validRoles,
'sort' => ['name' => 'asc']
]),
'individual' => $this->Users->Individuals->find('list', [ 'individual' => $this->Users->Individuals->find('list', [
'sort' => ['email' => 'asc'] 'sort' => ['email' => 'asc']
]), ]),
@ -98,6 +110,12 @@ class UsersController extends AppController
public function edit($id = false) public function edit($id = false)
{ {
$currentUser = $this->ACL->getUser(); $currentUser = $this->ACL->getUser();
$validRoles = [];
if (!$currentUser['role']['perm_admin']) {
$validRoles = $this->Users->Roles->find('list')->select(['id', 'name'])->order(['name' => 'asc'])->where(['perm_admin' => 0])->all()->toArray();
} else {
$validRoles = $this->Users->Roles->find('list')->order(['name' => 'asc'])->all()->toArray();
}
if (empty($id)) { if (empty($id)) {
$id = $currentUser['id']; $id = $currentUser['id'];
} else { } else {
@ -128,6 +146,20 @@ class UsersController extends AppController
$params['fields'][] = 'role_id'; $params['fields'][] = 'role_id';
$params['fields'][] = 'organisation_id'; $params['fields'][] = 'organisation_id';
$params['fields'][] = 'disabled'; $params['fields'][] = 'disabled';
} else if (!empty($this->ACL->getUser()['role']['perm_org_admin'])) {
$params['fields'][] = 'username';
$params['fields'][] = 'role_id';
$params['fields'][] = 'disabled';
if (!$currentUser['role']['perm_admin']) {
$params['afterFind'] = function ($data, &$params) use ($currentUser, $validRoles) {
if (!$currentUser['role']['perm_admin'] && $currentUser['role']['perm_org_admin']) {
if (!in_array($data['role_id'], array_keys($validRoles))) {
throw new MethodNotAllowedException(__('You cannot edit the given privileged user.'));
}
}
return $data;
};
}
} }
$this->CRUD->edit($id, $params); $this->CRUD->edit($id, $params);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
@ -135,9 +167,7 @@ class UsersController extends AppController
return $responsePayload; return $responsePayload;
} }
$dropdownData = [ $dropdownData = [
'role' => $this->Users->Roles->find('list', [ 'role' => $validRoles,
'sort' => ['name' => 'asc']
]),
'individual' => $this->Users->Individuals->find('list', [ 'individual' => $this->Users->Individuals->find('list', [
'sort' => ['email' => 'asc'] 'sort' => ['email' => 'asc']
]), ]),
@ -161,6 +191,23 @@ class UsersController extends AppController
public function delete($id) public function delete($id)
{ {
$validRoles = [];
if (!$currentUser['role']['perm_admin']) {
$validRoles = $this->Users->Roles->find('list')->order(['name' => 'asc'])->all()->toArray();
}
$params = [
'beforeSave' => function($data) use ($currentUser, $validRoles) {
if (!$currentUser['role']['perm_admin']) {
if ($data['organisation_id'] !== $currentUser['organisation_id']) {
throw new MethodNotAllowedException(__('You do not have permission to remove the given user.'));
}
if (!in_array($data['role_id'], array_keys($validRoles))) {
throw new MethodNotAllowedException(__('You do not have permission to remove the given user.'));
}
}
return $data;
}
];
$this->CRUD->delete($id); $this->CRUD->delete($id);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) { if (!empty($responsePayload)) {

View File

@ -1,4 +1,4 @@
{ {
"version": "0.1", "version": "1.3",
"application": "Cerebrate" "application": "Cerebrate"
} }

View File

@ -102,12 +102,48 @@ echo $this->element('genericElements/IndexTable/index_table', [
[ [
'open_modal' => '/users/edit/[onclick_params_data_path]', 'open_modal' => '/users/edit/[onclick_params_data_path]',
'modal_params_data_path' => 'id', 'modal_params_data_path' => 'id',
'icon' => 'edit' 'icon' => 'edit',
'complex_requirement' => [
'options' => [
'datapath' => [
'role_id' => 'role_id'
]
],
'function' => function ($row, $options) use ($loggedUser, $validRoles) {
if (empty($loggedUser['role']['perm_admin'])) {
if (empty($loggedUser['role']['perm_org_admin'])) {
return false;
}
if (!isset($validRoles[$options['datapath']['role_id']])) {
return false;
}
}
return true;
}
]
], ],
[ [
'open_modal' => '/users/delete/[onclick_params_data_path]', 'open_modal' => '/users/delete/[onclick_params_data_path]',
'modal_params_data_path' => 'id', 'modal_params_data_path' => 'id',
'icon' => 'trash' 'icon' => 'trash',
'complex_requirement' => [
'options' => [
'datapath' => [
'role_id' => 'role_id'
]
],
'function' => function ($row, $options) use ($loggedUser, $validRoles) {
if (empty($loggedUser['role']['perm_admin'])) {
if (empty($loggedUser['role']['perm_org_admin'])) {
return false;
}
if (!isset($validRoles[$options['datapath']['role_id']])) {
return false;
}
}
return true;
}
]
], ],
] ]
] ]