Allow removing order masters without adding a new one

This commit is contained in:
Thibaud Fabre
2016-07-29 14:46:18 +02:00
parent d558192a74
commit 303a39e359
8 changed files with 152 additions and 68 deletions

View File

@@ -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;
}
}
}

View File

@@ -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,

View File

@@ -27,7 +27,6 @@ class Controller
$this->app = $app;
}
/**
* @return \appbox
*/

View File

@@ -9,24 +9,12 @@
*/
namespace Alchemy\Phrasea\Controller;
class LazyLocator
{
/** @var \Pimple */
private $pimple;
private $serviceId;
/**
* @param \Pimple $pimple
* @param string $serviceId
/**
* Class LazyLocator
* @package Alchemy\Phrasea\Controller
* @deprecated Use Alchemy\Phrasea\Core\LazyLocator
*/
public function __construct(\Pimple $pimple, $serviceId)
{
$this->pimple = $pimple;
$this->serviceId = $serviceId;
}
public function __invoke()
{
return $this->pimple->offsetGet($this->serviceId);
}
class LazyLocator extends \Alchemy\Phrasea\Core\LazyLocator
{
// Stub left for BC
}

View File

@@ -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'))
;
});

View File

@@ -0,0 +1,41 @@
<?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\Core;
class LazyLocator
{
/**
* @var \Pimple
*/
private $pimple;
/**
* @var string
*/
private $serviceId;
/**
* @param \Pimple $pimple
* @param string $serviceId
*/
public function __construct(\Pimple $pimple, $serviceId)
{
$this->pimple = $pimple;
$this->serviceId = $serviceId;
}
/**
* @return mixed
*/
public function __invoke()
{
return $this->pimple->offsetGet($this->serviceId);
}
}

View File

@@ -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')
);
}

View File

@@ -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);