1717import static com .google .common .base .Strings .isNullOrEmpty ;
1818import static com .google .common .collect .ImmutableList .toImmutableList ;
1919import static google .registry .model .console .RegistrarRole .ACCOUNT_MANAGER ;
20+ import static google .registry .model .console .RegistrarRole .TECH_CONTACT ;
2021import static google .registry .persistence .transaction .TransactionManagerFactory .tm ;
2122import static google .registry .request .Action .Method .DELETE ;
2223import 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
0 commit comments