| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Services\Auth; |
| 4 |
|
| 5 |
class User implements \JsonSerializable |
| 6 |
{ |
| 7 |
protected $role = 'guest'; |
| 8 |
|
| 9 |
protected $permissions = []; |
| 10 |
|
| 11 |
protected $username = ''; |
| 12 |
|
| 13 |
protected $homedir = ''; |
| 14 |
|
| 15 |
protected $name = ''; |
| 16 |
|
| 17 |
protected $email = ''; |
| 18 |
|
| 19 |
protected $avatar = ''; |
| 20 |
|
| 21 |
protected $available_roles = ['guest', 'user', 'admin']; |
| 22 |
|
| 23 |
protected $available_permissions = ['read', 'write', 'upload', 'download', 'batchdownload', 'zip']; |
| 24 |
|
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
public function isGuest(): bool |
| 30 |
{ |
| 31 |
return 'guest' == $this->role; |
| 32 |
} |
| 33 |
|
| 34 |
public function isUser(): bool |
| 35 |
{ |
| 36 |
return 'user' == $this->role; |
| 37 |
} |
| 38 |
|
| 39 |
public function isAdmin(): bool |
| 40 |
{ |
| 41 |
return 'admin' == $this->role; |
| 42 |
} |
| 43 |
|
| 44 |
public function hasRole($check): bool |
| 45 |
{ |
| 46 |
if (is_array($check)) { |
| 47 |
return in_array($this->getRole(), $check); |
| 48 |
} |
| 49 |
|
| 50 |
return $this->getRole() == $check; |
| 51 |
} |
| 52 |
|
| 53 |
public function getRole(): string |
| 54 |
{ |
| 55 |
return $this->role; |
| 56 |
} |
| 57 |
|
| 58 |
public function hasPermissions($check): bool |
| 59 |
{ |
| 60 |
if (empty($check)) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
if (is_array($check)) { |
| 65 |
return count(array_intersect($check, $this->getPermissions())) == count($check); |
| 66 |
} |
| 67 |
|
| 68 |
return in_array($check, $this->getPermissions()); |
| 69 |
} |
| 70 |
|
| 71 |
public function setName(string $name): void |
| 72 |
{ |
| 73 |
$this->name = $name; |
| 74 |
} |
| 75 |
|
| 76 |
public function getName(): string |
| 77 |
{ |
| 78 |
return $this->name; |
| 79 |
} |
| 80 |
|
| 81 |
public function setEmail(string $email): void |
| 82 |
{ |
| 83 |
$this->email = $email; |
| 84 |
} |
| 85 |
|
| 86 |
public function getEmail(): string |
| 87 |
{ |
| 88 |
return $this->email; |
| 89 |
} |
| 90 |
|
| 91 |
public function setAvatar(string $avatar): void |
| 92 |
{ |
| 93 |
$this->avatar = $avatar; |
| 94 |
} |
| 95 |
|
| 96 |
public function getAvatar(): string |
| 97 |
{ |
| 98 |
return $this->avatar; |
| 99 |
} |
| 100 |
|
| 101 |
public function setUsername(string $username) |
| 102 |
{ |
| 103 |
$this->username = $username; |
| 104 |
} |
| 105 |
|
| 106 |
public function getUsername(): string |
| 107 |
{ |
| 108 |
return $this->username; |
| 109 |
} |
| 110 |
|
| 111 |
public function setHomedir(string $homedir) |
| 112 |
{ |
| 113 |
$this->homedir = $homedir; |
| 114 |
} |
| 115 |
|
| 116 |
public function getHomeDir(): string |
| 117 |
{ |
| 118 |
return $this->homedir; |
| 119 |
} |
| 120 |
|
| 121 |
public function setRole(string $role) |
| 122 |
{ |
| 123 |
$this->checkValidRole($role); |
| 124 |
|
| 125 |
$this->role = $role; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @throws \Exception |
| 130 |
*/ |
| 131 |
public function setPermissions($permissions, $encoded = false) |
| 132 |
{ |
| 133 |
if ($encoded) { |
| 134 |
$permissions = explode('|', $permissions); |
| 135 |
} |
| 136 |
|
| 137 |
$this->checkValidPermissions($permissions); |
| 138 |
|
| 139 |
$this->permissions = $permissions; |
| 140 |
} |
| 141 |
|
| 142 |
public function getPermissions($encoded = false) |
| 143 |
{ |
| 144 |
return $encoded ? implode('|', $this->permissions) : $this->permissions; |
| 145 |
} |
| 146 |
|
| 147 |
public function jsonSerialize(): array |
| 148 |
{ |
| 149 |
return [ |
| 150 |
'role' => $this->getRole(), |
| 151 |
'permissions' => $this->getPermissions(), |
| 152 |
'homedir' => $this->getHomeDir(), |
| 153 |
'username' => $this->getUsername(), |
| 154 |
'name' => $this->getName(), |
| 155 |
'email' => $this->getEmail(), |
| 156 |
'avatar' => $this->getAvatar(), |
| 157 |
]; |
| 158 |
} |
| 159 |
|
| 160 |
protected function checkValidRole($role) |
| 161 |
{ |
| 162 |
if (! in_array($role, $this->available_roles)) { |
| 163 |
throw new \Exception("User role {$role} does not exists."); |
| 164 |
} |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
protected function checkValidPermissions(array $permissions) |
| 170 |
{ |
| 171 |
foreach ($permissions as $permission) { |
| 172 |
if ($permission && ! in_array($permission, $this->available_permissions)) { |
| 173 |
throw new \Exception("Permission {$permission} does not exists."); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
|