Merge branch 'master' into PHRAS-2741-worker-service-part1

This commit is contained in:
Nicolas Maillat
2020-03-02 20:15:32 +01:00
committed by GitHub
19 changed files with 827 additions and 243 deletions

View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\Collection;
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class PublishCollectionCommand extends Command
{
/**
* Constructor
*/
public function __construct($name = null)
{
parent::__construct('collection:publish');
$this->setDescription('Publish collection in Phraseanet')
->addOption('collection_id', null, InputOption::VALUE_REQUIRED, 'The base_id of the collection to publish but keep with existing right into present in application box.')
->setHelp('');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
try {
$collection = \collection::getByBaseId($this->container,(int)$input->getOption('collection_id'));
$dialog = $this->getHelperSet()->get('dialog');
do {
$continue = mb_strtolower($dialog->ask($output, '<question> Do you want really publish this collection? (y/N)</question>', 'N'));
} while ( ! in_array($continue, ['y', 'n']));
if ($continue !== 'y') {
$output->writeln('Aborting !');
return;
}
$collection->enable($this->container->getApplicationBox());
$output->writeln('<info>Publish collection successful</info>');
} catch (\Exception $e) {
$output->writeln('<error>Publish collection failed : '.$e->getMessage().'</error>');
}
return 0;
}
}

View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\Collection;
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class UnPublishCollectionCommand extends Command
{
/**
* Constructor
*/
public function __construct($name = null)
{
parent::__construct('collection:unpublish');
$this->setDescription('Unpublish collection in Phraseanet')
->addOption('collection_id', null, InputOption::VALUE_REQUIRED, 'The base_id of the collection to unpublish, the base_id is the same id used in API.')
->setHelp('');
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
try {
$collection = \collection::getByBaseId($this->container,(int)$input->getOption('collection_id'));
$dialog = $this->getHelperSet()->get('dialog');
do {
$continue = mb_strtolower($dialog->ask($output, sprintf("<question> Do you want really unpublish the collection %s? (y/N)</question>", $collection->get_name()), 'N'));
} while ( ! in_array($continue, ['y', 'n']));
if ($continue !== 'y') {
$output->writeln('<info>Aborting !</>');
return;
}
$collection->disable($this->container->getApplicationBox());
$output->writeln('<info>Unpublish collection successful</info>');
} catch (\Exception $e) {
$output->writeln('<error>Unpublish collection failed : '.$e->getMessage().'</error>');
}
return 0;
}
}

View File

