Inject RdapAuthorization

We currently create it from injected arguments, and pass it to every function.

Instead, we just create a provider for it and inject it where needed.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=247072517
This commit is contained in:
guyben 2019-05-07 12:48:54 -07:00 committed by jianglai
parent ce7456ea66
commit 3b8a8892bb
10 changed files with 70 additions and 102 deletions

View file

@ -14,10 +14,15 @@
package google.registry.rdap;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.FluentLogger;
import dagger.Module;
import dagger.Provides;
import google.registry.request.Parameter;
import google.registry.request.RequestParameters;
import google.registry.request.auth.AuthResult;
import google.registry.request.auth.AuthenticatedRegistrarAccessor;
import google.registry.request.auth.UserAuthInfo;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
@ -25,6 +30,8 @@ import javax.servlet.http.HttpServletRequest;
@Module
public final class RdapModule {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Provides
@Parameter("name")
static Optional<String> provideName(HttpServletRequest req) {
@ -90,4 +97,22 @@ public final class RdapModule {
static Optional<String> provideCursor(HttpServletRequest req) {
return RequestParameters.extractOptionalParameter(req, "cursor");
}
@Provides
static RdapAuthorization provideRdapAuthorization(
AuthResult authResult, AuthenticatedRegistrarAccessor registrarAccessor) {
if (!authResult.userAuthInfo().isPresent()) {
return RdapAuthorization.PUBLIC_AUTHORIZATION;
}
UserAuthInfo userAuthInfo = authResult.userAuthInfo().get();
if (userAuthInfo.isUserAdmin()) {
return RdapAuthorization.ADMINISTRATOR_AUTHORIZATION;
}
ImmutableSet<String> clientIds = registrarAccessor.getAllClientIdWithRoles().keySet();
if (clientIds.isEmpty()) {
logger.atWarning().log("Couldn't find registrar for User %s.", authResult.userIdForLogging());
return RdapAuthorization.PUBLIC_AUTHORIZATION;
}
return RdapAuthorization.create(RdapAuthorization.Role.REGISTRAR, clientIds);
}
}