0
0
Fork 0
mirror of https://github.com/verdigado/organization_folders.git synced 2024-11-21 20:28:11 +01:00

added occ commands to inspect organization providers, organizations and organization roles

This commit is contained in:
Jonathan Treffler 2024-11-03 02:19:17 +01:00
parent 21f3a39b06
commit f85a93aa3a
11 changed files with 202 additions and 9 deletions

View file

@ -27,5 +27,9 @@
<command>OCA\OrganizationFolders\Command\Resource\ListResources</command> <command>OCA\OrganizationFolders\Command\Resource\ListResources</command>
<command>OCA\OrganizationFolders\Command\ResourceMember\CreateResourceMember</command> <command>OCA\OrganizationFolders\Command\ResourceMember\CreateResourceMember</command>
<command>OCA\OrganizationFolders\Command\ResourceMember\ListResourceMembers</command> <command>OCA\OrganizationFolders\Command\ResourceMember\ListResourceMembers</command>
<command>OCA\OrganizationFolders\Command\OrganizationProvider\ListOrganizationProviders</command>
<command>OCA\OrganizationFolders\Command\OrganizationProvider\ListOrganizations</command>
<command>OCA\OrganizationFolders\Command\OrganizationProvider\ListOrganizationRoles</command>
<command>OCA\OrganizationFolders\Command\OrganizationProvider\GetOrganizationRole</command>
</commands> </commands>
</info> </info>

View file

@ -5,10 +5,10 @@ namespace OCA\OrganizationFolders\Command;
use OC\Core\Command\Base; use OC\Core\Command\Base;
use OCP\IDateTimeFormatter; use OCP\IDateTimeFormatter;
use OCA\OrganizationFolders\Model\OrganizationFolder;
use OCA\OrganizationFolders\Service\OrganizationFolderService; use OCA\OrganizationFolders\Service\OrganizationFolderService;
use OCA\OrganizationFolders\Service\ResourceService; use OCA\OrganizationFolders\Service\ResourceService;
use OCA\OrganizationFolders\Service\ResourceMemberService; use OCA\OrganizationFolders\Service\ResourceMemberService;
use OCA\OrganizationFolders\OrganizationProvider\OrganizationProviderManager;
use OCA\OrganizationFolders\Interface\TableSerializable; use OCA\OrganizationFolders\Interface\TableSerializable;
abstract class BaseCommand extends Base { abstract class BaseCommand extends Base {
@ -18,6 +18,7 @@ abstract class BaseCommand extends Base {
protected readonly OrganizationFolderService $organizationFolderService, protected readonly OrganizationFolderService $organizationFolderService,
protected ResourceService $resourceService, protected ResourceService $resourceService,
protected ResourceMemberService $resourceMemberService, protected ResourceMemberService $resourceMemberService,
protected OrganizationProviderManager $organizationProviderManager,
) { ) {
parent::__construct(); parent::__construct();
} }

View file

@ -14,7 +14,7 @@ class GetOrganizationFolder extends BaseCommand {
$this $this
->setName('organization-folders:get') ->setName('organization-folders:get')
->setDescription('Get organization folder by id') ->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(); parent::configure();
} }

View file

@ -14,7 +14,7 @@ class RemoveOrganizationFolder extends BaseCommand {
$this $this
->setName('organization-folders:remove') ->setName('organization-folders:remove')
->setDescription('Remove a new organization folder') ->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 { protected function execute(InputInterface $input, OutputInterface $output): int {

View file

@ -0,0 +1,41 @@
<?php
namespace OCA\OrganizationFolders\Command\OrganizationProvider;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use OCA\OrganizationFolders\Command\BaseCommand;
class GetOrganizationRole extends BaseCommand {
protected function configure(): void {
$this
->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("<error>organization provider not found</error>");
return 0;
}
$role = $this->organizationProviderManager->getOrganizationProvider($providerId)->getRole($roleId);
$this->writeTableInOutputFormat($input, $output, [$this->formatTableSerializable($role)]);
return 0;
} catch (Exception $e) {
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
return 1;
}
}
}

View file

@ -0,0 +1,39 @@
<?php
namespace OCA\OrganizationFolders\Command\OrganizationProvider;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use OCA\OrganizationFolders\Command\BaseCommand;
class ListOrganizationProviders extends BaseCommand {
protected function configure(): void {
$this
->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("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
return 1;
}
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace OCA\OrganizationFolders\Command\OrganizationProvider;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use OCA\OrganizationFolders\Command\BaseCommand;
class ListOrganizationRoles extends BaseCommand {
protected function configure(): void {
$this
->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("<error>organization provider not found</error>");
return 0;
}
$roles = $this->organizationProviderManager->getOrganizationProvider($providerId)->getRolesOfOrganization($organizationId);
$this->writeTableInOutputFormat($input, $output, $this->formatTableSerializables($roles));
return 0;
} catch (Exception $e) {
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
return 1;
}
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace OCA\OrganizationFolders\Command\OrganizationProvider;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use OCA\OrganizationFolders\Command\BaseCommand;
class ListOrganizations extends BaseCommand {
protected function configure(): void {
$this
->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("<error>organization provider not found</error>");
return 0;
}
$organizations = $this->organizationProviderManager->getOrganizationProvider($providerId)->getSubOrganizations($parentOrganizationId);
$this->writeTableInOutputFormat($input, $output, $this->formatTableSerializables($organizations));
return 0;
} catch (Exception $e) {
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
return 1;
}
}
}

View file

@ -2,7 +2,9 @@
namespace OCA\OrganizationFolders\Model; namespace OCA\OrganizationFolders\Model;
class Organization implements \JsonSerializable { use OCA\OrganizationFolders\Interface\TableSerializable;
class Organization implements \JsonSerializable, TableSerializable {
public function __construct( public function __construct(
private int $id, private int $id,
private string $membersGroup, private string $membersGroup,
@ -23,4 +25,11 @@ class Organization implements \JsonSerializable {
'membersGroup' => $this->membersGroup, 'membersGroup' => $this->membersGroup,
]; ];
} }
public function tableSerialize(?array $params = null): array {
return [
'Id' => $this->id,
'Members Group' => $this->membersGroup,
];
}
} }

View file

@ -2,16 +2,18 @@
namespace OCA\OrganizationFolders\Model; namespace OCA\OrganizationFolders\Model;
class OrganizationRole implements \JsonSerializable { use OCA\OrganizationFolders\Interface\TableSerializable;
class OrganizationRole implements \JsonSerializable, TableSerializable {
public function __construct( public function __construct(
private int $id, private string $id,
private int $organizationId, private int $organizationId,
private string $friendlyName, private string $friendlyName,
private string $membersGroup, private string $membersGroup,
) { ) {
} }
public function getId(): int { public function getId(): string {
return $this->id; return $this->id;
} }
@ -35,4 +37,13 @@ class OrganizationRole implements \JsonSerializable {
'membersGroup' => $this->membersGroup, '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,
];
}
} }

View file

@ -57,7 +57,7 @@ abstract class OrganizationProvider {
* *
* @return Organization[] * @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) * 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 * @return OrganizationRole
* @throws OrganizationRoleNotFound * @throws OrganizationRoleNotFound
*/ */
abstract public function getRole(int $id): OrganizationRole; abstract public function getRole(string $id): OrganizationRole;
/** /**
* Get all roles of a specific organization * Get all roles of a specific organization