GroupController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\UserBundle\Controller\Api;
  11. use FOS\RestBundle\Context\Context;
  12. use FOS\RestBundle\Controller\Annotations\QueryParam;
  13. use FOS\RestBundle\Controller\Annotations\View;
  14. use FOS\RestBundle\Request\ParamFetcherInterface;
  15. use FOS\RestBundle\View\View as FOSRestView;
  16. use FOS\UserBundle\Model\GroupInterface;
  17. use JMS\Serializer\SerializationContext;
  18. use Nelmio\ApiDocBundle\Annotation\ApiDoc;
  19. use Sonata\DatagridBundle\Pager\PagerInterface;
  20. use Sonata\UserBundle\Model\GroupManagerInterface;
  21. use Symfony\Component\Form\FormFactoryInterface;
  22. use Symfony\Component\Form\FormInterface;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. /**
  26. * @author Hugo Briand <briand@ekino.com>
  27. */
  28. class GroupController
  29. {
  30. /**
  31. * @var GroupManagerInterface
  32. */
  33. protected $groupManager;
  34. /**
  35. * @var FormFactoryInterface
  36. */
  37. protected $formFactory;
  38. /**
  39. * @param GroupManagerInterface $groupManager Sonata group manager
  40. * @param FormFactoryInterface $formFactory Symfony form factory
  41. */
  42. public function __construct(GroupManagerInterface $groupManager, FormFactoryInterface $formFactory)
  43. {
  44. $this->groupManager = $groupManager;
  45. $this->formFactory = $formFactory;
  46. }
  47. /**
  48. * Returns a paginated list of groups.
  49. *
  50. * @ApiDoc(
  51. * resource=true,
  52. * output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}}
  53. * )
  54. *
  55. * @QueryParam(name="page", requirements="\d+", default="1", description="Page for groups list pagination (1-indexed)")
  56. * @QueryParam(name="count", requirements="\d+", default="10", description="Number of groups by page")
  57. * @QueryParam(name="enabled", requirements="0|1", nullable=true, strict=true, description="Enabled/disabled groups only?")
  58. *
  59. * @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true)
  60. *
  61. * @param ParamFetcherInterface $paramFetcher
  62. *
  63. * @return PagerInterface
  64. */
  65. public function getGroupsAction(ParamFetcherInterface $paramFetcher)
  66. {
  67. $orderByQueryParam = new QueryParam();
  68. $orderByQueryParam->name = 'orderBy';
  69. $orderByQueryParam->requirements = 'ASC|DESC';
  70. $orderByQueryParam->nullable = true;
  71. $orderByQueryParam->strict = true;
  72. $orderByQueryParam->description = 'Query groups order by clause (key is field, value is direction)';
  73. if (property_exists($orderByQueryParam, 'map')) {
  74. $orderByQueryParam->map = true;
  75. } else {
  76. $orderByQueryParam->array = true;
  77. }
  78. $paramFetcher->addParam($orderByQueryParam);
  79. $supportedFilters = array(
  80. 'enabled' => '',
  81. );
  82. $page = $paramFetcher->get('page');
  83. $limit = $paramFetcher->get('count');
  84. $sort = $paramFetcher->get('orderBy');
  85. $criteria = array_intersect_key($paramFetcher->all(), $supportedFilters);
  86. foreach ($criteria as $key => $value) {
  87. if (null === $value) {
  88. unset($criteria[$key]);
  89. }
  90. }
  91. if (!$sort) {
  92. $sort = array();
  93. } elseif (!is_array($sort)) {
  94. $sort = array($sort, 'asc');
  95. }
  96. return $this->groupManager->getPager($criteria, $page, $limit, $sort);
  97. }
  98. /**
  99. * Retrieves a specific group.
  100. *
  101. * @ApiDoc(
  102. * requirements={
  103. * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="group id"}
  104. * },
  105. * output={"class"="FOS\UserBundle\Model\GroupInterface", "groups"={"sonata_api_read"}},
  106. * statusCodes={
  107. * 200="Returned when successful",
  108. * 404="Returned when group is not found"
  109. * }
  110. * )
  111. *
  112. * @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true)
  113. *
  114. * @param $id
  115. *
  116. * @return GroupInterface
  117. */
  118. public function getGroupAction($id)
  119. {
  120. return $this->getGroup($id);
  121. }
  122. /**
  123. * Adds a group.
  124. *
  125. * @ApiDoc(
  126. * input={"class"="sonata_user_api_form_group", "name"="", "groups"={"sonata_api_write"}},
  127. * output={"class"="Sonata\UserBundle\Model\Group", "groups"={"sonata_api_read"}},
  128. * statusCodes={
  129. * 200="Returned when successful",
  130. * 400="Returned when an error has occurred while group creation",
  131. * }
  132. * )
  133. *
  134. * @param Request $request A Symfony request
  135. *
  136. * @return GroupInterface
  137. *
  138. * @throws NotFoundHttpException
  139. */
  140. public function postGroupAction(Request $request)
  141. {
  142. return $this->handleWriteGroup($request);
  143. }
  144. /**
  145. * Updates a group.
  146. *
  147. * @ApiDoc(
  148. * requirements={
  149. * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="group identifier"}
  150. * },
  151. * input={"class"="sonata_user_api_form_group", "name"="", "groups"={"sonata_api_write"}},
  152. * output={"class"="Sonata\UserBundle\Model\Group", "groups"={"sonata_api_read"}},
  153. * statusCodes={
  154. * 200="Returned when successful",
  155. * 400="Returned when an error has occurred while group creation",
  156. * 404="Returned when unable to find group"
  157. * }
  158. * )
  159. *
  160. * @param int $id Group identifier
  161. * @param Request $request A Symfony request
  162. *
  163. * @return GroupInterface
  164. *
  165. * @throws NotFoundHttpException
  166. */
  167. public function putGroupAction($id, Request $request)
  168. {
  169. return $this->handleWriteGroup($request, $id);
  170. }
  171. /**
  172. * Deletes a group.
  173. *
  174. * @ApiDoc(
  175. * requirements={
  176. * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="group identifier"}
  177. * },
  178. * statusCodes={
  179. * 200="Returned when group is successfully deleted",
  180. * 400="Returned when an error has occurred while group deletion",
  181. * 404="Returned when unable to find group"
  182. * }
  183. * )
  184. *
  185. * @param int $id A Group identifier
  186. *
  187. * @return \FOS\RestBundle\View\View
  188. *
  189. * @throws NotFoundHttpException
  190. */
  191. public function deleteGroupAction($id)
  192. {
  193. $group = $this->getGroup($id);
  194. $this->groupManager->deleteGroup($group);
  195. return array('deleted' => true);
  196. }
  197. /**
  198. * Write a Group, this method is used by both POST and PUT action methods.
  199. *
  200. * @param Request $request Symfony request
  201. * @param int|null $id A Group identifier
  202. *
  203. * @return FormInterface
  204. */
  205. protected function handleWriteGroup($request, $id = null)
  206. {
  207. $groupClassName = $this->groupManager->getClass();
  208. $group = $id ? $this->getGroup($id) : new $groupClassName('');
  209. $form = $this->formFactory->createNamed(null, 'sonata_user_api_form_group', $group, array(
  210. 'csrf_protection' => false,
  211. ));
  212. $form->submit($request);
  213. if ($form->isValid()) {
  214. $group = $form->getData();
  215. $this->groupManager->updateGroup($group);
  216. $view = FOSRestView::create($group);
  217. if (class_exists('FOS\RestBundle\Context\Context')) {
  218. $context = new Context();
  219. $context->setGroups(array('sonata_api_read'));
  220. $view->setContext($context);
  221. } else {
  222. $serializationContext = SerializationContext::create();
  223. $serializationContext->setGroups(array('sonata_api_read'));
  224. $serializationContext->enableMaxDepthChecks();
  225. $view->setSerializationContext($serializationContext);
  226. }
  227. return $view;
  228. }
  229. return $form;
  230. }
  231. /**
  232. * Retrieves group with id $id or throws an exception if it doesn't exist.
  233. *
  234. * @param $id
  235. *
  236. * @return GroupInterface
  237. *
  238. * @throws NotFoundHttpException
  239. */
  240. protected function getGroup($id)
  241. {
  242. $group = $this->groupManager->findGroupBy(array('id' => $id));
  243. if (null === $group) {
  244. throw new NotFoundHttpException(sprintf('Group (%d) not found', $id));
  245. }
  246. return $group;
  247. }
  248. }