Merge branch 'develop' of github.com:cerebrate-project/cerebrate into develop
commit
54ee91ba1a
|
@ -157,9 +157,6 @@ class CRUDComponent extends Component
|
|||
{
|
||||
$this->getMetaTemplates();
|
||||
$data = $this->Table->newEmptyEntity();
|
||||
if (!empty($params['fields'])) {
|
||||
$this->Controller->set('fields', $params['fields']);
|
||||
}
|
||||
if ($this->request->is('post')) {
|
||||
$patchEntityParams = [
|
||||
'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->set('entity', $data);
|
||||
}
|
||||
|
@ -295,21 +295,18 @@ class CRUDComponent extends Component
|
|||
$data->where($params['conditions']);
|
||||
}
|
||||
$data = $data->first();
|
||||
if (isset($params['afterFind'])) {
|
||||
$data = $params['afterFind']($data, $params);
|
||||
}
|
||||
if (empty($data)) {
|
||||
throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias));
|
||||
}
|
||||
$data = $this->getMetaFields($id, $data);
|
||||
if (!empty($params['fields'])) {
|
||||
$this->Controller->set('fields', $params['fields']);
|
||||
}
|
||||
if ($this->request->is(['post', 'put'])) {
|
||||
$patchEntityParams = [
|
||||
'associated' => []
|
||||
];
|
||||
$input = $this->__massageInput($params);
|
||||
if (!empty($params['fields'])) {
|
||||
$patchEntityParams['fields'] = $params['fields'];
|
||||
}
|
||||
$data = $this->Table->patchEntity($data, $input, $patchEntityParams);
|
||||
if (isset($params['beforeSave'])) {
|
||||
$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->set('entity', $data);
|
||||
}
|
||||
|
@ -469,7 +469,11 @@ class CRUDComponent extends Component
|
|||
}
|
||||
$data = $data->first();
|
||||
if (isset($params['beforeSave'])) {
|
||||
$data = $params['beforeSave']($data);
|
||||
try {
|
||||
$data = $params['beforeSave']($data);
|
||||
} catch (Exception $e) {
|
||||
$data = false;
|
||||
}
|
||||
}
|
||||
if (!empty($data)) {
|
||||
$success = $this->Table->delete($data);
|
||||
|
|
|
@ -71,6 +71,7 @@ class SharingGroupsController extends AppController
|
|||
if (empty($currentUser['role']['perm_admin'])) {
|
||||
$params['conditions'] = ['organisation_id' => $currentUser['organisation_id']];
|
||||
}
|
||||
$params['fields'] = ['name', 'releasability', 'description', 'active'];
|
||||
$this->CRUD->edit($id, $params);
|
||||
$responsePayload = $this->CRUD->getResponsePayload();
|
||||
if (!empty($responsePayload)) {
|
||||
|
|
|
@ -33,16 +33,30 @@ class UsersController extends AppController
|
|||
if (!empty($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');
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
$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([
|
||||
'beforeSave' => function($data) use ($currentUser) {
|
||||
'beforeSave' => function($data) use ($currentUser, $validRoles) {
|
||||
if (!$currentUser['role']['perm_admin']) {
|
||||
$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);
|
||||
return $data;
|
||||
|
@ -65,9 +79,7 @@ class UsersController extends AppController
|
|||
$org_conditions = ['id' => $currentUser['organisation_id']];
|
||||
}
|
||||
$dropdownData = [
|
||||
'role' => $this->Users->Roles->find('list', [
|
||||
'sort' => ['name' => 'asc']
|
||||
]),
|
||||
'role' => $validRoles,
|
||||
'individual' => $this->Users->Individuals->find('list', [
|
||||
'sort' => ['email' => 'asc']
|
||||
]),
|
||||
|
@ -98,6 +110,12 @@ class UsersController extends AppController
|
|||
public function edit($id = false)
|
||||
{
|
||||
$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)) {
|
||||
$id = $currentUser['id'];
|
||||
} else {
|
||||
|
@ -128,6 +146,20 @@ class UsersController extends AppController
|
|||
$params['fields'][] = 'role_id';
|
||||
$params['fields'][] = 'organisation_id';
|
||||
$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);
|
||||
$responsePayload = $this->CRUD->getResponsePayload();
|
||||
|
@ -135,9 +167,7 @@ class UsersController extends AppController
|
|||
return $responsePayload;
|
||||
}
|
||||
$dropdownData = [
|
||||
'role' => $this->Users->Roles->find('list', [
|
||||
'sort' => ['name' => 'asc']
|
||||
]),
|
||||
'role' => $validRoles,
|
||||
'individual' => $this->Users->Individuals->find('list', [
|
||||
'sort' => ['email' => 'asc']
|
||||
]),
|
||||
|
@ -161,6 +191,23 @@ class UsersController extends AppController
|
|||
|
||||
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);
|
||||
$responsePayload = $this->CRUD->getResponsePayload();
|
||||
if (!empty($responsePayload)) {
|
||||
|
|
|
@ -66,6 +66,7 @@ class IndividualsTable extends AppTable
|
|||
$this->patchEntity($existingIndividual, $individual);
|
||||
$entityToSave = $existingIndividual;
|
||||
}
|
||||
$entityToSave->setDirty('modified', false);
|
||||
$savedEntity = $this->save($entityToSave, ['associated' => false]);
|
||||
if (!$savedEntity) {
|
||||
return null;
|
||||
|
|
|
@ -71,6 +71,7 @@ class OrganisationsTable extends AppTable
|
|||
$this->patchEntity($existingOrg, $org);
|
||||
$entityToSave = $existingOrg;
|
||||
}
|
||||
$entityToSave->setDirty('modified', false);
|
||||
$savedEntity = $this->save($entityToSave, ['associated' => false]);
|
||||
if (!$savedEntity) {
|
||||
return null;
|
||||
|
|
|
@ -66,6 +66,7 @@ class SharingGroupsTable extends AppTable
|
|||
$this->patchEntity($existingSG, $input);
|
||||
$entityToSave = $existingSG;
|
||||
}
|
||||
$entityToSave->setDirty('modified', false);
|
||||
$savedEntity = $this->save($entityToSave, ['associated' => false]);
|
||||
if (!$savedEntity) {
|
||||
return null;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"version": "0.1",
|
||||
"version": "1.3",
|
||||
"application": "Cerebrate"
|
||||
}
|
||||
|
|
|
@ -102,12 +102,48 @@ echo $this->element('genericElements/IndexTable/index_table', [
|
|||
[
|
||||
'open_modal' => '/users/edit/[onclick_params_data_path]',
|
||||
'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]',
|
||||
'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;
|
||||
}
|
||||
]
|
||||
],
|
||||
]
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue