0

FOSUserBundleでユーザー名の編集を禁止するにはどうすればよいですか?

これで、プロファイル編集ページに入り、ユーザー名を変更できます。たとえば、これをROLE_ADMINに対してのみ許可するにはどうすればよいですか?

ただし、プロファイルで電子メールを編集できるようにするため。

見つかった解決策:

class ProfileFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    $builder->remove('username');
}

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

安全ですか?

4

1 に答える 1

2

ROLE_ADMINのみがフォームフィールドを編集できるようにするには、「security.context」サービスをフォームタイプに渡してから、次のような操作を行う必要があります。

if ($this->securityContext->isGranted('ROLE_ADMIN')) {
   $builder->add('username');
}

//or if username is already added

if (!$this->securityContext->isGranted('ROLE_ADMIN')) {
   $builder->remove('username');
}
于 2012-12-14T19:21:05.647 に答える