Skip to content

Commit 5db0f9e

Browse files
author
James Williams
authored
Allow updating of user passwords (#196)
## What is the goal of this PR? We've added the ability to update user passwords through TypeDB Console. This can be done with the syntax: `user password <username>` The user is then prompted to input a new password. ## What are the changes implemented in this PR? We've added a `REPLCommand` subclass `Password` that contains the necessary information to update a user's password.
1 parent 82b0d54 commit 5db0f9e

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

TypeDBConsole.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.vaticle.typedb.console.command.REPLCommand;
3737
import com.vaticle.typedb.console.command.TransactionREPLCommand;
3838
import com.vaticle.typedb.console.common.Printer;
39-
import com.vaticle.typedb.console.common.Utils;
4039
import com.vaticle.typedb.console.common.exception.TypeDBConsoleException;
4140
import com.vaticle.typeql.lang.TypeQL;
4241
import com.vaticle.typeql.lang.common.TypeQLArg;
@@ -195,8 +194,9 @@ private void runREPLMode(CLIOptions options) {
195194
} else if (command.isUserList()) {
196195
runUserList(client);
197196
} else if (command.isUserCreate()) {
198-
REPLCommand.User.Create userCommand = command.asUserCreate();
199-
runUserCreate(client, userCommand.user(), userCommand.password());
197+
runUserCreate(client, command.asUserCreate().user(), command.asUserCreate().password());
198+
} else if (command.isUserPassword()) {
199+
runUserPassword(client, command.asUserPassword().user(), command.asUserPassword().password());
200200
} else if (command.isUserDelete()) {
201201
runUserDelete(client, command.asUserDelete().user());
202202
} else if (command.isDatabaseList()) {
@@ -256,6 +256,7 @@ private Completers.TreeCompleter getCompleter(TypeDBClient client) {
256256
nodes.add(node(REPLCommand.User.token,
257257
node(REPLCommand.User.List.token),
258258
node(REPLCommand.User.Create.token),
259+
node(REPLCommand.User.Password.token),
259260
node(REPLCommand.User.Delete.token,
260261
node(userNameCompleter))
261262
));
@@ -501,6 +502,22 @@ private boolean runUserCreate(TypeDBClient client, String user, String password)
501502
}
502503
}
503504

505+
private boolean runUserPassword(TypeDBClient client, String user, String password) {
506+
try {
507+
if (!client.isCluster()) {
508+
printer.error("The command 'user update' is only available in TypeDB Cluster.");
509+
return false;
510+
}
511+
TypeDBClient.Cluster clientCluster = client.asCluster();
512+
clientCluster.users().get(user).password(password);
513+
printer.info("Updated password for user '" + user + "'");
514+
return true;
515+
} catch (TypeDBClientException e) {
516+
printer.error(e.getMessage());
517+
return false;
518+
}
519+
}
520+
504521
private boolean runUserDelete(TypeDBClient client, String user) {
505522
try {
506523
if (!client.isCluster()) {

command/REPLCommand.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ default User.Create asUserCreate() {
8080
throw new TypeDBConsoleException(ILLEGAL_CAST);
8181
}
8282

83+
default boolean isUserPassword() {
84+
return false;
85+
}
86+
87+
default User.Password asUserPassword() {
88+
throw new TypeDBConsoleException(ILLEGAL_CAST);
89+
}
90+
8391
default boolean isUserDelete() {
8492
return false;
8593
}
@@ -371,6 +379,39 @@ public User.Create asUserCreate() {
371379
}
372380
}
373381

382+
public static class Password extends REPLCommand.User {
383+
384+
public static String token = "password";
385+
private static String helpCommand = User.token + " " + token + " " + "<username>";
386+
private static String description = "Update the password of user with name <username>";
387+
388+
private final String user;
389+
private final String password;
390+
391+
public Password(String user, String password) {
392+
this.user = user;
393+
this.password = password;
394+
}
395+
396+
public String user() {
397+
return user;
398+
}
399+
400+
public String password() {
401+
return password;
402+
}
403+
404+
@Override
405+
public boolean isUserPassword() {
406+
return true;
407+
}
408+
409+
@Override
410+
public Password asUserPassword() {
411+
return this;
412+
}
413+
}
414+
374415
public static class Delete extends REPLCommand.User {
375416

376417
public static String token = "delete";
@@ -623,6 +664,7 @@ static String createHelpMenu(TypeDBClient client) {
623664
menu.addAll(Arrays.asList(
624665
pair(User.List.helpCommand, User.List.description),
625666
pair(User.Create.helpCommand, User.Create.description),
667+
pair(User.Password.helpCommand, User.Password.description),
626668
pair(User.Delete.helpCommand, User.Delete.description)));
627669
}
628670

@@ -678,6 +720,11 @@ else if (tokens.length == 2 && tokens[0].equals(User.token) && tokens[1].equals(
678720
if (passwordReader == null) throw new TypeDBConsoleException(UNABLE_TO_READ_PASSWORD_INTERACTIVELY);
679721
String password = Utils.readPassword(passwordReader, "Password: ");
680722
command = new User.Create(name, password);
723+
} else if (tokens.length == 3 && tokens[0].equals(User.token) && tokens[1].equals(User.Password.token)) {
724+
String name = tokens[2];
725+
if (passwordReader == null) throw new TypeDBConsoleException(UNABLE_TO_READ_PASSWORD_INTERACTIVELY);
726+
String password = Utils.readPassword(passwordReader, "Password: ");
727+
command = new User.Password(name, password);
681728
} else if (tokens.length == 3 && tokens[0].equals(User.token) && tokens[1].equals(User.Delete.token)) {
682729
String name = tokens[2];
683730
command = new User.Delete(name);

0 commit comments

Comments
 (0)