@@ -1,7 +1,16 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2020 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\Databox;
use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Databox\DataboxConnectionSettings;
use Alchemy\Phrasea\Databox\DataboxService;
@@ -13,42 +22,84 @@ use Symfony\Component\Console\Output\OutputInterface;
class MountDataboxCommand extends Command
{
protected function configure()
/**
* Constructor
*/
public function __construct($name = null)
{
$this->setName('databox:mount')
->addArgument('databox', InputArgument::REQUIRED, 'Database name for the databox', null)
->addArgument('owner', InputArgument::REQUIRED, 'Email of the databox admin user', null)
->addOption('connection', 'c', InputOption::VALUE_NONE, 'Flag to set new database settings')
->addOption('db-host', null, InputOption::VALUE_OPTIONAL, 'MySQL server host', 'localhost')
->addOption('db-port', null, InputOption::VALUE_OPTIONAL, 'MySQL server port', 3306)
->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'MySQL server user', 'phrasea')
->addOption('db-password', null, InputOption::VALUE_OPTIONAL, 'MySQL server password', null);
parent::__construct('databox:mount');
$this->setDescription('Mount databox')
->addArgument('databox', InputArgument::REQUIRED, 'Database name in Mysql', null)
->addArgument('user_id', InputArgument::REQUIRED, 'The Id of user owner (this account became full admin on this databox)', null)
->addOption('db-host', null, InputOption::VALUE_OPTIONAL, 'MySQL server host')
->addOption('db-port', null, InputOption::VALUE_OPTIONAL, 'MySQL server port')
->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'MySQL server user')
->addOption('db-password', null, InputOption::VALUE_OPTIONAL, 'MySQL server password')
;
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$databoxName = $input->getArgument('databox');
$connectionSettings = $input->getOption('connection') == false ? null : new DataboxConnectionSettings(
$input->getOption('db-host'),
$input->getOption('db-port'),
$input->getOption('db-user'),
$input->getOption('db-password')
);
try {
/** @var UserRepository $userRepository */
$userRepository = $this->container['repo.users'];
/** @var DataboxService $databoxService */
$databoxService = $this->container['databox.service'];
/** @var UserRepository $userRepository */
$userRepository = $this->container['repo.users'];
$owner = $userRepository->findByEmail($input->getArgument('owner'));
$owner = $userRepository->find($input->getArgument('user_id'));
$databoxService->mountDatabox(
$databoxName,
$owner,
$connectionSettings
);
if (empty($owner)) {
$output->writeln('<error>User not found ! </error>');
$output->writeln('Databox mounted');
return;
}
if ($owner->isGuest() || !$this->container->getAclForUser($owner)->is_admin()) {
$output->writeln('<error>Admin role is required for the owner ! </error>');
return;
}
$databoxName = $input->getArgument('databox');
$dialog = $this->getHelperSet()->get('dialog');
$connectionSettings = new DataboxConnectionSettings(
$input->getOption('db-host')?:$this->container['conf']->get(['main', 'database', 'host']),
$input->getOption('db-port')?:$this->container['conf']->get(['main', 'database', 'port']),
$input->getOption('db-user')?:$this->container['conf']->get(['main', 'database', 'user']),
$input->getOption('db-password')?:$this->container['conf']->get(['main', 'database', 'password'])
);
do {
$continue = mb_strtolower($dialog->ask($output, '<question> Do you want really mount this databox? (y/N)</question>', 'N'));
}
while ( ! in_array($continue, ['y', 'n']));
if ($continue !== 'y') {
$output->writeln('Aborting !');
return;
}
/** @var DataboxService $databoxService */
$databoxService = $this->container['databox.service'];
\phrasea::clear_sbas_params($this->container);
$databox = $databoxService->mountDatabox(
$databoxName,
$owner,
$connectionSettings
);
$output->writeln("\n\t<info>Data-Box ID ".$databox->get_sbas_id()." mounted successful !</info>\n");
} catch (\Exception $e) {
$output->writeln('<error>Mount databox failed :'.$e->getMessage().'</error>');
}
return 0;
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\Databox;
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UnMountDataboxCommand extends Command
{
/**
* Constructor
*/
public function __construct($name = null)
{
parent::__construct('databox:unmount');
$this->setDescription('Unmount databox')
->addArgument('databox_id', InputArgument::REQUIRED, 'The id of the databox to unmount', null)
;
return $this;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
try {
$databox = $this->container->findDataboxById($input->getArgument('databox_id'));
$dialog = $this->getHelperSet()->get('dialog');
do {
$continue = mb_strtolower($dialog->ask($output, '<question> Do you want really unmount this databox? (y/N)</question>', 'N'));
} while ( ! in_array($continue, ['y', 'n']));
if ($continue !== 'y') {
$output->writeln('Aborting !');
return;
}
$databox->unmount_databox();
$output->writeln('<info>Unmount databox successful</info>');
} catch (\Exception $e) {
$output->writeln('<error>Unmount databox failed : '.$e->getMessage().'</error>');
}
return 0;
}
}

View File

