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