mirror of
https://github.com/verdigado/organization_folders.git
synced 2024-11-24 13:40:27 +01:00
ensure resource name is unique
This commit is contained in:
parent
949e1a42d1
commit
af661c5c26
4 changed files with 74 additions and 17 deletions
|
@ -17,7 +17,7 @@ class ResourceMapper extends QBMapper {
|
|||
public const RESOURCES_TABLE = "organizationfolders_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"];
|
||||
|
||||
public function __construct(IDBConnection $db) {
|
||||
|
@ -75,6 +75,25 @@ class ResourceMapper extends QBMapper {
|
|||
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
|
||||
*
|
||||
|
@ -87,7 +106,7 @@ class ResourceMapper extends QBMapper {
|
|||
*/
|
||||
public function insert(Entity $entity): Entity {
|
||||
$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") {
|
||||
$typeSpecificTable = self::FOLDER_RESOURCES_TABLE;
|
||||
|
|
9
lib/Errors/ResourceNameNotUnique.php
Normal file
9
lib/Errors/ResourceNameNotUnique.php
Normal 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");
|
||||
}
|
||||
}
|
9
lib/Errors/ResourceNotFound.php
Normal file
9
lib/Errors/ResourceNotFound.php
Normal 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]);
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ use OCA\OrganizationFolders\Db\FolderResource;
|
|||
use OCA\OrganizationFolders\Db\ResourceMapper;
|
||||
use OCA\OrganizationFolders\Errors\InvalidResourceType;
|
||||
use OCA\OrganizationFolders\Errors\ResourceNotFound;
|
||||
use OCA\OrganizationFolders\Errors\ResourceNameNotUnique;
|
||||
|
||||
class ResourceService {
|
||||
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 {
|
||||
if ($e instanceof DoesNotExistException ||
|
||||
$e instanceof MultipleObjectsReturnedException) {
|
||||
|
@ -40,6 +45,7 @@ class ResourceService {
|
|||
public function create(
|
||||
string $type,
|
||||
int $organizationFolderId,
|
||||
string $name,
|
||||
?int $parentResource = null,
|
||||
bool $active = true,
|
||||
|
||||
|
@ -53,30 +59,36 @@ class ResourceService {
|
|||
throw new InvalidResourceType($type);
|
||||
}
|
||||
|
||||
$resource->setGroupFolderId($groupFolderId);
|
||||
$resource->setParentResource($parentResource);
|
||||
$resource->setActive($active);
|
||||
$resource->setLastUpdatedTimestamp(time());
|
||||
if(!$this->mapper->existsWithName($organizationFolderId, $parentResource, $name)) {
|
||||
$resource->setGroupFolderId($organizationFolderId);
|
||||
$resource->setName($name);
|
||||
$resource->setParentResource($parentResource);
|
||||
$resource->setActive($active);
|
||||
$resource->setLastUpdatedTimestamp(time());
|
||||
|
||||
if($type === "folder") {
|
||||
if(isset($membersAclPermission, $managersAclPermission, $inheritedAclPermission)) {
|
||||
$resource->setMembersAclPermission($membersAclPermission);
|
||||
$resource->setManagersAclPermission($managersAclPermission);
|
||||
$resource->setInheritedAclPermission($inheritedAclPermission);
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Folder specific parameters must be included, when creating a resource of type folder");
|
||||
if($type === "folder") {
|
||||
if(isset($membersAclPermission, $managersAclPermission, $inheritedAclPermission)) {
|
||||
$resource->setMembersAclPermission($membersAclPermission);
|
||||
$resource->setManagersAclPermission($managersAclPermission);
|
||||
$resource->setInheritedAclPermission($inheritedAclPermission);
|
||||
} else {
|
||||
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 */
|
||||
public function update(
|
||||
int $id,
|
||||
|
||||
?string $name = null,
|
||||
?int $parentResource = null,
|
||||
?bool $active = null,
|
||||
|
||||
|
@ -90,6 +102,14 @@ class ResourceService {
|
|||
$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)) {
|
||||
$resource->setActive($active);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue