new: [user:permissionLimitation] Added current permission status while in `add` or `edit` context

Also moved the notification key from meta-fields to meta-template-fields
refacto/CRUDComponent
Sami Mokaddem 2023-02-24 15:22:18 +01:00
parent aead79a4c3
commit 59f8608d50
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
3 changed files with 192 additions and 124 deletions

View File

@ -120,6 +120,12 @@ class UsersController extends AppController
if (Configure::read('keycloak.enabled')) { if (Configure::read('keycloak.enabled')) {
$this->Users->enrollUserRouter($data); $this->Users->enrollUserRouter($data);
} }
},
'afterFind' => function ($user, &$params) use ($currentUser) {
if (!empty($user)) { // We don't have a 404
$user = $this->fetchTable('PermissionLimitations')->attachLimitations($user);
}
return $user;
} }
]); ]);
$responsePayload = $this->CRUD->getResponsePayload(); $responsePayload = $this->CRUD->getResponsePayload();
@ -227,6 +233,14 @@ class UsersController extends AppController
if (!$this->ACL->canEditUser($currentUser, $user)) { if (!$this->ACL->canEditUser($currentUser, $user)) {
throw new MethodNotAllowedException(__('You cannot edit the given user.')); throw new MethodNotAllowedException(__('You cannot edit the given user.'));
} }
$user = $this->fetchTable('PermissionLimitations')->attachLimitations($user);
}
return $user;
};
} else {
$params['afterFind'] = function ($user, &$params) use ($currentUser) {
if (!empty($user)) { // We don't have a 404
$user = $this->fetchTable('PermissionLimitations')->attachLimitations($user);
} }
return $user; return $user;
}; };

View File

@ -30,6 +30,9 @@ class PermissionLimitationsTable extends AppTable
public function getListOfLimitations(\App\Model\Entity\User $data) public function getListOfLimitations(\App\Model\Entity\User $data)
{ {
$Users = TableRegistry::getTableLocator()->get('Users'); $Users = TableRegistry::getTableLocator()->get('Users');
$includeOrganisationPermissions = !empty($data['organisation_id']);
$ownOrgUserIds = [];
if ($includeOrganisationPermissions) {
$ownOrgUserIds = $Users->find('list', [ $ownOrgUserIds = $Users->find('list', [
'keyField' => 'id', 'keyField' => 'id',
'valueField' => 'id', 'valueField' => 'id',
@ -37,6 +40,7 @@ class PermissionLimitationsTable extends AppTable
'organisation_id' => $data['organisation_id'] 'organisation_id' => $data['organisation_id']
] ]
])->all()->toList(); ])->all()->toList();
}
$MetaFields = TableRegistry::getTableLocator()->get('MetaFields'); $MetaFields = TableRegistry::getTableLocator()->get('MetaFields');
$raw = $this->find()->select(['scope', 'permission', 'max_occurrence'])->disableHydration()->toArray(); $raw = $this->find()->select(['scope', 'permission', 'max_occurrence'])->disableHydration()->toArray();
$limitations = []; $limitations = [];
@ -70,11 +74,14 @@ class PermissionLimitationsTable extends AppTable
if (!empty($ownOrgUserIds)) { if (!empty($ownOrgUserIds)) {
$conditions['parent_id IN'] = array_values($ownOrgUserIds); $conditions['parent_id IN'] = array_values($ownOrgUserIds);
} }
$limitations[$field]['organisation']['current'] = '?';
if ($includeOrganisationPermissions) {
$limitations[$field]['organisation']['current'] = $MetaFields->find('all', [ $limitations[$field]['organisation']['current'] = $MetaFields->find('all', [
'conditions' => $conditions, 'conditions' => $conditions,
])->count(); ])->count();
} }
} }
}
return $limitations; return $limitations;
} }
@ -89,17 +96,19 @@ class PermissionLimitationsTable extends AppTable
if (!empty($data['MetaTemplates'])) { if (!empty($data['MetaTemplates'])) {
foreach ($data['MetaTemplates'] as &$metaTemplate) { foreach ($data['MetaTemplates'] as &$metaTemplate) {
foreach ($metaTemplate['meta_template_fields'] as &$meta_template_field) { foreach ($metaTemplate['meta_template_fields'] as &$meta_template_field) {
$boolean = $meta_template_field['type'] === 'boolean'; if (isset($permissionLimitations[$meta_template_field['field']])) {
foreach ($meta_template_field['metaFields'] as &$metaField) { foreach ($permissionLimitations[$meta_template_field['field']] as $scope => $value) {
if (isset($permissionLimitations[$metaField['field']])) {
foreach ($permissionLimitations[$metaField['field']] as $scope => $value) {
$messageType = 'warning'; $messageType = 'warning';
if ($value['current'] == '?') {
$messageType = 'info';
} else {
if ($value['limit'] > $value['current']) { if ($value['limit'] > $value['current']) {
$messageType = 'info'; $messageType = 'info';
} }
if ($value['limit'] < $value['current']) { if ($value['limit'] < $value['current']) {
$messageType = 'danger'; $messageType = 'danger';
} }
}
if (empty($metaField[$messageType])) { if (empty($metaField[$messageType])) {
$metaField[$messageType] = ''; $metaField[$messageType] = '';
} }
@ -109,7 +118,7 @@ class PermissionLimitationsTable extends AppTable
$value['current'], $value['current'],
$value['limit'] $value['limit']
); );
$metaField[$messageType] .= sprintf( $meta_template_field[$messageType] .= sprintf(
' <span title="%s"><span class="text-dark"><i class="fas fa-%s"></i>: </span>%s/%s</span>', ' <span title="%s"><span class="text-dark"><i class="fas fa-%s"></i>: </span>%s/%s</span>',
$altText, $altText,
$icons[$scope], $icons[$scope],
@ -121,7 +130,6 @@ class PermissionLimitationsTable extends AppTable
} }
} }
} }
}
return $data; return $data;
} }
} }

