diff --git a/lib/Alchemy/Phrasea/Controller/Prod/UsrLists.php b/lib/Alchemy/Phrasea/Controller/Prod/UsrLists.php index aab8714cbe..e68190f734 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/UsrLists.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/UsrLists.php @@ -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,152 +35,84 @@ class UsrLists implements ControllerProviderInterface /** * Get all lists */ - $controllers->get('/all/', function(Application $app, Request $request) { - - $datas = array( - 'success' => false - , 'message' => '' - , 'result' => null - ); - - $lists = new \Doctrine\Common\Collections\ArrayCollection(); - - try { - $em = $app['phraseanet.core']->getEntityManager(); - - $repository = $em->getRepository('\Entities\UsrList'); - - $lists = $repository->findUserLists($app['phraseanet.core']->getAuthenticatedUser()); - - $result = array(); - - foreach ($lists as $list) { - $owners = $entries = array(); - - foreach ($list->getOwners() as $owner) { - $owners[] = array( - 'usr_id' => $owner->getUser()->get_id(), - 'display_name' => $owner->getUser()->get_display_name(), - 'position' => $owner->getUser()->get_position(), - 'job' => $owner->getUser()->get_job(), - 'company' => $owner->getUser()->get_company(), - 'email' => $owner->getUser()->get_email(), - 'role' => $owner->getRole() - ); - } - - foreach ($list->getEntries() as $entry) { - $entries[] = array( - 'usr_id' => $owner->getUser()->get_id(), - 'display_name' => $owner->getUser()->get_display_name(), - 'position' => $owner->getUser()->get_position(), - 'job' => $owner->getUser()->get_job(), - 'company' => $owner->getUser()->get_company(), - 'email' => $owner->getUser()->get_email(), - ); - } - - /* @var $list \Entities\UsrList */ - $result[] = array( - 'name' => $list->getName(), - 'created' => $list->getCreated()->format(DATE_ATOM), - 'updated' => $list->getUpdated()->format(DATE_ATOM), - 'owners' => $owners, - 'users' => $entries - ); - } - - $datas = array( - 'success' => true - , 'message' => '' - , 'result' => $result - ); - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - - } - - if ($request->getRequestFormat() == 'json') { - return $app->json($datas); - } else { - return $app['twig']->render('prod/actions/Feedback/lists-all.html.twig', array('lists' => $lists)); - } - } - ); + $controllers->get('/all/', $this->call('getAll')); /** * Creates a list */ - $controllers->post('/list/', function(Application $app) { - $request = $app['request']; - - $list_name = $request->get('name'); - - $datas = array( - 'success' => false - , 'message' => sprintf(_('Unable to create list %s'), $list_name) - , 'list_id' => null - ); - - try { - if ( ! $list_name) { - throw new ControllerException(_('List name is required')); - } - - $em = $app['phraseanet.core']->getEntityManager(); - - $List = new \Entities\UsrList(); - - $Owner = new \Entities\UsrListOwner(); - $Owner->setRole(\Entities\UsrListOwner::ROLE_ADMIN); - $Owner->setUser($app['phraseanet.core']->getAuthenticatedUser()); - $Owner->setList($List); - - $List->setName($list_name); - $List->addUsrListOwner($Owner); - - $em->persist($Owner); - $em->persist($List); - $em->flush(); - - $datas = array( - 'success' => true - , 'message' => sprintf(_('List %s has been created'), $list_name) - , 'list_id' => $List->getId() - ); - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - - } - - return $app->json($datas); - } - ); + $controllers->post('/list/', $this->call('createList')); /** * Gets a list */ - $controllers->get('/list/{list_id}/', function(Application $app, Request $request, $list_id) { + $controllers->get('/list/{list_id}/', $this->call('displayList')) + ->assert('list_id', '\d+'); - $result = array(); + /** + * Update a list + */ + $controllers->post('/list/{list_id}/update/', $this->call('updateList')) + ->assert('list_id', '\d+'); - $user = $app['phraseanet.core']->getAuthenticatedUser(); - $em = $app['phraseanet.core']->getEntityManager(); + /** + * Delete a list + */ + $controllers->post('/list/{list_id}/delete/', $this->call('removeList')) + ->assert('list_id', '\d+'); - $repository = $em->getRepository('\Entities\UsrList'); + /** + * 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+'); - $list = $repository->findUserListByUserAndId($user, $list_id); + /** + * Adds a usr_id to a list + */ + $controllers->post('/list/{list_id}/add/', $this->call('addUsers')) + ->assert('list_id', '\d+'); - $entries = new \Doctrine\Common\Collections\ArrayCollection(); - $owners = new \Doctrine\Common\Collections\ArrayCollection(); + $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 ArrayCollection(); + + try { + $em = $app['phraseanet.core']->getEntityManager(); + + $repository = $em->getRepository('\Entities\UsrList'); + + $lists = $repository->findUserLists($app['phraseanet.core']->getAuthenticatedUser()); + + $result = array(); + + foreach ($lists as $list) { + $owners = $entries = array(); foreach ($list->getOwners() as $owner) { $owners[] = array( @@ -197,377 +128,483 @@ class UsrLists implements ControllerProviderInterface foreach ($list->getEntries() as $entry) { $entries[] = array( - 'usr_id' => $entry->getUser()->get_id(), - 'display_name' => $entry->getUser()->get_display_name(), - 'position' => $entry->getUser()->get_position(), - 'job' => $entry->getUser()->get_job(), - 'company' => $entry->getUser()->get_company(), - 'email' => $entry->getUser()->get_email(), + 'usr_id' => $owner->getUser()->get_id(), + 'display_name' => $owner->getUser()->get_display_name(), + 'position' => $owner->getUser()->get_position(), + 'job' => $owner->getUser()->get_job(), + 'company' => $owner->getUser()->get_company(), + 'email' => $owner->getUser()->get_email(), ); } - return $app->json(array( - 'result' => array( - 'id' => $list->getId(), - 'name' => $list->getName(), - 'created' => $list->getCreated()->format(DATE_ATOM), - 'updated' => $list->getUpdated()->format(DATE_ATOM), - 'owners' => $owners, - 'users' => $entries - ) - )); + /* @var $list \Entities\UsrList */ + $result[] = array( + 'name' => $list->getName(), + 'created' => $list->getCreated()->format(DATE_ATOM), + 'updated' => $list->getUpdated()->format(DATE_ATOM), + 'owners' => $owners, + 'users' => $entries + ); } + + $datas = array( + 'success' => true + , 'message' => '' + , 'result' => $result + ); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { + + } + + if ($request->getRequestFormat() == 'json') { + return $app->json($datas); + } + + return $app['twig']->render('prod/actions/Feedback/lists-all.html.twig', array('lists' => $lists)); + } + + public function createList(Application $app) + { + $request = $app['request']; + + $list_name = $request->get('name'); + + $datas = array( + 'success' => false + , 'message' => sprintf(_('Unable to create list %s'), $list_name) + , 'list_id' => null ); - /** - * Update a list - */ - $controllers->post('/list/{list_id}/update/', function(Application $app, $list_id) { - $request = $app['request']; + try { + if ( ! $list_name) { + throw new ControllerException(_('List name is required')); + } + $em = $app['phraseanet.core']->getEntityManager(); + + $List = new UsrList(); + + $Owner = new UsrListOwner(); + $Owner->setRole(UsrListOwner::ROLE_ADMIN); + $Owner->setUser($app['phraseanet.core']->getAuthenticatedUser()); + $Owner->setList($List); + + $List->setName($list_name); + $List->addUsrListOwner($Owner); + + $em->persist($Owner); + $em->persist($List); + $em->flush(); + + $datas = array( + 'success' => true + , 'message' => sprintf(_('List %s has been created'), $list_name) + , 'list_id' => $List->getId() + ); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { + + } + + return $app->json($datas); + } + + public function displayList(Application $app, Request $request, $list_id) + { + $user = $app['phraseanet.core']->getAuthenticatedUser(); + $em = $app['phraseanet.core']->getEntityManager(); + + $repository = $em->getRepository('\Entities\UsrList'); + + $list = $repository->findUserListByUserAndId($user, $list_id); + + $entries = new ArrayCollection(); + $owners = new ArrayCollection(); + + foreach ($list->getOwners() as $owner) { + $owners[] = array( + 'usr_id' => $owner->getUser()->get_id(), + 'display_name' => $owner->getUser()->get_display_name(), + 'position' => $owner->getUser()->get_position(), + 'job' => $owner->getUser()->get_job(), + 'company' => $owner->getUser()->get_company(), + 'email' => $owner->getUser()->get_email(), + 'role' => $owner->getRole() + ); + } + + foreach ($list->getEntries() as $entry) { + $entries[] = array( + 'usr_id' => $entry->getUser()->get_id(), + 'display_name' => $entry->getUser()->get_display_name(), + 'position' => $entry->getUser()->get_position(), + 'job' => $entry->getUser()->get_job(), + 'company' => $entry->getUser()->get_company(), + 'email' => $entry->getUser()->get_email(), + ); + } + + return $app->json(array( + 'result' => array( + 'id' => $list->getId(), + 'name' => $list->getName(), + 'created' => $list->getCreated()->format(DATE_ATOM), + 'updated' => $list->getUpdated()->format(DATE_ATOM), + 'owners' => $owners, + 'users' => $entries + ) + )); + } + + public function updateList(Application $app, $list_id) + { + $request = $app['request']; + + $datas = array( + 'success' => false + , 'message' => _('Unable to update list') + ); + + try { + $list_name = $request->get('name'); + + if ( ! $list_name) { + throw new ControllerException(_('List name is required')); + } + + $user = $app['phraseanet.core']->getAuthenticatedUser(); + $em = $app['phraseanet.core']->getEntityManager(); + + $repository = $em->getRepository('\Entities\UsrList'); + + $list = $repository->findUserListByUserAndId($user, $list_id); + + if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) { + throw new ControllerException(_('You are not authorized to do this')); + } + + $list->setName($list_name); + + $em->flush(); + + $datas = array( + 'success' => true + , 'message' => _('List has been updated') + ); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { + + } + + return $app->json($datas); + } + + public function removeList(Application $app, $list_id) + { + $em = $app['phraseanet.core']->getEntityManager(); + + try { + $repository = $em->getRepository('\Entities\UsrList'); + + $user = $app['phraseanet.core']->getAuthenticatedUser(); + + $list = $repository->findUserListByUserAndId($user, $list_id); + + if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_ADMIN) { + throw new ControllerException(_('You are not authorized to do this')); + } + + $em->remove($list); + $em->flush(); + + $datas = array( + 'success' => true + , 'message' => sprintf(_('List has been deleted')) + ); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { + + $datas = array( + 'success' => false + , 'message' => sprintf(_('Unable to delete list')) + ); + } + + return $app->json($datas); + } + + public function removeUser(Application $app, $list_id, $usr_id) + { + $em = $app['phraseanet.core']->getEntityManager(); + + try { + $repository = $em->getRepository('\Entities\UsrList'); + + $user = $app['phraseanet.core']->getAuthenticatedUser(); + + $list = $repository->findUserListByUserAndId($user, $list_id); + /* @var $list \Entities\UsrList */ + + if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) { + throw new ControllerException(_('You are not authorized to do this')); + } + + $entry_repository = $em->getRepository('\Entities\UsrListEntry'); + + $user_entry = $entry_repository->findEntryByListAndUsrId($list, $usr_id); + + $em->remove($user_entry); + $em->flush(); + + $datas = array( + 'success' => true + , 'message' => _('Entry removed from list') + ); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { + + $datas = array( + 'success' => false + , 'message' => _('Unable to remove entry from list ' . $e->getMessage()) + ); + } + + return $app->json($datas); + } + + public function addUsers(Application $app, Request $request, $list_id) + { + $em = $app['phraseanet.core']->getEntityManager(); + $user = $app['phraseanet.core']->getAuthenticatedUser(); + + try { + if ( ! is_array($request->get('usr_ids'))) { + throw new Controller\Exception('Invalid or missing parameter usr_ids'); + } + + $repository = $em->getRepository('\Entities\UsrList'); + + $list = $repository->findUserListByUserAndId($user, $list_id); + /* @var $list \Entities\UsrList */ + + if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) { + throw new ControllerException(_('You are not authorized to do this')); + } + + $inserted_usr_ids = array(); + + foreach ($request->get('usr_ids') as $usr_id) { + $user_entry = \User_Adapter::getInstance($usr_id, $app['phraseanet.appbox']); + + if ($list->has($user_entry)) + continue; + + $entry = new UsrListEntry(); + $entry->setUser($user_entry); + $entry->setList($list); + + $list->addUsrListEntry($entry); + + $em->persist($entry); + + $inserted_usr_ids[] = $user_entry->get_id(); + } + + $em->flush(); + + if (count($inserted_usr_ids) > 1) { $datas = array( - 'success' => false - , 'message' => _('Unable to update list') + 'success' => true + , 'message' => sprintf(_('%d Users added to list'), count($inserted_usr_ids)) + , 'result' => $inserted_usr_ids + ); + } else { + $datas = array( + 'success' => true + , 'message' => sprintf(_('%d User added to list'), count($inserted_usr_ids)) + , 'result' => $inserted_usr_ids ); - - try { - $list_name = $request->get('name'); - - if ( ! $list_name) { - throw new ControllerException(_('List name is required')); - } - - $user = $app['phraseanet.core']->getAuthenticatedUser(); - $em = $app['phraseanet.core']->getEntityManager(); - - $repository = $em->getRepository('\Entities\UsrList'); - - $list = $repository->findUserListByUserAndId($user, $list_id); - - if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) { - throw new ControllerException(_('You are not authorized to do this')); - } - - $list->setName($list_name); - - $em->flush(); - - $datas = array( - 'success' => true - , 'message' => _('List has been updated') - ); - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - - } - - return $app->json($datas); } - )->assert('list_id', '\d+'); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { - /** - * Delete a list - */ - $controllers->post('/list/{list_id}/delete/', function(Application $app, $list_id) { - $em = $app['phraseanet.core']->getEntityManager(); + $datas = array( + 'success' => false + , 'message' => _('Unable to add usr to list') + ); + } - try { - $repository = $em->getRepository('\Entities\UsrList'); + return $app->json($datas); + } - $user = $app['phraseanet.core']->getAuthenticatedUser(); + public function displayShares(Application $app, Request $request, $list_id) + { + $em = $app['phraseanet.core']->getEntityManager(); + $user = $app['phraseanet.core']->getAuthenticatedUser(); - $list = $repository->findUserListByUserAndId($user, $list_id); + $list = null; - if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN) { - throw new ControllerException(_('You are not authorized to do this')); - } + try { + $repository = $em->getRepository('\Entities\UsrList'); - $em->remove($list); - $em->flush(); - - $datas = array( - 'success' => true - , 'message' => sprintf(_('List has been deleted')) - ); - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - - $datas = array( - 'success' => false - , 'message' => sprintf(_('Unable to delete list')) - ); - } - - 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) { - $em = $app['phraseanet.core']->getEntityManager(); - - try { - $repository = $em->getRepository('\Entities\UsrList'); - - $user = $app['phraseanet.core']->getAuthenticatedUser(); - - $list = $repository->findUserListByUserAndId($user, $list_id); - /* @var $list \Entities\UsrList */ - - if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) { - throw new ControllerException(_('You are not authorized to do this')); - } - - $entry_repository = $em->getRepository('\Entities\UsrListEntry'); - - $user_entry = $entry_repository->findEntryByListAndUsrId($list, $usr_id); - - $em->remove($user_entry); - $em->flush(); - - $datas = array( - 'success' => true - , 'message' => _('Entry removed from list') - ); - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - - $datas = array( - 'success' => false - , 'message' => _('Unable to remove entry from list ' . $e->getMessage()) - ); - } - - 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) { - $em = $app['phraseanet.core']->getEntityManager(); - $user = $app['phraseanet.core']->getAuthenticatedUser(); - - try { - if ( ! is_array($request->get('usr_ids'))) { - throw new Controller\Exception('Invalid or missing parameter usr_ids'); - } - - $repository = $em->getRepository('\Entities\UsrList'); - - $list = $repository->findUserListByUserAndId($user, $list_id); - /* @var $list \Entities\UsrList */ - - if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) { - throw new ControllerException(_('You are not authorized to do this')); - } - - $inserted_usr_ids = array(); - - foreach ($request->get('usr_ids') as $usr_id) { - $user_entry = \User_Adapter::getInstance($usr_id, $app['phraseanet.appbox']); - - if ($list->has($user_entry)) - continue; - - $entry = new \Entities\UsrListEntry(); - $entry->setUser($user_entry); - $entry->setList($list); - - $list->addUsrListEntry($entry); - - $em->persist($entry); - - $inserted_usr_ids[] = $user_entry->get_id(); - } - - $em->flush(); - - if (count($inserted_usr_ids) > 1) { - $datas = array( - 'success' => true - , 'message' => sprintf(_('%d Users added to list'), count($inserted_usr_ids)) - , 'result' => $inserted_usr_ids - ); - } else { - $datas = array( - 'success' => true - , 'message' => sprintf(_('%d User added to list'), count($inserted_usr_ids)) - , 'result' => $inserted_usr_ids - ); - } - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - - $datas = array( - 'success' => false - , 'message' => _('Unable to add usr to list') - ); - } - - 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) { - - $em = $app['phraseanet.core']->getEntityManager(); - $user = $app['phraseanet.core']->getAuthenticatedUser(); + $list = $repository->findUserListByUserAndId($user, $list_id); + /* @var $list \Entities\UsrList */ + if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_ADMIN) { $list = null; - - try { - $repository = $em->getRepository('\Entities\UsrList'); - - $list = $repository->findUserListByUserAndId($user, $list_id); - /* @var $list \Entities\UsrList */ - - if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN) { - $list = null; - throw new \Exception(_('You are not authorized to do this')); - } - } catch (\Exception $e) { - - } - - return new Response($app['twig']->render('prod/actions/Feedback/List-Share.html.twig', array('list' => $list))); - })->assert('list_id', '\d+'); - - /** - * 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(); - $user = $app['phraseanet.core']->getAuthenticatedUser(); - - $availableRoles = array( - \Entities\UsrListOwner::ROLE_USER, - \Entities\UsrListOwner::ROLE_EDITOR, - \Entities\UsrListOwner::ROLE_ADMIN, - ); - - if ( ! $app['request']->get('role')) - throw new \Exception_BadRequest('Missing role parameter'); - elseif ( ! in_array($app['request']->get('role'), $availableRoles)) - throw new \Exception_BadRequest('Role is invalid'); - - try { - $repository = $em->getRepository('\Entities\UsrList'); - - $list = $repository->findUserListByUserAndId($user, $list_id); - /* @var $list \Entities\UsrList */ - - if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR) { - throw new ControllerException(_('You are not authorized to do this')); - } - - $new_owner = \User_Adapter::getInstance($usr_id, $app['phraseanet.appbox']); - - if ($list->hasAccess($new_owner)) { - if ($new_owner->get_id() == $user->get_id()) { - throw new ControllerException('You can not downgrade your Admin right'); - } - - $owner = $list->getOwner($new_owner); - } else { - $owner = new \Entities\UsrListOwner(); - $owner->setList($list); - $owner->setUser($new_owner); - - $list->addUsrListOwner($owner); - - $em->persist($owner); - } - - $role = $app['request']->get('role'); - - $owner->setRole($role); - - $em->flush(); - - $datas = array( - 'success' => true - , 'message' => _('List shared to user') - ); - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - - $datas = array( - 'success' => false - , 'message' => _('Unable to share the list with the usr') - ); - } - - return $app->json($datas); + throw new \Exception(_('You are not authorized to do this')); } - )->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) { - $em = $app['phraseanet.core']->getEntityManager(); - $user = $app['phraseanet.core']->getAuthenticatedUser(); + } catch (\Exception $e) { - try { - $repository = $em->getRepository('\Entities\UsrList'); + } - $list = $repository->findUserListByUserAndId($user, $list_id); - /* @var $list \Entities\UsrList */ + return $app['twig']->render('prod/actions/Feedback/List-Share.html.twig', array('list' => $list)); + } - if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN) { - throw new \Exception(_('You are not authorized to do this')); - } + public function shareWithUser(Application $app, $list_id, $usr_id) + { + $em = $app['phraseanet.core']->getEntityManager(); + $user = $app['phraseanet.core']->getAuthenticatedUser(); - $owners_repository = $em->getRepository('\Entities\UsrListOwner'); + $availableRoles = array( + UsrListOwner::ROLE_USER, + UsrListOwner::ROLE_EDITOR, + UsrListOwner::ROLE_ADMIN, + ); - $owner = $owners_repository->findByListAndUsrId($list, $usr_id); + if ( ! $app['request']->get('role')) + throw new \Exception_BadRequest('Missing role parameter'); + elseif ( ! in_array($app['request']->get('role'), $availableRoles)) + throw new \Exception_BadRequest('Role is invalid'); - $em->remove($owner); - $em->flush(); + try { + $repository = $em->getRepository('\Entities\UsrList'); - $datas = array( - 'success' => true - , 'message' => _('Owner removed from list') - ); - } catch (ControllerException $e) { - $datas = array( - 'success' => false - , 'message' => $e->getMessage() - ); - } catch (\Exception $e) { - $datas = array( - 'success' => false - , 'message' => _('Unable to remove usr from list') - ); + $list = $repository->findUserListByUserAndId($user, $list_id); + /* @var $list \Entities\UsrList */ + + if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) { + throw new ControllerException(_('You are not authorized to do this')); + } + + $new_owner = \User_Adapter::getInstance($usr_id, $app['phraseanet.appbox']); + + if ($list->hasAccess($new_owner)) { + if ($new_owner->get_id() == $user->get_id()) { + throw new ControllerException('You can not downgrade your Admin right'); } - return $app->json($datas); - } - )->assert('list_id', '\d+')->assert('usr_id', '\d+'); + $owner = $list->getOwner($new_owner); + } else { + $owner = new UsrListOwner(); + $owner->setList($list); + $owner->setUser($new_owner); - return $controllers; + $list->addUsrListOwner($owner); + + $em->persist($owner); + } + + $role = $app['request']->get('role'); + + $owner->setRole($role); + + $em->flush(); + + $datas = array( + 'success' => true + , 'message' => _('List shared to user') + ); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { + + $datas = array( + 'success' => false + , 'message' => _('Unable to share the list with the usr') + ); + } + + return $app->json($datas); + } + + public function unshareWithUser(Application $app, $list_id, $usr_id) + { + $em = $app['phraseanet.core']->getEntityManager(); + $user = $app['phraseanet.core']->getAuthenticatedUser(); + + try { + $repository = $em->getRepository('\Entities\UsrList'); + + $list = $repository->findUserListByUserAndId($user, $list_id); + /* @var $list \Entities\UsrList */ + + if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_ADMIN) { + throw new \Exception(_('You are not authorized to do this')); + } + + $owners_repository = $em->getRepository('\Entities\UsrListOwner'); + + $owner = $owners_repository->findByListAndUsrId($list, $usr_id); + + $em->remove($owner); + $em->flush(); + + $datas = array( + 'success' => true + , 'message' => _('Owner removed from list') + ); + } catch (ControllerException $e) { + $datas = array( + 'success' => false + , 'message' => $e->getMessage() + ); + } catch (\Exception $e) { + $datas = array( + 'success' => false + , 'message' => _('Unable to remove usr from list') + ); + } + + return $app->json($datas); + } + + /** + * 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); } }