@@ -0,0 +1,328 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2020 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\User;
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Helper\DialogHelper;
use Alchemy\Phrasea\ControllerProvider\Api\V2;
use Alchemy\Phrasea\Core\LazyLocator;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Alchemy\Phrasea\Model\Entities\ApiAccount;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Notification\Mail\MailRequestPasswordSetup;
use Alchemy\Phrasea\Notification\Mail\MailRequestEmailConfirmation;
use Alchemy\Phrasea\Notification\Receiver;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class UserApplicationsCommand extends Command
{
/**
* Constructor
*/
public function __construct($name = null)
{
parent::__construct('user:applications');
$this->setDescription('List, Create, Edit, Delete application in Phraseanet <comment>(experimental)</>')
->addOption('list', null, InputOption::VALUE_NONE, 'List all applications or user applications if --user_id is set')
->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 is set')
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete application in Phraseanet, require an app_id')
->addOption('user_id', 'u', InputOption::VALUE_REQUIRED, 'The Id of user owner of application (user_id), required to Create, Edit and Delete.')
->addOption('app_id', 'a', InputOption::VALUE_REQUIRED, 'The application ID, required for Edit and Delete')
->addOption('name', null, InputOption::VALUE_REQUIRED, 'The desired name for application, required for Create and Edit.')
->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'The kind of application, Desktop or Web.',ApiApplication::WEB_TYPE)
->addOption('description', 'd', InputOption::VALUE_REQUIRED, 'The desired description for application.')
->addOption('website', 'w', InputOption::VALUE_OPTIONAL, 'The desired url, eg: -w "https://www.alchemy.fr".')
->addOption('callback', 'c', InputOption::VALUE_OPTIONAL, 'The desired endpoint for callback, required for web kind eg: -c "https://www.alchemy.fr/callback"')
->addOption('webhook_url', null, InputOption::VALUE_REQUIRED, 'The webhook url')
->addOption('active', null, InputOption::VALUE_OPTIONAL, 'Activate or deactivate the app, values true or false', 'true')
->addOption('generate_token', null, InputOption::VALUE_NONE, 'Generate or regenerate the access token')
->addOption('password_oauth2_gt', null, InputOption::VALUE_OPTIONAL, 'Activate or deactivate password OAuth2 grant type , values true or false', 'false')
->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');
$active = $input->getOption('active');
$generateToken = $input->getOption('generate_token');
$passwordOauth2Gt = $input->getOption('password_oauth2_gt');
$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('<error>User not found</error>');
return 0;
}
if (!$name) {
$output->writeln('<error>Name of application must be provide with option --name.</error>');
return 0;
}
if (!$description) {
$output->writeln('<error>Desciption of application must be provide.</error>');
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 ($generateToken) {
$apiOauthTokenManipulator->create($account);
}
if ($passwordOauth2Gt) {
if (in_array($passwordOauth2Gt, ['true', 'false'])) {
$application->setGrantPassword(($passwordOauth2Gt == 'true') ? true : false);
$applicationManipulator->update($application);
} else {
$output->writeln('<error> Value of option --password_oauth2_gt should be "true" or "false"</error>');
return 0;
}
}
if ($webhookUrl) {
$applicationManipulator->setWebhookUrl($application, $webhookUrl);
$applicationManipulator->update($application);
}
if ($active) {
if (in_array($active, ['true', 'false'])) {
$application->setActivated(($active == 'true') ? true : false);
$applicationManipulator->update($application);
} else {
$output->writeln('<error>Value of option --active should be "true" or "false"</error>');
return 0;
}
} else {
$application->setActivated(true);
$applicationManipulator->update($application);
}
$this->showApllicationInformation($apiOauthRepository, $account, $application, $jsonformat, $output);
} catch (\Exception $e) {
$output->writeln('<error>Create an application for user failed : '.$e->getMessage().'</error>');
}
} elseif ($edit) {
if (!$appId) {
$output->writeln('<error>ID of the application must be provided with option --app_id to edit the application.</error>');
return 0;
}
$application = $apiApllicationConverter->convert($appId);
$account = $accountRepository->findByUserAndApplication($application->getCreator(), $application);
if (!$account) {
$output->writeln('<error>ApiAccount not found!</error>');
return 0;
}
if ($name) {
$application->setName($name);
}
if ($type) {
$applicationManipulator->setType($application, $type);
if ($type == ApiApplication::DESKTOP_TYPE) {
$applicationManipulator->setRedirectUri($application, ApiApplication::NATIVE_APP_REDIRECT_URI);
}
}
if ($description) {
$application->setDescription($description);
}
if ($website) {
$applicationManipulator->setWebsiteUrl($application, $website);
}
if ($urlCallback) {
$applicationManipulator->setRedirectUri($application, $urlCallback);
}
if ($generateToken) {
if (null !== $devToken = $apiOauthRepository->findDeveloperToken($account)) {
$apiOauthTokenManipulator->renew($devToken);
} else {
$apiOauthTokenManipulator->create($account);
}
}
if ($passwordOauth2Gt) {
if (in_array($passwordOauth2Gt, ['true', 'false'])) {
$application->setGrantPassword(($passwordOauth2Gt == 'true') ? true : false);
} else {
$output->writeln('<error> Value of option --password_oauth2_gt should be "true" or "false"</error>');
return 0;
}
}
if ($webhookUrl) {
$applicationManipulator->setWebhookUrl($application, $webhookUrl);
}
if ($active) {
if (in_array($active, ['true', 'false'])) {
$application->setActivated(($active == 'true') ? true : false);
} else {
$output->writeln('<error>Value of option --active should be "true" or "false"</error>');
return 0;
}
}
$applicationManipulator->update($application);
$this->showApllicationInformation($apiOauthRepository, $account, $application, $jsonformat, $output);
} elseif ($list) {
if ($userId) {
if (null === $user = $userRepository->find($userId)) {
$output->writeln('<error>User not found</error>');
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 = ['app_id', 'user_id', 'name', 'client_id', 'callback_url', 'generated token', 'grant_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('<error>ID of the application must be provided with option --app_id to delete the app.</error>');
return 0;
}
$application = $apiApllicationConverter->convert($appId);
if (is_null($application->getCreator())) {
/** @var DialogHelper $dialog */
$dialog = $this->getHelperSet()->get('dialog');
$continue = $dialog->askConfirmation($output, "<question>It's a special phraseanet application, do you want really to delete it? (N/y)</>", false);
if (!$continue) {
$output->writeln("<info>See you later !</>");
return 0;
}
}
$applicationManipulator->delete($application);
$output->writeln("<info>Application ID $appId deleted successfully !</info>");
}
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', 'grant_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)
;
}
}
}

