我尝试在没有FOSUser的情况下创建身份验证。 但我有一个问题。当我登录时,在工具栏中我没有"认证" :
我不明白为什么。
user.php的
<?php
namespace FermierMalin\Bundle\UserBundle\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;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="FermierMalin\Bundle\UserBundle\Entity\UserRepository")
* @UniqueEntity(fields="email", message="Ce compte existe déjà")
* @ORM\HasLifecycleCallbacks
*/
class User implements UserInterface, \Serializable
{
public function __construct()
{
$this->roles = new ArrayCollection();
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @Assert\NotBlank(message="L'adresse email est requise")
* @Assert\Email(message="L'adresse email n'est pas valide")
* @ORM\Column (type="string", length=255, unique=true)
*/
private $email;
/**
* @var string
*
* @Assert\NotBlank(message="Le mot de passe est requis")
* @Assert\Length(min=4, max=50, minMessage="Le mot de passe doit faire au moins {{ limit }} caractères", maxMessage="Le mot de passe doit faire moins de {{ limit }} caractères")
* @ORM\Column (type="string", length=255)
*/
private $password;
/**
* @var string
*
* @Assert\NotBlank(message="Le prénom est requis")
* @Assert\Length(min=2, max=50, minMessage="Le prénom doit faire au moins {{ limit }} caractères", maxMessage="Le prénom doit faire moins de {{ limit }} caractères")
* @ORM\Column(name="first_name", type="string", length=255)
*/
private $firstName;
/**
* @var string
*
* @Assert\Length(min=2, max=50, minMessage="Le nom doit faire au moins {{ limit }} caractères", maxMessage="Le nom doit faire moins de {{ limit }} caractères")
* @ORM\Column(name="last_name", type="string", length=255, nullable=true)
*/
private $lastName;
/**
* @var \Datetime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \Datetime
*
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set firstName
*
* @param string $firstName
* @return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
* @return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return User
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return User
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
*
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('now'));
}
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
));
}
/**
* @see \Serializable::unserialize()
* @param string $serialized
*/
public function unserialize($serialized)
{
list (
$this->id,
) = unserialize($serialized);
}
/**
* @inheritDoc
*
* @return Role[] The user roles
*/
public function getRoles()
{
return $this->roles->toArray();
}
/**
* @inheritDoc
*
* @return string|null The salt
*/
public function getSalt()
{
return null;
}
/**
* Returns the username used to authenticate the user.
*
* @return string The username
*/
public function getUsername()
{
return $this->getEmail();
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
}
/**
* Add roles
*
* @param Role $roles
* @return User
*/
public function addRole(Role $roles)
{
$this->roles[] = $roles;
return $this;
}
/**
* Remove roles
*
* @param Role $roles
*/
public function removeRole(Role $roles)
{
$this->roles->removeElement($roles);
}
}
Role.php
<?php
namespace FermierMalin\Bundle\UserBundle\Entity;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="app_role")
* @ORM\Entity()
*/
class Role implements RoleInterface
{
/**
* @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="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @see RoleInterface
*/
public function getRole()
{
return $this->role;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Role
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set role
*
* @param string $role
* @return Role
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Add users
*
* @param User $users
* @return Role
*/
public function addUser(User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* @param User $users
*/
public function removeUser(User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}
Security.yml:
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
encoders:
FermierMalin\Bundle\UserBundle\Entity\User:
algorithm: md5
encode_as_base64: false
iterations: 0
role_hierarchy:
ROLE_GROWER: ROLE_USER
ROLE_CONSUMER: ROLE_USER
providers:
main:
entity: { class: FermierMalin\Bundle\UserBundle\Entity\User, property: email}
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
provider: main
form_login:
login_path: /connexion
check_path: /login_check
username_parameter: _email
logout:
path: /logout
target: /
我的用户有角色,所以我不明白这种行为!
答案 0 :(得分:0)
在security.yml文件中,对于已记录的用户没有像以下那样的访问控制权。
ACCESS_CONTROL:
user_secured_pages:
path: /user
roles: [ROLE_USER]
您可以通过添加日志记录用户的相应角色来尝试吗?