Skip to content

Commit dbcec05

Browse files
committed
Merge pull request #867 from stof/fosub_2
Fosub 2
2 parents 5a4db1f + 226bb5e commit dbcec05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1146
-830
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ language: php
33
php:
44
- 5.3
55
- 5.4
6+
- 5.5
67

7-
before_script: composer install --dev
8+
before_script: composer install --dev --prefer-source
89

910
notifications:
1011

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
### 2.0.0 (2013-XX-XX)
5+
6+
* [BC break] Refactored the structure of controller to dispatch events instead of using form handlers
7+
* Removed all form handlers
8+
49
### 1.3.1 (2012-12-22)
510

611
* Replaced the deprecated validation constraints by the new ones

Controller/ChangePasswordController.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace FOS\UserBundle\Controller;
1313

14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FormEvent;
16+
use FOS\UserBundle\Event\FilterUserResponseEvent;
17+
use FOS\UserBundle\Model\UserInterface;
1418
use Symfony\Component\DependencyInjection\ContainerAware;
19+
use Symfony\Component\HttpFoundation\Request;
1520
use Symfony\Component\HttpFoundation\RedirectResponse;
1621
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17-
use FOS\UserBundle\Model\UserInterface;
1822

1923
/**
2024
* Controller managing the password change
@@ -27,47 +31,47 @@ class ChangePasswordController extends ContainerAware
2731
/**
2832
* Change user password
2933
*/
30-
public function changePasswordAction()
34+
public function changePasswordAction(Request $request)
3135
{
3236
$user = $this->container->get('security.context')->getToken()->getUser();
3337
if (!is_object($user) || !$user instanceof UserInterface) {
3438
throw new AccessDeniedException('This user does not have access to this section.');
3539
}
3640

37-
$form = $this->container->get('fos_user.change_password.form');
38-
$formHandler = $this->container->get('fos_user.change_password.form.handler');
41+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
42+
$formFactory = $this->container->get('fos_user.change_password.form.factory');
43+
44+
$form = $formFactory->createForm();
45+
$form->setData($user);
46+
47+
if ($request->isMethod('POST')) {
48+
$form->bind($request);
49+
50+
if ($form->isValid()) {
51+
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
52+
$userManager = $this->container->get('fos_user.user_manager');
53+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
54+
$dispatcher = $this->container->get('event_dispatcher');
3955

40-
$process = $formHandler->process($user);
41-
if ($process) {
42-
$this->setFlash('fos_user_success', 'change_password.flash.success');
56+
$event = new FormEvent($form, $request);
57+
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);
4358

44-
return new RedirectResponse($this->getRedirectionUrl($user));
59+
$userManager->updateUser($user);
60+
61+
if (null === $response = $event->getResponse()) {
62+
$url = $this->container->get('router')->generate('fos_user_profile_show');
63+
$response = new RedirectResponse($url);
64+
}
65+
66+
$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
67+
68+
return $response;
69+
}
4570
}
4671

4772
return $this->container->get('templating')->renderResponse(
4873
'FOSUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
4974
array('form' => $form->createView())
5075
);
5176
}
52-
53-
/**
54-
* Generate the redirection url when the resetting is completed.
55-
*
56-
* @param \FOS\UserBundle\Model\UserInterface $user
57-
*
58-
* @return string
59-
*/
60-
protected function getRedirectionUrl(UserInterface $user)
61-
{
62-
return $this->container->get('router')->generate('fos_user_profile_show');
63-
}
64-
65-
/**
66-
* @param string $action
67-
* @param string $value
68-
*/
69-
protected function setFlash($action, $value)
70-
{
71-
$this->container->get('session')->setFlash($action, $value);
72-
}
7377
}

Controller/GroupController.php

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
namespace FOS\UserBundle\Controller;
1313

14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FilterGroupResponseEvent;
16+
use FOS\UserBundle\Event\FormEvent;
1417
use Symfony\Component\DependencyInjection\ContainerAware;
18+
use Symfony\Component\HttpFoundation\Request;
1519
use Symfony\Component\HttpFoundation\RedirectResponse;
1620
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1721

