1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saito\User; |
4
|
|
|
|
5
|
|
|
use App\Model\Entity\User; |
6
|
|
|
use Cake\Core\Exception\Exception; |
7
|
|
|
use Cake\I18n\Time; |
8
|
|
|
use Cake\Utility\Hash; |
9
|
|
|
use Saito\App\Registry; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Main implementor of ForumsUserInterface. |
13
|
|
|
* |
14
|
|
|
* @package Saito\User |
15
|
|
|
*/ |
16
|
|
|
trait SaitoUserTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* User ID |
20
|
|
|
* |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected $_id = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Stores if a user is logged in |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $_isLoggedIn = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* User settings |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $_settings = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
public function setSettings($settings) |
43
|
|
|
{ |
44
|
|
|
if (empty($settings)) { |
45
|
|
|
$this->_id = null; |
46
|
|
|
$this->_settings = null; |
|
|
|
|
47
|
|
|
$this->_isLoggedIn = false; |
48
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} elseif ($settings instanceof User) { |
51
|
|
|
$settings = $settings->toArray(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (empty($settings) || !is_array($settings)) { |
55
|
|
|
throw new \RuntimeException("Can't set user.", 1434705388); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!empty($settings['id'])) { |
59
|
|
|
$this->_id = (int)$settings['id']; |
60
|
|
|
$this->_isLoggedIn = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->_settings = $settings; |
64
|
|
|
|
65
|
|
|
// perf-cheat |
66
|
|
|
if (!empty($this->_settings['last_refresh'])) { |
67
|
|
|
$this->_settings['last_refresh_unix'] = dateToUnix($this->_settings['last_refresh']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
//// performance cheat |
71
|
|
|
// adds a property 'ignores' which in a array holds all users ignored by this users as keys: |
72
|
|
|
// ['<key is user-id of ignored user> => <trueish>, …] |
73
|
|
|
if (!empty($this->_settings['user_ignores'])) { |
74
|
|
|
$this->_settings['ignores'] = array_fill_keys( |
75
|
|
|
Hash::extract( |
76
|
|
|
$this->_settings, |
77
|
|
|
'user_ignores.{n}.blocked_user_id' |
78
|
|
|
), |
79
|
|
|
1 |
80
|
|
|
); |
81
|
|
|
unset($this->_settings['user_ignores']); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get single user setting. |
87
|
|
|
* |
88
|
|
|
* @param string $setting setting-key |
89
|
|
|
* @return null|mixed null if setting not found |
90
|
|
|
*/ |
91
|
|
|
public function get($setting) |
92
|
|
|
{ |
93
|
|
|
if (!isset($this->_settings[$setting])) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->_settings[$setting]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set single setting. |
102
|
|
|
* |
103
|
|
|
* @param string $setting setting-key |
104
|
|
|
* @param mixed $value value to set |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function set($setting, $value) |
108
|
|
|
{ |
109
|
|
|
$this->_settings[$setting] = $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function getSettings() |
116
|
|
|
{ |
117
|
|
|
return $this->_settings; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritDoc} |
122
|
|
|
*/ |
123
|
|
|
public function getId() |
124
|
|
|
{ |
125
|
|
|
return $this->_id; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritDoc} |
130
|
|
|
*/ |
131
|
|
|
public function isLoggedIn() |
132
|
|
|
{ |
133
|
|
|
return $this->_isLoggedIn; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
|
|
public function isUser($user) |
140
|
|
|
{ |
141
|
|
|
if (is_numeric($user)) { |
142
|
|
|
$id = (int)$user; |
143
|
|
|
} elseif ($user instanceof ForumsUserInterface || $user instanceof User) { |
144
|
|
|
$id = $user->get('id'); |
145
|
|
|
} else { |
146
|
|
|
throw new \InvalidArgumentException("Can't compare users.", 1434704215); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $id === $this->getId(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Checks if user is forbidden. |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isLocked(): bool |
158
|
|
|
{ |
159
|
|
|
return (bool)$this->get('user_lock'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Checks if user is forbidden. |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function isActivated() : bool |
168
|
|
|
{ |
169
|
|
|
return !$this->get('activate_code'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get role. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getRole() |
178
|
|
|
{ |
179
|
|
|
if ($this->_id === null) { |
180
|
|
|
return 'anon'; |
181
|
|
|
} else { |
182
|
|
|
return $this->get('user_type'); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Check if user has permission to access a resource. |
188
|
|
|
* |
189
|
|
|
* @param string $resource resource |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function permission($resource) |
193
|
|
|
{ |
194
|
|
|
$permission = Registry::get('Permission'); |
195
|
|
|
|
196
|
|
|
return $permission->check($this->getRole(), $resource); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..