Skip to content

Commit

Permalink
Permissions: only update permission record in the DB if necessary (so…
Browse files Browse the repository at this point in the history
…mething has changed). (#2652)

relates to xibosignageltd/xibo-private#797
  • Loading branch information
dasgarner authored Jul 26, 2024
1 parent 638bf7c commit b903b26
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
40 changes: 32 additions & 8 deletions lib/Entity/Permission.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/*
* Copyright (c) 2022 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
Expand Down Expand Up @@ -123,21 +123,45 @@ public function __clone()
$this->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();
}
}
}

Expand Down
82 changes: 29 additions & 53 deletions lib/Factory/PermissionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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 = [];

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit b903b26

Please sign in to comment.