mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
Fix usrlist unit tests
This commit is contained in:
@@ -37,261 +37,261 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
* Get all lists
|
* Get all lists
|
||||||
*/
|
*/
|
||||||
$controllers->get('/list/all/', function(Application $app)
|
$controllers->get('/list/all/', function(Application $app)
|
||||||
{
|
{
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
|
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
|
|
||||||
$lists = $repository->findUserLists($app['Core']->getAuthenticatedUser());
|
$lists = $repository->findUserLists($app['Core']->getAuthenticatedUser());
|
||||||
|
|
||||||
$datas = array('lists' => array());
|
$datas = array('lists' => array());
|
||||||
|
|
||||||
foreach ($lists as $list)
|
foreach ($lists as $list)
|
||||||
{
|
{
|
||||||
$owners = $entries = array();
|
$owners = $entries = array();
|
||||||
|
|
||||||
foreach ($list->getOwners() as $owner)
|
foreach ($list->getOwners() as $owner)
|
||||||
{
|
{
|
||||||
$owners[] = array(
|
$owners[] = array(
|
||||||
'usr_id' => $owner->getUser()->get_id(),
|
'usr_id' => $owner->getUser()->get_id(),
|
||||||
'display_name' => $owner->getUser()->get_display_name(),
|
'display_name' => $owner->getUser()->get_display_name(),
|
||||||
'position' => $owner->getUser()->get_position(),
|
'position' => $owner->getUser()->get_position(),
|
||||||
'job' => $owner->getUser()->get_job(),
|
'job' => $owner->getUser()->get_job(),
|
||||||
'company' => $owner->getUser()->get_company(),
|
'company' => $owner->getUser()->get_company(),
|
||||||
'email' => $owner->getUser()->get_email(),
|
'email' => $owner->getUser()->get_email(),
|
||||||
'role' => $owner->getRole()
|
'role' => $owner->getRole()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($list->getEntries() as $entry)
|
foreach ($list->getEntries() as $entry)
|
||||||
{
|
{
|
||||||
$entries[] = array(
|
$entries[] = array(
|
||||||
'usr_id' => $owner->getUser()->get_id(),
|
'usr_id' => $owner->getUser()->get_id(),
|
||||||
'display_name' => $owner->getUser()->get_display_name(),
|
'display_name' => $owner->getUser()->get_display_name(),
|
||||||
'position' => $owner->getUser()->get_position(),
|
'position' => $owner->getUser()->get_position(),
|
||||||
'job' => $owner->getUser()->get_job(),
|
'job' => $owner->getUser()->get_job(),
|
||||||
'company' => $owner->getUser()->get_company(),
|
'company' => $owner->getUser()->get_company(),
|
||||||
'email' => $owner->getUser()->get_email(),
|
'email' => $owner->getUser()->get_email(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
$datas['lists'][] = array(
|
$datas['lists'][] = array(
|
||||||
'name' => $list->getName(),
|
'name' => $list->getName(),
|
||||||
'created' => $list->getCreated()->format(DATE_ATOM),
|
'created' => $list->getCreated()->format(DATE_ATOM),
|
||||||
'updated' => $list->getUpdated()->format(DATE_ATOM),
|
'updated' => $list->getUpdated()->format(DATE_ATOM),
|
||||||
'owners' => $owners,
|
'owners' => $owners,
|
||||||
'users' => $entries
|
'users' => $entries
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a list
|
* Creates a list
|
||||||
*/
|
*/
|
||||||
$controllers->post('/list/', function(Application $app)
|
$controllers->post('/list/', function(Application $app)
|
||||||
{
|
{
|
||||||
$request = $app['request'];
|
$request = $app['request'];
|
||||||
|
|
||||||
$list_name = $request->get('name');
|
$list_name = $request->get('name');
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => sprintf(_('Unable to create list %s'), $list_name)
|
, 'message' => sprintf(_('Unable to create list %s'), $list_name)
|
||||||
);
|
);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if(!$list_name)
|
if (!$list_name)
|
||||||
{
|
{
|
||||||
throw new ControllerException(_('List name is required'));
|
throw new ControllerException(_('List name is required'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
|
|
||||||
$List = new \Entities\UsrList();
|
$List = new \Entities\UsrList();
|
||||||
|
|
||||||
$Owner = new \Entities\UsrListOwner();
|
$Owner = new \Entities\UsrListOwner();
|
||||||
$Owner->setRole(\Entities\UsrListOwner::ROLE_ADMIN);
|
$Owner->setRole(\Entities\UsrListOwner::ROLE_ADMIN);
|
||||||
$Owner->setUser($app['Core']->getAuthenticatedUser());
|
$Owner->setUser($app['Core']->getAuthenticatedUser());
|
||||||
$Owner->setList($List);
|
$Owner->setList($List);
|
||||||
|
|
||||||
$List->setName($list_name);
|
$List->setName($list_name);
|
||||||
$List->addUsrListOwner($Owner);
|
$List->addUsrListOwner($Owner);
|
||||||
|
|
||||||
$em->persist($Owner);
|
$em->persist($Owner);
|
||||||
$em->persist($List);
|
$em->persist($List);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
, 'message' => sprintf(_('List %s has been created'), $list_name)
|
, 'message' => sprintf(_('List %s has been created'), $list_name)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (ControllerException $e)
|
catch (ControllerException $e)
|
||||||
{
|
{
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => $e->getMessage()
|
, 'message' => $e->getMessage()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list
|
* Gets a list
|
||||||
*/
|
*/
|
||||||
$controllers->get('/list/{list_id}/', function(Application $app, $list_id)
|
$controllers->get('/list/{list_id}/', function(Application $app, $list_id)
|
||||||
{
|
{
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
|
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
|
|
||||||
$owners = $entries = $lists = array();
|
$owners = $entries = $lists = array();
|
||||||
|
|
||||||
foreach ($list->getOwners() as $owner)
|
foreach ($list->getOwners() as $owner)
|
||||||
{
|
{
|
||||||
$owners[] = array(
|
$owners[] = array(
|
||||||
'usr_id' => $owner->getUser()->get_id(),
|
'usr_id' => $owner->getUser()->get_id(),
|
||||||
'display_name' => $owner->getUser()->get_display_name(),
|
'display_name' => $owner->getUser()->get_display_name(),
|
||||||
'position' => $owner->getUser()->get_position(),
|
'position' => $owner->getUser()->get_position(),
|
||||||
'job' => $owner->getUser()->get_job(),
|
'job' => $owner->getUser()->get_job(),
|
||||||
'company' => $owner->getUser()->get_company(),
|
'company' => $owner->getUser()->get_company(),
|
||||||
'email' => $owner->getUser()->get_email(),
|
'email' => $owner->getUser()->get_email(),
|
||||||
'role' => $owner->getRole()
|
'role' => $owner->getRole()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($list->getEntries() as $entry)
|
foreach ($list->getEntries() as $entry)
|
||||||
{
|
{
|
||||||
$entries[] = array(
|
$entries[] = array(
|
||||||
'usr_id' => $owner->getUser()->get_id(),
|
'usr_id' => $owner->getUser()->get_id(),
|
||||||
'display_name' => $owner->getUser()->get_display_name(),
|
'display_name' => $owner->getUser()->get_display_name(),
|
||||||
'position' => $owner->getUser()->get_position(),
|
'position' => $owner->getUser()->get_position(),
|
||||||
'job' => $owner->getUser()->get_job(),
|
'job' => $owner->getUser()->get_job(),
|
||||||
'company' => $owner->getUser()->get_company(),
|
'company' => $owner->getUser()->get_company(),
|
||||||
'email' => $owner->getUser()->get_email(),
|
'email' => $owner->getUser()->get_email(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
$datas = array('list' => array(
|
$datas = array('list' => array(
|
||||||
'name' => $list->getName(),
|
'name' => $list->getName(),
|
||||||
'created' => $list->getCreated()->format(DATE_ATOM),
|
'created' => $list->getCreated()->format(DATE_ATOM),
|
||||||
'updated' => $list->getUpdated()->format(DATE_ATOM),
|
'updated' => $list->getUpdated()->format(DATE_ATOM),
|
||||||
'owners' => $owners,
|
'owners' => $owners,
|
||||||
'users' => $entries
|
'users' => $entries
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update a list
|
* Update a list
|
||||||
*/
|
*/
|
||||||
$controllers->post('/list/{list_id}/update/', function(Application $app, $list_id)
|
$controllers->post('/list/{list_id}/update/', function(Application $app, $list_id)
|
||||||
{
|
{
|
||||||
$request = $app['request'];
|
$request = $app['request'];
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => _('Unable to update list')
|
, 'message' => _('Unable to update list')
|
||||||
);
|
);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$list_name = $request->get('name');
|
$list_name = $request->get('name');
|
||||||
|
|
||||||
if(!$list_name)
|
if (!$list_name)
|
||||||
{
|
{
|
||||||
throw new ControllerException(_('List name is required'));
|
throw new ControllerException(_('List name is required'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
|
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
|
|
||||||
$list->setName($list_name);
|
$list->setName($list_name);
|
||||||
|
|
||||||
$em->merge($list);
|
$em->merge($list);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
, 'message' => _('List has been updated')
|
, 'message' => _('List has been updated')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (ControllerException $e)
|
catch (ControllerException $e)
|
||||||
{
|
{
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => $e->getMessage()
|
, 'message' => $e->getMessage()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a list
|
* Delete a list
|
||||||
*/
|
*/
|
||||||
$controllers->post('/list/{list_id}/delete/', function(Application $app, $list_id)
|
$controllers->post('/list/{list_id}/delete/', function(Application $app, $list_id)
|
||||||
{
|
{
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
|
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
|
|
||||||
$em->remove($list);
|
$em->remove($list);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
, 'message' => sprintf(_('List has been deleted'))
|
, 'message' => sprintf(_('List has been deleted'))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (\Exception $e)
|
catch (\Exception $e)
|
||||||
{
|
{
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => sprintf(_('Unable to delete list'))
|
, 'message' => sprintf(_('Unable to delete list'))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
@@ -299,201 +299,211 @@ class UsrLists implements ControllerProviderInterface
|
|||||||
* Remove a usr_id from a list
|
* Remove a usr_id from a list
|
||||||
*/
|
*/
|
||||||
$controllers->post('/list/{list_id}/remove/{entry_id}/', function(Application $app, $list_id, $entry_id)
|
$controllers->post('/list/{list_id}/remove/{entry_id}/', function(Application $app, $list_id, $entry_id)
|
||||||
{
|
{
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
|
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
$entry_repository = $em->getRepository('\Entities\UsrListEntry');
|
$entry_repository = $em->getRepository('\Entities\UsrListEntry');
|
||||||
|
|
||||||
$user_entry = $entry_repository->findEntryByListAndEntryId($list, $entry_id);
|
$user_entry = $entry_repository->findEntryByListAndEntryId($list, $entry_id);
|
||||||
|
|
||||||
$em->remove($user_entry);
|
$em->remove($user_entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
, 'message' => _('Entry removed from list')
|
, 'message' => _('Entry removed from list')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (\Exception $e)
|
catch (\Exception $e)
|
||||||
{
|
{
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => _('Unable to remove entry from list')
|
, 'message' => _('Unable to remove entry from list')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a usr_id to a list
|
* Adds a usr_id to a list
|
||||||
*/
|
*/
|
||||||
$controllers->post('/list/{list_id}/add/{usr_id}/', function(Application $app, $list_id, $usr_id)
|
$controllers->post('/list/{list_id}/add/{usr_id}/', function(Application $app, $list_id, $usr_id)
|
||||||
{
|
{
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
$user_entry = \User_Adapter::getInstance($usr_id, \appbox::get_instance());
|
$user_entry = \User_Adapter::getInstance($usr_id, \appbox::get_instance());
|
||||||
|
|
||||||
$entry = new \Entities\UsrListEntry();
|
$entry = new \Entities\UsrListEntry();
|
||||||
$entry->setUser($user_entry);
|
$entry->setUser($user_entry);
|
||||||
$entry->setList($list);
|
$entry->setList($list);
|
||||||
|
|
||||||
$list->addUsrListEntry($entry);
|
$list->addUsrListEntry($entry);
|
||||||
|
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
$em->merge($list);
|
$em->merge($list);
|
||||||
|
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
, 'message' => _('Usr added to list')
|
, 'message' => _('Usr added to list')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (\Exception $e)
|
catch (\Exception $e)
|
||||||
{
|
{
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => false
|
'success' => false
|
||||||
, 'message' => _('Unable to add usr to list')
|
, 'message' => _('Unable to add usr to list')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Share a list to a user with an optionnal role
|
* 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)
|
$controllers->post('/list/{list_id}/share/{usr_id}/', function(Application $app, $list_id, $usr_id)
|
||||||
{
|
{
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
|
|
||||||
try
|
$availableRoles = array(
|
||||||
{
|
\Entities\UsrListOwner::ROLE_USER,
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
\Entities\UsrListOwner::ROLE_EDITOR,
|
||||||
|
\Entities\UsrListOwner::ROLE_ADMIN,
|
||||||
|
);
|
||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
if (!$app['request']->get('role'))
|
||||||
/* @var $list \Entities\UsrList */
|
throw new \Exception_BadRequest('Missing role parameter');
|
||||||
|
elseif (!in_array($app['request']->get('role'), $availableRoles))
|
||||||
|
throw new \Exception_BadRequest('Role is invalid');
|
||||||
|
|
||||||
if($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR)
|
try
|
||||||
{
|
{
|
||||||
throw new \Exception('You are not authorized to do this');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
}
|
|
||||||
|
|
||||||
$new_owner = \User_Adapter::getInstance($usr_id, \appbox::get_instance());
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
if($list->hasAccess($new_owner))
|
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_EDITOR)
|
||||||
{
|
{
|
||||||
$owner = $list->getOwner($new_owner);
|
throw new \Exception('You are not authorized to do this');
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
$owner = new \Entities\UsrListOwner();
|
|
||||||
$owner->setList($list);
|
|
||||||
$owner->setUser($new_owner);
|
|
||||||
|
|
||||||
$list->addUsrListOwner($owner);
|
$new_owner = \User_Adapter::getInstance($usr_id, \appbox::get_instance());
|
||||||
|
|
||||||
$em->persist($owner);
|
if ($list->hasAccess($new_owner))
|
||||||
$em->merge($list);
|
{
|
||||||
}
|
$owner = $list->getOwner($new_owner);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$owner = new \Entities\UsrListOwner();
|
||||||
|
$owner->setList($list);
|
||||||
|
$owner->setUser($new_owner);
|
||||||
|
|
||||||
$role = $app['request']->get('role', \Entities\UsrListOwner::ROLE_USER);
|
$list->addUsrListOwner($owner);
|
||||||
|
|
||||||
$owner->setRole($role);
|
$em->persist($owner);
|
||||||
|
$em->merge($list);
|
||||||
|
}
|
||||||
|
|
||||||
$em->merge($owner);
|
$role = $app['request']->get('role');
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
$datas = array(
|
$owner->setRole($role);
|
||||||
'success' => true
|
|
||||||
, 'message' => _('List shared to user')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (\Exception $e)
|
|
||||||
{
|
|
||||||
|
|
||||||
$datas = array(
|
$em->merge($owner);
|
||||||
'success' => false
|
$em->flush();
|
||||||
, 'message' => _('Unable to share the list with the usr')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
$datas = array(
|
||||||
|
'success' => true
|
||||||
|
, 'message' => _('List shared to user')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (\Exception $e)
|
||||||
|
{
|
||||||
|
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
$datas = array(
|
||||||
}
|
'success' => false
|
||||||
|
, 'message' => _('Unable to share the list with the usr')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
|
|
||||||
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
|
}
|
||||||
);
|
);
|
||||||
/**
|
/**
|
||||||
* UnShare a list to a user
|
* UnShare a list to a user
|
||||||
*/
|
*/
|
||||||
$controllers->post('/list/{list_id}/unshare/{owner_id}/', function(Application $app, $list_id, $owner_id)
|
$controllers->post('/list/{list_id}/unshare/{usr_id}/', function(Application $app, $list_id, $usr_id)
|
||||||
{
|
{
|
||||||
$em = $app['Core']->getEntityManager();
|
$em = $app['Core']->getEntityManager();
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$repository = $em->getRepository('\Entities\UsrList');
|
$repository = $em->getRepository('\Entities\UsrList');
|
||||||
|
|
||||||
$list = $repository->findUserListByUserAndId($user, $list_id);
|
$list = $repository->findUserListByUserAndId($user, $list_id);
|
||||||
/* @var $list \Entities\UsrList */
|
/* @var $list \Entities\UsrList */
|
||||||
|
|
||||||
if($list->getOwner($user)->getList() < \Entities\UsrListOwner::ROLE_ADMIN)
|
if ($list->getOwner($user)->getRole() < \Entities\UsrListOwner::ROLE_ADMIN)
|
||||||
{
|
{
|
||||||
throw new \Exception('You are not authorized to do this');
|
throw new \Exception('You are not authorized to do this');
|
||||||
}
|
}
|
||||||
|
|
||||||
$owners_repository = $em->getRepository('\Entities\UsrListOwner');
|
$owners_repository = $em->getRepository('\Entities\UsrListOwner');
|
||||||
|
|
||||||
$owner = $owners_repository->findByListAndOwner($list, $owner_id);
|
$owner = $owners_repository->findByListAndUsrId($list, $usr_id);
|
||||||
|
|
||||||
$em->remove($owner);
|
$em->remove($owner);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
$datas = array(
|
$datas = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
, 'message' => _('Owner removed from list')
|
, 'message' => _('Owner removed from list')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (\Exception $e)
|
catch (\Exception $e)
|
||||||
{
|
{
|
||||||
|
$datas = array(
|
||||||
|
'success' => false
|
||||||
|
, 'message' => _('Unable to remove usr from list')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$datas = array(
|
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
||||||
'success' => false
|
|
||||||
, 'message' => _('Unable to add usr to list')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$Json = $app['Core']['Serializer']->serialize($datas, 'json');
|
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
||||||
|
}
|
||||||
return new Response($Json, 200, array('Content-Type' => 'application/json'));
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@@ -127,7 +127,8 @@ class WorkZone implements ControllerProviderInterface
|
|||||||
'/attachStories/'
|
'/attachStories/'
|
||||||
, function(Application $app, Request $request)
|
, function(Application $app, Request $request)
|
||||||
{
|
{
|
||||||
|
if(!$request->get('stories'))
|
||||||
|
throw new \Exception_BadRequest();
|
||||||
|
|
||||||
$user = $app['Core']->getAuthenticatedUser();
|
$user = $app['Core']->getAuthenticatedUser();
|
||||||
|
|
||||||
@@ -137,7 +138,7 @@ class WorkZone implements ControllerProviderInterface
|
|||||||
$StoryWZRepo = $em->getRepository('\Entities\StoryWZ');
|
$StoryWZRepo = $em->getRepository('\Entities\StoryWZ');
|
||||||
|
|
||||||
$alreadyFixed = $done = 0;
|
$alreadyFixed = $done = 0;
|
||||||
|
|
||||||
foreach (explode(';', $request->get('stories')) as $element)
|
foreach (explode(';', $request->get('stories')) as $element)
|
||||||
{
|
{
|
||||||
$element = explode('_', $element);
|
$element = explode('_', $element);
|
||||||
@@ -196,7 +197,7 @@ class WorkZone implements ControllerProviderInterface
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
$message = sprintf(
|
$message = sprintf(
|
||||||
_('%1$d Story attached to the WorkZone, %2$d already attached')
|
_('%1$d Stories attached to the WorkZone, %2$d already attached')
|
||||||
, $done
|
, $done
|
||||||
, $alreadyFixed
|
, $alreadyFixed
|
||||||
);
|
);
|
||||||
|
@@ -12,6 +12,7 @@ use Doctrine\ORM\EntityRepository;
|
|||||||
*/
|
*/
|
||||||
class UsrListOwnerRepository extends EntityRepository
|
class UsrListOwnerRepository extends EntityRepository
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
@@ -37,4 +38,36 @@ class UsrListOwnerRepository extends EntityRepository
|
|||||||
return $owner;
|
return $owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param \Entities\UsrList $list
|
||||||
|
* @param type $usr_id
|
||||||
|
* @return \Entities\UsrList
|
||||||
|
*/
|
||||||
|
public function findByListAndUsrId(\Entities\UsrList $list, $usr_id)
|
||||||
|
{
|
||||||
|
$dql = 'SELECT o FROM Entities\UsrListOwner o
|
||||||
|
JOIN o.list l
|
||||||
|
WHERE l.id = :list_id AND o.usr_id = :usr_id';
|
||||||
|
|
||||||
|
$params = array(
|
||||||
|
'usr_id' => $usr_id,
|
||||||
|
'list_id' => $list->getId()
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = $this->_em->createQuery($dql);
|
||||||
|
$query->setParameters($params);
|
||||||
|
|
||||||
|
$owner = $query->getSingleResult();
|
||||||
|
|
||||||
|
/* @var $owner \Entities\UsrListOwner */
|
||||||
|
if (null === $owner)
|
||||||
|
{
|
||||||
|
throw new \Exception_NotFound(_('Owner is not found'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $owner;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user