mirror of
https://github.com/verdigado/organization_folders.git
synced 2024-11-24 05:30:27 +01:00
Added organization folder member entity, mapper, service, enums and exceptions
This commit is contained in:
parent
ddb1854e28
commit
afb731e56b
5 changed files with 302 additions and 0 deletions
81
lib/Db/OrganizationFolderMember.php
Normal file
81
lib/Db/OrganizationFolderMember.php
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\OrganizationFolders\Db;
|
||||||
|
|
||||||
|
use JsonSerializable;
|
||||||
|
use OCA\OrganizationFolders\Interface\TableSerializable;
|
||||||
|
|
||||||
|
use OCP\AppFramework\Db\Entity;
|
||||||
|
|
||||||
|
use OCA\OrganizationFolders\Enum\OrganizationFolderMemberPermissionLevel;
|
||||||
|
use OCA\OrganizationFolders\Enum\PrincipalType;
|
||||||
|
use OCA\OrganizationFolders\Model\Principal;
|
||||||
|
|
||||||
|
class OrganizationFolderMember extends Entity implements JsonSerializable, TableSerializable {
|
||||||
|
protected $organizationFolderId;
|
||||||
|
protected $permissionLevel;
|
||||||
|
protected $principalType;
|
||||||
|
protected $principalId;
|
||||||
|
protected $createdTimestamp;
|
||||||
|
protected $lastUpdatedTimestamp;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->addType('organizationFolderId','integer');
|
||||||
|
$this->addType('permissionLevel','integer');
|
||||||
|
$this->addType('principalType','integer');
|
||||||
|
$this->addType('createdTimestamp','integer');
|
||||||
|
$this->addType('lastUpdatedTimestamp','integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPrincipal(): Principal {
|
||||||
|
return new Principal(PrincipalType::from($this->principalType), $this->principalId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPrincipal(Principal $principal) {
|
||||||
|
$principalType = $principal->getType();
|
||||||
|
if($principalType === PrincipalType::GROUP || $principalType === PrincipalType::ROLE) {
|
||||||
|
$this->setPrincipalType($principalType->value);
|
||||||
|
} else {
|
||||||
|
throw new \Exception("individual users are not allowed as organization folder members");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setPrincipalId($principal->getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPermissionLevel(int $permissionLevel) {
|
||||||
|
if($permissionLevel >= 1 && $permissionLevel <= 3) {
|
||||||
|
if ($permissionLevel === $this->permissionLevel) {
|
||||||
|
// no change
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->markFieldUpdated("permissionLevel");
|
||||||
|
$this->permissionLevel = $permissionLevel;
|
||||||
|
} else {
|
||||||
|
throw new \Exception("invalid organization folder member permission level");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize(): array {
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'organizationFolderId' => $this->organizationFolderId,
|
||||||
|
'permissionLevel' => $this->permissionLevel,
|
||||||
|
'principal' => $this->getPrincipal(),
|
||||||
|
'createdTimestamp' => $this->createdTimestamp,
|
||||||
|
'lastUpdatedTimestamp' => $this->lastUpdatedTimestamp,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tableSerialize(?array $params = null): array {
|
||||||
|
return [
|
||||||
|
'Id' => $this->id,
|
||||||
|
'Organization Folder Id' => $this->organizationFolderId,
|
||||||
|
'Permission Level' => OrganizationFolderMemberPermissionLevel::from($this->permissionLevel)->name,
|
||||||
|
'Principal Type' => PrincipalType::from($this->principalType)->name,
|
||||||
|
'Principal Id' => $this->principalId,
|
||||||
|
'Created' => $this->createdTimestamp,
|
||||||
|
'LastUpdated' => $this->lastUpdatedTimestamp,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
72
lib/Db/OrganizationFolderMemberMapper.php
Normal file
72
lib/Db/OrganizationFolderMemberMapper.php
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\OrganizationFolders\Db;
|
||||||
|
|
||||||
|
use OCP\AppFramework\Db\DoesNotExistException;
|
||||||
|
use OCP\AppFramework\Db\Entity;
|
||||||
|
use OCP\AppFramework\Db\QBMapper;
|
||||||
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||||
|
use OCP\IDBConnection;
|
||||||
|
|
||||||
|
class OrganizationFolderMemberMapper extends QBMapper {
|
||||||
|
public const ORGANIZATIONFOLDER_MEMBERS_TABLE = "organizationfolders_members";
|
||||||
|
|
||||||
|
public function __construct(IDBConnection $db) {
|
||||||
|
parent::__construct($db, self::ORGANIZATIONFOLDER_MEMBERS_TABLE, OrganizationFolderMember::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return Entity|OrganizationFolderMember
|
||||||
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
|
||||||
|
* @throws DoesNotExistException
|
||||||
|
*/
|
||||||
|
public function find(int $id): OrganizationFolderMember {
|
||||||
|
/* @var $qb IQueryBuilder */
|
||||||
|
$qb = $this->db->getQueryBuilder();
|
||||||
|
|
||||||
|
$qb->select('*')
|
||||||
|
->from(self::ORGANIZATIONFOLDER_MEMBERS_TABLE)
|
||||||
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
|
||||||
|
|
||||||
|
return $this->findEntity($qb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $organizationFolderId
|
||||||
|
* @param array{permissionLevel: int, principalType: int} $filters
|
||||||
|
* @return array
|
||||||
|
* @psalm-return OrganizationFolderMember[]
|
||||||
|
*/
|
||||||
|
public function findAll(int $organizationFolderId, array $filters = []): array {
|
||||||
|
/* @var $qb IQueryBuilder */
|
||||||
|
$qb = $this->db->getQueryBuilder();
|
||||||
|
|
||||||
|
$qb->select('*')
|
||||||
|
->from(self::ORGANIZATIONFOLDER_MEMBERS_TABLE)
|
||||||
|
->where($qb->expr()->eq('organization_folder_id', $qb->createNamedParameter($organizationFolderId, IQueryBuilder::PARAM_INT)));
|
||||||
|
|
||||||
|
if(isset($filters["permissionLevel"])) {
|
||||||
|
$qb->andWhere($qb->expr()->eq('permission_level', $qb->createNamedParameter($filters["permissionLevel"], IQueryBuilder::PARAM_INT)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isset($filters["principalType"])) {
|
||||||
|
$qb->andWhere($qb->expr()->eq('principal_type', $qb->createNamedParameter($filters["principalType"], IQueryBuilder::PARAM_INT)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->findEntities($qb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exists(int $organizationFolderId, int $principalType, string $principalId): bool {
|
||||||
|
/* @var $qb IQueryBuilder */
|
||||||
|
$qb = $this->db->getQueryBuilder();
|
||||||
|
|
||||||
|
$qb->select($qb->createFunction('COUNT(1)'))
|
||||||
|
->from(self::ORGANIZATIONFOLDER_MEMBERS_TABLE)
|
||||||
|
->where($qb->expr()->eq('organization_folder_id', $qb->createNamedParameter($organizationFolderId, IQueryBuilder::PARAM_INT)))
|
||||||
|
->andWhere($qb->expr()->eq('principal_type', $qb->createNamedParameter($principalType, IQueryBuilder::PARAM_INT)))
|
||||||
|
->andWhere($qb->expr()->eq('principal_id', $qb->createNamedParameter($principalId)));
|
||||||
|
|
||||||
|
return $qb->executeQuery()->fetch()["COUNT(1)"] === 1;
|
||||||
|
}
|
||||||
|
}
|
11
lib/Enum/OrganizationFolderMemberPermissionLevel.php
Normal file
11
lib/Enum/OrganizationFolderMemberPermissionLevel.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\OrganizationFolders\Enum;
|
||||||
|
|
||||||
|
enum OrganizationFolderMemberPermissionLevel: int {
|
||||||
|
use FromNameEnum;
|
||||||
|
|
||||||
|
case MEMBER = 1;
|
||||||
|
case MANAGER = 2;
|
||||||
|
case ADMIN = 3;
|
||||||
|
}
|
9
lib/Errors/OrganizationFolderMemberNotFound.php
Normal file
9
lib/Errors/OrganizationFolderMemberNotFound.php
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\OrganizationFolders\Errors;
|
||||||
|
|
||||||
|
class OrganizationFolderMemberNotFound extends NotFoundException {
|
||||||
|
public function __construct($id) {
|
||||||
|
parent::__construct(\OCA\OrganizationFolders\Db\OrganizationFolderMember::class, ["id" => $id]);
|
||||||
|
}
|
||||||
|
}
|
129
lib/Service/OrganizationFolderMemberService.php
Normal file
129
lib/Service/OrganizationFolderMemberService.php
Normal file
|
@ -0,0 +1,129 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\OrganizationFolders\Service;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
use OCP\AppFramework\Db\DoesNotExistException;
|
||||||
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||||
|
|
||||||
|
use OCA\OrganizationFolders\Errors\OrganizationFolderMemberNotFound;
|
||||||
|
|
||||||
|
use OCA\OrganizationFolders\Db\OrganizationFolderMember;
|
||||||
|
use OCA\OrganizationFolders\Db\OrganizationFolderMemberMapper;
|
||||||
|
use OCA\OrganizationFolders\Enum\OrganizationFolderMemberPermissionLevel;
|
||||||
|
use OCA\OrganizationFolders\Enum\PrincipalType;
|
||||||
|
use OCA\OrganizationFolders\Model\Principal;
|
||||||
|
|
||||||
|
class OrganizationFolderMemberService {
|
||||||
|
public function __construct(
|
||||||
|
protected OrganizationFolderMemberMapper $mapper,
|
||||||
|
protected OrganizationFolderService $organizationFolderService,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $organizationFolderId
|
||||||
|
* @param array{permissionLevel: OrganizationFolderMemberPermissionLevel, principalType: PrincipalType} $filters
|
||||||
|
* @return array
|
||||||
|
* @psalm-return OrganizationFolderMember[]
|
||||||
|
*/
|
||||||
|
public function findAll(int $organizationFolderId, $filters = []): array {
|
||||||
|
$mapperFilters = [
|
||||||
|
"permissionLevel" => $filters['permissionLevel']?->value ?: null,
|
||||||
|
"principalType" => $filters['principalType']?->value ?: null,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this->mapper->findAll($organizationFolderId, $mapperFilters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws OrganizationFolderMemberNotFound
|
||||||
|
*/
|
||||||
|
|
||||||
|
private function handleException(Exception $e): void {
|
||||||
|
if ($e instanceof DoesNotExistException ||
|
||||||
|
$e instanceof MultipleObjectsReturnedException) {
|
||||||
|
throw new OrganizationFolderMemberNotFound($e->getMessage());
|
||||||
|
} else {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @psalm-param int $id
|
||||||
|
* @throws OrganizationFolderMemberNotFound
|
||||||
|
*/
|
||||||
|
public function find(int $id): OrganizationFolderMember {
|
||||||
|
try {
|
||||||
|
return $this->mapper->find($id);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->handleException($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(
|
||||||
|
int $organizationFolderId,
|
||||||
|
OrganizationFolderMemberPermissionLevel $permissionLevel,
|
||||||
|
Principal $principal,
|
||||||
|
): OrganizationFolderMember {
|
||||||
|
$organizationFolder = $this->organizationFolderService->find($organizationFolderId);
|
||||||
|
|
||||||
|
$member = new OrganizationFolderMember();
|
||||||
|
|
||||||
|
$member->setOrganizationFolderId($organizationFolder->getId());
|
||||||
|
$member->setPermissionLevel($permissionLevel->value);
|
||||||
|
$member->setPrincipal($principal);
|
||||||
|
|
||||||
|
$creationTime = time();
|
||||||
|
$member->setCreatedTimestamp($creationTime);
|
||||||
|
$member->setLastUpdatedTimestamp($creationTime);
|
||||||
|
|
||||||
|
$member = $this->mapper->insert($member);
|
||||||
|
|
||||||
|
$this->organizationFolderService->applyPermissions($organizationFolder->getId());
|
||||||
|
|
||||||
|
return $member;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(int $id, ?OrganizationFolderMemberPermissionLevel $permissionLevel = null, ?Principal $principal = null): OrganizationFolderMember {
|
||||||
|
try {
|
||||||
|
$member = $this->mapper->find($id);
|
||||||
|
|
||||||
|
if(isset($permissionLevel)) {
|
||||||
|
$member->setPermissionLevel($permissionLevel->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isset($principal)) {
|
||||||
|
$member->setPrincipal($principal);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($member->getUpdatedFields()) > 0) {
|
||||||
|
$member->setLastUpdatedTimestamp(time());
|
||||||
|
|
||||||
|
$member = $this->mapper->update($member);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->organizationFolderService->applyPermissions($member->getOrganizationFolderId());
|
||||||
|
|
||||||
|
return $member;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->handleException($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(int $id): OrganizationFolderMember {
|
||||||
|
try {
|
||||||
|
$member = $this->mapper->find($id);
|
||||||
|
|
||||||
|
$this->mapper->delete($member);
|
||||||
|
|
||||||
|
$this->organizationFolderService->applyPermissions($member->getOrganizationFolderId());
|
||||||
|
|
||||||
|
return $member;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->handleException($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue