我正在学习Symfony 4,我迷失了用户角色,我不知道从哪里开始。
因为它将是一个类似于网站的内部网用户不会自己注册,管理员将注册它们并设置角色。管理员还可以动态创建新角色。
我想将用户角色存储在数据库中。我有2个实体User
和Role
。 (关系尚未定义,这是正常的)
这是我的User
实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(fields="email", message="Email already taken")
* @UniqueEntity(fields="username", message="Username already taken")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", unique=true)
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastname;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @ORM\Column(type="date")
*/
private $birthdate;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $roleId;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $username;
/**
* @Assert\NotBlank()
* @Assert\Length(max=4096)
*/
private $plainPassword;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $password;
/**
* @ORM\Column(name="is_active", type="boolean", nullable=true)
*/
private $isActive;
// GETTERS
public function getId()
{
return $this->id;
}
public function getFirstname()
{
return $this->firstname;
}
public function getLastname()
{
return $this->lastname;
}
public function getBirthdate()
{
return $this->birthdate;
}
public function getEmail()
{
return $this->email;
}
public function getRoleId()
{
return $this->roleId;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function getUsername()
{
return $this->username;
}
public function getPlainPassword()
{
return $this->plainPassword;
}
public function getPassword()
{
return $this->password;
}
public function getSalt()
{
return null;
}
// SETTERS
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
public function setEmail($email)
{
$this->email = $email;
}
public function setBirthdate($birthdate)
{
$this->birthdate = $birthdate;
}
public function setRoleId($roleId)
{
$this->roleId = $roleId;
}
public function setUsername($username)
{
$this->username = $username;
}
public function setPlainPassword($password)
{
$this->plainPassword = $password;
}
public function setPassword($password)
{
$this->password = $password;
}
// FUNCTIONS
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
public function eraseCredentials()
{
}
}
这是我的Role
实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\RoleRepository")
*/
class Role
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer", unique=true)
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $type;
// GETTERS
public function getId()
{
return $this->id;
}
public function getType()
{
return $this->type;
}
// SETTERS
public function setType($type): void
{
$this->type = $type;
}
}
这样做是否是一个好习惯?
在Symfony文档中,我们可以随处读取“宏”ROLE_USER
,ROLE_ADMIN
...它们在哪里定义?我们可以自定义吗?
答案 0 :(得分:16)
Symfony用于支持具有RoleInterface的角色实体,就像您的用户实现UserInterface一样。决定这是不必要的,因为roles-table通常只包含2个字段(id,name),这意味着我们也可以将角色直接存储在users表中。
因此,如果您想要遵循Symfony最佳实践,您只需在用户中拥有自己的角色,例如:
class User implements UserInterface
{
/** @Column(type="json") */
private $roles = [];
public function getRoles(): array
{
return array_unique(array_merge(['ROLE_USER'], $this->roles));
}
public function setRoles(array $roles)
{
$this->roles = $roles;
}
public function resetRoles()
{
$this->roles = [];
}
}
如果您不想将角色存储为JSON编码的字符串,您还可以使用@Column(type="array") */
或编写自定义DBAL类型,例如某种EnumType。您可能还希望保护setter免受无效数据的影响或添加验证。
关于文档中使用的角色的第二个问题:Symfony对某些功能有一些预定义的角色和伪角色:
ROLE_USER
和ROLE_ADMIN
等其他角色纯属典范,您可以根据需要使用它们。您甚至不必使用ROLE_
开始自己的角色(尽管某些安全功能依赖于此约定,除非您进行一些手动更改,否则事情将无法按预期执行)。
答案 1 :(得分:0)
推荐阅读当前Symfony文档(此答案时的版本4): https://symfony.com/doc/current/security.html#hierarchical-roles
Symfony建议定义角色继承,请查看它是否有帮助;从上面的链接
” 您可以通过创建角色层次结构来定义角色继承规则,而不是为每个用户分配许多角色:
# config/packages/security.yaml
security:
# ...
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
具有ROLE_ADMIN角色的用户也将具有ROLE_USER角色。具有ROLE_SUPER_ADMIN的用户将自动具有ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH和ROLE_USER(从ROLE_ADMIN继承)。
为使角色层次结构正常工作,请勿尝试手动调用$ user-> getRoles()。例如,在从基本控制器扩展的控制器中:
// BAD - $user->getRoles() will not know about the role hierarchy
$hasAccess = in_array('ROLE_ADMIN', $user->getRoles());
// GOOD - use of the normal security methods
$hasAccess = $this->isGranted('ROLE_ADMIN');
$this->denyAccessUnlessGranted('ROLE_ADMIN');
”复制结束
我认为,如果继承满足您的需要,将角色存储为字符串就足够了。