| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gbere\SimpleAuth\Entity; |
||
| 6 | |||
| 7 | use DateTime; |
||
| 8 | use Doctrine\Common\Collections\ArrayCollection; |
||
| 9 | use Doctrine\Common\Collections\Collection; |
||
| 10 | use Doctrine\ORM\Mapping as ORM; |
||
| 11 | use Gbere\SimpleAuth\Model\UserInterface; |
||
| 12 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
||
| 13 | use Symfony\Component\Validator\Constraints as Assert; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @ORM\Entity() |
||
| 17 | * @ORM\Table(name="user") |
||
| 18 | * @ORM\InheritanceType("SINGLE_TABLE") |
||
| 19 | * @ORM\DiscriminatorColumn(name="type", type="string", length=4) |
||
| 20 | * @ORM\DiscriminatorMap({ |
||
| 21 | * "user"="User", |
||
| 22 | * "adm"="AdminUser", |
||
| 23 | * }) |
||
| 24 | * @ORM\HasLifecycleCallbacks() |
||
| 25 | * @UniqueEntity("email") |
||
| 26 | */ |
||
| 27 | abstract class UserBase implements UserInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | * @ORM\Id() |
||
| 32 | * @ORM\GeneratedValue() |
||
| 33 | * @ORM\Column(type="integer") |
||
| 34 | */ |
||
| 35 | private $id; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 40 | * @Assert\NotBlank() |
||
| 41 | * @Assert\Email() |
||
| 42 | */ |
||
| 43 | private $email; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | * @ORM\Column(type="string", length=80) |
||
| 48 | * @Assert\NotBlank() |
||
| 49 | */ |
||
| 50 | private $name; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Collection |
||
| 54 | * @ORM\ManyToMany(targetEntity="Gbere\SimpleAuth\Entity\Role", inversedBy="users") |
||
| 55 | * @ORM\JoinTable(name="user_role") |
||
| 56 | */ |
||
| 57 | private $roles; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string The hashed password |
||
| 61 | * @ORM\Column(type="string") |
||
| 62 | * @Assert\NotBlank() |
||
| 63 | */ |
||
| 64 | private $password; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var DateTime |
||
| 68 | * @ORM\Column(type="datetime") |
||
| 69 | */ |
||
| 70 | private $createdAt; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | * @ORM\Column(type="boolean") |
||
| 75 | */ |
||
| 76 | private $enabled; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string|null |
||
| 80 | * @ORM\Column(type="string", length=100, nullable=true, unique=true) |
||
| 81 | */ |
||
| 82 | private $confirmationToken; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var DateTime|null |
||
| 86 | * @ORM\Column(type="datetime", nullable=true) |
||
| 87 | */ |
||
| 88 | private $passwordRequestAt; |
||
| 89 | |||
| 90 | public function __construct() |
||
| 91 | { |
||
| 92 | $this->enabled = false; |
||
| 93 | $this->roles = new ArrayCollection(); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getId(): ?int |
||
| 97 | { |
||
| 98 | return $this->id; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function getEmail(): ?string |
||
| 102 | { |
||
| 103 | return $this->email; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function setEmail(string $email): self |
||
| 107 | { |
||
| 108 | $this->email = $email; |
||
| 109 | |||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * A visual identifier that represents this user. |
||
| 115 | * |
||
| 116 | * @see UserInterface |
||
| 117 | */ |
||
| 118 | public function getUsername(): string |
||
| 119 | { |
||
| 120 | return $this->email; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getName(): ?string |
||
| 124 | { |
||
| 125 | return $this->name; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function setName(string $name): self |
||
| 129 | { |
||
| 130 | $this->name = $name; |
||
| 131 | |||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return array<Role|string> |
||
| 137 | */ |
||
| 138 | public function getRoles(): array |
||
| 139 | { |
||
| 140 | $roles = []; |
||
| 141 | /** @var Role $role */ |
||
| 142 | foreach ($this->roles as $role) { |
||
| 143 | $roles[] = $role->getName(); |
||
| 144 | } |
||
| 145 | |||
| 146 | if (AdminUser::class === static::class) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 147 | $roles[] = 'ROLE_ADMIN'; |
||
| 148 | } |
||
| 149 | |||
| 150 | return array_unique($roles); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function getRolesCollection(): Collection |
||
| 154 | { |
||
| 155 | return $this->roles; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function addRoleEntity(Role $role): self |
||
| 159 | { |
||
| 160 | if (false === $this->roles->contains($role)) { |
||
| 161 | $this->roles[] = $role; |
||
| 162 | } |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function removeRoleEntity(Role $role): self |
||
| 168 | { |
||
| 169 | if ($this->roles->contains($role)) { |
||
| 170 | $this->roles->removeElement($role); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @see UserInterface |
||
| 178 | */ |
||
| 179 | public function getPassword(): ?string |
||
| 180 | { |
||
| 181 | return $this->password; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function setPassword(string $password): self |
||
| 185 | { |
||
| 186 | $this->password = $password; |
||
| 187 | |||
| 188 | return $this; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getCreatedAt(): ?DateTime |
||
| 192 | { |
||
| 193 | return $this->createdAt; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function setCreatedAt(DateTime $createdAt): self |
||
| 197 | { |
||
| 198 | $this->createdAt = $createdAt; |
||
| 199 | |||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @ORM\PrePersist |
||
| 205 | */ |
||
| 206 | public function setCreatedAtValue(): void |
||
| 207 | { |
||
| 208 | $this->createdAt = new DateTime(); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @see UserInterface |
||
| 213 | */ |
||
| 214 | public function getSalt(): ?string |
||
| 215 | { |
||
| 216 | // not needed when using the "bcrypt" algorithm in security.yaml |
||
| 217 | return null; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @see UserInterface |
||
| 222 | */ |
||
| 223 | public function eraseCredentials(): void |
||
| 224 | { |
||
| 225 | // If you store any temporary, sensitive data on the user, clear it here |
||
| 226 | // $this->plainPassword = null; |
||
| 227 | } |
||
| 228 | |||
| 229 | public function isEnabled(): ?bool |
||
| 230 | { |
||
| 231 | return $this->enabled; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function hasEnabled(bool $enabled): self |
||
| 235 | { |
||
| 236 | $this->enabled = $enabled; |
||
| 237 | |||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | public function getConfirmationToken(): ?string |
||
| 242 | { |
||
| 243 | return $this->confirmationToken; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function setConfirmationToken(?string $confirmationToken): self |
||
| 247 | { |
||
| 248 | $this->confirmationToken = $confirmationToken; |
||
| 249 | |||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @throws \Exception |
||
| 255 | */ |
||
| 256 | public function generateToken(): self |
||
| 257 | { |
||
| 258 | $this->setConfirmationToken(bin2hex(random_bytes(50))); |
||
| 259 | |||
| 260 | return $this; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function getPasswordRequestAt(): ?DateTime |
||
| 264 | { |
||
| 265 | return $this->passwordRequestAt; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function setPasswordRequestAt(?DateTime $passwordRequestAt): self |
||
| 269 | { |
||
| 270 | $this->passwordRequestAt = $passwordRequestAt; |
||
| 271 | |||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | } |
||
| 275 |