0
0
Fork 0
mirror of https://github.com/verdigado/organization_folders.git synced 2024-12-06 11:22:41 +01:00

added resource members

This commit is contained in:
Jonathan Treffler 2024-10-31 17:29:15 +01:00
parent 851fa0a1cc
commit 92c431e856
14 changed files with 311 additions and 20 deletions

View file

@ -8,6 +8,7 @@ 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\Interface\TableSerializable;
abstract class BaseCommand extends Base {
@ -16,19 +17,20 @@ abstract class BaseCommand extends Base {
private readonly IDateTimeFormatter $dateTimeFormatter,
protected readonly OrganizationFolderService $organizationFolderService,
protected ResourceService $resourceService,
protected ResourceMemberService $resourceMemberService,
) {
parent::__construct();
}
protected function formatTableSerializable(TableSerializable $serializable): array {
return $serializable->tableSerialize();
protected function formatTableSerializable(TableSerializable $serializable, ?array $params = null): array {
return $serializable->tableSerialize($params);
}
protected function formatOrganizationFolders(array $organizationFolders) {
return array_map($this->formatTableSerializable(...), $organizationFolders);
}
protected function formatResources(array $resources): array {
return array_map($this->formatTableSerializable(...), $resources);
protected function formatTableSerializables(array $serializables, ?array $params = null): array {
$result = [];
foreach($serializables as $serializable) {
$result[] = $serializable->tableSerialize($params);
}
return $result;
}
}

View file

@ -21,7 +21,7 @@ class ListOrganizationFolders extends BaseCommand {
try {
$organizationFolderGroupfolders = $this->organizationFolderService->getAll();
$this->writeTableInOutputFormat($input, $output, $this->formatOrganizationFolders($organizationFolderGroupfolders));
$this->writeTableInOutputFormat($input, $output, $this->formatTableSerializables($organizationFolderGroupfolders));
return 0;
} catch (Exception $e) {
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");

View file

@ -27,7 +27,7 @@ class ListResources extends BaseCommand {
$resources = $this->resourceService->findAll($organizationFolderId, $parentResourceId);
$this->writeTableInOutputFormat($input, $output, $this->formatResources($resources));
$this->writeTableInOutputFormat($input, $output, $this->formatTableSerializables($resources));
return 0;
} catch (Exception $e) {
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");

View file

@ -0,0 +1,48 @@
<?php
namespace OCA\OrganizationFolders\Command\ResourceMember;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use OCA\OrganizationFolders\Command\BaseCommand;
use OCA\OrganizationFolders\Enum\MemberType;
use OCA\OrganizationFolders\Enum\MemberPermissionLevel;
class CreateResourceMember extends BaseCommand {
protected function configure(): void {
$this
->setName('organization-folders:create-resource-member')
->setDescription('Create a new member of resource')
->addOption('resource-id', null, InputOption::VALUE_REQUIRED, 'Id of resource to create member of')
->addOption('permission-level', null, InputOption::VALUE_REQUIRED, 'Permissions level of member (valid values: MEMBER, MANAGER)')
->addOption('type', null, InputOption::VALUE_REQUIRED, 'Type of principal (valid values: USER, GROUP, ROLE)')
->addOption('principal', null, InputOption::VALUE_OPTIONAL, 'For type user: "[user_id]", for group: "[group_name]", for role: "[organization_provider_id]:[role_id]"');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$resourceId = $input->getOption('resource-id');
$permissionLevel = MemberPermissionLevel::fromNameOrValue($input->getOption('permission-level'));
$type = MemberType::fromNameOrValue($input->getOption('type'));
$principal = $input->getOption('principal');
try {
$resource = $this->resourceMemberService->create(
resourceId: $resourceId,
permissionLevel: $permissionLevel,
type: $type,
principal: $principal,
);
$this->writeTableInOutputFormat($input, $output, [$this->formatTableSerializable($resource)]);
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,35 @@
<?php
namespace OCA\OrganizationFolders\Command\ResourceMember;
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 ListResourceMembers extends BaseCommand {
protected function configure(): void {
$this
->setName('organization-folders:list-resource-members')
->addArgument('resource-id', InputArgument::REQUIRED, 'Id of Resource')
->setDescription('List all members of resource.');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
try {
$resourceId = $input->getArgument('resource-id');
$members = $this->resourceMemberService->findAll($resourceId);
$this->writeTableInOutputFormat($input, $output, $this->formatTableSerializables($members));
return 0;
} catch (Exception $e) {
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
return 1;
}
}
}