Skip to content

Commit 07cbfce

Browse files
Copilotswissspidy
andauthored
Introduce wp db users command for user management (#303)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent b9a78c4 commit 07cbfce

4 files changed

Lines changed: 416 additions & 1 deletion

File tree

‎composer.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151
"db tables",
5252
"db size",
5353
"db columns",
54-
"db status"
54+
"db status",
55+
"db users",
56+
"db users create"
5557
]
5658
},
5759
"autoload": {

‎db-command.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
}
1111

1212
WP_CLI::add_command( 'db', 'DB_Command' );
13+
WP_CLI::add_command( 'db users', 'WP_CLI\DB\Users_Command' );

‎features/db-users.feature‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
Feature: Manage database users
2+
3+
# The cleanup uses `I try` with a plain DROP USER, as MySQL 5.6 has no DROP USER IF EXISTS.
4+
@require-mysql-or-mariadb
5+
Scenario: Create a database user without privileges
6+
Given a WP install
7+
And I try `wp db query "DROP USER 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
8+
9+
When I run `wp db users create wpcli_dbusers_1 '%' --password=first-Pa55 --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
10+
Then STDOUT should be:
11+
"""
12+
Success: Created database user 'wpcli_dbusers_1'@'%'.
13+
"""
14+
15+
When I run `wp db query "SELECT User FROM mysql.user WHERE User = 'wpcli_dbusers_1'" --skip-column-names --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
16+
Then STDOUT should be:
17+
"""
18+
wpcli_dbusers_1
19+
"""
20+
21+
When I try `wp db query "SELECT 1" --dbuser=wpcli_dbusers_1 --dbpass=first-Pa55`
22+
Then the return code should be 1
23+
And STDERR should contain:
24+
"""
25+
denied
26+
"""
27+
28+
When I try `wp db users create wpcli_dbusers_1 '%' --password=dupe-Pa55-Value --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
29+
Then the return code should be 1
30+
And STDERR should contain:
31+
"""
32+
Error: ERROR 1396
33+
"""
34+
And STDERR should not contain:
35+
"""
36+
dupe-Pa55-Value
37+
"""
38+
39+
When I run `wp db query "DROP USER 'wpcli_dbusers_1'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
40+
41+
@require-mysql-or-mariadb
42+
Scenario: Create a database user with privileges on the WordPress database
43+
Given a WP install
44+
And I try `wp db query "DROP USER 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
45+
46+
When I run `echo "it's a \"tricky\" pass\\word;" | wp db users create wpcli_dbusers_2 '%' --grant-privileges --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
47+
Then STDOUT should be:
48+
"""
49+
Success: Created database user 'wpcli_dbusers_2'@'%' with all privileges on '{DB_NAME}'.
50+
"""
51+
52+
When I run `wp db query "SELECT option_value FROM wp_options WHERE option_name = 'siteurl'" --skip-column-names --dbuser=wpcli_dbusers_2 --dbpass="it's a \"tricky\" pass\\word;"`
53+
Then STDOUT should be:
54+
"""
55+
https://example.com
56+
"""
57+
58+
When I run `wp db query "DROP USER 'wpcli_dbusers_2'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
59+
60+
@require-mysql-or-mariadb
61+
Scenario: A password with an internal CRLF is kept as is
62+
Given a WP install
63+
And I try `wp db query "DROP USER 'wpcli_dbusers_5'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
64+
65+
When I run `printf 'crlf\r\nPa55\n' | wp db users create wpcli_dbusers_5 '%' --grant-privileges --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
66+
Then STDOUT should contain:
67+
"""
68+
Success:
69+
"""
70+
71+
When I run `wp db query "SELECT 'connected'" --skip-column-names --dbuser=wpcli_dbusers_5 --dbpass="$(printf 'crlf\r\nPa55')"`
72+
Then STDOUT should be:
73+
"""
74+
connected
75+
"""
76+
77+
When I try `wp db query "SELECT 'connected'" --skip-column-names --dbuser=wpcli_dbusers_5 --dbpass="$(printf 'crlf\nPa55')"`
78+
Then the return code should be 1
79+
And STDERR should contain:
80+
"""
81+
ERROR 1045
82+
"""
83+
84+
When I run `wp db query "DROP USER 'wpcli_dbusers_5'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
85+
86+
@require-mysql-or-mariadb
87+
Scenario: The new user's password is not leaked into the debug output
88+
Given a WP install
89+
And I try `wp db query "DROP USER 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
90+
91+
When I try `wp db users create wpcli_dbusers_3 '%' --password=leak-Ch3ck-Value --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} --debug=db`
92+
Then the return code should be 0
93+
And STDERR should contain:
94+
"""
95+
Creating database user 'wpcli_dbusers_3'@'%'.
96+
"""
97+
And STDERR should not contain:
98+
"""
99+
leak-Ch3ck-Value
100+
"""
101+
102+
When I run `wp db query "DROP USER 'wpcli_dbusers_3'@'%'" --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD}`
103+
104+
@require-mysql-or-mariadb
105+
Scenario: A password is required
106+
Given a WP install
107+
108+
When I try `wp db users create wpcli_dbusers_4 '%' --dbuser={DB_ROOT_USER} --dbpass={DB_ROOT_PASSWORD} < /dev/null`
109+
Then the return code should be 1
110+
And STDERR should contain:
111+
"""
112+
Error: A password is required.
113+
"""
114+
115+
@require-mysql-or-mariadb
116+
Scenario: Only the create subcommand is available
117+
Given a WP install
118+
119+
When I try `wp db users drop --yes`
120+
Then the return code should be 1
121+
And STDERR should contain:
122+
"""
123+
is not a registered subcommand of 'db users'
124+
"""
125+
126+
When I run `wp db check`
127+
Then STDOUT should contain:
128+
"""
129+
Success: Database checked.
130+
"""
131+
132+
@require-sqlite
133+
Scenario: Database users are not supported with SQLite
134+
Given a WP install
135+
136+
When I try `wp db users create someone --password=some-Pa55`
137+
Then the return code should be 1
138+
And STDERR should be:
139+
"""
140+
Error: Database users are not supported with SQLite.
141+
"""

0 commit comments

Comments
 (0)