From 44ef7891bbb2932077b0363f77d3f3d85030bdda Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 27 Feb 2020 17:51:57 +0300 Subject: [PATCH] add command application:app to create edit delete and list application in phraseanet --- bin/console | 3 + .../Application/ApplicationAppCommand.php | 281 ++++++++++++++++++ 2 files changed, 284 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Command/Application/ApplicationAppCommand.php diff --git a/bin/console b/bin/console index c973195ff6..1dfa643be2 100755 --- a/bin/console +++ b/bin/console @@ -31,6 +31,7 @@ use Alchemy\Phrasea\Command\Compile\Configuration; use Alchemy\Phrasea\Command\RecordAdd; use Alchemy\Phrasea\Command\RescanTechnicalDatas; use Alchemy\Phrasea\CLI; +use Alchemy\Phrasea\Command\Application\ApplicationAppCommand; use Alchemy\Phrasea\Command\Plugin\AddPlugin; use Alchemy\Phrasea\Command\Plugin\RemovePlugin; use Alchemy\Phrasea\Command\CheckConfig; @@ -112,6 +113,8 @@ $cli->command(new \module_console_fieldsDelete('fields:delete')); $cli->command(new \module_console_fieldsRename('fields:rename')); $cli->command(new \module_console_fieldsMerge('fields:merge')); +$cli->command(new ApplicationAppCommand('application:app')); + $cli->command(new CreateCollection('collection:create')); $cli->command(new ListCollectionCommand('collection:list')); diff --git a/lib/Alchemy/Phrasea/Command/Application/ApplicationAppCommand.php b/lib/Alchemy/Phrasea/Command/Application/ApplicationAppCommand.php new file mode 100644 index 0000000000..5494c4f73b --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Application/ApplicationAppCommand.php @@ -0,0 +1,281 @@ +setDescription('Create, edit, delete application in Phraseanet (experimental)') + ->addOption('user_id', 'u', InputOption::VALUE_REQUIRED, 'The desired login for created user.') + ->addOption('app_id', 'a', InputOption::VALUE_REQUIRED, 'The application ID') + ->addOption('name', null, InputOption::VALUE_REQUIRED, 'The desired name for application user.') + ->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'The desired name for application user.',ApiApplication::WEB_TYPE) + ->addOption('description', 'd', InputOption::VALUE_REQUIRED, 'The desired description for application user.') + ->addOption('website', 'w', InputOption::VALUE_OPTIONAL, 'The desired url for application user.') + ->addOption('callback', 'c', InputOption::VALUE_OPTIONAL, 'The desired url for application user.') + ->addOption('webhook_url', null, InputOption::VALUE_REQUIRED, 'Define a webhook url on edit') + ->addOption('create_token', null, InputOption::VALUE_NONE, 'Generate an access token when app is created') + ->addOption('activate_password', null, InputOption::VALUE_OPTIONAL, 'Activate password OAuth2 grant type , values true or false', 'false') + ->addOption('create', null, InputOption::VALUE_NONE, 'Create application for user in Phraseanet') + ->addOption('edit', null, InputOption::VALUE_NONE, 'Edit application in Phraseanet work only if app_id and user_id are set') + ->addOption('delete', null, InputOption::VALUE_NONE, 'Delete application in Phraseanet, need app_id') + ->addOption('list', null, InputOption::VALUE_NONE, 'List all application or user application if --user_id is set') + ->addOption('jsonformat', null, InputOption::VALUE_NONE, 'Output in json format') + + ->setHelp(''); + + return $this; + } + + protected function doExecute(InputInterface $input, OutputInterface $output) + { + $userId = $input->getOption('user_id'); + $appId = $input->getOption('app_id'); + $name = $input->getOption('name'); + $type = $input->getOption('type'); + $description = $input->getOption('description'); + $website = $input->getOption('website'); + $urlCallback = $input->getOption('callback'); + $webhookUrl = $input->getOption('webhook_url'); + $createToken = $input->getOption('create_token'); + $activatePassword = $input->getOption('activate_password'); + $create = $input->getOption('create'); + $edit = $input->getOption('edit'); + $delete = $input->getOption('delete'); + $list = $input->getOption('list'); + $jsonformat = $input->getOption('jsonformat'); + + $applicationManipulator = $this->container['manipulator.api-application']; + $apiOauthTokenManipulator = $this->container['manipulator.api-oauth-token']; + $accountRepository = $this->container['repo.api-accounts']; + $apiApllicationConverter = $this->container['converter.api-application']; + $userRepository = $this->container['repo.users']; + $apiOauthRepository = $this->container['repo.api-oauth-tokens']; + + if ($create) { + if (null === $user = $userRepository->find($userId)) { + $output->writeln('User not found'); + return 0; + } + + if (!$name) { + $output->writeln('Name of application must be provide with option --name.'); + return 0; + } + + if (!$description) { + $output->writeln('Desciption of application must be provide.'); + + return 0; + } + + try { + $application = $applicationManipulator + ->create( + $name, + $type, + $description, + $website, + $user, + $urlCallback + ); + + $apiAccountManipulator = $this->container['manipulator.api-account']; + $apiAccountManipulator->create($application, $user, V2::VERSION); + + $account = $accountRepository->findByUserAndApplication($user, $application); + + if ($createToken) { + $apiOauthTokenManipulator->create($account); + } + + if ($activatePassword) { + if (in_array($activatePassword, ['true', 'false'])) { + $application->setGrantPassword(($activatePassword == 'true') ? true : false); + $applicationManipulator->update($application); + } else { + $output->writeln(' Value of option --activate_password should be "true" or "false"'); + + return 0; + } + } + + $this->showApllicationInformation($apiOauthRepository, $account, $application, $jsonformat, $output); + } catch (\Exception $e) { + $output->writeln('Create an application for user failed : '.$e->getMessage().''); + } + } elseif ($edit) { + if (!$appId) { + $output->writeln('ID of the application must be provided with option --app_id to edit the application.'); + + return 0; + } + + if (null === $user = $userRepository->find($userId)) { + $output->writeln('User not found'); + return 0; + } + + $application = $apiApllicationConverter->convert($appId); + $account = $accountRepository->findByUserAndApplication($user, $application); + + if (!$account) { + $output->writeln('ApiAccount not found!Check the given user_id and app_id!'); + + return 0; + } + + if ($name) { + $application->setName($name); + } + if ($type) { + $applicationManipulator->setType($application, $type); + } + if ($description) { + $application->setDescription($description); + } + if ($website) { + $applicationManipulator->setWebsiteUrl($application, $website); + } + if ($urlCallback) { + $applicationManipulator->setRedirectUri($application, $urlCallback); + } + if ($createToken) { + $apiOauthTokenManipulator->create($account); + } + if ($activatePassword) { + if (in_array($activatePassword, ['true', 'false'])) { + $application->setGrantPassword(($activatePassword == 'true') ? true : false); + } else { + $output->writeln(' Value of option --activate_password should be "true" or "false"'); + + return 0; + } + } + if ($webhookUrl) { + $applicationManipulator->setWebhookUrl($application, $webhookUrl); + } + + $applicationManipulator->update($application); + + $this->showApllicationInformation($apiOauthRepository, $account, $application, $jsonformat, $output); + } elseif ($list) { + if ($userId) { + if (null === $user = $userRepository->find($userId)) { + $output->writeln('User not found'); + + return 0; + } + + $accounts = $accountRepository->findByUser($user); + } else { + $accounts = $accountRepository->findAll(); + } + + $applicationList = []; + + foreach ($accounts as $account) { + $application = $account->getApplication(); + $token = $apiOauthRepository->findDeveloperToken($account); + + $applicationList[] = [ + $application->getId(), + $account->getUser()->getId(), + $application->getName(), + $application->getClientId(), + $application->getRedirectUri(), + ($token) ? $token->getOauthToken() : '-', + $application->isPasswordGranted() ? "true": "false" + ]; + } + + $applicationTable = $this->getHelperSet()->get('table'); + $headers = ['ID', 'user ID', 'Name', 'client ID', 'Callback Url', 'generated token', 'activate_password status']; + + if ($jsonformat ) { + foreach ($applicationList as $appList) { + $appInfo[] = array_combine($headers, $appList); + } + + echo json_encode($appInfo); + } else { + $applicationTable = $this->getHelperSet()->get('table'); + $applicationTable + ->setHeaders($headers) + ->setRows($applicationList) + ->render($output) + ; + } + } elseif ($delete) { + if (!$appId) { + $output->writeln('ID of the application must be provided with option --app_id to delete the app.'); + + return 0; + } + + $application = $apiApllicationConverter->convert($appId); + + $applicationManipulator->delete($application); + + $output->writeln("Application ID $appId deleted successfully !"); + } + + return 0; + } + + private function showApllicationInformation($apiOauthRepository, ApiAccount $account, ApiApplication $application, $jsonformat, $output) + { + $token = $account ? $apiOauthRepository->findDeveloperToken($account) : null; + + $applicationCreated = [ + $application->getClientSecret(), + $application->getClientId(), + $this->container["conf"]->get("servername") . "api/oauthv2/authorize", + $this->container["conf"]->get("servername") . "api/oauthv2/token", + ($token) ? $token->getOauthToken() : '-', + $application->isPasswordGranted() ? "true": "false" + ]; + + $headers = ['client secret', 'client ID', 'Authorize endpoint url', 'Access endpoint', 'generated token', 'activate_password status']; + if ($jsonformat ) { + $createdAppInfo = array_combine($headers, $applicationCreated); + echo json_encode($createdAppInfo); + } else { + $table = $this->getHelperSet()->get('table'); + $table + ->setHeaders($headers) + ->setRows([$applicationCreated]) + ->render($output) + ; + } + } +}