Allow admins read-only access to all registrars

We want to be able to view / test / debug how the registrar console looks for our clients.

However, we don't want to accidentally change the data for registrars, especially in a "non-accountable" way (where we later don't know who did that change)

So we do 2 things here:

- Add a "mode" (read-only and read-write) to the getRegistrarForUser function. We set it according to what we want to do with the registrar. Currently, read-write is only requested for the "update" RegistrarSetting action. Admins will have read-only access to all registrars, but read-write access only to the "admin registrar" (or whatever registrar they are contacts for).

- Support an undocumented "clientId=XXX" query param that replaces the "guessClientIdForUser" function in the original page load. We can then set it when we want to view a different account.

We also change the navigation links on the HTML page to preserve the query.

-------------------------

This might be used also for a better user experience for our clients, especially those with multiple "clientId"s (some registrar entities have multiple "registrar" objects)

Currently, they have to have a separate user for each clientId, and only have one user allowed which has both read and write permissions.

Using this change, we can give them the possibility to add users on their own, some with read-only access (to view billing information without being able to change anything), and use a single user for all their clientIds.

-------------------------

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=215480610
This commit is contained in:
guyben 2018-10-02 16:32:35 -07:00 committed by jianglai
parent 5038fa917c
commit 1d621bd14d
13 changed files with 267 additions and 63 deletions

View file

@ -30,7 +30,7 @@ import java.util.function.Function;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
/** Authenticated Registrar access helper class. */
/** HTTP session management helper class. */
@Immutable
public class SessionUtils {
@ -43,6 +43,9 @@ public class SessionUtils {
@Inject
public SessionUtils() {}
/** Type of access we're requesting. */
public enum AccessType {READ_ONLY, READ_WRITE}
/**
* Loads Registrar on behalf of an authorised user.
*
@ -50,10 +53,13 @@ public class SessionUtils {
* access the requested registrar.
*
* @param clientId ID of the registrar we request
* @param accessType what kind of access do we want for this registrar - just read it or write as
* well? (different users might have different access levels)
* @param authResult AuthResult of the user on behalf of which we want to access the data
*/
public Registrar getRegistrarForUser(String clientId, AuthResult authResult) {
return getAndAuthorize(Registrar::loadByClientId, clientId, authResult);
public Registrar getRegistrarForUser(
String clientId, AccessType accessType, AuthResult authResult) {
return getAndAuthorize(Registrar::loadByClientId, clientId, accessType, authResult);
}
/**
@ -63,15 +69,19 @@ public class SessionUtils {
* access the requested registrar.
*
* @param clientId ID of the registrar we request
* @param accessType what kind of access do we want for this registrar - just read it or write as
* well? (different users might have different access levels)
* @param authResult AuthResult of the user on behalf of which we want to access the data
*/
public Registrar getRegistrarForUserCached(String clientId, AuthResult authResult) {
return getAndAuthorize(Registrar::loadByClientIdCached, clientId, authResult);
public Registrar getRegistrarForUserCached(
String clientId, AccessType accessType, AuthResult authResult) {
return getAndAuthorize(Registrar::loadByClientIdCached, clientId, accessType, authResult);
}
Registrar getAndAuthorize(
private Registrar getAndAuthorize(
Function<String, Optional<Registrar>> registrarLoader,
String clientId,
AccessType accessType,
AuthResult authResult) {
UserAuthInfo userAuthInfo =
authResult.userAuthInfo().orElseThrow(() -> new ForbiddenException("Not logged in"));
@ -97,8 +107,17 @@ public class SessionUtils {
return registrar;
}
if (isAdmin && accessType == AccessType.READ_ONLY) {
// Admins have read-only access to all registrars
logger.atInfo().log(
"Allowing admin %s read-only access to registrar %s.", userIdForLogging, clientId);
return registrar;
}
throw new ForbiddenException(
String.format("User %s doesn't have access to registrar %s", userIdForLogging, clientId));
String.format(
"User %s doesn't have %s access to registrar %s",
userIdForLogging, accessType, clientId));
}
/**