From f85a93aa3af1edb7dc461e97507f2eabf998e717 Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Sun, 3 Nov 2024 02:19:17 +0100 Subject: [PATCH] added occ commands to inspect organization providers, organizations and organization roles --- appinfo/info.xml | 4 ++ lib/Command/BaseCommand.php | 3 +- .../GetOrganizationFolder.php | 2 +- .../RemoveOrganizationFolder.php | 2 +- .../GetOrganizationRole.php | 41 ++++++++++++++++ .../ListOrganizationProviders.php | 39 +++++++++++++++ .../ListOrganizationRoles.php | 41 ++++++++++++++++ .../ListOrganizations.php | 47 +++++++++++++++++++ lib/Model/Organization.php | 11 ++++- lib/Model/OrganizationRole.php | 17 +++++-- .../OrganizationProvider.php | 4 +- 11 files changed, 202 insertions(+), 9 deletions(-) create mode 100644 lib/Command/OrganizationProvider/GetOrganizationRole.php create mode 100644 lib/Command/OrganizationProvider/ListOrganizationProviders.php create mode 100644 lib/Command/OrganizationProvider/ListOrganizationRoles.php create mode 100644 lib/Command/OrganizationProvider/ListOrganizations.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 7d87662..c6baa37 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -27,5 +27,9 @@ OCA\OrganizationFolders\Command\Resource\ListResources OCA\OrganizationFolders\Command\ResourceMember\CreateResourceMember OCA\OrganizationFolders\Command\ResourceMember\ListResourceMembers + OCA\OrganizationFolders\Command\OrganizationProvider\ListOrganizationProviders + OCA\OrganizationFolders\Command\OrganizationProvider\ListOrganizations + OCA\OrganizationFolders\Command\OrganizationProvider\ListOrganizationRoles + OCA\OrganizationFolders\Command\OrganizationProvider\GetOrganizationRole \ No newline at end of file diff --git a/lib/Command/BaseCommand.php b/lib/Command/BaseCommand.php index 756f99c..82939d3 100644 --- a/lib/Command/BaseCommand.php +++ b/lib/Command/BaseCommand.php @@ -5,10 +5,10 @@ namespace OCA\OrganizationFolders\Command; use OC\Core\Command\Base; use OCP\IDateTimeFormatter; -use OCA\OrganizationFolders\Model\OrganizationFolder; use OCA\OrganizationFolders\Service\OrganizationFolderService; use OCA\OrganizationFolders\Service\ResourceService; use OCA\OrganizationFolders\Service\ResourceMemberService; +use OCA\OrganizationFolders\OrganizationProvider\OrganizationProviderManager; use OCA\OrganizationFolders\Interface\TableSerializable; abstract class BaseCommand extends Base { @@ -18,6 +18,7 @@ abstract class BaseCommand extends Base { protected readonly OrganizationFolderService $organizationFolderService, protected ResourceService $resourceService, protected ResourceMemberService $resourceMemberService, + protected OrganizationProviderManager $organizationProviderManager, ) { parent::__construct(); } diff --git a/lib/Command/OrganizationFolder/GetOrganizationFolder.php b/lib/Command/OrganizationFolder/GetOrganizationFolder.php index 21a6ce5..a348565 100644 --- a/lib/Command/OrganizationFolder/GetOrganizationFolder.php +++ b/lib/Command/OrganizationFolder/GetOrganizationFolder.php @@ -14,7 +14,7 @@ class GetOrganizationFolder extends BaseCommand { $this ->setName('organization-folders:get') ->setDescription('Get organization folder by id') - ->addArgument('id', null, InputArgument::REQUIRED, 'Id of the organization folder to get'); + ->addArgument('id', InputArgument::REQUIRED, 'Id of the organization folder to get'); parent::configure(); } diff --git a/lib/Command/OrganizationFolder/RemoveOrganizationFolder.php b/lib/Command/OrganizationFolder/RemoveOrganizationFolder.php index c70b9dc..1778c24 100644 --- a/lib/Command/OrganizationFolder/RemoveOrganizationFolder.php +++ b/lib/Command/OrganizationFolder/RemoveOrganizationFolder.php @@ -14,7 +14,7 @@ class RemoveOrganizationFolder extends BaseCommand { $this ->setName('organization-folders:remove') ->setDescription('Remove a new organization folder') - ->addArgument('id', null, InputArgument::REQUIRED, 'Id of the organization folder to remove'); + ->addArgument('id', InputArgument::REQUIRED, 'Id of the organization folder to remove'); } protected function execute(InputInterface $input, OutputInterface $output): int { diff --git a/lib/Command/OrganizationProvider/GetOrganizationRole.php b/lib/Command/OrganizationProvider/GetOrganizationRole.php new file mode 100644 index 0000000..fed3c65 --- /dev/null +++ b/lib/Command/OrganizationProvider/GetOrganizationRole.php @@ -0,0 +1,41 @@ +setName('organization-folders:get-organization-role') + ->setDescription('Get a specific organization role by id') + ->addArgument('provider-id', InputArgument::REQUIRED, 'provider to query') + ->addArgument('role-id', InputArgument::REQUIRED, ''); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + try { + $providerId = $input->getArgument('provider-id'); + $roleId = $input->getArgument('role-id'); + + if(!$this->organizationProviderManager->hasOrganizationProvider($providerId)) { + $output->writeln("organization provider not found"); + return 0; + } + + $role = $this->organizationProviderManager->getOrganizationProvider($providerId)->getRole($roleId); + + $this->writeTableInOutputFormat($input, $output, [$this->formatTableSerializable($role)]); + return 0; + } catch (Exception $e) { + $output->writeln("Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}"); + return 1; + } + } +} diff --git a/lib/Command/OrganizationProvider/ListOrganizationProviders.php b/lib/Command/OrganizationProvider/ListOrganizationProviders.php new file mode 100644 index 0000000..b10c05c --- /dev/null +++ b/lib/Command/OrganizationProvider/ListOrganizationProviders.php @@ -0,0 +1,39 @@ +setName('organization-folders:list-organization-providers') + ->setDescription('List all registered organization providers'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + try { + $organizationProviders = $this->organizationProviderManager->getOrganizationProviders(); + + $result = []; + + foreach($organizationProviders as $id => $organizationProvider) { + $result[] = [ + "Id" => $id, + "Class" => $organizationProvider::class, + ]; + } + + $this->writeTableInOutputFormat($input, $output, $result); + return 0; + } catch (Exception $e) { + $output->writeln("Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}"); + return 1; + } + } +} diff --git a/lib/Command/OrganizationProvider/ListOrganizationRoles.php b/lib/Command/OrganizationProvider/ListOrganizationRoles.php new file mode 100644 index 0000000..e9098a3 --- /dev/null +++ b/lib/Command/OrganizationProvider/ListOrganizationRoles.php @@ -0,0 +1,41 @@ +setName('organization-folders:list-organization-roles') + ->setDescription('List all roles in a specific organization') + ->addArgument('provider-id', InputArgument::REQUIRED, 'provider to query') + ->addArgument('organization-id', InputArgument::REQUIRED, 'organization id to query roles of'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + try { + $providerId = $input->getArgument('provider-id'); + $organizationId = (int)$input->getArgument('organization-id'); + + if(!$this->organizationProviderManager->hasOrganizationProvider($providerId)) { + $output->writeln("organization provider not found"); + return 0; + } + + $roles = $this->organizationProviderManager->getOrganizationProvider($providerId)->getRolesOfOrganization($organizationId); + + $this->writeTableInOutputFormat($input, $output, $this->formatTableSerializables($roles)); + return 0; + } catch (Exception $e) { + $output->writeln("Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}"); + return 1; + } + } +} diff --git a/lib/Command/OrganizationProvider/ListOrganizations.php b/lib/Command/OrganizationProvider/ListOrganizations.php new file mode 100644 index 0000000..701f6cc --- /dev/null +++ b/lib/Command/OrganizationProvider/ListOrganizations.php @@ -0,0 +1,47 @@ +setName('organization-folders:list-organizations') + ->setDescription('List all organizations provided by a specific organization provider') + ->addArgument('provider-id', InputArgument::REQUIRED, 'provider to query') + ->addArgument('parent-organization-id', InputArgument::OPTIONAL, 'parent organization to fetch child organizations of. Using top-level if omitted'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + try { + $providerId = $input->getArgument('provider-id'); + + if(ctype_digit($input->getArgument('parent-organization-id'))) { + $parentOrganizationId = (int)$input->getArgument('parent-organization-id'); + } else { + $parentOrganizationId = null; + } + + + if(!$this->organizationProviderManager->hasOrganizationProvider($providerId)) { + $output->writeln("organization provider not found"); + return 0; + } + + $organizations = $this->organizationProviderManager->getOrganizationProvider($providerId)->getSubOrganizations($parentOrganizationId); + + $this->writeTableInOutputFormat($input, $output, $this->formatTableSerializables($organizations)); + return 0; + } catch (Exception $e) { + $output->writeln("Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}"); + return 1; + } + } +} diff --git a/lib/Model/Organization.php b/lib/Model/Organization.php index fc9bece..e028c71 100644 --- a/lib/Model/Organization.php +++ b/lib/Model/Organization.php @@ -2,7 +2,9 @@ namespace OCA\OrganizationFolders\Model; -class Organization implements \JsonSerializable { +use OCA\OrganizationFolders\Interface\TableSerializable; + +class Organization implements \JsonSerializable, TableSerializable { public function __construct( private int $id, private string $membersGroup, @@ -23,4 +25,11 @@ class Organization implements \JsonSerializable { 'membersGroup' => $this->membersGroup, ]; } + + public function tableSerialize(?array $params = null): array { + return [ + 'Id' => $this->id, + 'Members Group' => $this->membersGroup, + ]; + } } \ No newline at end of file diff --git a/lib/Model/OrganizationRole.php b/lib/Model/OrganizationRole.php index 3fc0f87..3c3258b 100644 --- a/lib/Model/OrganizationRole.php +++ b/lib/Model/OrganizationRole.php @@ -2,16 +2,18 @@ namespace OCA\OrganizationFolders\Model; -class OrganizationRole implements \JsonSerializable { +use OCA\OrganizationFolders\Interface\TableSerializable; + +class OrganizationRole implements \JsonSerializable, TableSerializable { public function __construct( - private int $id, + private string $id, private int $organizationId, private string $friendlyName, private string $membersGroup, ) { } - public function getId(): int { + public function getId(): string { return $this->id; } @@ -35,4 +37,13 @@ class OrganizationRole implements \JsonSerializable { 'membersGroup' => $this->membersGroup, ]; } + + public function tableSerialize(?array $params = null): array { + return [ + 'Id' => $this->id, + 'Name' => $this->friendlyName, + 'Organization Id' => $this->organizationId, + 'Members Group' => $this->membersGroup, + ]; + } } \ No newline at end of file diff --git a/lib/OrganizationProvider/OrganizationProvider.php b/lib/OrganizationProvider/OrganizationProvider.php index 8c7d606..9e913d8 100644 --- a/lib/OrganizationProvider/OrganizationProvider.php +++ b/lib/OrganizationProvider/OrganizationProvider.php @@ -57,7 +57,7 @@ abstract class OrganizationProvider { * * @return Organization[] */ - abstract public function getSubOrganizations(?int $parentOrganizationId): array; + abstract public function getSubOrganizations(?int $parentOrganizationId = null): array; /** * Get a specific role by its id (must be unique within organization provider, not just within parent organization) @@ -65,7 +65,7 @@ abstract class OrganizationProvider { * @return OrganizationRole * @throws OrganizationRoleNotFound */ - abstract public function getRole(int $id): OrganizationRole; + abstract public function getRole(string $id): OrganizationRole; /** * Get all roles of a specific organization