GroupController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  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 FOS\UserBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. /**
  15. * RESTful controller managing group CRUD
  16. *
  17. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  18. * @author Christophe Coevoet <stof@notk.org>
  19. */
  20. class GroupController extends ContainerAware
  21. {
  22. /**
  23. * Show all groups
  24. */
  25. public function listAction()
  26. {
  27. $groups = $this->container->get('fos_user.group_manager')->findGroups();
  28. return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:list.html.'.$this->getEngine(), array('groups' => $groups));
  29. }
  30. /**
  31. * Show one group
  32. */
  33. public function showAction($groupname)
  34. {
  35. $group = $this->findGroupBy('name', $groupname);
  36. return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:show.html.'.$this->getEngine(), array('group' => $group));
  37. }
  38. /**
  39. * Edit one group, show the edit form
  40. */
  41. public function editAction($groupname)
  42. {
  43. $group = $this->findGroupBy('name', $groupname);
  44. $form = $this->container->get('fos_user.group.form');
  45. $formHandler = $this->container->get('fos_user.group.form.handler');
  46. $process = $formHandler->process($group);
  47. if ($process) {
  48. $this->setFlash('fos_user_success', 'group.flash.updated');
  49. $groupUrl = $this->container->get('router')->generate('fos_user_group_show', array('groupname' => $group->getName()));
  50. return new RedirectResponse($groupUrl);
  51. }
  52. return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:edit.html.'.$this->getEngine(), array(
  53. 'form' => $form->createview(),
  54. 'groupname' => $group->getName(),
  55. ));
  56. }
  57. /**
  58. * Show the new form
  59. */
  60. public function newAction()
  61. {
  62. $form = $this->container->get('fos_user.group.form');
  63. $formHandler = $this->container->get('fos_user.group.form.handler');
  64. $process = $formHandler->process();
  65. if ($process) {
  66. $this->setFlash('fos_user_success', 'group.flash.created');
  67. $parameters = array('groupname' => $form->getData('group')->getName());
  68. $url = $this->container->get('router')->generate('fos_user_group_show', $parameters);
  69. return new RedirectResponse($url);
  70. }
  71. return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:new.html.'.$this->getEngine(), array(
  72. 'form' => $form->createview(),
  73. ));
  74. }
  75. /**
  76. * Delete one group
  77. */
  78. public function deleteAction($groupname)
  79. {
  80. $group = $this->findGroupBy('name', $groupname);
  81. $this->container->get('fos_user.group_manager')->deleteGroup($group);
  82. $this->setFlash('fos_user_success', 'group.flash.deleted');
  83. return new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));
  84. }
  85. /**
  86. * Find a group by a specific property
  87. *
  88. * @param string $key property name
  89. * @param mixed $value property value
  90. *
  91. * @throws NotFoundException if user does not exist
  92. * @return \FOS\UserBundle\Model\GroupInterface
  93. */
  94. protected function findGroupBy($key, $value)
  95. {
  96. if (!empty($value)) {
  97. $group = $this->container->get('fos_user.group_manager')->{'findGroupBy'.ucfirst($key)}($value);
  98. }
  99. if (empty($group)) {
  100. throw new NotFoundHttpException(sprintf('The group with "%s" does not exist for value "%s"', $key, $value));
  101. }
  102. return $group;
  103. }
  104. protected function getEngine()
  105. {
  106. return $this->container->getParameter('fos_user.template.engine');
  107. }
  108. /**
  109. * @param string $action
  110. * @param string $value
  111. */
  112. protected function setFlash($action, $value)
  113. {
  114. $this->container->get('session')->getFlashBag()->set($action, $value);
  115. }
  116. }