我对Symfony选民有疑问。 我有一个看起来像这样的实体UserPermissionList:
class UserPermissionList {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=50)
*/
private $path;
/**
* @ORM\Column(type="smallint")
*/
private $view;
public function getId(): ?int {
return $this->id;
}
public function getName(): ?string {
return $this->name;
}
public function setName(string $name): self {
$this->name = $name;
return $this;
}
/**
* @return mixed
*/
public function getPath() {
return $this->path;
}
/**
* @param mixed $path
*/
public function setPath($path): void {
$this->path = $path;
}
/**
* @return mixed
*/
public function getView() {
return $this->view;
}
/**
* @param mixed $view
*/
public function setView($view): void {
$this->view = $view;
}
}
路径属性是特定路由的实际路径,例如:/ desktop或/ mobile
我的用户实体如下:
class User implements UserInterface
{
public const ROLE_USER = 'ROLE_USER';
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\UserPermissionList")
* @ORM\JoinTable(name="user_permisions")
*/
private $permissions;
public function __construct()
{
$this->permissions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|UserPermissionList[]
*/
public function getPermissions(): Collection
{
return $this->permissions;
}
public function addPermission(UserPermissionList $permission): self
{
if (!$this->permissions->contains($permission)) {
$this->permissions[] = $permission;
}
return $this;
}
public function removePermission(UserPermissionList $permission): self
{
if ($this->permissions->contains($permission)) {
$this->permissions->removeElement($permission);
}
return $this;
}}
现在,当User与UserPermissionList中的特定权限相关时,我需要进行逻辑设置以允许他访问此Entity中指定的路由。
例如。我具有以下名称的权限:桌面,路径:/ dekstop和视图:2(意味着权限只能在桌面设备上显示)。
只有与UserPermissionList中的此权限相关的用户才能访问页面/ desktop。
我的选民应该是什么样子?
答案 0 :(得分:0)
编辑。好的,我这样选民了:
class ViewVoter extends Voter
{
/**
* @var UserPermissionListRepository
*/
private $permissionListRepository;
public function __construct(UserPermissionListRepository $permissionListRepository)
{
$this->permissionListRepository = $permissionListRepository;
}
protected function supports($attribute, $subject)
{
$permissions = $this->permissionListRepository->findAll();
$permissionList = [];
foreach ($permissions as $permission) {
$permissionList[] = $permission->getPath();
}
return in_array($attribute, $permissionList)
&& $subject instanceof User;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$permissions = $subject->getPermissions();
$permissionList = [];
foreach ($permissions as $permission) {
$permissionList[] = $permission->getPath();
}
return in_array($attribute, $permissionList);
}
}
并且比我的控制器中的例如在DesktopController中,我添加注释:
@Security("is_granted('/desktop', user)")