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:
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 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;
|
||||||
|
|
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\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,7 +59,9 @@ class ResourceService {
|
||||||
throw new InvalidResourceType($type);
|
throw new InvalidResourceType($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
$resource->setGroupFolderId($groupFolderId);
|
if(!$this->mapper->existsWithName($organizationFolderId, $parentResource, $name)) {
|
||||||
|
$resource->setGroupFolderId($organizationFolderId);
|
||||||
|
$resource->setName($name);
|
||||||
$resource->setParentResource($parentResource);
|
$resource->setParentResource($parentResource);
|
||||||
$resource->setActive($active);
|
$resource->setActive($active);
|
||||||
$resource->setLastUpdatedTimestamp(time());
|
$resource->setLastUpdatedTimestamp(time());
|
||||||
|
@ -71,12 +79,16 @@ class ResourceService {
|
||||||
$resource = $this->mapper->insert($resource);
|
$resource = $this->mapper->insert($resource);
|
||||||
|
|
||||||
return $resource;
|
return $resource;
|
||||||
|
} else {
|
||||||
|
throw new ResourceNameNotUnique();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue