我想从数据库中拉出用于认证用户的角色。要做到这一点,我已经创建了一个扩展的角色很像贝娄码组对象:symfony2角色类中的角色和名称有什么区别?
// src/Acme/Bundle/UserBundle/Entity/Group.php
namespace Acme\UserBundle\Entity;
use Symfony\Component\Security\Core\Role\Role;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="acme_groups")
* @ORM\Entity()
*/
class Group extends Role
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* @ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
// ... getters and setters for each property
/**
* @see RoleInterface
*/
public function getRole()
{
return $this->role;
}
}
什么让我困惑的是,我看不到这个类之间的关系,并在这种security.yml相应字段作为:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
所以名称会在右侧:和角色在右侧?例如,在
ROLE_ADMIN: ROLE_USER
将ROLE_ADMIN是组名和ROLE_USER成为角色?对我来说没有意义的是symfony2如何将角色属性作为单个变量而不是数组来实现。由于在声明中
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
有多个角色,不只是一个角色。这些如何与上面定义的类相对应?我正在关注symfony2食谱http://symfony.com/doc/current/cookbook/security/entity_provider.html#managing-roles-in-the-database。
这一切都有道理,非常感谢! –