1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Framework. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Authorization; |
13
|
|
|
|
14
|
|
|
use Linna\Authentication\Password; |
15
|
|
|
use Linna\Authentication\User; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Enhanched User. |
19
|
|
|
*/ |
20
|
|
|
class EnhancedUser extends User |
21
|
|
|
{ |
22
|
|
|
use PermissionTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array<int> Contain roles for the user |
26
|
|
|
*/ |
27
|
|
|
private array $roles = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class Constructor. |
31
|
|
|
* |
32
|
|
|
* @param Password $password |
33
|
|
|
* @param array<mixed> $roles |
34
|
|
|
* @param array<mixed> $permissions |
35
|
|
|
*/ |
36
|
219 |
|
public function __construct(Password $password, array $roles = [], array $permissions = []) |
37
|
|
|
{ |
38
|
219 |
|
parent::__construct($password); |
39
|
|
|
|
40
|
219 |
|
$this->roles = $roles; |
41
|
219 |
|
$this->permission = $permissions; |
42
|
219 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check if an user has a role, use Role instance. |
46
|
|
|
* |
47
|
|
|
* @param Role $role |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
21 |
|
public function hasRole(Role $role): bool |
52
|
|
|
{ |
53
|
21 |
|
return $this->hasRoleById($role->getId()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check if an user has a role, use role Id. |
58
|
|
|
* |
59
|
|
|
* @param int $roleId |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
42 |
|
public function hasRoleById(int $roleId): bool |
64
|
|
|
{ |
65
|
42 |
|
if (isset($this->roles[$roleId])) { |
66
|
14 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
28 |
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Check if an user has a role, use role name. |
74
|
|
|
* |
75
|
|
|
* @param string $roleName |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
21 |
|
public function hasRoleByName(string $roleName): bool |
80
|
|
|
{ |
81
|
21 |
|
if (\in_array($roleName, \array_column($this->roles, 'name'), true)) { |
82
|
7 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
14 |
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|