@@ -36,51 +40,91 @@ public function listAction()
3640
/**
3741
* Show one group
3842
*/
39-
public function showAction($groupname)
43+
public function showAction($groupName)
4044
{
41-
$group = $this->findGroupBy('name', $groupname);
45+
$group = $this->findGroupBy('name', $groupName);
4246

4347
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:show.html.'.$this->getEngine(), array('group' => $group));
4448
}
4549

4650
/**
4751
* Edit one group, show the edit form
4852
*/
49-
public function editAction($groupname)
53+
public function editAction(Request $request, $groupName)
5054
{
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');
55+
$group = $this->findGroupBy('name', $groupName);
56+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
57+
$formFactory = $this->container->get('fos_user.group.form.factory');
5458

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+
$form = $formFactory->createForm();
60+
$form->setData($group);
5961

60-
return new RedirectResponse($groupUrl);
62+
if ($request->isMethod('POST')) {
63+
$form->bind($request);
64+
65+
if ($form->isValid()) {
66+
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
67+
$groupManager = $this->container->get('fos_user.group_manager');
68+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
69+
$dispatcher = $this->container->get('event_dispatcher');
70+
71+
$event = new FormEvent($form, $request);
72+
$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_SUCCESS, $event);
73+
74+
$groupManager->updateGroup($group);
75+
76+
if (null === $response = $event->getResponse()) {
77+
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
78+
$response = new RedirectResponse($url);
79+
}
80+
81+
$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_COMPLETED, new FilterGroupResponseEvent($group, $request, $response));
82+
83+
return $response;
84+
}
6185
}
6286

6387
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:edit.html.'.$this->getEngine(), array(
6488
'form' => $form->createview(),
65-
'groupname' => $group->getName(),
89+
'group_name' => $group->getName(),
6690
));
6791
}
6892

6993
/**
7094
* Show the new form
7195
*/
72-
public function newAction()
96+
public function newAction(Request $request)
7397
{
74-
$form = $this->container->get('fos_user.group.form');
75-
$formHandler = $this->container->get('fos_user.group.form.handler');
98+
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
99+
$groupManager = $this->container->get('fos_user.group_manager');
100+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
101+
$formFactory = $this->container->get('fos_user.group.form.factory');
102+
103+
$group = $groupManager->createGroup('');
104+
$form = $formFactory->createForm();
105+
$form->setData($group);
106+
107+
if ($request->isMethod('POST')) {
108+
$form->bind($request);
109+
110+
if ($form->isValid()) {
111+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
112+
$dispatcher = $this->container->get('event_dispatcher');
113+
114+
$event = new FormEvent($form, $request);
115+
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_SUCCESS, $event);
76116

77-
$process = $formHandler->process();
78-
if ($process) {
79-
$this->setFlash('fos_user_success', 'group.flash.created');
80-
$parameters = array('groupname' => $form->getData('group')->getName());
81-
$url = $this->container->get('router')->generate('fos_user_group_show', $parameters);
117+
$groupManager->updateGroup($group);
82118

83-
return new RedirectResponse($url);
119+
if (null === $response = $event->getResponse()) {
120+
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
121+
$response = new RedirectResponse($url);
122+
}
123+
124+
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_COMPLETED, new FilterGroupResponseEvent($group, $request, $response));
125+
126+
return $response;
127+
}
84128
}
85129

86130
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:new.html.'.$this->getEngine(), array(
@@ -91,13 +135,18 @@ public function newAction()
91135
/**
92136
* Delete one group
93137
*/
94-
public function deleteAction($groupname)
138+
public function deleteAction(Request $request, $groupName)
95139
{
96-
$group = $this->findGroupBy('name', $groupname);
140+
$group = $this->findGroupBy('name', $groupName);
97141
$this->container->get('fos_user.group_manager')->deleteGroup($group);
98-
$this->setFlash('fos_user_success', 'group.flash.deleted');
99142

100-
return new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));
143+
$response = new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));
144+
145+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
146+
$dispatcher = $this->container->get('event_dispatcher');
147+
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_COMPLETED, new FilterGroupResponseEvent($group, $request, $response));
148+
149+
return $response;
101150
}
102151

