Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

Symfony 4 roles management

$
0
0

I am currently working on a project in Symfony 4 a website for a library.While configuring roles in the file security.yaml, I decommented this line :

access_control:      { path: ^/admin, roles: ROLE_ADMIN }      { path: ^/profil, roles: ROLE_USER }

And than I have this error :

image_error_roles

My User class :

<?phpnamespace App\Entity;use Doctrine\ORM\Mapping as ORM;use App\Repository\UtilisateurRepository;use Symfony\Component\Validator\Constraints as Assert;use Symfony\Component\Security\Core\User\UserInterface;use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;/** * @ORM\Entity(repositoryClass=UtilisateurRepository::class) * @UniqueEntity( * fields={"Email"}, * message="L'email que vous avez indiqué est déjà utilisé !"  * ) */class Utilisateur implements UserInterface{    /**     * @ORM\Id     * @ORM\GeneratedValue     * @ORM\Column(type="integer")     */    private $id;    /**     * @ORM\Column(type="string", length=255)     */    private $Nom;    /**     * @ORM\Column(type="string", length=255)     */    private $Prenom;    /**     * @ORM\Column(type="string", length=255)     * @Assert\Email()     */    private $Email;    /**     * @ORM\Column(type="string", length=255)     */    private $Adresse;    /**     * @ORM\Column(type="string", length=255)    */    private $Mdp;    /**     * @ORM\Column(type="string", length=255)     */    private $Username;    /**     * @ORM\Column(type="string", length=255)     * @Assert\Length(min="8", minMessage="Votre mot de passe doit faire minimum 8 caractères")     * @Assert\EqualTo(propertyPath="Confirm_Password",message="Votre mot de passe doit être pareil")     */    private $Password;    /*    * @Assert\EqualTo(propertyPath="Password",message="Votre mot de passe doit être pareil")    */    public $Confirm_Password;    /**     * @ORM\ManyToOne(targetEntity=TypeUtilisateur::class, inversedBy="utilisateur")     * @ORM\JoinColumn(nullable=false)     */    private $typeUtilisateur;    /**     * @ORM\Column(type="json")     */    private $roles = [];    public function getId(): ?int    {        return $this->id;    }    public function getNom(): ?string    {        return $this->Nom;    }    public function setNom(string $Nom): self    {        $this->Nom = $Nom;        return $this;    }    public function getPrenom(): ?string    {        return $this->Prenom;    }    public function setPrenom(string $Prenom): self    {        $this->Prenom = $Prenom;        return $this;    }    public function getEmail(): ?string    {        return $this->Email;    }    public function setEmail(string $Email): self    {        $this->Email = $Email;        return $this;    }    public function getAdresse(): ?string    {        return $this->Adresse;    }    public function setAdresse(string $Adresse): self    {        $this->Adresse = $Adresse;        return $this;    }    public function getMdp(): ?string    {        return $this->Mdp;    }    public function setMdp(string $Mdp): self    {        $this->Mdp = $Mdp;        return $this;    }    public function eraseCredentials(){}    public function getsalt(){}    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 getTypeUtilisateur(): ?TypeUtilisateur    {        return $this->typeUtilisateur;    }    public function setTypeUtilisateur(?TypeUtilisateur $typeUtilisateur): self    {        $this->typeUtilisateur = $typeUtilisateur;        return $this;    }    public function getRoles()    {        $roles = $this->roles;        $roles[] = 'ROLE_USER';        return array_unique($roles);    }    public function setRoles(array $roles): self    {        $this->roles = $roles;        return $this;    }}

File security.yaml :

security:  encoders:    App\Entity\Utilisateur:      algorithm: bcrypt  # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers  providers:    users_in_memory: { memory: null }    in_database:      entity:        class: App\Entity\Utilisateur        property: Email  firewalls:    dev:      pattern: ^/(_(profiler|wdt)|css|images|js)/      security: false    main:      anonymous: true      lazy: true      provider: in_database      form_login:        login_path: security_login        check_path: security_login      logout:        path: security_logout        target: home      # activate different ways to authenticate      # https://symfony.com/doc/current/security.html#firewalls-authentication      # https://symfony.com/doc/current/security/impersonating_user.html      # switch_user: true  # Easy way to control access for large sections of your site  # Note: Only the *first* access control that matches will be used  access_control:      { path: ^/admin, roles: ROLE_ADMIN }      { path: ^/profil, roles: ROLE_USER }  # role_hierarchy:     ROLE_LIBRAIRE: ROLE_USER     ROLE_ADMIN: ROLE_LIBRAIRE

I don't know what is the problem, thanks for you help !


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>