Skip to content

Commit 3a3494d

Browse files
committed
fix: [encryptionKeys:ui] Aligned UI with what users can actually do
1 parent e111dac commit 3a3494d

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

src/Controller/EncryptionKeysController.php

+7-17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class EncryptionKeysController extends AppController
2121

2222
public function index()
2323
{
24+
$currentUser = $this->ACL->getUser();
2425
$this->EncryptionKeys->initializeGpg();
2526
$Model = $this->EncryptionKeys;
2627
$this->CRUD->index([
@@ -33,7 +34,7 @@ public function index()
3334
],
3435
'contain' => $this->containFields,
3536
'statisticsFields' => $this->statisticsFields,
36-
'afterFind' => function($data) use ($Model) {
37+
'afterFind' => function($data) use ($Model, $currentUser) {
3738
if ($data['type'] === 'pgp') {
3839
$keyInfo = $Model->verifySingleGPG($data);
3940
$data['status'] = __('OK');
@@ -45,6 +46,7 @@ public function index()
4546
$data['fingerprint'] = $keyInfo[4];
4647
}
4748
}
49+
$data['_canBeEdited'] = $Model->canEdit($currentUser, $data);
4850
return $data;
4951
}
5052
]);
@@ -96,24 +98,12 @@ private function buildBeforeSave(array $params, $currentUser, array &$orgConditi
9698
}
9799
$params['beforeSave'] = function($entity) use($currentUser) {
98100
if ($entity['owner_model'] === 'organisation') {
99-
if ($entity['owner_id'] !== $currentUser['organisation_id']) {
101+
if (!$this->EncryptionKeys->canEditForOrganisation($currentUser, $entity)) {
100102
throw new MethodNotAllowedException(__('Selected organisation cannot be linked by the current user.'));
101103
}
102-
} else {
103-
if ($currentUser['role']['perm_org_admin']) {
104-
$this->loadModel('Alignments');
105-
$validIndividuals = $this->Alignments->find('list', [
106-
'keyField' => 'individual_id',
107-
'valueField' => 'id',
108-
'conditions' => ['organisation_id' => $currentUser['organisation_id']]
109-
])->toArray();
110-
if (!isset($validIndividuals[$entity['owner_id']])) {
111-
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.'));
112-
}
113-
} else {
114-
if ($entity['owner_id'] !== $currentUser['id']) {
115-
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.'));
116-
}
104+
} else if ($entity['owner_model'] === 'individual') {
105+
if (!$this->EncryptionKeys->canEditForIndividual($currentUser, $entity)) {
106+
throw new MethodNotAllowedException(__('Selected individual cannot be linked by the current user.'));
117107
}
118108
}
119109
return $entity;

src/Model/Table/EncryptionKeysTable.php

+54
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Model\Table;
44

55
use App\Model\Table\AppTable;
6+
use Cake\ORM\TableRegistry;
67
use Cake\ORM\Table;
78
use Cake\Validation\Validator;
89
use Cake\Event\EventInterface;
@@ -147,4 +148,57 @@ public function initializeGpg()
147148
return null;
148149
}
149150
}
151+
152+
public function canEdit($user, $entity): bool
153+
{
154+
if ($entity['owner_model'] === 'organisation') {
155+
return $this->canEditForOrganisation($user, $entity);
156+
} else if ($entity['owner_model'] === 'individual') {
157+
return $this->canEditForIndividual($user, $entity);
158+
}
159+
return false;
160+
}
161+
162+
public function canEditForOrganisation($user, $entity): bool
163+
{
164+
if ($entity['owner_model'] !== 'organisation') {
165+
return false;
166+
}
167+
if (!empty($user['role']['perm_admin'])) {
168+
return true;
169+
}
170+
if (
171+
$user['role']['perm_org_admin'] &&
172+
$entity['owner_id'] === $user['organisation_id']
173+
) {
174+
return true;
175+
}
176+
return false;
177+
}
178+
179+
public function canEditForIndividual($user, $entity): bool
180+
{
181+
if ($entity['owner_model'] !== 'individual') {
182+
return false;
183+
}
184+
if (!empty($user['role']['perm_admin'])) {
185+
return true;
186+
}
187+
if ($user['role']['perm_org_admin']) {
188+
$this->Alignments = TableRegistry::get('Alignments');
189+
$validIndividuals = $this->Alignments->find('list', [
190+
'keyField' => 'individual_id',
191+
'valueField' => 'id',
192+
'conditions' => ['organisation_id' => $user['organisation_id']]
193+
])->toArray();
194+
if (isset($validIndividuals[$entity['owner_id']])) {
195+
return true;
196+
}
197+
} else {
198+
if ($entity['owner_id'] === $user['id']) {
199+
return true;
200+
}
201+
}
202+
return false;
203+
}
150204
}

templates/EncryptionKeys/index.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,22 @@
8080
[
8181
'open_modal' => '/encryptionKeys/edit/[onclick_params_data_path]',
8282
'modal_params_data_path' => 'id',
83-
'icon' => 'edit'
83+
'icon' => 'edit',
84+
'complex_requirement' => [
85+
'function' => function ($row, $options) {
86+
return $row['_canBeEdited'];
87+
}
88+
]
8489
],
8590
[
8691
'open_modal' => '/encryptionKeys/delete/[onclick_params_data_path]',
8792
'modal_params_data_path' => 'id',
88-
'icon' => 'trash'
93+
'icon' => 'trash',
94+
'complex_requirement' => [
95+
'function' => function ($row, $options) {
96+
return $row['_canBeEdited'];
97+
}
98+
]
8999
],
90100
]
91101
]

0 commit comments

Comments
 (0)