mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
Refactor user lists
This commit is contained in:
@@ -11,15 +11,14 @@
|
|||||||
|
|
||||||
namespace Alchemy\Phrasea\Controller\Prod;
|
namespace Alchemy\Phrasea\Controller\Prod;
|
||||||
|
|
||||||
use Silex\Application,
|
use Entities\UsrList;
|
||||||
Silex\ControllerProviderInterface,
|
use Entities\UsrListEntry;
|
||||||
Silex\ControllerCollection;
|
use Entities\UsrListOwner;
|
||||||
use Alchemy\Phrasea\Helper\Record as RecordHelper,
|
use Silex\Application;
|
||||||
Alchemy\Phrasea\Out\Module\PDF as PDFExport,
|
use Silex\ControllerProviderInterface;
|
||||||
Alchemy\Phrasea\Controller\Exception as ControllerException;
|
use Alchemy\Phrasea\Controller\Exception as ControllerException;
|
||||||
use Symfony\Component\HttpFoundation\Response,
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
Symfony\Component\HttpFoundation\Request,
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
Symfony\Component\HttpFoundation\RedirectResponse;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -36,15 +35,72 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
/**
|
/**
|
||||||
* Get all lists
|
* Get all lists
|
||||||
*/
|
*/
|
||||||
$controllers->get('/all/', function(Application $app, Request $request) {
|
$controllers->get('/all/', $this->call('getAll'));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a list
|
||||||
|
*/
|
||||||
|
$controllers->post('/list/', $this->call('createList'));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a list
|
||||||
|
*/
|
||||||
|
$controllers->get('/list/{list_id}/', $this->call('displayList'))
|
||||||
|
->assert('list_id', '\d+');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a list
|
||||||
|
*/
|
||||||
|
$controllers->post('/list/{list_id}/update/', $this->call('updateList'))
|
||||||
|
->assert('list_id', '\d+');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a list
|
||||||
|
*/
|
||||||
|
$controllers->post('/list/{list_id}/delete/', $this->call('removeList'))
|
||||||
|
->assert('list_id', '\d+');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a usr_id from a list
|
||||||
|
*/
|
||||||
|
$controllers->post('/list/{list_id}/remove/{usr_id}/', $this->call('removeUser'))
|
||||||
|
->assert('list_id', '\d+')
|
||||||
|
->assert('usr_id', '\d+');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a usr_id to a list
|
||||||
|
*/
|
||||||
|
$controllers->post('/list/{list_id}/add/', $this->call('addUsers'))
|
||||||
|
->assert('list_id', '\d+');
|
||||||
|
|
||||||
|
$controllers->get('/list/{list_id}/share/', $this->call('displayShares'))
|
||||||
|
->assert('list_id', '\d+');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Share a list to a user with an optionnal role
|
||||||
|
*/
|
||||||
|
$controllers->post('/list/{list_id}/share/{usr_id}/', $this->call('shareWithUser'))
|
||||||
|
->assert('list_id', '\d+')
|
||||||
|
->assert('usr_id', '\d+');
|
||||||
|
/**
|
||||||
|
* UnShare a list to a user
|
||||||
|
*/
|
||||||
|
$controllers->post('/list/{list_id}/unshare/{usr_id}/', $this->call('unshareWithUser'))
|
||||||
|
->assert('list_id', '\d+')
|
||||||
|
->assert('usr_id', '\d+');
|
||||||
|
|
||||||
|
return $controllers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAll(Application $app, Request $request)
|
||||||
|
{
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => ''
|
, 'message' => ''
|
||||||
, 'result' => null
|
, 'result' => null
|
||||||
);
|
);
|
||||||
|
|
||||||
$lists = new \Doctrine\Common\Collections\ArrayCollection();
|
$lists = new ArrayCollection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
@@ -107,16 +163,13 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
if ($request->getRequestFormat() == 'json') {
|
if ($request->getRequestFormat() == 'json') {
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return $app['twig']->render('prod/actions/Feedback/lists-all.html.twig', array('lists' => $lists));
|
return $app['twig']->render('prod/actions/Feedback/lists-all.html.twig', array('lists' => $lists));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
public function createList(Application $app)
|
||||||
* Creates a list
|
{
|
||||||
*/
|
|
||||||
$controllers->post('/list/', function(Application $app) {
|
|
||||||
$request = $app['request'];
|
$request = $app['request'];
|
||||||
|
|
||||||
$list_name = $request->get('name');
|
$list_name = $request->get('name');
|
||||||
@@ -134,10 +187,10 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
|
|
||||||
$List = new \Entities\UsrList();
|
$List = new UsrList();
|
||||||
|
|
||||||
$Owner = new \Entities\UsrListOwner();
|
$Owner = new UsrListOwner();
|
||||||
$Owner->setRole(\Entities\UsrListOwner::ROLE_ADMIN);
|
$Owner->setRole(UsrListOwner::ROLE_ADMIN);
|
||||||
$Owner->setUser($app['phraseanet.core']->getAuthenticatedUser());
|
$Owner->setUser($app['phraseanet.core']->getAuthenticatedUser());
|
||||||
$Owner->setList($List);
|
$Owner->setList($List);
|
||||||
|
|
||||||
@@ -164,15 +217,9 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a list
|
|
||||||
*/
|
|
||||||
$controllers->get('/list/{list_id}/', function(Application $app, Request $request, $list_id) {
|
|
||||||
|
|
||||||
$result = array();
|
|
||||||
|
|
||||||
|
public function displayList(Application $app, Request $request, $list_id)
|
||||||
|
{
|
||||||
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
|
|
||||||
@@ -180,8 +227,8 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
|
|
||||||
$entries = new \Doctrine\Common\Collections\ArrayCollection();
|
$entries = new ArrayCollection();
|
||||||
$owners = new \Doctrine\Common\Collections\ArrayCollection();
|
$owners = new ArrayCollection();
|
||||||
|
|
||||||
foreach ($list->getOwners() as $owner) {
|
foreach ($list->getOwners() as $owner) {
|
||||||
$owners[] = array(
|
$owners[] = array(
|
||||||
@@ -217,12 +264,9 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
)
|
)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
public function updateList(Application $app, $list_id)
|
||||||
* Update a list
|
{
|
||||||
*/
|
|
||||||
$controllers->post('/list/{list_id}/update/', function(Application $app, $list_id) {
|
|
||||||
$request = $app['request'];
|
$request = $app['request'];
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
@@ -244,7 +288,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
|
|
||||||
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) {
|
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) {
|
||||||
throw new ControllerException(_('You are not authorized to do this'));
|
throw new ControllerException(_('You are not authorized to do this'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,12 +311,9 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
}
|
}
|
||||||
)->assert('list_id', '\d+');
|
|
||||||
|
|
||||||
/**
|
public function removeList(Application $app, $list_id)
|
||||||
* Delete a list
|
{
|
||||||
*/
|
|
||||||
$controllers->post('/list/{list_id}/delete/', function(Application $app, $list_id) {
|
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -282,7 +323,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
|
|
||||||
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN) {
|
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_ADMIN) {
|
||||||
throw new ControllerException(_('You are not authorized to do this'));
|
throw new ControllerException(_('You are not authorized to do this'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,12 +349,9 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
}
|
}
|
||||||
)->assert('list_id', '\d+');
|
|
||||||
|
|
||||||
/**
|
public function removeUser(Application $app, $list_id, $usr_id)
|
||||||
* Remove a usr_id from a list
|
{
|
||||||
*/
|
|
||||||
$controllers->post('/list/{list_id}/remove/{usr_id}/', function(Application $app, $list_id, $usr_id) {
|
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -324,7 +362,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) {
|
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) {
|
||||||
throw new ControllerException(_('You are not authorized to do this'));
|
throw new ControllerException(_('You are not authorized to do this'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,12 +392,9 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
}
|
}
|
||||||
)->assert('list_id', '\d+')->assert('entry_id', '\d+');
|
|
||||||
|
|
||||||
/**
|
public function addUsers(Application $app, Request $request, $list_id)
|
||||||
* Adds a usr_id to a list
|
{
|
||||||
*/
|
|
||||||
$controllers->post('/list/{list_id}/add/', function(Application $app, Request $request, $list_id) {
|
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
||||||
|
|
||||||
@@ -373,7 +408,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) {
|
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) {
|
||||||
throw new ControllerException(_('You are not authorized to do this'));
|
throw new ControllerException(_('You are not authorized to do this'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +420,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
if ($list->has($user_entry))
|
if ($list->has($user_entry))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$entry = new \Entities\UsrListEntry();
|
$entry = new UsrListEntry();
|
||||||
$entry->setUser($user_entry);
|
$entry->setUser($user_entry);
|
||||||
$entry->setList($list);
|
$entry->setList($list);
|
||||||
|
|
||||||
@@ -426,10 +461,9 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
}
|
}
|
||||||
)->assert('list_id', '\d+')->assert('usr_id', '\d+');
|
|
||||||
|
|
||||||
$controllers->get('/list/{list_id}/share/', function(Application $app, Request $request, $list_id) {
|
|
||||||
|
|
||||||
|
public function displayShares(Application $app, Request $request, $list_id)
|
||||||
|
{
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
||||||
|
|
||||||
@@ -441,7 +475,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN) {
|
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_ADMIN) {
|
||||||
$list = null;
|
$list = null;
|
||||||
throw new \Exception(_('You are not authorized to do this'));
|
throw new \Exception(_('You are not authorized to do this'));
|
||||||
}
|
}
|
||||||
@@ -449,20 +483,18 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response($app['twig']->render('prod/actions/Feedback/List-Share.html.twig', array('list' => $list)));
|
return $app['twig']->render('prod/actions/Feedback/List-Share.html.twig', array('list' => $list));
|
||||||
})->assert('list_id', '\d+');
|
}
|
||||||
|
|
||||||
/**
|
public function shareWithUser(Application $app, $list_id, $usr_id)
|
||||||
* Share a list to a user with an optionnal role
|
{
|
||||||
*/
|
|
||||||
$controllers->post('/list/{list_id}/share/{usr_id}/', function(Application $app, $list_id, $usr_id) {
|
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
||||||
|
|
||||||
$availableRoles = array(
|
$availableRoles = array(
|
||||||
\Entities\UsrListOwner::ROLE_USER,
|
UsrListOwner::ROLE_USER,
|
||||||
\Entities\UsrListOwner::ROLE_EDITOR,
|
UsrListOwner::ROLE_EDITOR,
|
||||||
\Entities\UsrListOwner::ROLE_ADMIN,
|
UsrListOwner::ROLE_ADMIN,
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( ! $app['request']->get('role'))
|
if ( ! $app['request']->get('role'))
|
||||||
@@ -476,7 +508,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) {
|
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) {
|
||||||
throw new ControllerException(_('You are not authorized to do this'));
|
throw new ControllerException(_('You are not authorized to do this'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +521,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
$owner = $list->getOwner($new_owner);
|
$owner = $list->getOwner($new_owner);
|
||||||
} else {
|
} else {
|
||||||
$owner = new \Entities\UsrListOwner();
|
$owner = new UsrListOwner();
|
||||||
$owner->setList($list);
|
$owner->setList($list);
|
||||||
$owner->setUser($new_owner);
|
$owner->setUser($new_owner);
|
||||||
|
|
||||||
@@ -523,11 +555,9 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
}
|
}
|
||||||
)->assert('list_id', '\d+')->assert('usr_id', '\d+');
|
|
||||||
/**
|
public function unshareWithUser(Application $app, $list_id, $usr_id)
|
||||||
* UnShare a list to a user
|
{
|
||||||
*/
|
|
||||||
$controllers->post('/list/{list_id}/unshare/{usr_id}/', function(Application $app, $list_id, $usr_id) {
|
|
||||||
$em = $app['phraseanet.core']->getEntityManager();
|
$em = $app['phraseanet.core']->getEntityManager();
|
||||||
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
$user = $app['phraseanet.core']->getAuthenticatedUser();
|
||||||
|
|
||||||
@@ -537,7 +567,7 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN) {
|
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_ADMIN) {
|
||||||
throw new \Exception(_('You are not authorized to do this'));
|
throw new \Exception(_('You are not authorized to do this'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -566,8 +596,15 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
|
|
||||||
return $app->json($datas);
|
return $app->json($datas);
|
||||||
}
|
}
|
||||||
)->assert('list_id', '\d+')->assert('usr_id', '\d+');
|
|
||||||
|
|
||||||
return $controllers;
|
/**
|
||||||
|
* Prefix the method to call with the controller class name
|
||||||
|
*
|
||||||
|
* @param string $method The method to call
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function call($method)
|
||||||
|
{
|
||||||
|
return sprintf('%s::%s', __CLASS__, $method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user