1 pièce(s) jointe(s)
Utiliser UserProvider lors de l'auth avec un profil qui hérite du l'entity User
Bonjour,
je cherche de l'aide concernant un erreur que j'ai rencontré.
En fait j'ai une entité etudiant qui hérite du User, le souci c'est que quand j'essai de me connecter en tant que Etudiant (login/logout) j'ai ce magnifique erreur :(
You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine.
Pièce jointe 563568
et ci dessous les classe user, etudiant (je n'utilise pas le FOSUser) et le fichier security.yaml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| <?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"user" = "User" , "etudiant" = "Etudiant"})
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
*/
private $nom;
/**
* @ORM\Column(type="string", length=255)
*/
private $prenom;
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
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;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| <?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\AttributeOverride;
use Doctrine\ORM\Mapping\Column;
/**
* @ORM\Entity(repositoryClass="App\Repository\EtudiantRepository")
* @ORM\AttributeOverrides({
* @AttributeOverride(name="id",
* column=@Column(
* name = "id",
* type = "integer",
* length = 140
* )
* )
* })
*/
class Etudiant extends User
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="date")
*/
private $date_naissance;
/**
* @ORM\Column(type="integer")
*/
private $sexe;
/**
* @ORM\Column(type="string", length=255)
*/
private $ville;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Encadrant", inversedBy="etudiants")
*/
private $encadrant;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Formation", inversedBy="etudiants")
* @ORM\JoinColumn(nullable=false)
*/
private $formation;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Stage", mappedBy="etudiant")
*/
private $stages;
protected $discr = 'etudiant';
public function __construct()
{
$this->stages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getDateNaissance(): ?\DateTimeInterface
{
return $this->date_naissance;
}
public function setDateNaissance(\DateTimeInterface $date_naissance): self
{
$this->date_naissance = $date_naissance;
return $this;
}
public function getSexe(): ?int
{
return $this->sexe;
}
public function setSexe(int $sexe): self
{
$this->sexe = $sexe;
return $this;
}
public function getVille(): ?string
{
return $this->ville;
}
public function setVille(string $ville): self
{
$this->ville = $ville;
return $this;
}
public function getEncadrant(): ?Encadrant
{
return $this->encadrant;
}
public function setEncadrant(?Encadrant $encadrant): self
{
$this->encadrant = $encadrant;
return $this;
}
public function getFormation(): ?Formation
{
return $this->formation;
}
public function setFormation(?Formation $formation): self
{
$this->formation = $formation;
return $this;
}
/**
* @return Collection|Stage[]
*/
public function getStages(): Collection
{
return $this->stages;
}
public function addStage(Stage $stage): self
{
if (!$this->stages->contains($stage)) {
$this->stages[] = $stage;
$stage->addEtudiant($this);
}
return $this;
}
public function removeStage(Stage $stage): self
{
if ($this->stages->contains($stage)) {
$this->stages->removeElement($stage);
$stage->removeEtudiant($this);
}
return $this;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| security:
encoders:
App\Entity\User:
algorithm: auto
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: lazy
provider: app_user_provider
guard:
authenticators:
- App\Security\LoginFormAuthenticator
logout:
path: app_logout
# where to redirect after logout
# target: app_any_route
access_denied_handler: App\Security\AccessDeniedHandler
# 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
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
access_control:
# - { path: '^/admin', roles: [ROLE_SUPER_ADMIN, ROLE_ADMIN]}
- { path: '^/admin', roles: ROLE_ADMIN}
- { path: ^/front, roles: ROLE_ETUDIANT } |
Merci d'avance .