我正在使用Doctrine的Slim框架。我有三张桌子
id | username | password | name
--------------------------------
1 | Lorel | ******** | Lorel
id | permission | description
-------------------------------
2 | READ_ACCESS | Lorel Ipsum
id | user_id | permission_id
-----------------------------
X | 1 | 2假设用户'1‘有权限'2’,有没有使用原则,我可以通过它来找出。
发布于 2019-01-26 06:49:33
我假设你是想做授权。我在Zend Framework3和Doctrine 2中有一个这样的设置,它们的关系是一样的,只是不确定如何将其转换为Slim Framework。但这里什么都没有;-)
User实体与角色有关系:
/**
* @var Collection|ArrayCollection|Role[]
* @ORM\ManyToMany(targetEntity="User\Entity\Role", inversedBy="users", fetch="LAZY")
* @ORM\JoinTable(
* name="user_user_roles",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*
*/
protected $roles;Role实体具有到用户的路由和反面
/**
* @var Collection|ArrayCollection|Route[]
* @ORM\ManyToMany(targetEntity="User\Entity\Route", inversedBy="roles", fetch="EAGER")
* @ORM\JoinTable(
* name="user_role_routes",
* joinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="route_id", referencedColumnName="id")}
* )
*/
protected $routes;
/**
* @var Collection|ArrayCollection|User[]
* @ORM\ManyToMany(targetEntity="User\Entity\User", mappedBy="roles", fetch="LAZY")
*/
protected $users;路由实体仅具有与角色相反的角色
/**
* @var Collection|ArrayCollection|Role[]
* @ORM\ManyToMany(targetEntity="User\Entity\Role", mappedBy="routes", fetch="LAZY")
*/
protected $roles;请注意,它涉及2关系:
确保在__construct中初始化每个集合,如下所示:
// Initialize only those within the Entity
public function __construct()
{
$this->users = new ArrayCollection();
}生成您的setter getter方法(不需要!)。创建添加/删除方法而不是设置器,如下所示(这是在路由实体中):
/**
* @param Collection|ArrayCollection|Role[] $roles
*
* @return Route
*/
public function addRoles(Collection $roles) : Route
{
foreach ($roles as $role) {
if ( ! $this->getRoles()->contains($role)) {
$this->getRoles()->add($role);
}
if ( ! $role->getRoutes()->contains($this)) {
$role->getRoutes()->add($this);
}
}
return $this;
}
/**
* @param Collection|ArrayCollection|Role[] $roles
*
* @return Route
*/
public function removeRoles(Collection $roles) : Route
{
foreach ($roles as $role) {
if ($this->getRoles()->contains($role)) {
$this->getRoles()->remove($role);
}
if ($role->getRoutes()->contains($this)) {
$role->getRoutes()->remove($this);
}
}
return $this;
}所以,就这样,这就是设置。我建议您包含Gedmo Doctrine extensions并将@Gedmo\Tree(type="nested")应用于您的Role实体。使管理(嵌套/继承)角色变得容易。请参见Managing Hierarchical Data in MySQL (和Gedmo Tree docs)
接下来,要检查用户是否有权访问某个路由,您需要某种形式的AuthenticationService。因为我不知道Slim,所以一定要用那个框架中的东西填进去。不过,逻辑是一样的。我使用要在路由访问中包含/使用的服务,该服务检查用户是否已知(已验证),如果不是,则分配一个Guest角色,然后检查要访问的路由是否为任何分配的角色所知。
/**
* @param string $route
*
* @return bool
* @throws Exception
*/
public function isGranted(string $route) : bool
{
// Get assigned Role[] array or set Guest Role
if ($this->getAuthenticationService()->hasIdentity()) {
/** @var User $user */
$user = $this->getAuthenticationService()->getIdentity();
/** @var Collection|Role[] $roles */
$roles = $user->getRoles();
} else {
$roles = new ArrayCollection(
[
$this->getObjectManager()->getRepository(Role::class)->findOneBy(['name' => Role::NO_ACCOUNT_ROLE]),
]
);
}
foreach ($roles as $role) {
if ($this->checkRoutes($role, $route)) {
return true;
}
}
return false;
}所以,我想说的是,所有这些都应该让你得到比去更多的东西。
GL和HF
https://stackoverflow.com/questions/54367352
复制相似问题