diff --git a/core/src/main/java/google/registry/tools/GetUserCommand.java b/core/src/main/java/google/registry/tools/GetUserCommand.java new file mode 100644 index 000000000..207c0f9b0 --- /dev/null +++ b/core/src/main/java/google/registry/tools/GetUserCommand.java @@ -0,0 +1,39 @@ +// Copyright 2024 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.tools; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import google.registry.model.console.User; +import google.registry.model.console.UserDao; +import java.util.List; + +/** Command to display one or more users. */ +@Parameters(separators = " =", commandDescription = "Show user(s)") +public class GetUserCommand implements Command { + + @Parameter(description = "Email address(es) of the user(s) to display", required = true) + private List mainParameters; + + @Override + public void run() throws Exception { + for (String emailAddress : mainParameters) { + System.out.println( + UserDao.loadUser(emailAddress) + .map(User::toString) + .orElse(String.format("No user with email address %s", emailAddress))); + } + } +} diff --git a/core/src/main/java/google/registry/tools/RegistryTool.java b/core/src/main/java/google/registry/tools/RegistryTool.java index f49faa208..1915a4f92 100644 --- a/core/src/main/java/google/registry/tools/RegistryTool.java +++ b/core/src/main/java/google/registry/tools/RegistryTool.java @@ -80,6 +80,7 @@ public final class RegistryTool { .put("get_routing_map", GetRoutingMapCommand.class) .put("get_sql_credential", GetSqlCredentialCommand.class) .put("get_tld", GetTldCommand.class) + .put("get_user", GetUserCommand.class) .put("ghostryde", GhostrydeCommand.class) .put("hash_certificate", HashCertificateCommand.class) .put("list_cursors", ListCursorsCommand.class) diff --git a/core/src/test/java/google/registry/tools/GetUserCommandTest.java b/core/src/test/java/google/registry/tools/GetUserCommandTest.java new file mode 100644 index 000000000..910bca40a --- /dev/null +++ b/core/src/test/java/google/registry/tools/GetUserCommandTest.java @@ -0,0 +1,138 @@ +// Copyright 2024 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.tools; + +import com.google.common.collect.ImmutableMap; +import google.registry.model.console.GlobalRole; +import google.registry.model.console.RegistrarRole; +import google.registry.model.console.User; +import google.registry.model.console.UserDao; +import google.registry.model.console.UserRoles; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Tests for {@link GetUserCommand}. */ +public class GetUserCommandTest extends CommandTestCase { + + @BeforeEach + void beforeEach() { + UserDao.saveUser( + new User.Builder() + .setEmailAddress("johndoe@theregistrar.com") + .setUserRoles( + new UserRoles.Builder() + .setRegistrarRoles( + ImmutableMap.of("TheRegistrar", RegistrarRole.PRIMARY_CONTACT)) + .build()) + .build()); + UserDao.saveUser( + new User.Builder() + .setEmailAddress("fte@google.com") + .setUserRoles( + new UserRoles.Builder().setIsAdmin(true).setGlobalRole(GlobalRole.FTE).build()) + .build()); + } + + @Test + void testSuccess() throws Exception { + runCommand("fte@google.com"); + assertStdoutIs( + """ + User: { + emailAddress=fte@google.com + id=2 + registryLockEmailAddress=null + registryLockPasswordHash=null + registryLockPasswordSalt=null + updateTimestamp=UpdateAutoTimestamp: { + lastUpdateTime=2022-09-01T00:00:00.000Z + } + userRoles=UserRoles: { + globalRole=FTE + isAdmin=true + registrarRoles={} + } + } + """); + } + + @Test + void testSuccess_multipleUsers() throws Exception { + runCommand("johndoe@theregistrar.com", "fte@google.com"); + assertStdoutIs( + """ + User: { + emailAddress=johndoe@theregistrar.com + id=1 + registryLockEmailAddress=null + registryLockPasswordHash=null + registryLockPasswordSalt=null + updateTimestamp=UpdateAutoTimestamp: { + lastUpdateTime=2022-09-01T00:00:00.000Z + } + userRoles=UserRoles: { + globalRole=NONE + isAdmin=false + registrarRoles={TheRegistrar=PRIMARY_CONTACT} + } + } + User: { + emailAddress=fte@google.com + id=2 + registryLockEmailAddress=null + registryLockPasswordHash=null + registryLockPasswordSalt=null + updateTimestamp=UpdateAutoTimestamp: { + lastUpdateTime=2022-09-01T00:00:00.000Z + } + userRoles=UserRoles: { + globalRole=FTE + isAdmin=true + registrarRoles={} + } + } + """); + } + + @Test + void testPartialSuccess_partiallyUnknown() throws Exception { + runCommand("johndoe@theregistrar.com", "asdf@asdf.com"); + assertStdoutIs( + """ + User: { + emailAddress=johndoe@theregistrar.com + id=1 + registryLockEmailAddress=null + registryLockPasswordHash=null + registryLockPasswordSalt=null + updateTimestamp=UpdateAutoTimestamp: { + lastUpdateTime=2022-09-01T00:00:00.000Z + } + userRoles=UserRoles: { + globalRole=NONE + isAdmin=false + registrarRoles={TheRegistrar=PRIMARY_CONTACT} + } + } + No user with email address asdf@asdf.com + """); + } + + @Test + void testFailure_unknownUser() throws Exception { + runCommand("asdf@asdf.com"); + assertStdoutIs("No user with email address asdf@asdf.com\n"); + } +}