-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.php
134 lines (119 loc) · 2.31 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
<?php
/**
* 用户组件
*
* @Author 陆之岇(Kraity)
* @Studio 南博网络科技工作室
* @GitHub https://github.com/krait-team
*/
class Nabo_User
{
/**
* @var array
*/
public $identity = [];
/**
* @var int
*/
public $uid;
/**
* @var mixed
*/
public $data;
/**
* @return string
*/
public function uin()
{
return $this->identity['uin'];
}
/**
* @return string
*/
public function token()
{
return $this->identity['token'];
}
/**
* @param $identity
* @throws Exception
*/
public function identity($identity)
{
$this->identity['name'] = (string)$identity['name'];
$this->identity['token'] = (string)$identity['token'];
}
/**
* @return mixed
* @throws Exception
*/
public function register()
{
// login
$user = wp_authenticate(
$this->identity['name'],
$this->identity['token']
);
// check
if (is_wp_error($this->data = $user)) {
// sleep
$this->sleep();
throw new Crash(
'用户不存在', 101
);
}
// current
$this->uid = (int)$user->ID;
wp_set_current_user($user->ID);
return $user;
}
/**
* @param $data
* @return array
* @throws Exception
*/
public function login($data)
{
return $this->response(
$this->register()
);
}
/**
* @param $credential
* @return array
* @throws Exception
*/
public function challenge($credential)
{
return $this->login($credential);
}
/**
* @param int $seconds
*/
public function sleep($seconds = 6)
{
sleep($seconds);
}
/**
* @param string $role
* @return bool
*/
public function pairing($role = 'administrator')
{
return empty($this->data) ? false :
boolval($this->data->caps[$role]);
}
/**
* @param $user
* @param $token
* @return array
*/
public function response($user, $token = false)
{
return array(
'user' => Nabo_Format::user(
$user, $token
)
);
}
}