mirror of
https://github.com/verdigado/organization_folders.git
synced 2024-12-06 11:22:41 +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,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue