0
0
Fork 0
mirror of https://github.com/verdigado/organization_folders.git synced 2024-11-21 20:28:11 +01:00

ensure resource name is unique

This commit is contained in:
Jonathan Treffler 2024-10-30 01:51:48 +01:00
parent 949e1a42d1
commit af661c5c26
4 changed files with 74 additions and 17 deletions

View file

@ -17,7 +17,7 @@ class ResourceMapper extends QBMapper {
public const RESOURCES_TABLE = "organizationfolders_resources"; public const RESOURCES_TABLE = "organizationfolders_resources";
public const FOLDER_RESOURCES_TABLE = "organizationfolders_folder_resources"; public const FOLDER_RESOURCES_TABLE = "organizationfolders_folder_resources";
private const updateableResourceProperties = ["parentResource", "active", "lastUpdatedTimestamp"]; private const updateableResourceProperties = ["parentResource", "active", "name", "lastUpdatedTimestamp"];
private const updateableFolderResourceProperties = ["membersAclPermission", "managersAclPermission", "inheritedAclPermission"]; private const updateableFolderResourceProperties = ["membersAclPermission", "managersAclPermission", "inheritedAclPermission"];
public function __construct(IDBConnection $db) { public function __construct(IDBConnection $db) {
@ -75,6 +75,25 @@ class ResourceMapper extends QBMapper {
return $this->findEntities($qb); return $this->findEntities($qb);
} }
public function existsWithName(int $organizationFolderId, ?int $parentResourceId, string $name): bool {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select($qb->createFunction('COUNT(1)'))
->from(self::RESOURCES_TABLE)
->where($qb->expr()->eq('organization_folder_id', $qb->createNamedParameter($organizationFolderId, IQueryBuilder::PARAM_INT)));
if(is_null($parentResourceId)) {
$qb->andWhere($qb->expr()->isNull('parent_resource'));
} else {
$qb->andWhere($qb->expr()->eq('parent_resource', $qb->createNamedParameter($parentResourceId, IQueryBuilder::PARAM_INT)));
}
$qb->andWhere($qb->expr()->eq('name', $qb->createNamedParameter($name, IQueryBuilder::PARAM_STR)));
return $qb->executeQuery()->fetch()["COUNT(1)"] === 1;
}
/** /**
* Creates a new entry in the db from an entity * Creates a new entry in the db from an entity
* *
@ -87,7 +106,7 @@ class ResourceMapper extends QBMapper {
*/ */
public function insert(Entity $entity): Entity { public function insert(Entity $entity): Entity {
$setProperties = array_keys($entity->getUpdatedFields()); $setProperties = array_keys($entity->getUpdatedFields());
$setResourceProperties = array_intersect(["id", ...self::updateableResourceProperties], $setProperties); $setResourceProperties = ["type", "organizationFolderId", ...array_intersect(["id", ...self::updateableResourceProperties], $setProperties)];
if($entity->getType() === "folder") { if($entity->getType() === "folder") {
$typeSpecificTable = self::FOLDER_RESOURCES_TABLE; $typeSpecificTable = self::FOLDER_RESOURCES_TABLE;

View file

@ -0,0 +1,9 @@
<?php
namespace OCA\OrganizationFolders\Errors;
class ResourceNameNotUnique extends \RuntimeException {
public function __construct() {
parent::__construct("Resource cannot have the same name as sibling resource");
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace OCA\OrganizationFolders\Errors;
class ResourceNotFound extends NotFoundException {
public function __construct($id) {
parent::__construct(OCA\OrganizationFolders\Db\Resource::class, ["id" => $id]);
}
}

View file

@ -12,6 +12,7 @@ use OCA\OrganizationFolders\Db\FolderResource;
use OCA\OrganizationFolders\Db\ResourceMapper; use OCA\OrganizationFolders\Db\ResourceMapper;
use OCA\OrganizationFolders\Errors\InvalidResourceType; use OCA\OrganizationFolders\Errors\InvalidResourceType;
use OCA\OrganizationFolders\Errors\ResourceNotFound; use OCA\OrganizationFolders\Errors\ResourceNotFound;
use OCA\OrganizationFolders\Errors\ResourceNameNotUnique;
class ResourceService { class ResourceService {
public function __construct( public function __construct(
@ -19,6 +20,10 @@ class ResourceService {
) { ) {
} }
public function findAll(int $groupfolderId, int $parentResourceId = null) {
return $this->mapper->findAll($groupfolderId, $parentResourceId);
}
private function handleException(Exception $e, int $id): void { private function handleException(Exception $e, int $id): void {
if ($e instanceof DoesNotExistException || if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) { $e instanceof MultipleObjectsReturnedException) {
@ -40,6 +45,7 @@ class ResourceService {
public function create( public function create(
string $type, string $type,
int $organizationFolderId, int $organizationFolderId,
string $name,
?int $parentResource = null, ?int $parentResource = null,
bool $active = true, bool $active = true,
@ -53,30 +59,36 @@ class ResourceService {
throw new InvalidResourceType($type); throw new InvalidResourceType($type);
} }
$resource->setGroupFolderId($groupFolderId); if(!$this->mapper->existsWithName($organizationFolderId, $parentResource, $name)) {
$resource->setParentResource($parentResource); $resource->setGroupFolderId($organizationFolderId);
$resource->setActive($active); $resource->setName($name);
$resource->setLastUpdatedTimestamp(time()); $resource->setParentResource($parentResource);
$resource->setActive($active);
$resource->setLastUpdatedTimestamp(time());
if($type === "folder") { if($type === "folder") {
if(isset($membersAclPermission, $managersAclPermission, $inheritedAclPermission)) { if(isset($membersAclPermission, $managersAclPermission, $inheritedAclPermission)) {
$resource->setMembersAclPermission($membersAclPermission); $resource->setMembersAclPermission($membersAclPermission);
$resource->setManagersAclPermission($managersAclPermission); $resource->setManagersAclPermission($managersAclPermission);
$resource->setInheritedAclPermission($inheritedAclPermission); $resource->setInheritedAclPermission($inheritedAclPermission);
} else { } else {
throw new \InvalidArgumentException("Folder specific parameters must be included, when creating a resource of type folder"); throw new \InvalidArgumentException("Folder specific parameters must be included, when creating a resource of type folder");
}
} }
$resource = $this->mapper->insert($resource);
return $resource;
} else {
throw new ResourceNameNotUnique();
} }
$resource = $this->mapper->insert($resource);
return $resource;
} }
/* Use named arguments to call this function */ /* Use named arguments to call this function */
public function update( public function update(
int $id, int $id,
?string $name = null,
?int $parentResource = null, ?int $parentResource = null,
?bool $active = null, ?bool $active = null,
@ -90,6 +102,14 @@ class ResourceService {
$resource->setParentResource($parentResource); $resource->setParentResource($parentResource);
} }
if(isset($name)) {
if($this->mapper->existsWithName($resource->getGroupFolderId(), $resource->getParentResource(), $name)) {
throw new ResourceNameNotUnique();
} else {
$resource->setName($name);
}
}
if(isset($active)) { if(isset($active)) {
$resource->setActive($active); $resource->setActive($active);
} }