forked from typecho/typecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
282 lines (247 loc) · 7.76 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
namespace Widget;
use Typecho\Common;
use Typecho\Cookie;
use Typecho\Db\Exception as DbException;
use Typecho\Widget;
use Utils\PasswordHash;
use Widget\Base\Users;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 当前登录用户
*
* @category typecho
* @package Widget
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class User extends Users
{
/**
* 用户组
*
* @var array
*/
public array $groups = [
'administrator' => 0,
'editor' => 1,
'contributor' => 2,
'subscriber' => 3,
'visitor' => 4
];
/**
* 用户
*
* @var array
*/
private array $currentUser;
/**
* 是否已经登录
*
* @var boolean|null
*/
private ?bool $hasLogin = null;
/**
* @param int $components
*/
protected function initComponents(int &$components)
{
$components = self::INIT_OPTIONS;
}
/**
* 执行函数
*
* @throws DbException
*/
public function execute()
{
if ($this->hasLogin()) {
$this->push($this->currentUser);
// update last activated time
$this->db->query($this->db
->update('table.users')
->rows(['activated' => $this->options->time])
->where('uid = ?', $this->currentUser['uid']));
// merge personal options
$options = $this->personalOptions->toArray();
foreach ($options as $key => $val) {
$this->options->{$key} = $val;
}
}
}
/**
* 判断用户是否已经登录
*
* @return boolean
* @throws DbException
*/
public function hasLogin(): ?bool
{
if (null !== $this->hasLogin) {
return $this->hasLogin;
} else {
$cookieUid = Cookie::get('__typecho_uid');
if (null !== $cookieUid) {
/** 验证登录 */
$user = $this->db->fetchRow($this->db->select()->from('table.users')
->where('uid = ?', intval($cookieUid))
->limit(1));
$cookieAuthCode = Cookie::get('__typecho_authCode');
if ($user && Common::hashValidate($user['authCode'], $cookieAuthCode)) {
$this->currentUser = $user;
return ($this->hasLogin = true);
}
$this->logout();
}
return ($this->hasLogin = false);
}
}
/**
* 用户登出函数
*
* @access public
* @return void
*/
public function logout()
{
self::pluginHandle()->trigger($logoutPluggable)->call('logout');
if ($logoutPluggable) {
return;
}
Cookie::delete('__typecho_uid');
Cookie::delete('__typecho_authCode');
}
/**
* 以用户名和密码登录
*
* @access public
* @param string $name 用户名
* @param string $password 密码
* @param boolean $temporarily 是否为临时登录
* @param integer $expire 过期时间
* @return boolean
* @throws DbException
*/
public function login(string $name, string $password, bool $temporarily = false, int $expire = 0): bool
{
//插件接口
$result = self::pluginHandle()->trigger($loginPluggable)->call('login', $name, $password, $temporarily, $expire);
if ($loginPluggable) {
return $result;
}
/** 开始验证用户 **/
$user = $this->db->fetchRow($this->db->select()
->from('table.users')
->where((strpos($name, '@') ? 'mail' : 'name') . ' = ?', $name)
->limit(1));
if (empty($user)) {
return false;
}
$hashValidate = self::pluginHandle()->trigger($hashPluggable)->call('hashValidate', $password, $user['password']);
if (!$hashPluggable) {
if ('$P$' == substr($user['password'], 0, 3)) {
$hasher = new PasswordHash(8, true);
$hashValidate = $hasher->checkPassword($password, $user['password']);
} else {
$hashValidate = Common::hashValidate($password, $user['password']);
}
}
if ($hashValidate) {
if (!$temporarily) {
$this->commitLogin($user, $expire);
}
/** 压入数据 */
$this->push($user);
$this->currentUser = $user;
$this->hasLogin = true;
self::pluginHandle()->call('loginSucceed', $this, $name, $password, $temporarily, $expire);
return true;
}
self::pluginHandle()->call('loginFail', $this, $name, $password, $temporarily, $expire);
return false;
}
/**
* @param $user
* @param int $expire
* @throws DbException
*/
public function commitLogin(&$user, int $expire = 0)
{
$authCode = function_exists('openssl_random_pseudo_bytes') ?
bin2hex(openssl_random_pseudo_bytes(16)) : sha1(Common::randString(20));
$user['authCode'] = $authCode;
Cookie::set('__typecho_uid', $user['uid'], $expire);
Cookie::set('__typecho_authCode', Common::hash($authCode), $expire);
//更新最后登录时间以及验证码
$this->db->query($this->db
->update('table.users')
->expression('logged', 'activated')
->rows(['authCode' => $authCode])
->where('uid = ?', $user['uid']));
}
/**
* 只需要提供uid或者完整user数组即可登录的方法, 多用于插件等特殊场合
*
* @param int | array $uid 用户id或者用户数据数组
* @param boolean $temporarily 是否为临时登录,默认为临时登录以兼容以前的方法
* @param integer $expire 过期时间
* @return boolean
* @throws DbException
*/
public function simpleLogin($uid, bool $temporarily = true, int $expire = 0): bool
{
if (is_array($uid)) {
$user = $uid;
} else {
$user = $this->db->fetchRow($this->db->select()
->from('table.users')
->where('uid = ?', $uid)
->limit(1));
}
if (empty($user)) {
self::pluginHandle()->call('simpleLoginFail', $this);
return false;
}
if (!$temporarily) {
$this->commitLogin($user, $expire);
}
$this->push($user);
$this->currentUser = $user;
$this->hasLogin = true;
self::pluginHandle()->call('simpleLoginSucceed', $this, $user);
return true;
}
/**
* 判断用户权限
*
* @access public
* @param string $group 用户组
* @param boolean $return 是否为返回模式
* @return boolean
* @throws DbException|Widget\Exception
*/
public function pass(string $group, bool $return = false): bool
{
if ($this->hasLogin()) {
if (array_key_exists($group, $this->groups) && $this->groups[$this->group] <= $this->groups[$group]) {
return true;
}
} else {
if ($return) {
return false;
} else {
//防止循环重定向
$this->response->redirect(defined('__TYPECHO_ADMIN__') ? $this->options->loginUrl .
(0 === strpos($this->request->getReferer() ?? '', $this->options->loginUrl) ? '' :
'?referer=' . urlencode($this->request->makeUriByRequest())) : $this->options->siteUrl);
}
}
if ($return) {
return false;
} else {
throw new Widget\Exception(_t('禁止访问'), 403);
}
}
}