|
3 | 3 | namespace CodedMonkey\Dirigent\Doctrine\Entity; |
4 | 4 |
|
5 | 5 | use CodedMonkey\Dirigent\Doctrine\Repository\UserRepository; |
| 6 | +use Doctrine\DBAL\Types\Types; |
6 | 7 | use Doctrine\ORM\Mapping\Column; |
7 | 8 | use Doctrine\ORM\Mapping\Entity; |
8 | 9 | use Doctrine\ORM\Mapping\GeneratedValue; |
@@ -34,8 +35,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFact |
34 | 35 | #[Column(length: 180, nullable: true)] |
35 | 36 | private ?string $email = null; |
36 | 37 |
|
37 | | - #[Column] |
38 | | - private array $roles = []; |
| 38 | + #[Column(type: Types::STRING, length: 64, enumType: UserRole::class)] |
| 39 | + private UserRole $role = UserRole::User; |
39 | 40 |
|
40 | 41 | #[Column] |
41 | 42 | private ?string $password = null; |
@@ -82,15 +83,28 @@ public function setEmail(?string $email): void |
82 | 83 |
|
83 | 84 | public function getRoles(): array |
84 | 85 | { |
85 | | - $roles = $this->roles; |
86 | | - $roles[] = 'ROLE_USER'; |
| 86 | + $roles = [$this->role->value]; |
| 87 | + |
| 88 | + // Add role hierarchy for Symfony security |
| 89 | + if ($this->role === UserRole::Owner) { |
| 90 | + $roles[] = 'ROLE_ADMIN'; |
| 91 | + $roles[] = 'ROLE_USER'; |
| 92 | + $roles[] = 'ROLE_ALLOWED_TO_SWITCH'; |
| 93 | + } elseif ($this->role === UserRole::Admin) { |
| 94 | + $roles[] = 'ROLE_USER'; |
| 95 | + } |
87 | 96 |
|
88 | 97 | return array_unique($roles); |
89 | 98 | } |
90 | 99 |
|
91 | | - public function setRoles(array $roles): void |
| 100 | + public function getRole(): UserRole |
| 101 | + { |
| 102 | + return $this->role; |
| 103 | + } |
| 104 | + |
| 105 | + public function setRole(UserRole $role): void |
92 | 106 | { |
93 | | - $this->roles = $roles; |
| 107 | + $this->role = $role; |
94 | 108 | } |
95 | 109 |
|
96 | 110 | public function getPassword(): ?string |
@@ -143,37 +157,39 @@ public function eraseCredentials(): void |
143 | 157 |
|
144 | 158 | public function isAdmin(): bool |
145 | 159 | { |
146 | | - return in_array('ROLE_ADMIN', $this->roles, true) || in_array('ROLE_SUPER_ADMIN', $this->roles, true); |
| 160 | + return $this->role === UserRole::Admin || $this->role === UserRole::Owner; |
147 | 161 | } |
148 | 162 |
|
149 | 163 | public function isSuperAdmin(): bool |
150 | 164 | { |
151 | | - return in_array('ROLE_SUPER_ADMIN', $this->roles, true); |
| 165 | + return $this->role === UserRole::Owner; |
| 166 | + } |
| 167 | + |
| 168 | + public function isOwner(): bool |
| 169 | + { |
| 170 | + return $this->role === UserRole::Owner; |
| 171 | + } |
| 172 | + |
| 173 | + public function isSuspended(): bool |
| 174 | + { |
| 175 | + return $this->role === UserRole::Suspended; |
152 | 176 | } |
153 | 177 |
|
154 | 178 | public function setAdmin(bool $admin): void |
155 | 179 | { |
156 | 180 | 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 | | - } |
| 181 | + $this->role = UserRole::Admin; |
| 182 | + } elseif ($this->role === UserRole::Admin) { |
| 183 | + $this->role = UserRole::User; |
164 | 184 | } |
165 | 185 | } |
166 | 186 |
|
167 | 187 | public function setSuperAdmin(bool $admin): void |
168 | 188 | { |
169 | 189 | 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 | | - } |
| 190 | + $this->role = UserRole::Owner; |
| 191 | + } elseif ($this->role === UserRole::Owner) { |
| 192 | + $this->role = UserRole::User; |
177 | 193 | } |
178 | 194 | } |
179 | 195 |
|
|
0 commit comments