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