From aae584f0b79425fea96afce4f4cb74d91ad524a0 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Tue, 24 Sep 2024 16:03:35 +0200 Subject: [PATCH] chg: [users:add] Update the permission limitation for org permission on /users/add --- src/Controller/Component/ACLComponent.php | 3 +- src/Controller/Component/CRUDComponent.php | 2 +- src/Controller/UsersController.php | 18 +++++ templates/Users/add.php | 84 +++++++++++++--------- 4 files changed, 73 insertions(+), 34 deletions(-) diff --git a/src/Controller/Component/ACLComponent.php b/src/Controller/Component/ACLComponent.php index 0f4c2c0..484e2de 100644 --- a/src/Controller/Component/ACLComponent.php +++ b/src/Controller/Component/ACLComponent.php @@ -245,7 +245,8 @@ class ACLComponent extends Component 'register' => ['*'], 'settings' => ['*'], 'toggle' => ['OR' => ['perm_org_admin', 'perm_community_admin']], - 'view' => ['*'] + 'view' => ['*'], + 'getLimitationForOrganisation' => ['OR' => ['perm_org_admin', 'perm_community_admin']], ], 'UserSettings' => [ 'index' => ['*'], diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 9660d58..607faf9 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -393,7 +393,7 @@ class CRUDComponent extends Component return false; } - private function getMetaTemplates(array $metaTemplateConditions = []) + public function getMetaTemplates(array $metaTemplateConditions = []) { $metaTemplates = []; if (!$this->metaFieldsSupported()) { diff --git a/src/Controller/UsersController.php b/src/Controller/UsersController.php index 8bcdaa9..4159d96 100644 --- a/src/Controller/UsersController.php +++ b/src/Controller/UsersController.php @@ -541,4 +541,22 @@ class UsersController extends AppController } $this->viewBuilder()->setLayout('login'); } + + public function getLimitationForOrganisation($org_id) { + $currentUser = $this->ACL->getUser(); + if (!$currentUser['role']['perm_community_admin']) { + $validOrgs = $this->Users->getValidOrgsForUser($currentUser); + if ($currentUser['role']['perm_group_admin']) { + if (!in_array($org_id, $validOrgs)) { + throw new MethodNotAllowedException(__('You do not have permission to assign that organisation.')); + } + } + } + $fakeUser = $this->Users->newEmptyEntity(); + $fakeUser->organisation_id = $org_id; // set fakeUser's to the selected org-id + $metaTemplates = $this->CRUD->getMetaTemplates(); + $fakeUser = $this->CRUD->attachMetaTemplatesIfNeeded($fakeUser, $metaTemplates->toArray()); + $fakeUser = $this->fetchTable('PermissionLimitations')->attachLimitations($fakeUser); + return $this->RestResponse->viewData($fakeUser, 'json'); + } } diff --git a/templates/Users/add.php b/templates/Users/add.php index 8afcd69..539711b 100644 --- a/templates/Users/add.php +++ b/templates/Users/add.php @@ -102,43 +102,63 @@ echo $this->element('genericElements/Form/genericForm', [ \ No newline at end of file