Skip to content

Commit fc1eb16

Browse files
authored
Remove Primary Contact from users editing screen (#2856)
1 parent ed25854 commit fc1eb16

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

console-webapp/src/app/users/userEditForm.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
></mat-label
3030
>
3131
<mat-select [(ngModel)]="user().role" name="userRole">
32-
<mat-option value="PRIMARY_CONTACT">Editor</mat-option>
32+
<mat-option value="TECH_CONTACT">Editor</mat-option>
3333
<mat-option value="ACCOUNT_MANAGER">Viewer</mat-option>
3434
</mat-select>
3535
</mat-form-field>

core/src/main/java/google/registry/ui/server/console/ConsoleUsersAction.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.google.common.base.Strings.isNullOrEmpty;
1818
import static com.google.common.collect.ImmutableList.toImmutableList;
1919
import static google.registry.model.console.RegistrarRole.ACCOUNT_MANAGER;
20+
import static google.registry.model.console.RegistrarRole.TECH_CONTACT;
2021
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
2122
import static google.registry.request.Action.Method.DELETE;
2223
import static google.registry.request.Action.Method.GET;
@@ -152,7 +153,7 @@ private void runAppendUserToExistingRegistrar() {
152153
updateUserRegistrarRoles(
153154
this.userData.get().emailAddress,
154155
registrarId,
155-
RegistrarRole.valueOf(this.userData.get().role));
156+
requestRoleToAllowedRoles(this.userData.get().role));
156157

157158
sendConfirmationEmail(registrarId, this.userData.get().emailAddress, "Added existing user");
158159
consoleApiParams.response().setStatus(SC_OK);
@@ -222,11 +223,9 @@ private void runCreate() throws IOException {
222223
throw e;
223224
}
224225

226+
RegistrarRole newRole = requestRoleToAllowedRoles(userData.get().role);
225227
UserRoles userRoles =
226-
new UserRoles.Builder()
227-
.setRegistrarRoles(
228-
ImmutableMap.of(registrarId, RegistrarRole.valueOf(userData.get().role)))
229-
.build();
228+
new UserRoles.Builder().setRegistrarRoles(ImmutableMap.of(registrarId, newRole)).build();
230229

231230
User.Builder builder = new User.Builder().setUserRoles(userRoles).setEmailAddress(newEmail);
232231
tm().put(builder.build());
@@ -238,9 +237,7 @@ private void runCreate() throws IOException {
238237
.setPayload(
239238
consoleApiParams
240239
.gson()
241-
.toJson(
242-
new UserData(
243-
newEmail, null, ACCOUNT_MANAGER.toString(), newUser.getPassword())));
240+
.toJson(new UserData(newEmail, null, newRole.toString(), newUser.getPassword())));
244241
finishAndPersistConsoleUpdateHistory(
245242
new ConsoleUpdateHistory.Builder()
246243
.setType(ConsoleUpdateHistory.Type.USER_CREATE)
@@ -257,7 +254,7 @@ private void runUpdateInTransaction() {
257254
updateUserRegistrarRoles(
258255
this.userData.get().emailAddress,
259256
registrarId,
260-
RegistrarRole.valueOf(this.userData.get().role));
257+
requestRoleToAllowedRoles(this.userData.get().role));
261258

262259
sendConfirmationEmail(registrarId, this.userData.get().emailAddress, "Updated user");
263260
consoleApiParams.response().setStatus(SC_OK);
@@ -333,6 +330,11 @@ private ImmutableList<User> getAllRegistrarUsers(String registrarId) {
333330
.collect(toImmutableList()));
334331
}
335332

333+
/** Maps a request role string to a RegistrarRole, using ACCOUNT_MANAGER as the default. */
334+
private RegistrarRole requestRoleToAllowedRoles(String role) {
335+
return TECH_CONTACT.name().equals(role) ? TECH_CONTACT : ACCOUNT_MANAGER;
336+
}
337+
336338
private boolean sendConfirmationEmail(String registrarId, String emailAddress, String operation) {
337339
Optional<Registrar> registrar = Registrar.loadByRegistrarId(registrarId);
338340
if (registrar.isEmpty()) { // Shouldn't happen, but worth checking

core/src/test/java/google/registry/ui/server/console/ConsoleUsersActionTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,26 @@ void testSuccess_createsUser() throws IOException {
170170
createAction(
171171
Optional.of(ConsoleApiParamsUtils.createFake(authResult)),
172172
Optional.of("POST"),
173-
Optional.of(new UserData("lol", null, RegistrarRole.ACCOUNT_MANAGER.toString(), null)));
173+
Optional.of(new UserData("lol", null, RegistrarRole.TECH_CONTACT.name(), null)));
174+
action.cloudTasksUtils = cloudTasksHelper.getTestCloudTasksUtils();
175+
when(directory.users()).thenReturn(users);
176+
when(users.insert(any(com.google.api.services.directory.model.User.class))).thenReturn(insert);
177+
action.run();
178+
assertThat(response.getStatus()).isEqualTo(SC_CREATED);
179+
assertThat(response.getPayload())
180+
.contains(
181+
"{\"emailAddress\":\"[email protected]\",\"role\":\"TECH_CONTACT\",\"password\":\"abcdefghijklmnop\"}");
182+
}
183+
184+
@Test
185+
void testSuccess_roleEnforcementCreate() throws IOException {
186+
User user = DatabaseHelper.createAdminUser("[email protected]");
187+
AuthResult authResult = AuthResult.createUser(user);
188+
ConsoleUsersAction action =
189+
createAction(
190+
Optional.of(ConsoleApiParamsUtils.createFake(authResult)),
191+
Optional.of("POST"),
192+
Optional.of(new UserData("lol", null, RegistrarRole.PRIMARY_CONTACT.name(), null)));
174193
action.cloudTasksUtils = cloudTasksHelper.getTestCloudTasksUtils();
175194
when(directory.users()).thenReturn(users);
176195
when(users.insert(any(com.google.api.services.directory.model.User.class))).thenReturn(insert);

0 commit comments

Comments
 (0)