From b903b265baf6cb90d1badf462697825e2833df14 Mon Sep 17 00:00:00 2001 From: Dan Garner Date: Fri, 26 Jul 2024 17:46:27 +0100 Subject: [PATCH] Permissions: only update permission record in the DB if necessary (something has changed). (#2652) relates to xibosignageltd/xibo-private#797 --- lib/Entity/Permission.php | 40 ++++++++++++--- lib/Factory/PermissionFactory.php | 82 +++++++++++-------------------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/lib/Entity/Permission.php b/lib/Entity/Permission.php index f5410c82f6..f87c5067bb 100644 --- a/lib/Entity/Permission.php +++ b/lib/Entity/Permission.php @@ -1,8 +1,8 @@ permissionId = null; } - public function save() + /** + * Save this permission + * @return void + */ + public function save(): void { if ($this->permissionId == 0) { // Check there is something to add if ($this->view != 0 || $this->edit != 0 || $this->delete != 0) { - $this->getLog()->debug(sprintf('Adding Permission for %s, %d. GroupId: %d - View = %d, Edit = %d, Delete = %d', $this->entity, $this->objectId, $this->groupId, $this->view, $this->edit, $this->delete)); + $this->getLog()->debug(sprintf( + 'save: Adding Permission for %s, %d. GroupId: %d - View = %d, Edit = %d, Delete = %d', + $this->entity, + $this->objectId, + $this->groupId, + $this->view, + $this->edit, + $this->delete, + )); + $this->add(); } } else { - $this->getLog()->debug(sprintf('Editing Permission for %s, %d. GroupId: %d - View = %d, Edit = %d, Delete = %d', $this->entity, $this->objectId, $this->groupId, $this->view, $this->edit, $this->delete)); - // Are we all 0 permissions - if ($this->view == 0 && $this->edit == 0 && $this->delete == 0) + $this->getLog()->debug(sprintf( + 'save: Editing Permission for %s, %d. GroupId: %d - View = %d, Edit = %d, Delete = %d', + $this->entity, + $this->objectId, + $this->groupId, + $this->view, + $this->edit, + $this->delete, + )); + + // If all permissions are set to 0, then we delete the record to tidy up + if ($this->view == 0 && $this->edit == 0 && $this->delete == 0) { $this->delete(); - else + } else if (count($this->getChangedProperties()) > 0) { + // Something has changed, so run the update. $this->update(); + } } } diff --git a/lib/Factory/PermissionFactory.php b/lib/Factory/PermissionFactory.php index ee869c32ec..9c21d32e96 100644 --- a/lib/Factory/PermissionFactory.php +++ b/lib/Factory/PermissionFactory.php @@ -143,15 +143,11 @@ public function getByObjectId($entity, $objectId) $params = array('entity' => $entity, 'objectId' => $objectId); foreach ($this->getStore()->select($sql, $params) as $row) { - $permission = $this->createEmpty(); - $permission->permissionId = $row['permissionId']; - $permission->groupId = $row['groupId']; - $permission->view = $row['view']; - $permission->edit = $row['edit']; - $permission->delete = $row['delete']; + $permission = $this->createEmpty()->hydrate($row, [ + 'intProperties' => ['view', 'edit', 'delete'], + ]); $permission->objectId = $objectId; $permission->entity = $entity; - $permission->entityId = $row['entityId']; $permissions[] = $permission; } @@ -285,23 +281,13 @@ public function getAllByObjectId($user, $entity, $objectId, $sortOrder = null, $ $sql = $select . $body . $order . $limit; - - foreach ($this->getStore()->select($sql, $params) as $row) { - // TODO Sanitizer? - $permission = $this->createEmpty(); - $permission->permissionId = intval($row['permissionId']); - $permission->groupId = intval($row['groupId']); - $permission->view = intval($row['view']); - $permission->edit = intval($row['edit']); - $permission->delete = intval($row['delete']); - $permission->objectId = intval($objectId); - $permission->entity = $entity; - $permission->entityId = intval($entityId); - $permission->isUser = intval($row['isuserspecific']); - $permission->group = ($row['group']); - - $permissions[] = $permission; + $row['entityId'] = $entityId; + $row['entity'] = $entity; + $row['objectId'] = $objectId; + $permissions[] = $this->createEmpty()->hydrate($row, [ + 'intProperties' => ['view', 'edit', 'delete', 'isUser'], + ]); } // Paging @@ -321,10 +307,16 @@ public function getAllByObjectId($user, $entity, $objectId, $sortOrder = null, $ */ public function getByGroupId($entity, $groupId) { - $permissions = array(); + $permissions = []; $sql = ' - SELECT `permission`.`permissionId`, `permission`.`groupId`, `permission`.`objectId`, `permission`.`view`, `permission`.`edit`, `permission`.`delete`, permissionentity.entityId + SELECT `permission`.`permissionId`, + `permission`.`groupId`, + `permission`.`objectId`, + `permission`.`view`, + `permission`.`edit`, + `permission`.`delete`, + `permissionentity`.`entityId` FROM `permission` INNER JOIN `permissionentity` ON `permissionentity`.entityId = permission.entityId @@ -333,22 +325,13 @@ public function getByGroupId($entity, $groupId) WHERE entity = :entity AND `permission`.`groupId` = :groupId '; - $params = array('entity' => 'Xibo\Entity\\' . $entity, 'groupId' => $groupId); - - + $params = ['entity' => 'Xibo\Entity\\' . $entity, 'groupId' => $groupId]; foreach ($this->getStore()->select($sql, $params) as $row) { - $permission = $this->createEmpty(); - $permission->permissionId = $row['permissionId']; - $permission->groupId = $row['groupId']; - $permission->view = $row['view']; - $permission->edit = $row['edit']; - $permission->delete = $row['delete']; - $permission->objectId = $row['objectId']; - $permission->entity = $entity; - $permission->entityId = $row['entityId']; - - $permissions[] = $permission; + $row['entity'] = $entity; + $permissions[] = $this->createEmpty()->hydrate($row, [ + 'intProperties' => ['view', 'edit', 'delete'], + ]); } return $permissions; @@ -360,7 +343,7 @@ public function getByGroupId($entity, $groupId) * @param int $userId * @return Permission[] */ - public function getByUserId($entity, $userId) + public function getByUserId($entity, $userId): array { $permissions = []; @@ -371,7 +354,7 @@ public function getByUserId($entity, $userId) `permission`.`view`, `permission`.`edit`, `permission`.`delete`, - `permissionentity`.entityId + `permissionentity`.`entityId` FROM `permission` INNER JOIN `permissionentity` ON `permissionentity`.entityId = permission.entityId @@ -402,17 +385,10 @@ public function getByUserId($entity, $userId) $params = ['entity' => $entity, 'userId' => $userId]; foreach ($this->getStore()->select($sql, $params) as $row) { - $permission = $this->createEmpty(); - $permission->permissionId = $row['permissionId']; - $permission->groupId = $row['groupId']; - $permission->view = $row['view']; - $permission->edit = $row['edit']; - $permission->delete = $row['delete']; - $permission->objectId = $row['objectId']; - $permission->entity = $entity; - $permission->entityId = $row['entityId']; - - $permissions[] = $permission; + $row['entity'] = $entity; + $permissions[] = $this->createEmpty()->hydrate($row, [ + 'intProperties' => ['view', 'edit', 'delete'], + ]); } return $permissions; @@ -422,7 +398,7 @@ public function getByUserId($entity, $userId) * Get Full Permissions * @return Permission */ - public function getFullPermissions() + public function getFullPermissions(): Permission { $permission = $this->createEmpty(); $permission->view = 1;