FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
entity-command/src/User_Session_Command.php at main · wp-cli/entity-command · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
wp-cli
/
entity-command
Public
Notifications
You must be signed in to change notification settings
Fork
97
Star
105
Code
Issues
3
Pull requests
4
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
entity-command
/
src
/
User_Session_Command.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
210 lines (189 loc) · 5.52 KB
Breadcrumbs
entity-command
/
src
/
User_Session_Command.php
Copy path
File metadata and controls
210 lines (189 loc) · 5.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
use
WP_CLI
\
Fetchers
\
User
as
UserFetcher
;
use
WP_CLI
\
Formatter
;
use
WP_CLI
\
Utils
;
/**
* Destroys and lists a user's sessions.
*
* ## EXAMPLES
*
* # List a user's sessions.
* $ wp user session list admin@example.com --format=csv
* login_time,expiration_time,ip,ua
* "2016-01-01 12:34:56","2016-02-01 12:34:56",127.0.0.1,"Mozilla/5.0..."
*
* # Destroy the most recent session of the given user.
* $ wp user session destroy admin
* Success: Destroyed session. 3 sessions remaining.
*
* @package wp-cli
*/
class
User_Session_Command
extends
WP_CLI_Command {
private
$
fields
= [
'
token
'
,
'
login_time
'
,
'
expiration_time
'
,
'
ip
'
,
'
ua
'
,
];
private
$
fetcher
;
public
function
__construct
() {
$
this
->
fetcher
=
new
UserFetcher
();
}
/**
* Destroy a session for the given user.
*
* ## OPTIONS
*
* <user>
* : User ID, user email, or user login.
*
* [<token>]
* : The token of the session to destroy. Defaults to the most recently created session.
*
* [--all]
* : Destroy all of the user's sessions.
*
* ## EXAMPLES
*
* # Destroy the most recent session of the given user.
* $ wp user session destroy admin
* Success: Destroyed session. 3 sessions remaining.
*
* # Destroy a specific session of the given user.
* $ wp user session destroy admin e073ad8540a9c2...
* Success: Destroyed session. 2 sessions remaining.
*
* # Destroy all the sessions of the given user.
* $ wp user session destroy admin --all
* Success: Destroyed all sessions.
*
* # Destroy all sessions for all users.
* $ wp user list --field=ID | xargs -n 1 wp user session destroy --all
* Success: Destroyed all sessions.
* Success: Destroyed all sessions.
*/
public
function
destroy
(
$
args
,
$
assoc_args
) {
$
user
=
$
this
->
fetcher
->
get_check
(
$
args
[
0
] );
$
token
=
$
args
[
1
] ??
null
;
$
all
= (
bool
)
Utils
\get_flag_value
(
$
assoc_args
,
'
all
'
,
false
);
$
manager
= WP_Session_Tokens::
get_instance
(
$
user
->
ID
);
if
(
$
token
&&
$
all
) {
WP_CLI
::
error
(
'
The --all flag cannot be specified along with a session token.
'
);
}
if
(
$
all
) {
$
manager
->
destroy_all
();
WP_CLI
::
success
(
'
Destroyed all sessions.
'
);
return
;
}
$
sessions
=
$
this
->
get_all_sessions
(
$
manager
);
if
( !
$
token
) {
if
(
empty
(
$
sessions
) ) {
WP_CLI
::
success
(
'
No sessions to destroy.
'
);
}
$
last
=
end
(
$
sessions
);
$
token
=
$
last
[
'
token
'
];
}
if
( !
isset
(
$
sessions
[
$
token
] ) ) {
WP_CLI
::
error
(
'
Session not found.
'
);
}
$
this
->
destroy_session
(
$
manager
,
$
token
);
$
remaining
=
count
(
$
manager
->
get_all
() );
WP_CLI
::
success
(
"
Destroyed session.
{
$
remaining
}
remaining.
"
);
}
/**
* List sessions for the given user.
*
* Note: The `token` field does not return the actual token, but a hash of
* it. The real token is not persisted and can only be found in the
* corresponding cookies on the client side.
*
* ## OPTIONS
*
* <user>
* : User ID, user email, or user login.
*
* [--fields=<fields>]
* : Limit the output to specific fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - count
* - ids
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each session:
*
* * token
* * login_time
* * expiration_time
* * ip
* * ua
*
* These fields are optionally available:
*
* * expiration
* * login
*
* ## EXAMPLES
*
* # List a user's sessions.
* $ wp user session list admin@example.com --format=csv
* login_time,expiration_time,ip,ua
* "2016-01-01 12:34:56","2016-02-01 12:34:56",127.0.0.1,"Mozilla/5.0..."
*
* @subcommand list
*/
public
function
list_
(
$
args
,
$
assoc_args
) {
$
user
=
$
this
->
fetcher
->
get_check
(
$
args
[
0
] );
$
formatter
=
$
this
->
get_formatter
(
$
assoc_args
);
$
manager
= WP_Session_Tokens::
get_instance
(
$
user
->
ID
);
$
sessions
=
$
this
->
get_all_sessions
(
$
manager
);
if
(
'
ids
'
===
$
formatter
->
format
) {
echo
implode
(
'
'
,
array_keys
(
$
sessions
) );
}
else
{
$
formatter
->
display_items
(
$
sessions
);
}
}
protected
function
get_all_sessions
(
WP_Session_Tokens
$
manager
) {
// Make the private session data accessible to WP-CLI
$
get_sessions
=
new
ReflectionMethod
(
$
manager
,
'
get_sessions
'
);
if
(
PHP_VERSION_ID
<
80100
) {
// @phpstan-ignore method.deprecated
$
get_sessions
->
setAccessible
(
true
);
}
/**
* @var array<array{token: string|int, login_time: string, login: int, expiration_time: string, expiration: int}> $sessions
*/
$
sessions
=
$
get_sessions
->
invoke
(
$
manager
);
array_walk
(
$
sessions
,
function
( &
$
session
,
$
token
) {
$
session
[
'
token
'
] =
$
token
;
$
session
[
'
login_time
'
] =
date
(
'
Y-m-d H:i:s
'
,
$
session
[
'
login
'
] );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$
session
[
'
expiration_time
'
] =
date
(
'
Y-m-d H:i:s
'
,
$
session
[
'
expiration
'
] );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
}
);
return
$
sessions
;
}
protected
function
destroy_session
(
WP_Session_Tokens
$
manager
,
$
token
) {
$
update_session
=
new
ReflectionMethod
(
$
manager
,
'
update_session
'
);
if
(
PHP_VERSION_ID
<
80100
) {
// @phpstan-ignore method.deprecated
$
update_session
->
setAccessible
(
true
);
}
return
$
update_session
->
invoke
(
$
manager
,
$
token
,
null
);
}
private
function
get_formatter
( &
$
assoc_args
) {
return
new
Formatter
(
$
assoc_args
,
$
this
->
fields
);
}
}
Back
|
FazBrowse Home
|
New Git URL