mirror of
https://github.com/verdigado/organization_folders.git
synced 2024-11-21 20:28:11 +01:00
added resource occ commands
This commit is contained in:
parent
0f81059343
commit
840fd70c37
8 changed files with 169 additions and 2 deletions
|
@ -18,4 +18,8 @@
|
|||
<database>mysql</database>
|
||||
<nextcloud min-version="29" max-version="30"/>
|
||||
</dependencies>
|
||||
<commands>
|
||||
<command>OCA\OrganizationFolders\Command\Resource\CreateResource</command>
|
||||
<command>OCA\OrganizationFolders\Command\Resource\ListResources</command>
|
||||
</commands>
|
||||
</info>
|
34
lib/Command/BaseCommand.php
Normal file
34
lib/Command/BaseCommand.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
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\Interface\TableSerializable;
|
||||
|
||||
abstract class BaseCommand extends Base {
|
||||
|
||||
public function __construct(
|
||||
private readonly IDateTimeFormatter $dateTimeFormatter,
|
||||
protected readonly OrganizationFolderService $organizationFolderService,
|
||||
protected ResourceService $resourceService,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function formatTableSerializable(TableSerializable $serializable): array {
|
||||
return $serializable->tableSerialize();
|
||||
}
|
||||
|
||||
protected function formatOrganizationFolders(array $organizationFolders) {
|
||||
return array_map($this->formatTableSerializable(...), $organizationFolders);
|
||||
}
|
||||
|
||||
protected function formatResources(array $resources): array {
|
||||
return array_map($this->formatTableSerializable(...), $resources);
|
||||
}
|
||||
}
|
60
lib/Command/Resource/CreateResource.php
Normal file
60
lib/Command/Resource/CreateResource.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OrganizationFolders\Command\Resource;
|
||||
|
||||
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;
|
||||
|
||||
class CreateResource extends BaseCommand {
|
||||
protected function configure(): void {
|
||||
$this
|
||||
->setName('organization-folders:create-resource')
|
||||
->setDescription('Create a new resource in organization folder')
|
||||
->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)');
|
||||
|
||||
// folder type options
|
||||
$this
|
||||
->addOption('members-acl-permission', null, InputOption::VALUE_OPTIONAL, 'acl permissions for members of resource')
|
||||
->addOption('managers-acl-permission', null, InputOption::VALUE_OPTIONAL, 'acl permissions for managers of resource')
|
||||
->addOption('inherited-acl-permission', null, InputOption::VALUE_OPTIONAL, 'acl permissions for users with access to the resource level above (or organization in case resource is top-level)');
|
||||
|
||||
parent::configure();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$organizationFolder = $input->getOption('organization-folder');
|
||||
$type = $input->getOption('type');
|
||||
$name = $input->getOption('name');
|
||||
$parentResource = $input->getOption('parent-resource');
|
||||
|
||||
$membersAclPermission = $input->getOption('members-acl-permission');
|
||||
$managersAclPermission = $input->getOption('managers-acl-permission');
|
||||
$inheritedAclPermission = $input->getOption('inherited-acl-permission');
|
||||
|
||||
try {
|
||||
$resource = $this->resourceService->create(
|
||||
type: $type,
|
||||
organizationFolderId: $organizationFolder,
|
||||
name: $name,
|
||||
parentResource: $parentResource,
|
||||
|
||||
membersAclPermission: $membersAclPermission,
|
||||
managersAclPermission: $managersAclPermission,
|
||||
inheritedAclPermission: $inheritedAclPermission,
|
||||
);
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
37
lib/Command/Resource/ListResources.php
Normal file
37
lib/Command/Resource/ListResources.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OrganizationFolders\Command\Resource;
|
||||
|
||||
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 ListResources extends BaseCommand {
|
||||
protected function configure(): void {
|
||||
$this
|
||||
->setName('organization-folders:list-resources')
|
||||
->addArgument('organization-folder-id', InputArgument::REQUIRED, 'Id of Organization Folder')
|
||||
->addArgument('parent-resource-id', InputArgument::OPTIONAL, 'Id of Organization Folder')
|
||||
->setDescription('List all resource in organization folder. Only shows one layer of tree at once, provide resource parent id to reveal child resources.');
|
||||
|
||||
parent::configure();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
try {
|
||||
$organizationFolderId = $input->getArgument('organization-folder-id');
|
||||
$parentResourceId = $input->getArgument('parent-resource-id');
|
||||
|
||||
$resources = $this->resourceService->findAll($organizationFolderId, $parentResourceId);
|
||||
|
||||
$this->writeTableInOutputFormat($input, $output, $this->formatResources($resources));
|
||||
return 0;
|
||||
} catch (Exception $e) {
|
||||
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -47,4 +47,22 @@ class FolderResource extends Resource {
|
|||
'inheritedAclPermission' => $this->inheritedAclPermission,
|
||||
];
|
||||
}
|
||||
|
||||
public function tableSerialize(): array {
|
||||
return [
|
||||
'Id' => $this->id,
|
||||
'Name' => $this->name,
|
||||
'Type' => "Folder",
|
||||
'Active' => ((bool)$this->active) ? 'yes' : 'no',
|
||||
'Last Updated' => $this->lastUpdatedTimestamp,
|
||||
|
||||
'Members ACL Permission' => $this->membersAclPermission,
|
||||
'Managers ACL Permission' => $this->managersAclPermission,
|
||||
'Inherited ACL Permission' => $this->inheritedAclPermission,
|
||||
];
|
||||
}
|
||||
|
||||
public function getType(): string {
|
||||
return "folder";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,11 @@
|
|||
namespace OCA\OrganizationFolders\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
use OCA\OrganizationFolders\Interface\TableSerializable;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
|
||||
abstract class Resource extends Entity implements JsonSerializable {
|
||||
abstract class Resource extends Entity implements JsonSerializable, TableSerializable {
|
||||
protected $organizationFolderId;
|
||||
protected $parentResource;
|
||||
protected $name;
|
||||
|
@ -19,4 +20,6 @@ abstract class Resource extends Entity implements JsonSerializable {
|
|||
$this->addType('active','bool');
|
||||
$this->addType('lastUpdatedTimestamp','integer');
|
||||
}
|
||||
|
||||
abstract public function getType(): string;
|
||||
}
|
||||
|
|
11
lib/Interface/TableSerializable.php
Normal file
11
lib/Interface/TableSerializable.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\OrganizationFolders\Interface;
|
||||
|
||||
interface TableSerializable {
|
||||
/**
|
||||
* Serializes the object to a dict array to be rendered into a occ command output table
|
||||
* @return array
|
||||
*/
|
||||
function tableSerialize();
|
||||
}
|
|
@ -13,7 +13,7 @@ use OCA\OrganizationFolders\Errors\OrganizationRoleNotFound;
|
|||
abstract class OrganizationProvider {
|
||||
protected $id;
|
||||
|
||||
public function getId() {
|
||||
public function getId(): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue