fix: [user:checkPermissionRestriction] Refactor the function to support more edge-cases

pull/121/head
Sami Mokaddem 2022-12-08 15:39:28 +01:00
parent 9a2e94989f
commit b121399304
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 17 additions and 6 deletions

View File

@ -88,22 +88,33 @@ class UsersTable extends AppTable
if (!isset($this->PermissionLimitations)) { if (!isset($this->PermissionLimitations)) {
$this->PermissionLimitations = TableRegistry::get('PermissionLimitations'); $this->PermissionLimitations = TableRegistry::get('PermissionLimitations');
} }
$new = $entity->isNew();
$permissions = $this->PermissionLimitations->getListOfLimitations($entity); $permissions = $this->PermissionLimitations->getListOfLimitations($entity);
foreach ($permissions as $permission_name => $permission) { foreach ($permissions as $permission_name => $permission) {
foreach ($permission as $scope => $permission_data) { foreach ($permission as $scope => $permission_data) {
if (!empty($entity['meta_fields'])) { $valueToCompareTo = $permission_data['current'];
$enabled = false;
$enabled = false;
if (!empty($entity->meta_fields)) {
foreach ($entity['meta_fields'] as $metaField) { foreach ($entity['meta_fields'] as $metaField) {
if ($metaField['field'] === $permission_name) { if ($metaField['field'] === $permission_name) {
$enabled = true; $enabled = true;
if ($metaField->isNew()) {
$valueToCompareTo += !empty($metaField->value) ? 1 : 0;
} else {
$valueToCompareTo += !empty($metaField->value) ? 0 : -1;
}
} }
} }
if (!$enabled) { }
continue;
if (!$enabled && !empty($entity->_metafields_to_delete)) {
foreach ($entity->_metafields_to_delete as $metaFieldToDelete) {
if ($metaFieldToDelete['field'] === $permission_name) {
$valueToCompareTo += !empty($metaFieldToDelete->value) ? -1 : 0;
}
} }
} }
$valueToCompareTo = $permission_data['current'] + ($new ? 1 : 0);
if ($valueToCompareTo > $permission_data['limit']) { if ($valueToCompareTo > $permission_data['limit']) {
return [ return [
$permission_name => $permission_name =>