vendor/bundles/FOS/UserBundle/Controller/ChangePasswordController.php
author cavaliet
Thu, 20 Oct 2011 18:35:34 +0200
changeset 14 fc78844c8a76
parent 3 e54dfe4d0b2b
permissions -rwxr-xr-x
debug addtag for document
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
/*
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * This file is part of the FOSUserBundle package.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * For the full copyright and license information, please view the LICENSE
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * file that was distributed with this source code.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
namespace FOS\UserBundle\Controller;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Symfony\Component\DependencyInjection\ContainerAware;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Symfony\Component\HttpFoundation\RedirectResponse;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
use FOS\UserBundle\Model\UserInterface;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
/**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * Controller managing the password change
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * @author Thibault Duplessis <thibault.duplessis@gmail.com>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * @author Christophe Coevoet <stof@notk.org>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
class ChangePasswordController extends ContainerAware
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
     * Change user password
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    public function changePasswordAction()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        $user = $this->container->get('security.context')->getToken()->getUser();
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        if (!is_object($user) || !$user instanceof UserInterface) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            throw new AccessDeniedException('This user does not have access to this section.');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        $form = $this->container->get('fos_user.change_password.form');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        $formHandler = $this->container->get('fos_user.change_password.form.handler');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $process = $formHandler->process($user);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        if ($process) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
            $this->setFlash('fos_user_success', 'change_password.flash.success');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            return new RedirectResponse($this->getRedirectionUrl($user));
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        return $this->container->get('templating')->renderResponse(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            'FOSUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            array('form' => $form->createView(), 'theme' => $this->container->getParameter('fos_user.template.theme'))
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
     * Generate the redirection url when the resetting is completed.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
     *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
     * @param UserInterface $user
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
     * @return string
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    protected function getRedirectionUrl(UserInterface $user)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        return $this->container->get('router')->generate('fos_user_profile_show');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    protected function setFlash($action, $value)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        $this->container->get('session')->setFlash($action, $value);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
}