diff --git a/bin/console b/bin/console index 7c2d146ea2..e2154bd2ab 100755 --- a/bin/console +++ b/bin/console @@ -23,7 +23,9 @@ use Alchemy\Phrasea\Command\SearchEngine\IndexPopulateCommand; use Alchemy\Phrasea\Command\Thesaurus\FindConceptsCommand; use Alchemy\Phrasea\Core\Version; use Alchemy\Phrasea\Command\CreateCollection; +use Alchemy\Phrasea\Command\Collection\ListCollectionCommand; use Alchemy\Phrasea\Command\Databox\CreateDataboxCommand; +use Alchemy\Phrasea\Command\Databox\ListDataboxCommand; use Alchemy\Phrasea\Command\MailTest; use Alchemy\Phrasea\Command\Compile\Configuration; use Alchemy\Phrasea\Command\RecordAdd; @@ -47,6 +49,7 @@ use Alchemy\Phrasea\Command\Task\TaskStart; use Alchemy\Phrasea\Command\Task\TaskState; use Alchemy\Phrasea\Command\Task\TaskStop; use Alchemy\Phrasea\Command\User\UserCreateCommand; +use Alchemy\Phrasea\Command\User\UserListCommand; use Alchemy\Phrasea\Command\UpgradeDBDatas; require_once __DIR__ . '/../lib/autoload.php'; @@ -109,9 +112,15 @@ $cli->command(new \module_console_fieldsRename('fields:rename')); $cli->command(new \module_console_fieldsMerge('fields:merge')); $cli->command(new CreateCollection('collection:create')); +$cli->command(new ListCollectionCommand('collection:list')); + +$cli->command(new ListDataboxCommand('databox:list')); $cli->command(new CreateDataboxCommand('databox:create')); + $cli->command(new UserCreateCommand('user:create')); +$cli->command(new UserListCommand('user:list')); + $cli->command(new RecordAdd('records:add')); $cli->command(new RescanTechnicalDatas('records:rescan-technical-datas')); diff --git a/lib/Alchemy/Phrasea/Command/Collection/ListCollectionCommand.php b/lib/Alchemy/Phrasea/Command/Collection/ListCollectionCommand.php new file mode 100644 index 0000000000..e76796e8b7 --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Collection/ListCollectionCommand.php @@ -0,0 +1,76 @@ +setDescription('List all collection in Phraseanet') + ->addOption('databox_id', 'd', InputOption::VALUE_REQUIRED, 'The id of the databox to list collection') + ->setHelp(''); + return $this; + } + protected function doExecute(InputInterface $input, OutputInterface $output) + { + try { + $databox = $this->container->findDataboxById($input->getOption('databox_id')); + $collections = $this->listDataboxCollections($databox); + + $table = $this->getHelperSet()->get('table'); + $table + ->setHeaders(['id local for API', 'id distant', 'name','label','status','total records']) + ->setRows($collections) + ->render($output); + } catch (\Exception $e) { + $output->writeln("{$e->getMessage()}"); + } + return 0; + } + + private function listDataboxCollections(\databox $databox) + { + return array_map(function (\collection $collection) { + return $this->listCollection($collection); + }, array_merge($databox->get_collections(),$this->getUnabledCollection($databox->get_activable_colls()))); + } + + private function getUnabledCollection($collections) + { + return array_map(function ($colId){ + return \collection::getByBaseId($this->container, $colId); + },$collections); + + } + + private function listCollection(\collection $collection) + { + return [ + $collection->get_base_id(), + $collection->get_coll_id(), + $collection->get_name(), + 'en: ' . $collection->get_label('en') . + ', de: ' . $collection->get_label('de') . + ', fr: ' . $collection->get_label('fr') . + ', nl: ' . $collection->get_label('nl'), + ($collection->is_active()) ? 'enabled' : 'disabled', + $collection->get_record_amount() + ]; + } +} \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Command/Databox/ListDataboxCommand.php b/lib/Alchemy/Phrasea/Command/Databox/ListDataboxCommand.php new file mode 100644 index 0000000000..49775437d5 --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Databox/ListDataboxCommand.php @@ -0,0 +1,62 @@ +setDescription('List all databox in Phraseanet') + ->setHelp(''); + + return $this; + } + + protected function doExecute(InputInterface $input, OutputInterface $output) + { + try { + $databoxes = array_map(function (\databox $databox) { + return $this->listDatabox($databox); + }, $this->container->getApplicationBox()->get_databoxes()); + + $table = $this->getHelperSet()->get('table'); + $table + ->setHeaders(['id', 'name', 'alias']) + ->setRows($databoxes) + ->render($output); + + } catch (\Exception $e) { + $output->writeln('Listing databox failed : '.$e->getMessage().''); + } + + return 0; + } + + private function listDatabox(\databox $databox) + { + return [ + $databox->get_sbas_id(), + $databox->get_dbname(), + $databox->get_viewname() + ]; + } + +} diff --git a/lib/Alchemy/Phrasea/Command/User/UserListCommand.php b/lib/Alchemy/Phrasea/Command/User/UserListCommand.php new file mode 100644 index 0000000000..1777c017cb --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/User/UserListCommand.php @@ -0,0 +1,266 @@ +setDescription('List of all user') + ->addOption('user_id', null, InputOption::VALUE_OPTIONAL, ' The id of user export only info this user ') + ->addOption('user_email', null, InputOption::VALUE_OPTIONAL, 'The mail of user export only info this user .') + ->addOption('database_id', null, InputOption::VALUE_OPTIONAL, 'Id of database.') + ->addOption('collection_id', null, InputOption::VALUE_OPTIONAL, 'Id of the collection.') + ->addOption('mail_lock_status', null, InputOption::VALUE_NONE, 'Status by mail locked') + ->addOption('guest', null, InputOption::VALUE_NONE, 'Only guest user') + ->addOption('created', null, InputOption::VALUE_OPTIONAL, 'Created at with operator,aaaa-mm-jj hh:mm:ss.') + ->addOption('updated', null, InputOption::VALUE_OPTIONAL, 'Update at with operator,aaaa-mm-jj hh:mm:ss.') + ->addOption('application', null, InputOption::VALUE_NONE, 'List application of user work only if --user_id is set') + ->addOption('right', null, InputOption::VALUE_NONE, 'Show right information') + ->addOption('adress', null, InputOption::VALUE_NONE, 'Show adress information') + ->setHelp(''); + + return $this; + } + + protected function doExecute(InputInterface $input, OutputInterface $output) + { + + $userId = $input->getOption('user_id'); + $userEmail = $input->getOption('user_email'); + $databaseId = $input->getOption('database_id'); + $collectionId = $input->getOption('collection_id'); + $lockStatus = $input->getOption('mail_lock_status'); + $guest = $input->getOption('guest'); + $application = $input->getOption('application'); + $withAdress = $input->getOption('adress'); + $created = $input->getOption('created'); + $updated = $input->getOption('updated'); + $withRight = $input->getOption('right'); + + $query = $this->container['phraseanet.user-query']; + + if($databaseId) $query->on_base_ids([$databaseId]); + if($collectionId) $query->on_sbas_ids([$collectionId]); + if($created) $this->addFilterDate($created,'created',$query); + if($updated) $this->addFilterDate($updated,'updated',$query); + if($userId) $query->addSqlFilter('Users.id = ?' ,[$userId]); + if($userEmail) $query->addSqlFilter('Users.email = ?' ,[$userEmail]); + if($lockStatus) $query->addSqlFilter('Users.mail_locked = 1'); + if($guest) $query->include_invite(true)->addSqlFilter('Users.guest = 1'); + + if ($application and !$userId) { + $output->writeln('You must provide --user_id when using --application option'); + return 0; + } + + $users = $query + ->execute()->get_results(); + + $userList = []; + $showApplication = false; + foreach ($users as $key => $user) { + if ($userId and $application) { + $showApplication = true; + } + $userList[] = $this->listUser($user,$withAdress,$withRight); + } + + $table = $this->getHelperSet()->get('table'); + $table + ->setHeaders($this->headerTable($withAdress,$withRight)) + ->setRows($userList) + ->render($output); + ; + + if ($showApplication) { + $applicationTable = $this->getHelperSet()->get('table'); + $applicationTable->setHeaders(array( + array(new TableCell('Applications', array('colspan' => 5))), + ['name','callback','client_secret','client_id','token'], + ))->setRows($this->getApplicationOfUser($users[0]))->render($output); + } + + return 0; + } + + /** + * @param $withAdress + * @param $withRight + * @return array + */ + private function headerTable($withAdress,$withRight) + { + $defaultHeader = ['id', 'login', 'email','last_model','first_name','last_name','gender','created','updated','status','locale']; + $adressHeader = [ 'address', 'zip_code', 'city', 'country', 'phone', 'fax', 'job','position', 'company', 'geoname_id']; + $rightHeader = [ 'admin', 'guest', 'mail_notification', 'ldap_created', 'mail_locked']; + + return $this->createInformation($withAdress,$withRight,$defaultHeader,['adress' => $adressHeader,'right' =>$rightHeader]); + } + + /** + * @param User $user + * @param $withAdress + * @param $withRight + * @return array + */ + private function listUser(User $user,$withAdress,$withRight) + { + switch ($user->getGender()) { + case User::GENDER_MRS: + $gender = 'Mrs'; + break; + case User::GENDER_MISS: + $gender = 'Miss'; + break; + case User::GENDER_MR: + default: + $gender = 'Mr'; + } + + $defaultInfo = [ + $user->getId(), + $user->getLogin() ?: '-', + $user->getEmail() ?: '-', + $user->getLastAppliedTemplate() ? $user->getLastAppliedTemplate()->getLogin() : '-', + $user->getFirstName() ?: '-', + $user->getLastName() ?: '-', + $gender, + NullableDateTime::format($user->getCreated(),'Y-m-d H:i:s'), + NullableDateTime::format($user->getUpdated(),'Y-m-d H:i:s'), + 'status', + $user->getLocale() ?: '-', + ]; + + return $this->createInformation($withAdress,$withRight,$defaultInfo,['adress' => $this->userAdress($user),'right' => $this->userRight($user)]); + } + + /** + * @param User $user + * @return array + */ + private function userAdress(User $user) + { + return [ + $user->getAddress() ?: '-', + $user->getZipCode() ?: '-', + $user->getCity() ?: '-', + $user->getCountry() ?: '-', + $user->getPhone() ?: '-', + $user->getFax() ?: '-', + $user->getJob() ?: '-', + $user->getActivity() ?: '-', + $user->getCompany() ?: '-', + $user->getGeonameId() ?: '-', + ]; + } + + /** + * @param User $user + * @return array + */ + private function userRight(User $user) + { + return [ + $user->isAdmin() ?: false, + $user->isGuest() ?: false, + $user->hasMailNotificationsActivated() ?: false, + $user->hasLdapCreated() ?: false, + $user->isMailLocked() ?: false, + ]; + } + + /** + * @param User $user + * @return array + */ + private function getApplicationOfUser(User $user) + { + $apiRepository = $this->container['repo.api-applications']; + $applications = $apiRepository->findByUser($user); + + if (empty($applications)) { + return []; + } + + $accountRepository = $this->container['repo.api-accounts']; + $apiOauthRepository = $this->container['repo.api-oauth-tokens']; + $usersApplication = []; + foreach ($applications as $application) { + $account = $accountRepository->findByUserAndApplication($user, $application); + $token = $account ? $apiOauthRepository->findDeveloperToken($account) : null; + $usersApplication[] = [ + $application->getName(), + $application->getRedirectUri(), + $application->getClientSecret(), + $application->getClientId(), + ($token) ? $token->getOauthToken() : '-' + ]; + } + + return $usersApplication; + } + + /** + * @param $withAdress + * @param $withRight + * @param $default + * @param $infoToMerge + * @return array + */ + private function createInformation($withAdress,$withRight,$default,$infoToMerge) + { + if ($withAdress && $withRight) { + $information = array_merge($default, $infoToMerge['adress'],$infoToMerge['right']); + } elseif ($withAdress && !$withRight) { + $information = array_merge($default, $infoToMerge['adress']); + } elseif(!$withAdress && $withRight) { + $information = array_merge($default, $infoToMerge['right']); + } else { + $information = $default; + } + + return $information; + } + + /** + * @param $date + * @param $type + * @param $query + */ + private function addFilterDate($date,$type,$query){ + + list($operator,$dateAt) = explode(',', $date); + + if (!in_array($operator,['=','>=','<=','>','<'])) { + throw new \InvalidArgumentException(" '=' or '<=' or '>=' or '>' or '<'"); + } + + $query->addSqlFilter($type.$operator.' ?' ,[$dateAt]); + } + +}