Skip to content

Fosub 2 #867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Dec 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ language: php
php:
- 5.3
- 5.4
- 5.5

before_script: composer install --dev
before_script: composer install --dev --prefer-source

notifications:
email: [email protected]
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

### 2.0.0 (2013-XX-XX)

* [BC break] Refactored the structure of controller to dispatch events instead of using form handlers
* Removed all form handlers

### 1.3.1 (2012-12-22)

* Replaced the deprecated validation constraints by the new ones
Expand Down
62 changes: 33 additions & 29 deletions Controller/ChangePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

namespace FOS\UserBundle\Controller;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;

/**
* Controller managing the password change
Expand All @@ -27,47 +31,47 @@ class ChangePasswordController extends ContainerAware
/**
* Change user password
*/
public function changePasswordAction()
public function changePasswordAction(Request $request)
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}

$form = $this->container->get('fos_user.change_password.form');
$formHandler = $this->container->get('fos_user.change_password.form.handler');
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.change_password.form.factory');

$form = $formFactory->createForm();
$form->setData($user);

if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');

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

return new RedirectResponse($this->getRedirectionUrl($user));
$userManager->updateUser($user);

if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('fos_user_profile_show');
$response = new RedirectResponse($url);
}

$dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

return $response;
}
}

return $this->container->get('templating')->renderResponse(
'FOSUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
array('form' => $form->createView())
);
}

/**
* Generate the redirection url when the resetting is completed.
*
* @param \FOS\UserBundle\Model\UserInterface $user
*
* @return string
*/
protected function getRedirectionUrl(UserInterface $user)
{
return $this->container->get('router')->generate('fos_user_profile_show');
}

/**
* @param string $action
* @param string $value
*/
protected function setFlash($action, $value)
{
$this->container->get('session')->setFlash($action, $value);
}
}
110 changes: 75 additions & 35 deletions Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

namespace FOS\UserBundle\Controller;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterGroupResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand All @@ -36,51 +40,91 @@ public function listAction()
/**
* Show one group
*/
public function showAction($groupname)
public function showAction($groupName)
{
$group = $this->findGroupBy('name', $groupname);
$group = $this->findGroupBy('name', $groupName);

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

/**
* Edit one group, show the edit form
*/
public function editAction($groupname)
public function editAction(Request $request, $groupName)
{
$group = $this->findGroupBy('name', $groupname);
$form = $this->container->get('fos_user.group.form');
$formHandler = $this->container->get('fos_user.group.form.handler');
$group = $this->findGroupBy('name', $groupName);
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.group.form.factory');

$process = $formHandler->process($group);
if ($process) {
$this->setFlash('fos_user_success', 'group.flash.updated');
$groupUrl = $this->container->get('router')->generate('fos_user_group_show', array('groupname' => $group->getName()));
$form = $formFactory->createForm();
$form->setData($group);

return new RedirectResponse($groupUrl);
if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
$groupManager = $this->container->get('fos_user.group_manager');
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');

$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_SUCCESS, $event);

$groupManager->updateGroup($group);

if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
$response = new RedirectResponse($url);
}

$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_COMPLETED, new FilterGroupResponseEvent($group, $request, $response));

return $response;
}
}

return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:edit.html.'.$this->getEngine(), array(
'form' => $form->createview(),
'groupname' => $group->getName(),
'group_name' => $group->getName(),
));
}

/**
* Show the new form
*/
public function newAction()
public function newAction(Request $request)
{
$form = $this->container->get('fos_user.group.form');
$formHandler = $this->container->get('fos_user.group.form.handler');
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
$groupManager = $this->container->get('fos_user.group_manager');
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.group.form.factory');

$group = $groupManager->createGroup('');
$form = $formFactory->createForm();
$form->setData($group);

if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');

$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_SUCCESS, $event);

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

return new RedirectResponse($url);
if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
$response = new RedirectResponse($url);
}

$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_COMPLETED, new FilterGroupResponseEvent($group, $request, $response));

return $response;
}
}

return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:new.html.'.$this->getEngine(), array(
Expand All @@ -91,13 +135,18 @@ public function newAction()
/**
* Delete one group
*/
public function deleteAction($groupname)
public function deleteAction(Request $request, $groupName)
{
$group = $this->findGroupBy('name', $groupname);
$group = $this->findGroupBy('name', $groupName);
$this->container->get('fos_user.group_manager')->deleteGroup($group);
$this->setFlash('fos_user_success', 'group.flash.deleted');

return new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));
$response = new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));

/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_COMPLETED, new FilterGroupResponseEvent($group, $request, $response));

return $response;
}

/**
Expand All @@ -106,7 +155,7 @@ public function deleteAction($groupname)
* @param string $key property name
* @param mixed $value property value
*
* @throws NotFoundException if user does not exist
* @throws NotFoundHttpException if user does not exist
* @return \FOS\UserBundle\Model\GroupInterface
*/
protected function findGroupBy($key, $value)
Expand All @@ -126,13 +175,4 @@ protected function getEngine()
{
return $this->container->getParameter('fos_user.template.engine');
}

/**
* @param string $action
* @param string $value
*/
protected function setFlash($action, $value)
{
$this->container->get('session')->setFlash($action, $value);
}
}
62 changes: 33 additions & 29 deletions Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

namespace FOS\UserBundle\Controller;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;

/**
* Controller managing the user profile
Expand All @@ -39,47 +43,47 @@ public function showAction()
/**
* Edit the user
*/
public function editAction()
public function editAction(Request $request)
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}

$form = $this->container->get('fos_user.profile.form');
$formHandler = $this->container->get('fos_user.profile.form.handler');
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.profile.form.factory');

$form = $formFactory->createForm();
$form->setData($user);

if ('POST' === $request->getMethod()) {
$form->bind($request);

if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->container->get('event_dispatcher');

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

return new RedirectResponse($this->getRedirectionUrl($user));
$userManager->updateUser($user);

if (null === $response = $event->getResponse()) {
$url = $this->container->get('router')->generate('fos_user_profile_show');
$response = new RedirectResponse($url);
}

$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

return $response;
}
}

return $this->container->get('templating')->renderResponse(
'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
array('form' => $form->createView())
);
}

/**
* Generate the redirection url when editing is completed.
*
* @param \FOS\UserBundle\Model\UserInterface $user
*
* @return string
*/
protected function getRedirectionUrl(UserInterface $user)
{
return $this->container->get('router')->generate('fos_user_profile_show');
}

/**
* @param string $action
* @param string $value
*/
protected function setFlash($action, $value)
{
$this->container->get('session')->setFlash($action, $value);
}
}
Loading