Last active
March 22, 2016 11:34
-
-
Save taiebb/0f4a41d790e2dbb041ee to your computer and use it in GitHub Desktop.
Object(Symfony\Component\Form\Form).data.plainPassword = null
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UserBundle\Controller\Admin; | |
use Symfony\Component\DependencyInjection\ContainerAware; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | |
use FOS\UserBundle\Model\UserInterface; | |
use FOS\UserBundle\Controller\ProfileController as BaseController; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
/** | |
* @Route("admin") | |
*/ | |
class ProfileAdminController extends BaseController { | |
/** | |
* Edit the ADMIN user | |
* @Route("/edit-admin-profile", name="edit_admin_profile") | |
* @Method({"GET", "POST"}) | |
*/ | |
public function editAction() { | |
$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.'); | |
} | |
$class = $this->container->getParameter('fos_user.model.user.class'); | |
$form = $this->container->get('form.factory') | |
->create(new \UserBundle\Form\Admin\ProfileAdminFormType($class)); | |
$formHandler = $this->getFormHandler($form); | |
$process = $formHandler->process($user); | |
if ($process) { | |
$this->setFlash('fos_user_success', 'profile.flash.updated'); | |
return new RedirectResponse($this->getRedirectionUrl($user)); | |
} | |
return $this->container->get('templating')->renderResponse( | |
'admin/Profile/edit.html.' . $this->container->getParameter('fos_user.template.engine') | |
, array('form' => $form->createView()) | |
); | |
} | |
/** | |
* @param string $action | |
* @param string $value | |
*/ | |
protected function setFlash($action, $value) { | |
$this->container->get('session')->getFlashBag()->set($action, $value); | |
} | |
protected function getFormHandler($form) { | |
return new \UserBundle\Form\Handler\ProfileAdminFormHandler( | |
$form, $this->container->get('request') | |
, $this->container->get('fos_user.user_manager') | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UserBundle\Form\Handler; | |
use FOS\UserBundle\Model\UserInterface; | |
use FOS\UserBundle\Model\UserManagerInterface; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
class ProfileAdminFormHandler { | |
protected $request; | |
protected $userManager; | |
protected $form; | |
public function __construct(FormInterface $form, Request $request, UserManagerInterface $userManager) { | |
$this->form = $form; | |
$this->request = $request; | |
$this->userManager = $userManager; | |
} | |
public function process(UserInterface $user) { | |
$this->form->setData($user); | |
if ('POST' === $this->request->getMethod()) { | |
$this->form->bind($this->request); | |
if ($this->form->isValid()) { | |
$this->onSuccess($user); | |
return true; | |
} | |
// Reloads the user to reset its username. This is needed when the | |
// username or password have been changed to avoid issues with the | |
// security layer. | |
$this->userManager->reloadUser($user); | |
} | |
return false; | |
} | |
protected function onSuccess(UserInterface $user) { | |
$this->userManager->updateUser($user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UserBundle\Form\Admin; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use FOS\UserBundle\Form\Type\ProfileFormType; | |
use UserBundle\Form\AdminProfileType; | |
class ProfileAdminFormType extends ProfileFormType { | |
public function buildForm(FormBuilderInterface $builder, array $options) { | |
parent::buildForm($builder, $options); | |
$builder | |
->add('enabled') | |
->add('adminProfile', new AdminProfileType()) | |
; | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) { | |
parent::setDefaultOptions($resolver); | |
$resolver->setDefaults(['translation_domain' => 'FOSUserBundle']); | |
} | |
public function getName() { | |
return 'admin_profile'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the FOSUserBundle package. | |
* | |
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace FOS\UserBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword as OldUserPassword; | |
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; | |
class ProfileFormType extends AbstractType | |
{ | |
private $class; | |
/** | |
* @param string $class The User class name | |
*/ | |
public function __construct($class) | |
{ | |
$this->class = $class; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
if (class_exists('Symfony\Component\Security\Core\Validator\Constraints\UserPassword')) { | |
$constraint = new UserPassword(); | |
} else { | |
// Symfony 2.1 support with the old constraint class | |
$constraint = new OldUserPassword(); | |
} | |
$this->buildUserForm($builder, $options); | |
$builder->add('current_password', 'password', array( | |
'label' => 'form.current_password', | |
'translation_domain' => 'FOSUserBundle', | |
'mapped' => false, | |
'constraints' => $constraint, | |
)); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => $this->class, | |
'intention' => 'profile', | |
)); | |
} | |
public function getName() | |
{ | |
return 'fos_user_profile'; | |
} | |
/** | |
* Builds the embedded form representing the user. | |
* | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
protected function buildUserForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle')) | |
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle')) | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
J'utilise "friendsofsymfony/user-bundle": "~1.3",