1
|
|
|
<?php |
2
|
|
|
namespace Staticus\Auth; |
3
|
|
|
|
4
|
|
|
use Staticus\Acl\AclServiceInterface; |
5
|
|
|
use Staticus\Acl\Roles; |
6
|
|
|
use Zend\Permissions\Acl\AclInterface; |
7
|
|
|
use Zend\Permissions\Acl\Acl; |
8
|
|
|
|
9
|
|
|
class User implements UserInterface |
10
|
|
|
{ |
11
|
|
|
protected $id; |
12
|
|
|
protected $roles = []; |
13
|
|
|
protected $namespace = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Acl|AclInterface |
17
|
|
|
*/ |
18
|
|
|
protected $acl; |
19
|
|
|
|
20
|
|
|
public function __construct(AclServiceInterface $acl, array $roles = []) |
21
|
|
|
{ |
22
|
|
|
$this->addRoles($roles); |
23
|
|
|
$this->acl = $acl->acl(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function isLoggedIn() |
30
|
|
|
{ |
31
|
|
|
return $this->id !== null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getId() |
35
|
|
|
{ |
36
|
|
|
return $this->id; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getRoles() |
43
|
|
|
{ |
44
|
|
|
return $this->roles; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function login($userId, array $roles) |
48
|
|
|
{ |
49
|
|
|
if (!$userId) { |
50
|
|
|
throw new Exceptions\RuntimeException('User Id cannot be empty'); |
51
|
|
|
} |
52
|
|
|
$this->id = $userId; |
53
|
|
|
$this->addRoles($roles); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function logout() |
57
|
|
|
{ |
58
|
|
|
$this->id = null; |
59
|
|
|
$this->roles = $this->getDefaultRoles(); |
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* @param \Zend\Permissions\Acl\Resource\ResourceInterface|string $resource |
63
|
|
|
* @param string $action |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function can($resource, $action) |
67
|
|
|
{ |
68
|
|
|
foreach ($this->roles as $role) { |
69
|
|
|
if ($this->acl->isAllowed($role, $resource, $action)) { |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function addRoles(array $roles) |
79
|
|
|
{ |
80
|
|
|
$this->roles = array_unique(array_merge($this->getDefaultRoles(), $roles)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $role |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function removeRole($role) |
88
|
|
|
{ |
89
|
|
|
if(($key = array_search($role, $this->roles, true)) !== false) { |
90
|
|
|
unset($this->roles[$key]); |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
if (empty($this->roles)) { |
95
|
|
|
$this->roles = $this->getDefaultRoles(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected function getDefaultRoles() |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
Roles::GUEST, |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $role |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function hasRole($role) |
116
|
|
|
{ |
117
|
|
|
|
118
|
|
|
return in_array($role, $this->roles, true); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the home namespace for this user |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getNamespace() |
126
|
|
|
{ |
127
|
|
|
return $this->namespace; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the home namespace for this user |
132
|
|
|
* @param string $namespace |
133
|
|
|
*/ |
134
|
|
|
public function setNamespace($namespace) |
135
|
|
|
{ |
136
|
|
|
$this->namespace = $namespace; |
137
|
|
|
} |
138
|
|
|
} |