在哪里定义安全角色? [英] Where to define security roles?

查看:158
本文介绍了在哪里定义安全角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户和一个组实体,它们都拥有一个角色数组。



现在我想保持选项打开以修改角色,添加它们



我应该在类中使用常量,还是应该将OneToOne关系与保存所有角色的表相关联?



最好的问候,
pus.dev



用户<=>角色
组< =>角色

  public function getRoles()
{
$ roles = $ this-&

foreach($ this-> getGroups()as $ group){
$ roles = array_merge($ roles,$ group-> getRoles());
}

//我们需要确保至少有一个角色
$ roles [] = static :: ROLE_DEFAULT;

return array_unique($ roles);
}


解决方案

如何创建角色表与每个用户的ManyToOne关系。



或者,您可以创建一个角色表,并与用户具有ManyToMany关系表。使用这个你可以动态定义角色,所以你不必硬编码可能的角色。



在OneToMany案例中,你可以通过写一个这样的函数来检索角色:

  / ** @OneToMany(...)* / 
/ ** $ roles包含字符串* /
protected $ roles;

public function getRoles(){
return $ this-> roles;
}

  / ** @OneToMany(...)* / 
/ ** $ roles包含整数* /
protected $ roles;

public function getRoles(){
$ rolesArr = array(1 =>'ROLE_ADMIN',2 =>'ROLE_USER',3 =>'ROLE_EDITOR'); //你应该重构$ rolesArr
$ retRoles = array();
foreach($ this-> roles as $ role){
$ retRoles [] = $ rolesArr [$ role];
}
return $ retRoles;在ManyToMany案例中,您可以通过编写一个函数来检索角色: / p>

  / ** @ManyToMany(...)* / 
protected $ roles;
// ...
public function getRoles(){
$ retRoles = array();
// symfony2需要一个字符串数组
foreach($ this-> roles as $ role){
$ retRoles [] = $ role-> getName(); //或$ retRoles [] ='ROLE_'。 $ role-> getName();
}
return $ retRoles;
}

并且不要忘记你的User模型必须实现symfony的内置用户界面。



对于群组角色,您可以这样做:

  class Group 
{
/ ** @ManyToMany(...)* /
protected $ roles;

public function getRoles(){
return $ this-> roles;
}
}

class User
{
/ ** @ ORM\Column(...)* /
protected $组;

/ ** @ManyToMany(...)* /
protected $ roles;
// ...

public function getRoles(){
$ retRoles = array();
// symfony2需要一个字符串数组
$ roles = $ this-> roles-> merge($ this-> group-> getRoles());
foreach($ roles as $ role){
$ retRoles [] = $ role-> getName(); //或$ retRoles [] ='ROLE_'。 $ role-> getName();
}
return $ retRoles;
}
}


I have an User and a Group Entity which both hold an array of roles.

Now I would like to keep the option open to modificate the roles, add them and so on.

Should I use constants in the classes for this or should I relate an OneToOne-relation to a table which keeps all the roles?

Best Regards, pus.dev

User <=> Role Group <=> Role

public function getRoles()
{
    $roles = $this->roles;

    foreach ($this->getGroups() as $group) {
        $roles = array_merge($roles, $group->getRoles());
    }

    // we need to make sure to have at least one role
    $roles[] = static::ROLE_DEFAULT;

    return array_unique($roles);
}

解决方案

How about creating a Roles table with ManyToOne relation to each user. One row of the Roles table would contain a role(string or constant int) and a user.

Alternatively you can create a Roles table and have a ManyToMany relation with the User table. Using this you could define roles dynamically, so you don't have to hardcode the possible roles.

In the OneToMany case you could retrieve the roles by writing such a function:

/** @OneToMany(...) */
/** $roles contains strings */
protected $roles;

public function getRoles() {
    return $this->roles;
}

OR

/** @OneToMany(...) */
/** $roles contains integers */
protected $roles;

public function getRoles() {
    $rolesArr = array(1 => 'ROLE_ADMIN', 2 => 'ROLE_USER', 3 => 'ROLE_EDITOR'); // you should refactor $rolesArr
    $retRoles = array();
    foreach($this->roles as $role) {
        $retRoles[] = $rolesArr[$role];
    }
    return $retRoles;
}

In the ManyToMany case you could retrieve the roles by writing such a function:

/** @ManyToMany(...) */
protected $roles;
// ...
public function getRoles() {
    $retRoles = array();
    // symfony2 requires a string array
    foreach($this->roles as $role) {
        $retRoles[] = $role->getName(); // or $retRoles[] = 'ROLE_' . $role->getName();
    }
    return $retRoles;
}

And dont forget that your User model must implement symfony's built-in User interface.

For group roles you can do this:

class Group
{
    /** @ManyToMany(...) */
    protected $roles;

    public function getRoles() {
        return $this->roles;
    }
}

class User
{
   /** @ORM\Column(...) */
   protected $group;

    /** @ManyToMany(...) */
    protected $roles;
    // ...

    public function getRoles() {
        $retRoles = array();
        // symfony2 requires a string array
        $roles = $this->roles->merge($this->group->getRoles());
        foreach($roles as $role) {
            $retRoles[] = $role->getName(); // or $retRoles[] = 'ROLE_' . $role->getName();
        }
        return $retRoles;
    }
}

这篇关于在哪里定义安全角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