roles => (role_id, role_name)
我为每个表格拥有相应的实体和仓库。我的security.yaml文件如下。
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: App\Entity\User
property: username
# if you're using multiple entity managers
# manager_name: customer
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
# pattern: ^/
# http_basic: ~
provider: our_db_provider
anonymous: true
form_login:
#login_path is GET request used ti display the form
# needs to be route names(alias) not the path.
login_path: login
#check_path is a POST request
check_path: logincheck
use_forward: true
default_target_path: default
always_use_default_target_path: true
我的 Entity/User
实现了 UserInterface
组件, 通过阅读文档我知道 getRoles()
方法负责更新用户角色。 我在我的 UserRolesRepository.php
中创建了一个自定义方法叫做getUserRoles($id)
, 在其中我成功返回了当前用户角色的字符串数组, 但是我无法从Entity
访问此方法。我知道不应该从 Entity 类中访问 Repository 方法,但是我卡在这个阶段。所以目前我的 getRoles()
方法返回静态数组 return array('ROLE_ADMIN', 'ROLE_EDITOR');
我的 User
Entity 类:
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Repository\UserRolesRepository;
use Doctrine\ORM\EntityRepository;
use App\Services\Helper;
/**
* @ORM\Table(name="`user`")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, nullable=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64, nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string", length=254, nullable=true)
*/
private $email;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isActive;
private $roles;
/**
* @ORM\Column(type="string", length=254, nullable=true)
*/
private $role;
public function __construct()
{
$this->isActive = true;
}
/**
* @return mixed
*/
public function getRole()
{
return $this->role;
}
/**
* @param mixed $role
*/
public function setRole($role)
{
$this->role = $role;
}
public function getId()
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(?string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
//return is required or else returns an fatal error.
public function getRoles()
{
return array('ROLE_ADMIN','ROLE_EDITOR');
}
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
public function serialize()
{
// TODO: Implement serialize() method.
return serialize(array(
$this->id,
$this->username,
$this->password,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
public function getSalt()
{
// TODO: Implement getSalt() method.
return null;
}
}
User
实体类中包含$roles
属性和Doctrine注释? - OK sure