View File

@@ -1573,9 +1573,9 @@ class V1Controller extends Controller
$options->setFirstResult((int)($request->get('offset_start') ?: 0));
$options->setMaxResults((int)$request->get('per_page') ?: 10);
$this->getSearchEngine()->resetCache();
$searchEngine = $this->getSearchEngine();
$search_result = $this->getSearchEngine()->query((string)$request->get('query'), $options);
$search_result = $searchEngine->query((string)$request->get('query'), $options);
$this->getUserManipulator()->logQuery($this->getAuthenticatedUser(), $search_result->getQueryText());
@@ -1583,12 +1583,12 @@ class V1Controller extends Controller
$collectionsReferencesByDatabox = $options->getCollectionsReferencesByDatabox();
foreach ($collectionsReferencesByDatabox as $sbid => $references) {
$databox = $this->findDataboxById($sbid);
$collectionsIds = array_map(function(CollectionReference $ref){return $ref->getCollectionId();}, $references);
$collectionsIds = array_map(function (CollectionReference $ref) {
return $ref->getCollectionId();
}, $references);
$this->getSearchEngineLogger()->log($databox, $search_result->getQueryText(), $search_result->getTotal(), $collectionsIds);
}
$this->getSearchEngine()->clearCache();
return $search_result;
}

View File

@@ -84,10 +84,14 @@ class DatabaseMetaProvider implements ServiceProviderInterface
$service = $app['phraseanet.cache-service'];
$config->setMetadataCacheImpl(
$service->factory('ORM_metadata', $app['orm.cache.driver'], $app['orm.cache.options'])
$app['orm.cache.factory.filesystem'](array(
'path' => $app['cache.path'].'/doctrine/metadata',
))
);
$config->setQueryCacheImpl(
$service->factory('ORM_query', $app['orm.cache.driver'], $app['orm.cache.options'])
$app['orm.cache.factory.filesystem'](array(
'path' => $app['cache.path'].'/doctrine/query',
))
);
$config->setResultCacheImpl(
$service->factory('ORM_result', $app['orm.cache.driver'], $app['orm.cache.options'])