diff --git a/lib/Alchemy/Phrasea/Collection/CollectionService.php b/lib/Alchemy/Phrasea/Collection/CollectionService.php index 1bc70198b5..8a2a8bc2f1 100644 --- a/lib/Alchemy/Phrasea/Collection/CollectionService.php +++ b/lib/Alchemy/Phrasea/Collection/CollectionService.php @@ -11,9 +11,12 @@ namespace Alchemy\Phrasea\Collection; use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Authentication\ACLProvider; use Alchemy\Phrasea\Collection\Reference\CollectionReference; use Alchemy\Phrasea\Databox\DataboxConnectionProvider; +use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Model\Entities\User; +use Alchemy\Phrasea\Model\Repositories\UserRepository; use Doctrine\DBAL\Connection; class CollectionService @@ -23,15 +26,31 @@ class CollectionService */ private $app; + /** + * @var Connection + */ private $connection; + /** + * @var DataboxConnectionProvider + */ private $connectionProvider; - public function __construct(Application $application, Connection $connection, DataboxConnectionProvider $connectionProvider) - { + /** + * @var callable + */ + private $userQueryFactory; + + public function __construct( + Application $application, + Connection $connection, + DataboxConnectionProvider $connectionProvider, + callable $userQueryFactory + ) { $this->app = $application; $this->connection = $connection; $this->connectionProvider = $connectionProvider; + $this->userQueryFactory = $userQueryFactory; } /** @@ -271,4 +290,53 @@ class CollectionService $this->app->getAclForUser($user)->update_rights_to_base($reference->getBaseId(), $rights); } + + public function setOrderMasters(CollectionReference $reference, array $userIds) + { + + /** @var UserRepository $userRepository */ + $userRepository = $this->app['repo.users']; + $users = $userRepository->findBy(['id' => $userIds]); + + $missingAdmins = array_diff($userIds, array_map(function (User $user) { + return $user->getId(); + }, $users)); + + if (! empty($missingAdmins)) { + throw new RuntimeException(sprintf('Invalid usrIds provided [%s].', implode(',', $missingAdmins))); + } + + $admins = $users; + + /** @var Connection $conn */ + $conn = $this->app->getApplicationBox()->get_connection(); + $conn->beginTransaction(); + + try { + $factory = $this->userQueryFactory; + /** @var \User_Query $userQuery */ + $userQuery = $factory(); + + $result = $userQuery->on_base_ids([ $reference->getBaseId()] ) + ->who_have_right(['order_master']) + ->execute()->get_results(); + + /** @var ACLProvider $acl */ + $acl = $this->app['acl']; + + foreach ($result as $user) { + $acl->get($user)->update_rights_to_base($reference->getBaseId(), ['order_master' => false]); + } + + foreach ($admins as $admin) { + $acl->get($admin)->update_rights_to_base($reference->getBaseId(), ['order_master' => true]); + } + + $conn->commit(); + } catch (\Exception $e) { + $conn->rollBack(); + throw $e; + } + + } } diff --git a/lib/Alchemy/Phrasea/Controller/Admin/CollectionController.php b/lib/Alchemy/Phrasea/Controller/Admin/CollectionController.php index ac082bb3ca..2ebe77a408 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/CollectionController.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/CollectionController.php @@ -10,13 +10,10 @@ namespace Alchemy\Phrasea\Controller\Admin; +use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application\Helper\UserQueryAware; -use Alchemy\Phrasea\Authentication\ACLProvider; +use Alchemy\Phrasea\Collection\CollectionService; use Alchemy\Phrasea\Controller\Controller; -use Alchemy\Phrasea\Exception\RuntimeException; -use Alchemy\Phrasea\Model\Entities\User; -use Alchemy\Phrasea\Model\Repositories\UserRepository; -use Doctrine\DBAL\Connection; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -24,6 +21,18 @@ class CollectionController extends Controller { use UserQueryAware; + /** + * @var CollectionService + */ + private $collectionService; + + public function __construct(Application $application, CollectionService $collectionService) + { + parent::__construct($application); + + $this->collectionService = $collectionService; + } + /** * Display collection information page * @@ -79,52 +88,23 @@ class CollectionController extends Controller */ public function setOrderAdmins(Request $request, $bas_id) { - $admins = array_values($request->request->get('admins', [])); + $admins = array_filter( + array_values($request->request->get('admins', [])), + function ($value) { return $value != false; } + ); - if (count($admins) === 0) { + if (false && count($admins) === 0) { $this->app->abort(400, 'No admins provided.'); } + if (!is_array($admins)) { $this->app->abort(400, 'Admins must be an array.'); } - /** @var UserRepository $userRepository */ - $userRepository = $this->app['repo.users']; - $users = $userRepository->findBy(['id' => $admins]); - $userIds = array_map(function (User $user) { - return $user->getId(); - }, $users); - $missingAdmins = array_diff($admins, $userIds); - if (!empty($missingAdmins)) { - throw new RuntimeException(sprintf('Invalid usrId %s provided.', reset($missingAdmins))); - } - $admins = $users; + $collection = $this->getApplicationBox()->get_collection($bas_id); + $collectionReference = $collection->getReference(); - /** @var Connection $conn */ - $conn = $this->app->getApplicationBox()->get_connection(); - $conn->beginTransaction(); - - try { - $userQuery = $this->createUserQuery(); - - $result = $userQuery->on_base_ids([$bas_id]) - ->who_have_right(['order_master']) - ->execute()->get_results(); - - /** @var ACLProvider $acl */ - $acl = $this->app['acl']; - foreach ($result as $user) { - $acl->get($user)->update_rights_to_base($bas_id, ['order_master' => false]); - } - - foreach ($admins as $admin) { - $acl->get($admin)->update_rights_to_base($bas_id, ['order_master' => true]); - } - $conn->commit(); - } catch (\Exception $e) { - $conn->rollBack(); - throw $e; - } + $this->collectionService->setOrderMasters($collectionReference, $admins); return $this->app->redirectPath('admin_display_collection', [ 'bas_id' => $bas_id, diff --git a/lib/Alchemy/Phrasea/Controller/Controller.php b/lib/Alchemy/Phrasea/Controller/Controller.php index 6b11fe6ab0..4f477ff8de 100644 --- a/lib/Alchemy/Phrasea/Controller/Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Controller.php @@ -26,8 +26,7 @@ class Controller { $this->app = $app; } - - + /** * @return \appbox */ diff --git a/lib/Alchemy/Phrasea/Controller/LazyLocator.php b/lib/Alchemy/Phrasea/Controller/LazyLocator.php index 39dbb8db05..92a96f9cb7 100644 --- a/lib/Alchemy/Phrasea/Controller/LazyLocator.php +++ b/lib/Alchemy/Phrasea/Controller/LazyLocator.php @@ -9,24 +9,12 @@ */ namespace Alchemy\Phrasea\Controller; -class LazyLocator +/** + * Class LazyLocator + * @package Alchemy\Phrasea\Controller + * @deprecated Use Alchemy\Phrasea\Core\LazyLocator + */ +class LazyLocator extends \Alchemy\Phrasea\Core\LazyLocator { - /** @var \Pimple */ - private $pimple; - private $serviceId; - - /** - * @param \Pimple $pimple - * @param string $serviceId - */ - public function __construct(\Pimple $pimple, $serviceId) - { - $this->pimple = $pimple; - $this->serviceId = $serviceId; - } - - public function __invoke() - { - return $this->pimple->offsetGet($this->serviceId); - } + // Stub left for BC } diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Admin/Collection.php b/lib/Alchemy/Phrasea/ControllerProvider/Admin/Collection.php index b58be0f9d6..915f1cd0d3 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Admin/Collection.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Admin/Collection.php @@ -27,7 +27,7 @@ class Collection implements ControllerProviderInterface, ServiceProviderInterfac public function register(Application $app) { $app['controller.admin.collection'] = $app->share(function (PhraseaApplication $app) { - return (new CollectionController($app)) + return (new CollectionController($app, $app->getApplicationBox()->getCollectionService())) ->setUserQueryFactory(new LazyLocator($app, 'phraseanet.user-query')) ; }); diff --git a/lib/Alchemy/Phrasea/Core/LazyLocator.php b/lib/Alchemy/Phrasea/Core/LazyLocator.php new file mode 100644 index 0000000000..ba274da753 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/LazyLocator.php @@ -0,0 +1,41 @@ +pimple = $pimple; + $this->serviceId = $serviceId; + } + + /** + * @return mixed + */ + public function __invoke() + { + return $this->pimple->offsetGet($this->serviceId); + } +} diff --git a/lib/classes/appbox.php b/lib/classes/appbox.php index 6309532b82..c45d21fda3 100644 --- a/lib/classes/appbox.php +++ b/lib/classes/appbox.php @@ -13,6 +13,7 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Collection\CollectionService; use Alchemy\Phrasea\Core\Configuration\AccessRestriction; use Alchemy\Phrasea\Core\Connection\ConnectionSettings; +use Alchemy\Phrasea\Core\LazyLocator; use Alchemy\Phrasea\Core\Version\AppboxVersionRepository; use Alchemy\Phrasea\Databox\DataboxConnectionProvider; use Alchemy\Phrasea\Databox\DataboxRepository; @@ -325,7 +326,8 @@ class appbox extends base $this->collectionService = new CollectionService( $this->app, $this->connection, - new DataboxConnectionProvider($this) + new DataboxConnectionProvider($this), + new LazyLocator($this->app, 'phraseanet.user-query') ); } diff --git a/templates/web/admin/collection/collection.html.twig b/templates/web/admin/collection/collection.html.twig index a7b2f747a3..e883743ffc 100644 --- a/templates/web/admin/collection/collection.html.twig +++ b/templates/web/admin/collection/collection.html.twig @@ -257,6 +257,12 @@ .appendTo( ul ); }; + $('#admin_adder').find('input').change(function () { + if (! this.checked) { + $('#admin_adder').submit(); + } + }); + //change display $('#pub_wm_none, #pub_wm_wm, #pub_wm_stamp').bind('click', function(){ var $this = $(this);