Permalink
Browse files

Add GUI to edit profile information

  • Loading branch information...
rande committed Jun 8, 2012
1 parent 691bd05 commit ee7e2018e983fa01110c36c1a994f256abf85603
View
@@ -0,0 +1,7 @@
+CHANGELOG
+=========
+
+### 2012-06-08
+
+* Introduce new field for user: firstname, lastname, gender, etc ...
+* add frontend edition of profile information
@@ -0,0 +1,111 @@
+<?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 Sonata\UserBundle\Controller;
+
+use Symfony\Component\DependencyInjection\ContainerAware;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+use Symfony\Component\Security\Core\Exception\AccessDeniedException;
+use Symfony\Component\HttpFoundation\Response;
+use FOS\UserBundle\Model\UserInterface;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+
+/**
+ * This class is inspirated from the FOS Profile Controller, except :
+ * - only twig is supported
+ * - separation of the user authentication form with the profile form
+ *
+ */
+class ProfileController extends Controller
+{
+
+ /**
+ * @return Response
+ *
+ * @throws AccessDeniedException
+ */
+ public function showAction()
+ {
+ $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.');
+ }
+
+ return $this->render('SonataUserBundle:Profile:show.html.twig', array(
+ 'user' => $user
+ ));
+ }
+
+ /**
+ * @return Response
+ *
+ * @throws AccessDeniedException
+ */
+ public function editAuthenticationAction()
+ {
+ $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('sonata_user_authentication_form');
+ $formHandler = $this->container->get('sonata_user_authentication_form_handler');
+
+ $process = $formHandler->process($user);
+ if ($process) {
+ $this->setFlash('fos_user_success', 'profile.flash.updated');
+
+ return new RedirectResponse($this->generateUrl('sonata_user_profile_show'));
+ }
+
+ return $this->render('SonataUserBundle:Profile:edit_authentication.html.twig', array(
+ 'form' => $form->createView(),
+ 'theme' => $this->container->getParameter('fos_user.template.theme')
+ ));
+ }
+
+ /**
+ * @return Response
+ *
+ * @throws AccessDeniedException
+ */
+ public function editProfileAction()
+ {
+ $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('sonata.user.profile.form');
+ $formHandler = $this->container->get('sonata.user.profile.form.handler');
+
+ $process = $formHandler->process($user);
+ if ($process) {
+ $this->setFlash('fos_user_success', 'profile.flash.updated');
+
+ return new RedirectResponse($this->generateUrl('sonata_user_profile_show'));
+ }
+
+ return $this->render('SonataUserBundle:Profile:edit_profile.html.twig', array(
+ 'form' => $form->createView(),
+ 'theme' => $this->container->getParameter('fos_user.template.theme')
+ ));
+ }
+
+ /**
+ * @param string $action
+ * @param string $value
+ */
+ protected function setFlash($action, $value)
+ {
+ $this->container->get('session')->setFlash($action, $value);
+ }
+}
@@ -59,6 +59,26 @@ public function getConfigTreeBuilder()
->scalarNode('user')->end()
->end()
->end()
+
+ // Original code from the FOS User Bundle
+ ->arrayNode('profile')
+ ->addDefaultsIfNotSet()
+ ->canBeUnset()
+ ->children()
+ ->arrayNode('form')
+ ->addDefaultsIfNotSet()
+ ->children()
+ ->scalarNode('type')->defaultValue('sonata_user_profile')->end()
+ ->scalarNode('handler')->defaultValue('sonata.user.profile.form.handler.default')->end()
+ ->scalarNode('name')->defaultValue('sonata_user_profile_form')->cannotBeEmpty()->end()
+ ->arrayNode('validation_groups')
+ ->prototype('scalar')->end()
+ ->defaultValue(array('Profile', 'Default'))
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
->end()
;
@@ -59,6 +59,8 @@ public function load(array $configs, ContainerBuilder $container)
));
$this->configureGoogleAuthenticator($config, $container);
+ $this->configureShortcut($container);
+ $this->configureProfile($config, $container);
}
/**
@@ -159,4 +161,26 @@ public function registerDoctrineMapping(array $config)
)
));
}
+
+ /**
+ * @param ContainerBuilder $container
+ */
+ public function configureShortcut(ContainerBuilder $container)
+ {
+ $container->setAlias('sonata_user_authentication_form', 'fos_user.profile.form');
+ $container->setAlias('sonata_user_authentication_form_handler', 'fos_user.profile.form.handler');
+ }
+
+ /**
+ * @param array $config
+ * @param ContainerBuilder $container
+ */
+ public function configureProfile(array $config, ContainerBuilder $container)
+ {
+ $container->setParameter('sonata.user.profile.form.type', $config['profile']['form']['type']);
+ $container->setParameter('sonata.user.profile.form.name', $config['profile']['form']['name']);
+ $container->setParameter('sonata.user.profile.form.validation_groups', $config['profile']['form']['validation_groups']);
+
+ $container->setAlias('sonata.user.profile.form.handler', $config['profile']['form']['handler']);
+ }
}
@@ -0,0 +1,88 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+
+namespace Sonata\UserBundle\Form\Handler;
+
+use Symfony\Component\Form\Form;
+use Symfony\Component\HttpFoundation\Request;
+
+use FOS\UserBundle\Model\UserInterface;
+use FOS\UserBundle\Model\UserManagerInterface;
+
+/**
+ *
+ * This file is an adapted version of FOS User Bundle ProfileFormHandler class
+ *
+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
+ */
+class ProfileFormHandler
+{
+ /**
+ * @var \Symfony\Component\HttpFoundation\Request
+ */
+ protected $request;
+ /**
+ * @var \FOS\UserBundle\Model\UserManagerInterface
+ */
+ protected $userManager;
+ /**
+ * @var \Symfony\Component\Form\Form
+ */
+ protected $form;
+
+ /**
+ * @param Form $form
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * @param UserManagerInterface $userManager
+ */
+ public function __construct(Form $form, Request $request, UserManagerInterface $userManager)
+ {
+ $this->form = $form;
+ $this->request = $request;
+ $this->userManager = $userManager;
+ }
+
+ /**
+ * @param UserInterface $user
+ *
+ * @return bool
+ */
+ public function process(UserInterface $user)
+ {
+ $this->form->setData($user);
+
+ if ('POST' == $this->request->getMethod()) {
+ $this->form->bindRequest($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;
+ }
+
+ /**
+ * @param UserInterface $user
+ */
+ protected function onSuccess(UserInterface $user)
+ {
+ $this->userManager->updateUser($user);
+ }
+}
View
@@ -0,0 +1,65 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+
+namespace Sonata\UserBundle\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilder;
+
+class ProfileType extends AbstractType
+{
+ private $class;
+
+ /**
+ * @param string $class The User class name
+ */
+ public function __construct($class)
+ {
+ $this->class = $class;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(FormBuilder $builder, array $options)
+ {
+ $builder
+ ->add('gender', null, array('required' => false))
+ ->add('firstname', null, array('required' => false))
+ ->add('lastname', null, array('required' => false))
+ ->add('dateOfBirth', 'date', array('required' => false))
+ ->add('website', null, array('required' => false))
+ ->add('biography', null, array('required' => false))
+ ->add('locale', null, array('required' => false))
+ ->add('timezone', null, array('required' => false))
+ ->add('phone', null, array('required' => false))
+ ;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getDefaultOptions(array $options)
+ {
+ return array(
+ 'data_class' => $this->class
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getName()
+ {
+ return 'sonata_user_profile';
+ }
+}
View
@@ -544,4 +544,12 @@ public function getToken()
{
return $this->token;
}
+
+ /**
+ * @return string
+ */
+ public function getFullname()
+ {
+ return sprintf("%s %s", $this->getFirstname(), $this->getLastname());
+ }
}
View
@@ -6,8 +6,30 @@
<services>
<service id="sonata.user.form.type.security_roles" class="Sonata\UserBundle\Form\Type\SecurityRolesType">
<tag name="form.type" alias="sonata_security_roles" />
+
<argument type="service" id="sonata.admin.pool" />
</service>
+
+ <!-- profile form -->
+ <service id="sonata.user.profile.form" factory-method="createNamed" factory-service="form.factory" class="Symfony\Component\Form\Form">
+ <argument>%sonata.user.profile.form.type%</argument>
+ <argument>%sonata.user.profile.form.name%</argument>
+ <argument />
+ <argument type="collection">
+ <argument key="validation_groups">%sonata.user.profile.form.validation_groups%</argument>
+ </argument>
+ </service>
+
+ <service id="sonata.user.profile.form.type" class="Sonata\UserBundle\Form\Type\ProfileType">
+ <argument>%fos_user.model.user.class%</argument>
+ <tag name="form.type" alias="sonata_user_profile" />
+ </service>
+
+ <service id="sonata.user.profile.form.handler.default" class="Sonata\UserBundle\Form\Handler\ProfileFormHandler" scope="request" public="false">
+ <argument type="service" id="sonata.user.profile.form" />
+ <argument type="service" id="request" />
+ <argument type="service" id="fos_user.user_manager" />
+ </service>
</services>
</container>
Oops, something went wrong.

0 comments on commit ee7e201

Please sign in to comment.