View File

@ -1,5 +1,7 @@
<?php <?php
use Cake\Core\Configure; use Cake\Core\Configure;
$passwordRequired = false; $passwordRequired = false;
$showPasswordField = false; $showPasswordField = false;
if ($this->request->getParam('action') === 'add') { if ($this->request->getParam('action') === 'add') {
@ -96,4 +98,48 @@
] ]
]); ]);
?> ?>
</div>
<script>
$(document).ready(function() {
const entity = <?= json_encode($entity) ?>;
console.log(entity);
if (entity.MetaTemplates) {
for (const [metaTemplateId, metaTemplate] of Object.entries(entity.MetaTemplates)) {
for (const [metaTemplateFieldId, metaTemplateField] of Object.entries(metaTemplate.meta_template_fields)) {
let metaFieldId = false
if (metaTemplateField.metaFields !== undefined && Object.keys(metaTemplateField.metaFields).length > 0) {
metaFieldId = Object.keys(metaTemplateField.metaFields)[0]
}
let metafieldInput
const baseQueryPath = `MetaTemplates.${metaTemplateId}.meta_template_fields.${metaTemplateFieldId}.metaFields`
if (metaFieldId) {
metafieldInput = document.getElementById(`${baseQueryPath}.${metaFieldId}.value-field`)
} else {
metafieldInput = document.getElementById(`${baseQueryPath}.new.0-field`)
}
if (metafieldInput !== null) {
const permissionWarnings = buildPermissionElement(metaTemplateField)
$(metafieldInput.parentElement).append(permissionWarnings)
}
}
}
}
function buildPermissionElement(metaTemplateField) {
const warningTypes = ['danger', 'warning', 'info', ]
const $span = $('<span>').addClass('ms-2')
warningTypes.forEach(warningType => {
if (metaTemplateField[warningType]) {
$theWarning = $('<span>')
.addClass([
`text-${warningType}`,
'ms-1',
])
.append($(metaTemplateField[warningType]))
$span.append($theWarning)
}
});
return $span
}
})
</script>