Refactor user lists

This commit is contained in:
Romain Neutron
2012-07-25 11:20:51 +02:00
parent 0247a47497
commit a4ebff35d2

View File

@@ -11,15 +11,14 @@
namespace Alchemy\Phrasea\Controller\Prod;
use Silex\Application,
Silex\ControllerProviderInterface,
Silex\ControllerCollection;
use Alchemy\Phrasea\Helper\Record as RecordHelper,
Alchemy\Phrasea\Out\Module\PDF as PDFExport,
Alchemy\Phrasea\Controller\Exception as ControllerException;
use Symfony\Component\HttpFoundation\Response,
Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\RedirectResponse;
use Entities\UsrList;
use Entities\UsrListEntry;
use Entities\UsrListOwner;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Alchemy\Phrasea\Controller\Exception as ControllerException;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Collections\ArrayCollection;
/**
*
@@ -36,15 +35,72 @@ class UsrLists implements ControllerProviderInterface
/**
* 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(
'success' => false
, 'message' => ''
, 'result' => null
);
$lists = new \Doctrine\Common\Collections\ArrayCollection();
$lists = new ArrayCollection();
try {
$em = $app['phraseanet.core']->getEntityManager();
@@ -107,16 +163,13 @@ class UsrLists implements ControllerProviderInterface
if ($request->getRequestFormat() == 'json') {
return $app->json($datas);
} else {
}
return $app['twig']->render('prod/actions/Feedback/lists-all.html.twig', array('lists' => $lists));
}
}
);
/**
* Creates a list
*/
$controllers->post('/list/', function(Application $app) {
public function createList(Application $app)
{
$request = $app['request'];
$list_name = $request->get('name');
@@ -134,10 +187,10 @@ class UsrLists implements ControllerProviderInterface
$em = $app['phraseanet.core']->getEntityManager();
$List = new \Entities\UsrList();
$List = new UsrList();
$Owner = new \Entities\UsrListOwner();
$Owner->setRole(\Entities\UsrListOwner::ROLE_ADMIN);
$Owner = new UsrListOwner();
$Owner->setRole(UsrListOwner::ROLE_ADMIN);
$Owner->setUser($app['phraseanet.core']->getAuthenticatedUser());
$Owner->setList($List);
@@ -164,15 +217,9 @@ class UsrLists implements ControllerProviderInterface
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();
$em = $app['phraseanet.core']->getEntityManager();
@@ -180,8 +227,8 @@ class UsrLists implements ControllerProviderInterface
$list = $repository->findUserListByUserAndId($user, $list_id);
$entries = new \Doctrine\Common\Collections\ArrayCollection();
$owners = new \Doctrine\Common\Collections\ArrayCollection();
$entries = new ArrayCollection();
$owners = new ArrayCollection();
foreach ($list->getOwners() as $owner) {
$owners[] = array(
@@ -217,12 +264,9 @@ class UsrLists implements ControllerProviderInterface
)
));
}
);
/**
* Update a list
*/
$controllers->post('/list/{list_id}/update/', function(Application $app, $list_id) {
public function updateList(Application $app, $list_id)
{
$request = $app['request'];
$datas = array(
@@ -244,7 +288,7 @@ class UsrLists implements ControllerProviderInterface
$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'));
}
@@ -267,12 +311,9 @@ class UsrLists implements ControllerProviderInterface
return $app->json($datas);
}
)->assert('list_id', '\d+');
/**
* Delete a list
*/
$controllers->post('/list/{list_id}/delete/', function(Application $app, $list_id) {
public function removeList(Application $app, $list_id)
{
$em = $app['phraseanet.core']->getEntityManager();
try {
@@ -282,7 +323,7 @@ class UsrLists implements ControllerProviderInterface
$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'));
}
@@ -308,12 +349,9 @@ class UsrLists implements ControllerProviderInterface
return $app->json($datas);
}
)->assert('list_id', '\d+');
/**
* Remove a usr_id from a list
*/
$controllers->post('/list/{list_id}/remove/{usr_id}/', function(Application $app, $list_id, $usr_id) {
public function removeUser(Application $app, $list_id, $usr_id)
{
$em = $app['phraseanet.core']->getEntityManager();
try {
@@ -324,7 +362,7 @@ class UsrLists implements ControllerProviderInterface
$list = $repository->findUserListByUserAndId($user, $list_id);
/* @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'));
}
@@ -354,12 +392,9 @@ class UsrLists implements ControllerProviderInterface
return $app->json($datas);
}
)->assert('list_id', '\d+')->assert('entry_id', '\d+');
/**
* Adds a usr_id to a list
*/
$controllers->post('/list/{list_id}/add/', function(Application $app, Request $request, $list_id) {
public function addUsers(Application $app, Request $request, $list_id)
{
$em = $app['phraseanet.core']->getEntityManager();
$user = $app['phraseanet.core']->getAuthenticatedUser();
@@ -373,7 +408,7 @@ class UsrLists implements ControllerProviderInterface
$list = $repository->findUserListByUserAndId($user, $list_id);
/* @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'));
}
@@ -385,7 +420,7 @@ class UsrLists implements ControllerProviderInterface
if ($list->has($user_entry))
continue;
$entry = new \Entities\UsrListEntry();
$entry = new UsrListEntry();
$entry->setUser($user_entry);
$entry->setList($list);
@@ -426,10 +461,9 @@ class UsrLists implements ControllerProviderInterface
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();
$user = $app['phraseanet.core']->getAuthenticatedUser();
@@ -441,7 +475,7 @@ class UsrLists implements ControllerProviderInterface
$list = $repository->findUserListByUserAndId($user, $list_id);
/* @var $list \Entities\UsrList */
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN) {
if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_ADMIN) {
$list = null;
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)));
})->assert('list_id', '\d+');
return $app['twig']->render('prod/actions/Feedback/List-Share.html.twig', array('list' => $list));
}
/**
* 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) {
public function shareWithUser(Application $app, $list_id, $usr_id)
{
$em = $app['phraseanet.core']->getEntityManager();
$user = $app['phraseanet.core']->getAuthenticatedUser();
$availableRoles = array(
\Entities\UsrListOwner::ROLE_USER,
\Entities\UsrListOwner::ROLE_EDITOR,
\Entities\UsrListOwner::ROLE_ADMIN,
UsrListOwner::ROLE_USER,
UsrListOwner::ROLE_EDITOR,
UsrListOwner::ROLE_ADMIN,
);
if ( ! $app['request']->get('role'))
@@ -476,7 +508,7 @@ class UsrLists implements ControllerProviderInterface
$list = $repository->findUserListByUserAndId($user, $list_id);
/* @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'));
}
@@ -489,7 +521,7 @@ class UsrLists implements ControllerProviderInterface
$owner = $list->getOwner($new_owner);
} else {
$owner = new \Entities\UsrListOwner();
$owner = new UsrListOwner();
$owner->setList($list);
$owner->setUser($new_owner);
@@ -523,11 +555,9 @@ class UsrLists implements ControllerProviderInterface
return $app->json($datas);
}
)->assert('list_id', '\d+')->assert('usr_id', '\d+');
/**
* UnShare a list to a user
*/
$controllers->post('/list/{list_id}/unshare/{usr_id}/', function(Application $app, $list_id, $usr_id) {
public function unshareWithUser(Application $app, $list_id, $usr_id)
{
$em = $app['phraseanet.core']->getEntityManager();
$user = $app['phraseanet.core']->getAuthenticatedUser();
@@ -537,7 +567,7 @@ class UsrLists implements ControllerProviderInterface
$list = $repository->findUserListByUserAndId($user, $list_id);
/* @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'));
}
@@ -566,8 +596,15 @@ class UsrLists implements ControllerProviderInterface
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);
}
}