103152
/**
@@ -106,7 +155,7 @@ public function deleteAction($groupname)
106155
* @param string $key property name
107156
* @param mixed $value property value
108157
*
109-
* @throws NotFoundException if user does not exist
158+
* @throws NotFoundHttpException if user does not exist
110159
* @return \FOS\UserBundle\Model\GroupInterface
111160
*/
112161
protected function findGroupBy($key, $value)
@@ -126,13 +175,4 @@ protected function getEngine()
126175
{
127176
return $this->container->getParameter('fos_user.template.engine');
128177
}
129-
130-
/**
131-
* @param string $action
132-
* @param string $value
133-
*/
134-
protected function setFlash($action, $value)
135-
{
136-
$this->container->get('session')->setFlash($action, $value);
137-
}
138178
}

Controller/ProfileController.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace FOS\UserBundle\Controller;
1313

14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FormEvent;
16+
use FOS\UserBundle\Event\FilterUserResponseEvent;
17+
use FOS\UserBundle\Model\UserInterface;
1418
use Symfony\Component\DependencyInjection\ContainerAware;
19+
use Symfony\Component\HttpFoundation\Request;
1520
use Symfony\Component\HttpFoundation\RedirectResponse;
1621
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17-
use FOS\UserBundle\Model\UserInterface;
1822

1923
/**
2024
* Controller managing the user profile
@@ -39,47 +43,47 @@ public function showAction()
3943
/**
4044
* Edit the user
4145
*/
42-
public function editAction()
46+
public function editAction(Request $request)
4347
{
4448
$user = $this->container->get('security.context')->getToken()->getUser();
4549
if (!is_object($user) || !$user instanceof UserInterface) {
4650
throw new AccessDeniedException('This user does not have access to this section.');
4751
}
4852

49-
$form = $this->container->get('fos_user.profile.form');
50-
$formHandler = $this->container->get('fos_user.profile.form.handler');
53+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
54+
$formFactory = $this->container->get('fos_user.profile.form.factory');
55+
56+
$form = $formFactory->createForm();
57+
$form->setData($user);
58+
59+
if ('POST' === $request->getMethod()) {
60+
$form->bind($request);
61+
62+
if ($form->isValid()) {
63+
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
64+
$userManager = $this->container->get('fos_user.user_manager');
65+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
66+
$dispatcher = $this->container->get('event_dispatcher');
5167

52-
$process = $formHandler->process($user);
53-
if ($process) {
54-
$this->setFlash('fos_user_success', 'profile.flash.updated');
68+
$event = new FormEvent($form, $request);
69+
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
5570

56-
return new RedirectResponse($this->getRedirectionUrl($user));
71+
$userManager->updateUser($user);
72+
73+
if (null === $response = $event->getResponse()) {
74+
$url = $this->container->get('router')->generate('fos_user_profile_show');
75+
$response = new RedirectResponse($url);
76+
}
77+
78+
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
79+
80+
return $response;
81+
}
5782
}
5883

5984
return $this->container->get('templating')->renderResponse(
6085
'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
6186
array('form' => $form->createView())
6287
);
6388
}
64-
65-
/**
66-
* Generate the redirection url when editing is completed.
67-
*
68-
* @param \FOS\UserBundle\Model\UserInterface $user
69-
*
70-
* @return string
71-
*/
72-
protected function getRedirectionUrl(UserInterface $user)
73-
{
74-
return $this->container->get('router')->generate('fos_user_profile_show');
75-
}
76-
77-
/**
78-
* @param string $action
79-
* @param string $value
80-
*/
81-
protected function setFlash($action, $value)
82-
{
83-
$this->container->get('session')->setFlash($action, $value);
84-
}
8589
}

0 commit comments

Comments
 (0)