public function shareWithUser(Request $request, $list_id, $usr_id) { $availableRoles = [UsrListOwner::ROLE_USER, UsrListOwner::ROLE_EDITOR, UsrListOwner::ROLE_ADMIN]; if (!$request->request->get('role')) { throw new BadRequestHttpException('Missing role parameter'); } elseif (!in_array($request->request->get('role'), $availableRoles)) { throw new BadRequestHttpException('Role is invalid'); } try { $repository = $this->getUsrListRepository(); $user = $this->getAuthenticatedUser(); $list = $repository->findUserListByUserAndId($user, $list_id); if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) { throw new ControllerException($this->app->trans('You are not authorized to do this')); } /** @var User $new_owner */ $new_owner = $this->getUserRepository()->find($usr_id); if ($list->hasAccess($new_owner)) { if ($new_owner->getId() == $user->getId()) { throw new ControllerException('You can not downgrade your Admin right'); } $owner = $list->getOwner($new_owner); } else { $owner = new UsrListOwner(); $owner->setList($list); $owner->setUser($new_owner); $list->addOwner($owner); $this->getEntityManager()->persist($owner); } $role = $request->request->get('role'); $owner->setRole($role); $this->getEntityManager()->flush(); $data = ['success' => true, 'message' => $this->app->trans('List shared to user')]; } catch (ControllerException $e) { $data = ['success' => false, 'message' => $e->getMessage()]; } catch (\Exception $e) { $data = ['success' => false, 'message' => $this->app->trans('Unable to share the list with the usr')]; } return $this->app->json($data); }
/** * {@inheritDoc} */ public function setList(\Alchemy\Phrasea\Model\Entities\UsrList $list = NULL) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setList', array($list)); return parent::setList($list); }