|
3 | 3 | namespace CodedMonkey\Dirigent\Doctrine\Entity; |
4 | 4 |
|
5 | 5 | use CodedMonkey\Dirigent\Doctrine\Repository\UserRepository; |
| 6 | +use CodedMonkey\Dirigent\Entity\UserRole; |
| 7 | +use Doctrine\DBAL\Types\Types; |
6 | 8 | use Doctrine\ORM\Mapping\Column; |
7 | 9 | use Doctrine\ORM\Mapping\Entity; |
8 | 10 | use Doctrine\ORM\Mapping\GeneratedValue; |
@@ -34,8 +36,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFact |
34 | 36 | #[Column(length: 180, nullable: true)] |
35 | 37 | private ?string $email = null; |
36 | 38 |
|
37 | | - #[Column] |
38 | | - private array $roles = []; |
| 39 | + #[Column(type: Types::STRING, length: 64, enumType: UserRole::class)] |
| 40 | + private UserRole $role = UserRole::User; |
39 | 41 |
|
40 | 42 | #[Column] |
41 | 43 | private ?string $password = null; |
@@ -82,15 +84,17 @@ public function setEmail(?string $email): void |
82 | 84 |
|
83 | 85 | public function getRoles(): array |
84 | 86 | { |
85 | | - $roles = $this->roles; |
86 | | - $roles[] = 'ROLE_USER'; |
| 87 | + return [$this->role->value]; |
| 88 | + } |
87 | 89 |
|
88 | | - return array_unique($roles); |
| 90 | + public function getRole(): UserRole |
| 91 | + { |
| 92 | + return $this->role; |
89 | 93 | } |
90 | 94 |
|
91 | | - public function setRoles(array $roles): void |
| 95 | + public function setRole(UserRole $role): void |
92 | 96 | { |
93 | | - $this->roles = $roles; |
| 97 | + $this->role = $role; |
94 | 98 | } |
95 | 99 |
|
96 | 100 | public function getPassword(): ?string |
@@ -143,38 +147,12 @@ public function eraseCredentials(): void |
143 | 147 |
|
144 | 148 | public function isAdmin(): bool |
145 | 149 | { |
146 | | - return in_array('ROLE_ADMIN', $this->roles, true) || in_array('ROLE_SUPER_ADMIN', $this->roles, true); |
| 150 | + return $this->role->isAdmin(); |
147 | 151 | } |
148 | 152 |
|
149 | 153 | public function isSuperAdmin(): bool |
150 | 154 | { |
151 | | - return in_array('ROLE_SUPER_ADMIN', $this->roles, true); |
152 | | - } |
153 | | - |
154 | | - public function setAdmin(bool $admin): void |
155 | | - { |
156 | | - if ($admin) { |
157 | | - if (!in_array('ROLE_ADMIN', $this->roles, true)) { |
158 | | - $this->roles[] = 'ROLE_ADMIN'; |
159 | | - } |
160 | | - } else { |
161 | | - if (false !== $key = array_search('ROLE_ADMIN', $this->roles, true)) { |
162 | | - unset($this->roles[$key]); |
163 | | - } |
164 | | - } |
165 | | - } |
166 | | - |
167 | | - public function setSuperAdmin(bool $admin): void |
168 | | - { |
169 | | - if ($admin) { |
170 | | - if (!in_array('ROLE_SUPER_ADMIN', $this->roles, true)) { |
171 | | - $this->roles[] = 'ROLE_SUPER_ADMIN'; |
172 | | - } |
173 | | - } else { |
174 | | - if (false !== $key = array_search('ROLE_SUPER_ADMIN', $this->roles, true)) { |
175 | | - unset($this->roles[$key]); |
176 | | - } |
177 | | - } |
| 155 | + return $this->role->isSuperAdmin(); |
178 | 156 | } |
179 | 157 |
|
180 | 158 | public function isTotpAuthenticationEnabled(): bool |
|
0 commit comments