【问题标题】:FOSUserBundle - How are roles handled?FOSUserBundle - 如何处理角色?
【发布时间】:2013-04-18 03:57:50
【问题描述】:

所以,我已经阅读了捆绑包的 documentation,但没有任何内容描述如何列出现有角色或为特定用户分配/更改卷。我需要能够:

  1. 为新注册的用户分配默认角色。
  2. 让管理员和超级管理员提升和降级用户。
    • 这要求我能够查看我可以选择哪些角色。

那么,这是怎么做到的呢?

【问题讨论】:

    标签: php symfony user-management


    【解决方案1】:

    你可以这样做:

    $roleHierarchy = $container->getParameter('security.role_hierarchy.roles');
    $roles = array_keys($roleHierarchy);
    $form = $this->createForm(new UserFormType($roles, $user->getRoles()), $user);
    

    在您的 UserFormType 中,您可以像这样添加角色字段:

    protected $roles;
    protected $userRoles;
    
    public function __construct($roles, $userRoles)
    {
    
        foreach ($roles as $role) {
            $theRoles[$role] = $role;
        }
        $this->roles = $theRoles;
        $this->userRoles = $userRoles;
    
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {               
    
        $builder->add('roles', 'choice', array(
                        'choices' => $this->roles,
                        'data' => $this->userRoles,
                        'expanded' => true,
                        'multiple' => true,
                    ));
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-05
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 2015-06-20
      • 1970-01-01
      相关资源
      最近更新 更多