Question

I'm a bit confused about how to access the current user in Symfony 2. Currently I'm trying to display a variation of a form (AbstractType) depending on the ROLES of the current user.

A similar question has already been answered by Gremo: Access currently logged in user in EntityRepository

My question is: Is there a Symfony 2 native way to access the user inside my AbstractType class without using JMSDiExtraBundle? Thanks!

Here's my current code:

namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class Comment extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        //somehow access the current user here

        $builder
            ->add('name')
            ->add('comment_text')
            ->add('comment_email')

        // Add more fields depending on user role

                ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\Comment'
        ));
    }

    public function getName()
    {
        return 'acme_demobundle_comment';
    }
}

Edit: I'm looking for the currently logged in user (security.context)

Was it helpful?

Solution

Into your controller, do something like this

$form = $this->createForm(new CommentType($this->get('security.context')
                                           ->isGranted('ROLE_ADMIN')), $comment);

Where ROLE_ADMIN is the role for which you want to discriminate.

Now, into your Type you have to retrieve it into the following way

private $isGranted;

public function __construct($roleFlag)
{
  $this->isGranted = $roleFlag;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
  $builder
    ->add('name')
    ->add('comment_text')
    ->add('comment_email');

    if($this->isGranted) {
      $builder
        ->add(....)
        ->add(....)
        [....]
        ->add(....);
}

OTHER TIPS

JMSDiExtraBundle provide (among other) annotations and shortcuts in order to define services, for example form types and doctrine listeners, that is just regular services but with particular tags. If i recall correctly the bundle is included in a standard Symfony 2.1 release, so why not using it?

Anyway to inject the user "the old way", use constructor injection for example:

class Comment extends AbstractType
{
    private $context;

    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $loggedUser = $this->context->getToken()->getUser();

        /* ... */
    }
}

And define it as a service with the form.type tag:

<service id="form.type.comment" class="Acme\DemoBundle\Form\Comment">
    <argument type="service" id="security.context" />
    <tag name="form.type" alias="comment" />
</service>

Why don't you inject the User as a ConstructorArgument:

$form = $this->createForm(new CommentType($user), $comment);

I'm new in Symphony so i hope this is not totaly wrong :-S

If the UserObject is part of the Comment Model you're working on with your form you'll be able to access it via:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $user = $builder->getData()->getUser();
    ....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top