Skip to content

Commit ed9f128

Browse files
committed
Add support for app_id field
1 parent 4be6c12 commit ed9f128

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

features/user-application-password.feature

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Feature: Manage user custom fields
134134
When I run `wp user application-password create 1 myapp1`
135135
Then STDOUT should not be empty
136136

137-
When I run `wp user application-password create 1 myapp2`
137+
When I run `wp user application-password create 1 myapp2 --app-id=42`
138138
Then STDOUT should not be empty
139139

140140
When I run `wp user application-password list 1 --name=myapp1 --field=uuid`
@@ -168,7 +168,7 @@ Feature: Manage user custom fields
168168
"""
169169
And STDOUT should contain:
170170
"""
171-
{"uuid":"{UUID2}","app_id":"","name":"myapp2","password":"{JSONHASH2}","created":{CREATED2},"last_used":null,"last_ip":null}
171+
{"uuid":"{UUID2}","app_id":"42","name":"myapp2","password":"{JSONHASH2}","created":{CREATED2},"last_used":null,"last_ip":null}
172172
"""
173173

174174
When I run `wp user application-password list 1 --format=json --fields=uuid,name`
@@ -184,37 +184,37 @@ Feature: Manage user custom fields
184184
When I run `wp user application-password list 1`
185185
Then STDOUT should be a table containing rows:
186186
| uuid | app_id | name | password | created | last_used | last_ip |
187-
| {UUID2} | | myapp2 | {HASH2} | {CREATED2} | | |
187+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
188188
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
189189

190190
When I run `wp user application-password list 1 --fields=uuid,app_id,name`
191191
Then STDOUT should be a table containing rows:
192192
| uuid | app_id | name |
193-
| {UUID2} | | myapp2 |
193+
| {UUID2} | 42 | myapp2 |
194194
| {UUID1} | | myapp1 |
195195

196196
When I run `wp user application-password list admin`
197197
Then STDOUT should be a table containing rows:
198198
| uuid | app_id | name | password | created | last_used | last_ip |
199-
| {UUID2} | | myapp2 | {HASH2} | {CREATED2} | | |
199+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
200200
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
201201

202202
When I run `wp user application-password list admin --orderby=created --order=asc`
203203
Then STDOUT should be a table containing rows:
204204
| uuid | app_id | name | password | created | last_used | last_ip |
205205
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
206-
| {UUID2} | | myapp2 | {HASH2} | {CREATED2} | | |
206+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
207207

208208
When I run `wp user application-password list admin --orderby=name --order=asc`
209209
Then STDOUT should be a table containing rows:
210210
| uuid | app_id | name | password | created | last_used | last_ip |
211211
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
212-
| {UUID2} | | myapp2 | {HASH2} | {CREATED2} | | |
212+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
213213

214214
When I run `wp user application-password list admin --orderby=name --order=desc`
215215
Then STDOUT should be a table containing rows:
216216
| uuid | app_id | name | password | created | last_used | last_ip |
217-
| {UUID2} | | myapp2 | {HASH2} | {CREATED2} | | |
217+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
218218
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
219219

220220
When I run `wp user application-password list admin --name=myapp2 --format=json`
@@ -237,6 +237,12 @@ Feature: Manage user custom fields
237237
myapp2
238238
"""
239239

240+
When I run `wp user application-password list 1 --field=name --app-id=42`
241+
Then STDOUT should be:
242+
"""
243+
myapp2
244+
"""
245+
240246
@require-wp-5.6
241247
Scenario: Get particular user application password hash
242248
Given a WP install

src/User_Application_Password_Command.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,22 @@ static function ( $a, $b ) use ( $orderby, $order ) {
166166
$fields = self::APPLICATION_PASSWORD_FIELDS;
167167

168168
foreach ( $fields as $field ) {
169-
if ( ! array_key_exists( $field, $assoc_args ) ) {
169+
if (
170+
! array_key_exists( $field, $assoc_args )
171+
&&
172+
! ( 'app_id' === $field && array_key_exists( 'app-id', $assoc_args ) )
173+
) {
170174
continue;
171175
}
172176

173177
$value = Utils\get_flag_value( $assoc_args, $field );
174178

179+
// Avoid confusion regarding the dash/underscore between creation flag
180+
// and querying field name.
181+
if ( 'app_id' === $field && array_key_exists( 'app-id', $assoc_args ) ) {
182+
$value = Utils\get_flag_value( $assoc_args, 'app-id' );
183+
}
184+
175185
$application_passwords = array_filter(
176186
$application_passwords,
177187
static function ( $application_password ) use ( $field, $value ) {
@@ -266,6 +276,9 @@ public function get( $args, $assoc_args ) {
266276
* <app-name>
267277
* : Unique name of the application to create an application password for.
268278
*
279+
* [--app-id=<app-id>]
280+
* : Application ID to attribute to the application password.
281+
*
269282
* [--porcelain]
270283
* : Output just the new password.
271284
*
@@ -280,6 +293,10 @@ public function get( $args, $assoc_args ) {
280293
* $ wp user application-passwords create 123 myapp --porcelain
281294
* ZG1bxdxdzjTwhsY8vK8l1C65
282295
*
296+
* # Create user application with a custom application ID for internal tracking
297+
* $ wp user application-passwords create 123 myapp --app-id=42 --porcelain
298+
* ZG1bxdxdzjTwhsY8vK8l1C65
299+
*
283300
* @param array $args Indexed array of positional arguments.
284301
* @param array $assoc_args Associative array of associative arguments.
285302
* @throws ExitException If the application password could not be created.
@@ -289,7 +306,14 @@ public function create( $args, $assoc_args ) {
289306

290307
list( $user_id, $app_name ) = $args;
291308

292-
$result = WP_Application_Passwords::create_new_application_password( $user_id, [ 'name' => $app_name ] );
309+
$app_id = Utils\get_flag_value( $assoc_args, 'app-id', '' );
310+
311+
$arguments = [
312+
'name' => $app_name,
313+
'app_id' => $app_id,
314+
];
315+
316+
$result = WP_Application_Passwords::create_new_application_password( $user_id, $arguments );
293317

294318
if ( $result instanceof WP_Error ) {
295319
WP_CLI::error( $result );

0 commit comments

Comments
 (0)