mirror of
https://github.com/verdigado/organization_folders.git
synced 2024-12-06 11:22:41 +01:00
split principal types into different subclasses, added principal factory, implemented principal friendlyNames and FullHierarchyNames, added parentOrganizationId attribute to organizations
This commit is contained in:
parent
225072bff7
commit
89ff5415dd
15 changed files with 321 additions and 47 deletions
36
lib/Model/GroupPrincipal.php
Normal file
36
lib/Model/GroupPrincipal.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OrganizationFolders\Model;
|
||||
|
||||
use OCP\IGroupManager;
|
||||
use \OCP\IGroup;
|
||||
|
||||
use OCA\OrganizationFolders\Enum\PrincipalType;
|
||||
|
||||
class GroupPrincipal extends Principal {
|
||||
private ?IGroup $group;
|
||||
|
||||
public function __construct(
|
||||
private IGroupManager $groupManager,
|
||||
private string $id,
|
||||
) {
|
||||
try {
|
||||
$this->group = $this->groupManager->get($id);
|
||||
$this->valid = !is_null($this->group);
|
||||
} catch (\Exception $e) {
|
||||
$this->valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getType(): PrincipalType {
|
||||
return PrincipalType::GROUP;
|
||||
}
|
||||
|
||||
public function getId(): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getFriendlyName(): string {
|
||||
return $this->group?->getDisplayName();
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ class Organization implements \JsonSerializable, TableSerializable {
|
|||
private int $id,
|
||||
private string $friendlyName,
|
||||
private string $membersGroup,
|
||||
private ?int $parentOrganizationId = null,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -24,6 +25,10 @@ class Organization implements \JsonSerializable, TableSerializable {
|
|||
return $this->membersGroup;
|
||||
}
|
||||
|
||||
public function getParentOrganizationId(): ?int {
|
||||
return $this->parentOrganizationId;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
|
|
|
@ -4,26 +4,30 @@ namespace OCA\OrganizationFolders\Model;
|
|||
|
||||
use OCA\OrganizationFolders\Enum\PrincipalType;
|
||||
|
||||
class Principal implements \JsonSerializable {
|
||||
public function __construct(
|
||||
private PrincipalType $type,
|
||||
private string $id,
|
||||
) {
|
||||
// check if id fits format
|
||||
}
|
||||
abstract class Principal implements \JsonSerializable {
|
||||
protected bool $valid;
|
||||
|
||||
public function getType(): PrincipalType {
|
||||
return $this->type;
|
||||
}
|
||||
abstract public function getType(): PrincipalType;
|
||||
|
||||
public function getId(): string {
|
||||
return $this->id;
|
||||
abstract public function getId(): string;
|
||||
|
||||
abstract public function getFriendlyName(): string;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @psalm-return string[]
|
||||
*/
|
||||
public function getFullHierarchyNames(): array {
|
||||
return [$this->getFriendlyName()];
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array {
|
||||
return [
|
||||
'type' => $this->type->value,
|
||||
'id' => $this->id,
|
||||
'type' => $this->getType(),
|
||||
'id' => $this->getId(),
|
||||
'valid' => $this->valid,
|
||||
'friendlyName' => $this->getFriendlyName(),
|
||||
'fullHierarchyNames' => $this->getFullHierarchyNames(),
|
||||
];
|
||||
}
|
||||
}
|
34
lib/Model/PrincipalFactory.php
Normal file
34
lib/Model/PrincipalFactory.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OrganizationFolders\Model;
|
||||
|
||||
use OCP\IUserManager;
|
||||
use OCP\IGroupManager;
|
||||
|
||||
use OCA\OrganizationFolders\Enum\PrincipalType;
|
||||
use OCA\OrganizationFolders\OrganizationProvider\OrganizationProviderManager;
|
||||
|
||||
class PrincipalFactory {
|
||||
public function __construct(
|
||||
protected IUserManager $userManager,
|
||||
protected IGroupManager $groupManager,
|
||||
protected OrganizationProviderManager $organizationProviderManager,
|
||||
) {
|
||||
}
|
||||
|
||||
public function buildPrincipal(PrincipalType $type, string $id): Principal {
|
||||
if($type === PrincipalType::USER) {
|
||||
return new UserPrincipal($this->userManager, $id);
|
||||
} else if ($type === PrincipalType::GROUP) {
|
||||
return new GroupPrincipal($this->groupManager, $id);
|
||||
} else if ($type === PrincipalType::ROLE) {
|
||||
[$organizationProviderId, $roleId] = explode(":", $id, 2);
|
||||
if(!(isset($organizationProviderId) && isset($roleId))) {
|
||||
throw new \Exception("Invalid id format for principal of type role");
|
||||
}
|
||||
return new RolePrincipal($this->organizationProviderManager, $organizationProviderId, $roleId);
|
||||
} else {
|
||||
throw new \Exception("invalid PrincipalType");
|
||||
}
|
||||
}
|
||||
}
|
50
lib/Model/RolePrincipal.php
Normal file
50
lib/Model/RolePrincipal.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OrganizationFolders\Model;
|
||||
|
||||
use OCA\OrganizationFolders\Enum\PrincipalType;
|
||||
use OCA\OrganizationFolders\OrganizationProvider\OrganizationProviderManager;
|
||||
|
||||
class RolePrincipal extends Principal {
|
||||
private ?OrganizationRole $role;
|
||||
|
||||
public function __construct(
|
||||
private OrganizationProviderManager $organizationProviderManager,
|
||||
private string $providerId,
|
||||
private string $roleId,
|
||||
) {
|
||||
try {
|
||||
$this->role = $this->organizationProviderManager->getOrganizationProvider($providerId)->getRole($roleId);
|
||||
$this->valid = !is_null($this->role);
|
||||
} catch (\Exception $e) {
|
||||
$this->valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getType(): PrincipalType {
|
||||
return PrincipalType::ROLE;
|
||||
}
|
||||
|
||||
public function getId(): string {
|
||||
return $this->providerId . ":" . $this->roleId;
|
||||
}
|
||||
|
||||
public function getFriendlyName(): string {
|
||||
return $this->role->getFriendlyName();
|
||||
}
|
||||
|
||||
public function getFullHierarchyNames(): array {
|
||||
$result = [];
|
||||
$result[] = $this->getFriendlyName();
|
||||
|
||||
$organizationProvider = $this->organizationProviderManager->getOrganizationProvider($this->providerId);
|
||||
$organization = $organizationProvider->getOrganization($this->role->getOrganizationId());
|
||||
$result[] = $organization->getFriendlyName();
|
||||
|
||||
while($organization->getParentOrganizationId() && $organization = $organizationProvider->getOrganization($organization->getParentOrganizationId())) {
|
||||
$result[] = $organization->getFriendlyName();
|
||||
}
|
||||
|
||||
return array_reverse($result);
|
||||
}
|
||||
}
|
36
lib/Model/UserPrincipal.php
Normal file
36
lib/Model/UserPrincipal.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OrganizationFolders\Model;
|
||||
|
||||
use OCP\IUserManager;
|
||||
use \OCP\IUser;
|
||||
|
||||
use OCA\OrganizationFolders\Enum\PrincipalType;
|
||||
|
||||
class UserPrincipal extends Principal {
|
||||
private ?IUser $user;
|
||||
|
||||
public function __construct(
|
||||
private IUserManager $userManager,
|
||||
private string $id,
|
||||
) {
|
||||
try {
|
||||
$this->user = $this->userManager->get($id);
|
||||
$this->valid = !is_null($this->user);
|
||||
} catch (\Exception $e) {
|
||||
$this->valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getType(): PrincipalType {
|
||||
return PrincipalType::USER;
|
||||
}
|
||||
|
||||
public function getId(): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getFriendlyName(): string {
|
||||
return $this->user?->getDisplayName();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue