Auth.php
17.3 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
namespace app\common\library;
use app\common\model\User;
use app\common\model\UserThird;
use fast\Random;
use fast\ucenter\client\Client;
use think\Cookie;
use think\Db;
use think\Exception;
use think\Request;
use think\Validate;
/**
* Auth类
*/
class Auth implements \JsonSerializable, \ArrayAccess
{
const ERR_ACCOUNT_IS_INCORRECT = 'Account is incorrect';
const ERR_ACCOUNT_NOT_EXIST = 'Account not exist';
const ERR_USERNAME_IS_INCORRECT = 'Username is incorrect';
const ERR_EMAIL_IS_INCORRECT = 'Email is incorrect';
const ERR_PASSWORD_IS_INCORRECT = 'Password is incorrect';
const ERR_USERNAME_OR_PASSWORD_IS_INCORRECT = 'Username or password is incorrect';
const ERR_USERNAME_ALREADY_EXIST = 'Username already exist';
const ERR_EMAIL_ALREADY_EXIST = 'Email already exist';
const ERR_MOBILE_ALREADY_EXIST = 'Mobile already exist';
const ERR_ACCOUNT_IS_LOCKED = 'Account is locked';
const ERR_USERNAME_OR_PASSWORD_IS_MODIFIED = 'Username or password is modified';
const ERR_YOU_ARE_NOT_LOGGED_IN = 'You are not logged in';
const ERR_ACCOUNT_ALREADY_LOGGED_IN_AT_ANOTHER_PLACE = 'Account already logged in at another';
protected static $instance = null;
private $_error = '';
private $_logined = FALSE;
private $user = NULL;
private $keeptime = 0;
private $requestUri = '';
public function __construct()
{
$this->user = new User;
}
/**
* 初始化
* @param array $options 参数
* @return Auth
*/
public static function instance($options = [])
{
if (is_null(self::$instance))
{
self::$instance = new static($options);
}
return self::$instance;
}
/**
*
* @return User
*/
public function getModel()
{
return $this->user;
}
public function __get($name)
{
return $this->check() ? $this->user->$name : NULL;
}
public function __call($name, $arguments)
{
return call_user_func_array([$this->user, $name], $arguments);
}
/**
* 注册用户
*
* @param string $username 用户名
* @param string $password 密码
* @param string $email 邮箱
* @param string $mobile 手机号
* @param string $extend 扩展参数
* @return boolean
*/
public function register($username, $password, $email = '', $mobile = '', $extend = [], $keeptime = 0, $sync = TRUE)
{
$rule = [
'username' => 'require|length:6,30',
'password' => 'require|length:6,30',
'email' => 'email',
'mobile' => 'regex:/^1\d{10}$/',
];
$msg = [
'username.require' => __('Username can not be empty'),
'username.length' => __('Username must be 6 to 30 characters'),
'password.require' => __('Password can not be empty'),
'password.length' => __('Password must be 6 to 30 characters'),
'email' => __('Email is incorrect'),
'mobile' => __('Mobile is incorrect'),
];
$data = [
'username' => $username,
'password' => $password,
'email' => $email,
'mobile' => $mobile,
];
$validate = new Validate($rule, $msg);
$result = $validate->check($data);
if (!$result)
{
$this->setError($validate->getError());
return FALSE;
}
// 检测用户名或邮箱、手机号是否存在
if (User::getByUsername($username))
{
$this->setError(__('Username already exist'));
return FALSE;
}
if ($email && User::getByEmail($email))
{
$this->setError(__('Email already exist'));
return FALSE;
}
if ($mobile && User::getByMobile($mobile))
{
$this->setError(__('Mobile already exist'));
return FALSE;
}
$ip = request()->ip();
$time = time();
$params = array_merge($data, [
'nickname' => $username,
'salt' => Random::alnum(),
'jointime' => $time,
'joinip' => $ip,
'logintime' => $time,
'loginip' => $ip,
'prevtime' => $time,
'status' => 'normal'
]);
$params['password'] = $this->getEncryptPassword($password, $params['salt']);
$params = array_merge($params, $extend);
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS && $sync)
{
$uc = new Client();
$user_id = $uc->uc_user_register($username, $password, $email);
// 如果小于0则说明发生错误
if ($user_id <= 0)
{
$this->setError($user_id > -4 ? self::ERR_USERNAME_IS_INCORRECT : self::ERR_EMAIL_IS_INCORRECT);
return FALSE;
}
else
{
$params['id'] = $user_id;
}
}
//账号注册时需要开启事务,避免出现垃圾数据
Db::startTrans();
try
{
$ret = $this->user->save($params);
Db::commit();
// 此时的Model中只包含部分数据
$this->user = $this->user->get($this->user->id);
$this->keeptime($keeptime);
return $this->syncLogin();
}
catch (Exception $e)
{
Db::rollback();
return FALSE;
}
}
/**
* 用户登录
*
* @param string $account 账号,用户名、邮箱、手机号
* @param string $password 密码
* @param int $keeptime 有效时长,默认为浏览器关闭
* @return array
*/
public function login($account, $password, $keeptime = 0, $sync = TRUE)
{
$field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
$user = $this->user->get([$field => $account]);
if ($user)
{
if ($user->status != 'normal')
{
$this->setError(self::ERR_ACCOUNT_IS_LOCKED);
return FALSE;
}
if ($user->password != $this->getEncryptPassword($password, $user->salt))
{
$this->setError(self::ERR_PASSWORD_IS_INCORRECT);
return FALSE;
}
$this->user = $user;
// 设置登录有效时长
$this->keeptime($keeptime);
return $this->syncLogin($sync);
}
else
{
$this->setError(self::ERR_ACCOUNT_IS_INCORRECT);
return FALSE;
}
}
/**
* 注销登录退出
* @return bool
*/
public function logout($token = NULL)
{
//设置登录标识
$this->_logined = FALSE;
$token = is_null($token) ? Cookie::get('token') : $token;
Token::delete($token);
Cookie::delete('user_id');
//Cookie::del('username');
Cookie::delete('token');
return TRUE;
}
/**
* 生成Token
* @return string
*/
public function token()
{
//$token = Encrypt::aesEncode($this->keeptime . '|' . $expiretime, Config::get('encrypt', 'aes_key'), TRUE);
$token = Random::uuid();
Token::set($token, $this->user->id, $this->keeptime);
return $token;
}
/**
* 初始化
*
* @param int $user_id 会员ID,默认从Cookie中取
* @param string $token 会员Token,默认从Cookie中取
*
* @return boolean
*/
public function init($user_id = NULL, $token = NULL)
{
$user_id = $user_id ? $user_id : Cookie::get('user_id');
$user_id = intval($user_id);
if ($user_id > 0)
{
if ($this->_error)
return FALSE;
$user = $this->get($user_id);
if (!$user)
{
$this->setError(self::ERR_ACCOUNT_NOT_EXIST);
return FALSE;
}
if ($user['status'] != 'normal')
{
$this->setError(self::ERR_ACCOUNT_IS_LOCKED);
return FALSE;
}
$token = $token ? $token : Cookie::get('token');
if (!Token::check($token))
{
return FALSE;
}
if (Token::identity($token) != $user['id'])
{
return FALSE;
}
$this->user = $user;
$this->_logined = TRUE;
return TRUE;
}
else
{
$this->setError(self::ERR_YOU_ARE_NOT_LOGGED_IN);
return FALSE;
}
}
/**
* 检测是否登录
*
* @return boolean
*/
public function check()
{
return $this->_logined;
}
/**
* 检测是否登录
*
* @return boolean
*/
public function isLogin()
{
return $this->check();
}
/**
* 获取当前请求的URI
* @return string
*/
public function getRequestUri()
{
return $this->requestUri;
}
/**
* 设置当前请求的URI
* @param string $uri
*/
public function setRequestUri($uri)
{
$this->requestUri = $uri;
}
/**
* 第三方登录
* @param string $platform
* @param array $params
* @param int $keeptime
* @return boolean
*/
public function connect($platform, $params = [], $keeptime = 0)
{
$time = time();
$values = [
'platform' => $platform,
'openid' => $params['openid'],
'openname' => isset($params['userinfo']['nickname']) ? $params['userinfo']['nickname'] : '',
'access_token' => $params['access_token'],
'refresh_token' => $params['refresh_token'],
'expires_in' => $params['expires_in'],
'logintime' => $time,
'expiretime' => $time + $params['expires_in'],
];
$this->keeptime($keeptime);
$userthird = UserThird::get(['platform' => $platform, 'openid' => $params['openid']]);
if ($userthird)
{
$this->user = $this->user->get($userthird['user_id']);
if (!$this->user)
{
return FALSE;
}
$userthird->save($values);
return $this->syncLogin();
}
else
{
// 先随机一个用户名,随后再变更为u+数字id
$username = Random::alnum(20);
$password = Random::alnum(6);
// 默认注册一个会员
$result = $this->register($username, $password, '', '', [], $keeptime);
if (!$result)
{
return FALSE;
}
$userarr = ['username' => 'u' . $this->user->id];
if (isset($params['userinfo']['nickname']))
$userarr['nickname'] = $params['userinfo']['nickname'];
if (isset($params['userinfo']['avatar']))
$userarr['avatar'] = $params['userinfo']['avatar'];
// 更新会员资料
$this->user->save($userarr);
// 保存第三方信息
$values['user_id'] = $this->user->id;
UserThird::create($values);
// 写入登录Cookies和Token
$this->writeStatus();
return TRUE;
}
}
/**
* 删除一个指定会员
* @param int $user_id
* @param bool $sync 是否同步删除
*/
public function delete($user_id, $sync = TRUE)
{
$user = $this->user->get($user_id);
if (!$user)
{
return FALSE;
}
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS && $sync)
{
$uc = new Client();
$re = $uc->uc_user_delete($user['id']);
// 如果小于0则说明发生错误
if ($re <= 0)
{
$this->setError(self::ERR_ACCOUNT_IS_LOCKED);
return FALSE;
}
}
// 调用事务删除账号
$result = Db::transaction(function($db) use($user_id) {
// 删除会员
User::destroy($user_id);
// 删除会员第三方登录
UserThird::destroy($user_id);
});
return $result ? TRUE : FALSE;
}
/**
* 直接登录账号
* @param int $user_id
* @param boolean $sync
* @return boolean
*/
public function direct($user_id, $sync = TRUE)
{
$this->user = $this->user->get($user_id);
if ($this->user)
{
$this->syncLogin($sync);
return TRUE;
}
else
{
return FALSE;
}
}
/**
* 获取密码加密方式
* @param string $password
* @param string $salt
* @return string
*/
public function getEncryptPassword($password, $salt = '')
{
return md5(md5($password) . $salt);
}
/**
* 检测当前控制器和方法是否匹配传递的数组
*
* @param array $arr 需要验证权限的数组
*/
public function match($arr = [])
{
$request = Request::instance();
$arr = is_array($arr) ? $arr : explode(',', $arr);
if (!$arr)
{
return FALSE;
}
// 是否存在
if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr))
{
return TRUE;
}
// 没找到匹配
return FALSE;
}
/**
* 同步登录信息
* @param int $sync 是否同步登录到UC
* @return boolean
*/
protected function syncLogin($sync = TRUE)
{
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS && $sync)
{
$uc = new Client();
$re = $uc->uc_user_login($this->user->id, $this->user->password . '#split#' . $this->user->salt, 3);
// 如果小于0则说明发生错误
if ($re <= 0)
{
$this->setError(self::ERR_USERNAME_OR_PASSWORD_IS_INCORRECT);
return FALSE;
}
}
//增加登录次数和设置最后登录时间
$this->user->save([
'prevtime' => $this->user->logintime,
'logintime' => time(),
'loginip' => request()->ip(),
]);
// 写入登录Cookies和Token
$this->writeStatus();
return TRUE;
}
/**
* 写入登录态和Cookie
*
* @param int $keeptime
*/
protected function writeStatus()
{
//设置登录标识
$this->_logined = TRUE;
$token = $this->token();
Cookie::set('user_id', $this->user->id, $this->keeptime);
Cookie::set('username', $this->user->username, 86400 * 365);
//加密安全字符
Cookie::set('token', $token, $this->keeptime);
$this->setError('');
}
/**
* 设置会话有效时间
* @param int $keeptime 默认为永久
*/
public function keeptime($keeptime = 0)
{
$this->keeptime = $keeptime;
}
/**
* 渲染用户数据
* @param array $datalist
* @param array $fields
* @param string $fieldkey
* @param string $renderkey
* @return array
*/
public function render(&$datalist, $fields = [], $fieldkey = 'user_id', $renderkey = 'userinfo')
{
$fields = !$fields ? ['id', 'nickname', 'level', 'avatar'] : (is_array($fields) ? $fields : explode(',', $fields));
$ids = [];
foreach ($datalist as $k => $v)
{
if (!isset($v[$fieldkey]))
continue;
$ids[] = $v[$fieldkey];
}
$list = [];
if ($ids)
{
if (!in_array('id', $fields))
{
$fields[] = 'id';
}
$ids = array_unique($ids);
$selectlist = User::where('id', 'in', $ids)->column($fields);
foreach ($selectlist as $k => $v)
{
$list[$v['id']] = $v;
}
}
foreach ($datalist as $k => &$v)
{
$v[$renderkey] = isset($list[$v[$fieldkey]]) ? $list[$v[$fieldkey]] : NULL;
}
unset($v);
return $datalist;
}
/**
* 设置错误信息
*
* @param $error
*/
public function setError($error)
{
$this->_error = $error;
return $this;
}
/**
* 获取错误信息
* @return string
*/
public function getError()
{
return __($this->_error);
}
public function __toString()
{
return $this->user->toJson();
}
// JsonSerializable
public function jsonSerialize()
{
return $this->user->toArray();
}
// ArrayAccess
public function offsetSet($name, $value)
{
$this->user->setAttr($name, $value);
}
public function offsetExists($name)
{
return $this->user->__isset($name);
}
public function offsetUnset($name)
{
$this->user->__unset($name);
}
public function offsetGet($name)
{
return $this->user->getAttr($name);
}
}