|
@@ -14,21 +14,63 @@ namespace Packagist\WebBundle\Form\Type;
|
|
|
|
|
|
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
|
|
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
|
|
+use Symfony\Component\Form\FormEvents;
|
|
|
|
+use Symfony\Component\Form\FormEvent;
|
|
|
|
+use Symfony\Component\Security\Core\SecurityContext;
|
|
|
|
+use Symfony\Component\Security\Core\Validator\Constraint\UserPassword as OldUserPassword;
|
|
|
|
+use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
*/
|
|
*/
|
|
class ProfileFormType extends BaseType
|
|
class ProfileFormType extends BaseType
|
|
{
|
|
{
|
|
|
|
+ private $securityContext;
|
|
|
|
+
|
|
|
|
+ public function __construct($class, SecurityContext $securityContext)
|
|
|
|
+ {
|
|
|
|
+ parent::__construct($class);
|
|
|
|
+
|
|
|
|
+ $this->securityContext = $securityContext;
|
|
|
|
+ }
|
|
|
|
+
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
|
{
|
|
{
|
|
- parent::buildForm($builder, $options);
|
|
|
|
|
|
+ $builder->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
|
|
|
|
+ ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'));
|
|
|
|
+
|
|
|
|
+ $user = $this->securityContext->getToken()->getUser();
|
|
|
|
+
|
|
|
|
+ $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($user)
|
|
|
|
+ {
|
|
|
|
+ if ( ! $user->getGithubId())
|
|
|
|
+ {
|
|
|
|
+ $this->addPasswordField($event);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
|
|
$builder->add('failureNotifications', null, array(
|
|
$builder->add('failureNotifications', null, array(
|
|
'required' => false,
|
|
'required' => false,
|
|
));
|
|
));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private function addPasswordField(&$event)
|
|
|
|
+ {
|
|
|
|
+ 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();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $event->getForm()->add('current_password', 'password', array(
|
|
|
|
+ 'label' => 'form.current_password',
|
|
|
|
+ 'translation_domain' => 'FOSUserBundle',
|
|
|
|
+ 'mapped' => false,
|
|
|
|
+ 'constraints' => $constraint,
|
|
|
|
+ ));
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* {@inheritdoc}
|
|
* {@inheritdoc}
|
|
*/
|
|
*/
|