From fdb4b8fc767ff8cda4899addf43e6ac43edd3a27 Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Wed, 6 Nov 2024 17:32:33 +0100 Subject: [PATCH] Added inherit managers attribute to resources --- lib/Command/Resource/CreateResource.php | 6 +++++- lib/Command/Resource/UpdateResource.php | 16 +++++++++++++++- lib/Db/FolderResource.php | 3 +++ lib/Db/Resource.php | 2 ++ lib/Db/ResourceMapper.php | 2 +- lib/Db/ResourceMember.php | 19 +++++++++++++++++++ .../Version000000Date20241014120000.php | 3 +++ lib/Service/ResourceService.php | 18 +++++++++++++++++- 8 files changed, 65 insertions(+), 4 deletions(-) diff --git a/lib/Command/Resource/CreateResource.php b/lib/Command/Resource/CreateResource.php index 2f32a42..258f970 100644 --- a/lib/Command/Resource/CreateResource.php +++ b/lib/Command/Resource/CreateResource.php @@ -17,7 +17,8 @@ class CreateResource extends BaseCommand { ->addOption('organization-folder', null, InputOption::VALUE_REQUIRED, 'Id of organization folder to create resource in') ->addOption('type', null, InputOption::VALUE_REQUIRED, 'Type of resource (valid values: folder)') ->addOption('name', null, InputOption::VALUE_REQUIRED, 'Name of resource') - ->addOption('parent-resource', null, InputOption::VALUE_OPTIONAL, 'Id of parent resource (leave out if creating at top level in organization folder)'); + ->addOption('parent-resource', null, InputOption::VALUE_OPTIONAL, 'Id of parent resource (leave out if creating at top level in organization folder)') + ->addOption('inherit-managers', null, InputOption::VALUE_REQUIRED, 'Wether managers of the parent level (parent resource or organization folder for top level resources) should have management permissions'); // folder type options $this @@ -33,6 +34,7 @@ class CreateResource extends BaseCommand { $type = $input->getOption('type'); $name = $input->getOption('name'); $parentResource = $input->getOption('parent-resource'); + $inheritManagers = $input->getOption('inherit-managers') === true || $input->getOption('inherit-managers') === "true"; $membersAclPermission = $input->getOption('members-acl-permission'); $managersAclPermission = $input->getOption('managers-acl-permission'); @@ -44,6 +46,8 @@ class CreateResource extends BaseCommand { organizationFolderId: $organizationFolder, name: $name, parentResourceId: $parentResource, + active: true, + inheritManagers : $inheritManagers, membersAclPermission: $membersAclPermission, managersAclPermission: $managersAclPermission, diff --git a/lib/Command/Resource/UpdateResource.php b/lib/Command/Resource/UpdateResource.php index dd09829..1dcd864 100644 --- a/lib/Command/Resource/UpdateResource.php +++ b/lib/Command/Resource/UpdateResource.php @@ -16,7 +16,9 @@ class UpdateResource extends BaseCommand { ->setName('organization-folders:resources:update') ->setDescription('Update a resource') ->addArgument('id', InputArgument::REQUIRED, 'Id of the resource to update') - ->addOption('name', null, InputOption::VALUE_OPTIONAL, 'New name of resource'); + ->addOption('name', null, InputOption::VALUE_OPTIONAL, 'New name of resource') + ->addOption('active', null, InputOption::VALUE_OPTIONAL, 'Activate/deactivate resource') + ->addOption('inherit-managers', null, InputOption::VALUE_OPTIONAL, 'Set wether managers of the parent level (parent resource or organization folder for top level resources) should have management permissions'); // folder type options $this @@ -31,6 +33,16 @@ class UpdateResource extends BaseCommand { $id = $input->getArgument('id'); $name = $input->getOption('name'); + $activeOption = $input->getOption('active'); + if(!is_null($activeOption)) { + $active = $activeOption === true || $activeOption === "true"; + } + + $inheritManagersOption = $input->getOption('inherit-managers'); + if(!is_null($inheritManagersOption)) { + $inheritManagers = $inheritManagersOption === true || $inheritManagersOption === "true"; + } + $membersAclPermission = $input->getOption('members-acl-permission'); $managersAclPermission = $input->getOption('managers-acl-permission'); $inheritedAclPermission = $input->getOption('inherited-acl-permission'); @@ -39,6 +51,8 @@ class UpdateResource extends BaseCommand { $resource = $this->resourceService->update( id: $id, name: $name, + active: $active, + inheritManagers: $inheritManagers, membersAclPermission: $membersAclPermission, managersAclPermission: $managersAclPermission, diff --git a/lib/Db/FolderResource.php b/lib/Db/FolderResource.php index 837f31f..c68ff7f 100644 --- a/lib/Db/FolderResource.php +++ b/lib/Db/FolderResource.php @@ -24,6 +24,7 @@ class FolderResource extends Resource { $instance->setOrganizationFolderId($row["organization_folder_id"]); $instance->setName($row["name"]); $instance->setActive($row["active"]); + $instance->setInheritManagers($row["inherit_managers"]); $instance->setLastUpdatedTimestamp($row["last_updated_timestamp"]); $instance->setMembersAclPermission($row["members_acl_permission"]); $instance->setManagersAclPermission($row["managers_acl_permission"]); @@ -43,6 +44,7 @@ class FolderResource extends Resource { 'type' => "folder", 'name' => $this->name, 'active' => $this->active, + 'inheritManagers' => $this->inheritManagers, 'lastUpdatedTimestamp' => $this->lastUpdatedTimestamp, 'membersAclPermission' => $this->membersAclPermission, @@ -58,6 +60,7 @@ class FolderResource extends Resource { 'Name' => $this->name, 'Type' => "Folder", 'Active' => ((bool)$this->active) ? 'yes' : 'no', + 'Inherit Managers' => ((bool)$this->inheritManagers) ? 'yes' : 'no', 'Last Updated' => $this->lastUpdatedTimestamp, 'Members ACL Permission' => $this->membersAclPermission, diff --git a/lib/Db/Resource.php b/lib/Db/Resource.php index 386679e..5304561 100644 --- a/lib/Db/Resource.php +++ b/lib/Db/Resource.php @@ -12,12 +12,14 @@ abstract class Resource extends Entity implements JsonSerializable, TableSeriali protected $parentResource; protected $name; protected $active; + protected $inheritManagers; protected $lastUpdatedTimestamp; public function __construct() { $this->addType('organizationFolderId','integer'); $this->addType('parentResource','integer'); $this->addType('active','bool'); + $this->addType('inheritManagers','bool'); $this->addType('lastUpdatedTimestamp','integer'); } diff --git a/lib/Db/ResourceMapper.php b/lib/Db/ResourceMapper.php index 1ca5f36..d27e64b 100644 --- a/lib/Db/ResourceMapper.php +++ b/lib/Db/ResourceMapper.php @@ -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", "name", "lastUpdatedTimestamp"]; + private const updateableResourceProperties = ["parentResource", "active", "name", "inheritManagers", "lastUpdatedTimestamp"]; private const updateableFolderResourceProperties = ["membersAclPermission", "managersAclPermission", "inheritedAclPermission", "fileId"]; public function __construct(IDBConnection $db) { diff --git a/lib/Db/ResourceMember.php b/lib/Db/ResourceMember.php index 97b5110..d41a8cb 100644 --- a/lib/Db/ResourceMember.php +++ b/lib/Db/ResourceMember.php @@ -49,4 +49,23 @@ class ResourceMember extends Entity implements JsonSerializable, TableSerializab 'LastUpdated' => $this->lastUpdatedTimestamp, ]; } + + public function getParsedPrincipal() { + if($this->type === MemberType::USER->value) { + return [ + "userId" => $this->principal, + ]; + } else if($this->type === MemberType::GROUP->value) { + return [ + "groupId" => $this->principal, + ]; + } else if($this->type === MemberType::ROLE->value) { + [$organizationProviderId, $roleId] = explode(":", $this->principal, 2); + + return [ + "organizationProviderId" => $organizationProviderId, + "roleId" => $roleId, + ]; + } + } } diff --git a/lib/Migration/Version000000Date20241014120000.php b/lib/Migration/Version000000Date20241014120000.php index 7fa881a..d986c3e 100644 --- a/lib/Migration/Version000000Date20241014120000.php +++ b/lib/Migration/Version000000Date20241014120000.php @@ -49,6 +49,9 @@ class Version000000Date20241014120000 extends SimpleMigrationStep { $table->addColumn('active', Types::BOOLEAN, [ 'notnull' => true, ]); + $table->addColumn('inherit_managers', Types::BOOLEAN, [ + 'notnull' => true, + ]); $table->addColumn('last_updated_timestamp', Types::BIGINT, [ 'notnull' => true, ]); diff --git a/lib/Service/ResourceService.php b/lib/Service/ResourceService.php index a9ad341..e31ff4b 100644 --- a/lib/Service/ResourceService.php +++ b/lib/Service/ResourceService.php @@ -67,6 +67,7 @@ class ResourceService { string $name, ?int $parentResourceId = null, bool $active = true, + bool $inheritManagers = true, ?int $membersAclPermission = null, ?int $managersAclPermission = null, @@ -82,6 +83,7 @@ class ResourceService { $resource->setOrganizationFolderId($organizationFolderId); $resource->setName($name); $resource->setActive($active); + $resource->setInheritManagers($inheritManagers); $resource->setLastUpdatedTimestamp(time()); if(isset($parentResourceId)) { @@ -131,6 +133,7 @@ class ResourceService { ?string $name = null, ?int $parentResource = null, ?bool $active = null, + ?bool $inheritManagers = null, ?int $membersAclPermission = null, ?int $managersAclPermission = null, @@ -160,6 +163,10 @@ class ResourceService { $resource->setActive($active); } + if(isset($inheritManagers)) { + $resource->setInheritManagers($inheritManagers); + } + if($resource->getType() === "folder") { if(isset($membersAclPermission)) { $resource->setMembersAclPermission($membersAclPermission); @@ -231,7 +238,8 @@ class ResourceService { } else if($resourceMember->getType() === MemberType::GROUP->value) { $mapping = $this->userMappingManager->mappingFromId("group", $resourceMember->getPrincipal()); } else if($resourceMember->getType() === MemberType::ROLE->value) { - [$organizationProviderId, $roleId] = explode(":", $resourceMember->getPrincipal(), 2); + ['organizationProviderId' => $organizationProviderId, 'roleId' => $roleId] = $resourceMember->getParsedPrincipal(); + $organizationProvider = $this->organizationProviderManager->getOrganizationProvider($organizationProviderId); $role = $organizationProvider->getRole($roleId); $mapping = $this->userMappingManager->mappingFromId("group", $role->getMembersGroup()); @@ -296,6 +304,14 @@ class ResourceService { return $subResources; } + public function getParentResource(Resource $resource): ?Resource { + if(!is_null($resource->getParentResource())) { + return $this->find($resource->getParentResource()); + } else { + return null; + } + } + public function deleteById(int $id): Resource { try { $resource = $this->mapper->find($id);