3

Symfony 2 で現在のユーザーにアクセスする方法について少し混乱しています。現在、現在のユーザーの ROLES に応じてフォーム (AbstractType) のバリエーションを表示しようとしています。

同様の質問がGremoによってすでに回答されています: EntityRepositoryで現在ログインしているユーザーにアクセスする

私の質問は: JMSDiExtraBundleを使用せずに、AbstractType クラス内のユーザーにアクセスする Symfony 2 ネイティブの方法はありますか? ありがとう!

これが私の現在のコードです:

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';
    }
}

編集:現在ログインしているユーザー(security.context)を探しています

4

4 に答える 4

14

コントローラに、次のようなことをします

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

ROLE_ADMIN差別したい役割はどこにありますか。

今、あなたTypeはそれを次の方法で取得する必要があります

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(....);
}
于 2012-10-02T13:17:06.883 に答える
6

JMSDiExtraBundle は、フォーム タイプやドクトリン リスナーなどのサービスを定義するために、(特に) 注釈とショートカットを提供します。これは単なる通常のサービスですが、特定のタグが付いています。私の記憶が正しければ、バンドルは標準の Symfony 2.1 リリースに含まれているので、それを使用しないのはなぜですか?

とにかく、ユーザーを「古い方法」で注入するには、たとえばコンストラクター注入を使用します。

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();

        /* ... */
    }
}

そして、次のform.typeタグを使用してサービスとして定義します。

<service id="form.type.comment" class="Acme\DemoBundle\Form\Comment">
    <argument type="service" id="security.context" />
    <tag name="form.type" alias="comment" />
</service>
于 2012-10-03T06:19:06.290 に答える
2

User を ConstructorArgument として注入してみませんか。

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

私はシンフォニーが初めてなので、これが完全に間違っていないことを願っています:-S

于 2012-10-02T08:41:59.487 に答える
-2

UserObject が、フォームで作業しているコメント モデルの一部である場合は、次の方法でアクセスできます。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $user = $builder->getData()->getUser();
    ....
于 2012-10-02T11:37:33.473 に答える