2024-10-31 17:29:15 +01:00
< ? 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 ;
2024-11-16 03:06:21 +01:00
use OCA\OrganizationFolders\Model\Principal ;
use OCA\OrganizationFolders\Enum\PrincipalType ;
use OCA\OrganizationFolders\Enum\ResourceMemberPermissionLevel ;
2024-10-31 17:29:15 +01:00
class CreateResourceMember extends BaseCommand {
protected function configure () : void {
$this
2024-11-05 14:05:04 +01:00
-> setName ( 'organization-folders:resource-members:create' )
2024-10-31 17:29:15 +01:00
-> 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)' )
2024-11-16 03:06:21 +01:00
-> addOption ( 'principal-type' , null , InputOption :: VALUE_REQUIRED , 'Type of principal (valid values: USER, GROUP, ROLE)' )
-> addOption ( 'principal-id' , null , InputOption :: VALUE_OPTIONAL , 'For type user: "[user_id]", for group: "[group_name]", for role: "[organization_provider_id]:[role_id]"' );
2024-10-31 17:29:15 +01:00
parent :: configure ();
}
protected function execute ( InputInterface $input , OutputInterface $output ) : int {
$resourceId = $input -> getOption ( 'resource-id' );
2024-11-16 03:06:21 +01:00
$permissionLevel = ResourceMemberPermissionLevel :: fromNameOrValue ( $input -> getOption ( 'permission-level' ));
$principalType = PrincipalType :: fromNameOrValue ( $input -> getOption ( 'principal-type' ));
$principalId = $input -> getOption ( 'principal-id' );
2024-10-31 17:29:15 +01:00
try {
2024-11-16 03:06:21 +01:00
$member = $this -> resourceMemberService -> create (
2024-10-31 17:29:15 +01:00
resourceId : $resourceId ,
permissionLevel : $permissionLevel ,
2024-11-16 03:06:21 +01:00
principal : new Principal ( $principalType , $principalId ),
2024-10-31 17:29:15 +01:00
);
2024-11-16 03:06:21 +01:00
$this -> writeTableInOutputFormat ( $input , $output , [ $this -> formatTableSerializable ( $member )]);
2024-10-31 17:29:15 +01:00
return 0 ;
} catch ( Exception $e ) {
$output -> writeln ( " <error>Exception \" { $e -> getMessage () } \" at { $e -> getFile () } line { $e -> getLine () } </error> " );
return 1 ;
}
}
}