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')) {
$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();
@ -227,6 +233,14 @@ class UsersController extends AppController
if (!$this->ACL->canEditUser($currentUser, $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;
};

View File

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

View File

@ -1,5 +1,7 @@
<?php
use Cake\Core\Configure;
$passwordRequired = false;
$showPasswordField = false;
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>