From 54c513613e206d47e7fc688fa58a80ce97251492 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Wed, 25 Nov 2020 16:06:31 +0100 Subject: [PATCH 1/3] fix: [sharingGroup] Various fixes for sharing groups - Renamed sharingGroupsOrgs table to follow cake4's recommendation - Fix case if logged user doesn't have an organisation - Provide all orgs if user is admin - Fix issue with model associated with sharingGroupOrgs - Fix addOrg missing entity for genericForm helper --- INSTALL/clean.sql | 2 ++ INSTALL/mysql.sql | 2 +- src/Controller/SharingGroupsController.php | 30 +++++++++++++--------- src/Model/Table/SharingGroupsTable.php | 1 - templates/SharingGroups/add_org.php | 5 ++-- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/INSTALL/clean.sql b/INSTALL/clean.sql index 40cd9ef..a7e8f80 100644 --- a/INSTALL/clean.sql +++ b/INSTALL/clean.sql @@ -8,6 +8,8 @@ DROP TABLE IF EXISTS encryption_keys; DROP TABLE IF EXISTS organisation_encryption_keys; DROP TABLE IF EXISTS organisations; DROP TABLE IF EXISTS roles; +DROP TABLE IF EXISTS sharing_groups; +DROP TABLE IF EXISTS organisations_sharing_groups; DROP TABLE IF EXISTS tags; DROP TABLE IF EXISTS user_keys; DROP TABLE IF EXISTS users; diff --git a/INSTALL/mysql.sql b/INSTALL/mysql.sql index 4566f4c..0a744fc 100644 --- a/INSTALL/mysql.sql +++ b/INSTALL/mysql.sql @@ -309,7 +309,7 @@ CREATE TABLE `sharing_groups` ( KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -CREATE TABLE `sharing_group_orgs` ( +CREATE TABLE `organisations_sharing_groups` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `sharing_group_id` int(10) unsigned NOT NULL, `organisation_id` int(10) unsigned NOT NULL, diff --git a/src/Controller/SharingGroupsController.php b/src/Controller/SharingGroupsController.php index 969e1b0..a37701c 100644 --- a/src/Controller/SharingGroupsController.php +++ b/src/Controller/SharingGroupsController.php @@ -29,12 +29,7 @@ class SharingGroupsController extends AppController ] ]); $dropdownData = [ - 'organisation' => $this->SharingGroups->Organisations->find('list', [ - 'sort' => ['name' => 'asc'], - 'conditions' => [ - 'id IN' => array_values(\Cake\Utility\Hash::extract($this->ACL->getUser(), 'individual.organisations.{n}.id')) - ] - ]) + 'organisation' => $this->getAvailableOrgForSg($this->ACL->getUser()) ]; if ($this->ParamHandler->isRest()) { return $this->restResponsePayload; @@ -61,12 +56,7 @@ class SharingGroupsController extends AppController return $this->restResponsePayload; } $dropdownData = [ - 'organisation' => $this->SharingGroups->Organisations->find('list', [ - 'sort' => ['name' => 'asc'], - 'conditions' => [ - 'id IN' => array_values(\Cake\Utility\Hash::extract($this->ACL->getUser(), 'individual.organisations.{n}.id')) - ] - ]) + 'organisation' => $this->getAvailableOrgForSg($this->ACL->getUser()) ]; $this->set(compact('dropdownData')); $this->set('metaGroup', 'Trust Circles'); @@ -160,4 +150,20 @@ class SharingGroupsController extends AppController $this->set('sharing_group_id', $id); $this->set('sharing_group_orgs', $sharingGroup['sharing_group_orgs']); } + + private function getAvailableOrgForSg($user) + { + $organisations = []; + if (!empty($user['role']['perm_admin'])) { + $organisations = $this->SharingGroups->Organisations->find('list')->order(['name' => 'ASC'])->toArray(); + } else if (!empty($user['individual']['organisations'])) { + $organisations = $this->SharingGroups->Organisations->find('list', [ + 'sort' => ['name' => 'asc'], + 'conditions' => [ + 'id IN' => array_values(\Cake\Utility\Hash::extract($user, 'individual.organisations.{n}.id')) + ] + ]); + } + return $organisations; + } } diff --git a/src/Model/Table/SharingGroupsTable.php b/src/Model/Table/SharingGroupsTable.php index 7204dec..5e73131 100644 --- a/src/Model/Table/SharingGroupsTable.php +++ b/src/Model/Table/SharingGroupsTable.php @@ -24,7 +24,6 @@ class SharingGroupsTable extends AppTable 'SharingGroupOrgs', [ 'className' => 'Organisations', - 'joinTable' => 'sgo', 'foreignKey' => 'sharing_group_id', 'targetForeignKey' => 'organisation_id' ] diff --git a/templates/SharingGroups/add_org.php b/templates/SharingGroups/add_org.php index 24b89c2..f4f11d5 100644 --- a/templates/SharingGroups/add_org.php +++ b/templates/SharingGroups/add_org.php @@ -12,8 +12,9 @@ ], 'submit' => [ 'action' => $this->request->getParam('action') - ] - ] + ], + ], + 'entity' => null, ]); ?> From 7f067acfa2cc676b67b3624ea7e2a8901f83d2a6 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Wed, 25 Nov 2020 16:36:59 +0100 Subject: [PATCH 2/3] chg: [sharingGroupOrgs] Renamed table back to original name --- INSTALL/clean.sql | 2 +- INSTALL/mysql.sql | 2 +- src/Model/Table/SharingGroupsTable.php | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/INSTALL/clean.sql b/INSTALL/clean.sql index a7e8f80..f7cc20e 100644 --- a/INSTALL/clean.sql +++ b/INSTALL/clean.sql @@ -9,7 +9,7 @@ DROP TABLE IF EXISTS organisation_encryption_keys; DROP TABLE IF EXISTS organisations; DROP TABLE IF EXISTS roles; DROP TABLE IF EXISTS sharing_groups; -DROP TABLE IF EXISTS organisations_sharing_groups; +DROP TABLE IF EXISTS sgo; DROP TABLE IF EXISTS tags; DROP TABLE IF EXISTS user_keys; DROP TABLE IF EXISTS users; diff --git a/INSTALL/mysql.sql b/INSTALL/mysql.sql index 0a744fc..9f888dc 100644 --- a/INSTALL/mysql.sql +++ b/INSTALL/mysql.sql @@ -309,7 +309,7 @@ CREATE TABLE `sharing_groups` ( KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -CREATE TABLE `organisations_sharing_groups` ( +CREATE TABLE `sgo` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `sharing_group_id` int(10) unsigned NOT NULL, `organisation_id` int(10) unsigned NOT NULL, diff --git a/src/Model/Table/SharingGroupsTable.php b/src/Model/Table/SharingGroupsTable.php index 5e73131..5b6fd66 100644 --- a/src/Model/Table/SharingGroupsTable.php +++ b/src/Model/Table/SharingGroupsTable.php @@ -25,6 +25,7 @@ class SharingGroupsTable extends AppTable [ 'className' => 'Organisations', 'foreignKey' => 'sharing_group_id', + 'joinTable' => 'sgo', 'targetForeignKey' => 'organisation_id' ] ); From 465b257d6e79290cf4a578f4e29876313f231533 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Wed, 25 Nov 2020 16:45:55 +0100 Subject: [PATCH 3/3] chg: [genericForm] Fallback entity to null (to model-less form) if not provided --- templates/SharingGroups/add_org.php | 3 +-- templates/element/genericElements/Form/genericForm.php | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/SharingGroups/add_org.php b/templates/SharingGroups/add_org.php index f4f11d5..79ce801 100644 --- a/templates/SharingGroups/add_org.php +++ b/templates/SharingGroups/add_org.php @@ -13,8 +13,7 @@ 'submit' => [ 'action' => $this->request->getParam('action') ], - ], - 'entity' => null, + ] ]); ?> diff --git a/templates/element/genericElements/Form/genericForm.php b/templates/element/genericElements/Form/genericForm.php index a0167a6..ed73cf1 100644 --- a/templates/element/genericElements/Form/genericForm.php +++ b/templates/element/genericElements/Form/genericForm.php @@ -15,6 +15,7 @@ $modelForForm = empty($data['model']) ? h(\Cake\Utility\Inflector::singularize(\Cake\Utility\Inflector::classify($this->request->getParam('controller')))) : h($data['model']); + $entity = isset($entity) ? $entity : null; $fieldsString = ''; $simpleFieldWhitelist = [ 'default', 'type', 'placeholder', 'label', 'empty', 'rows', 'div', 'required'