Move permissions check from UserActor to ActorAttributeHandler to make the async conversion easier

This commit is contained in:
Colin McDonald 2016-06-21 17:01:59 -04:00
parent fddb61b00e
commit a0188e2d8c
2 changed files with 6 additions and 15 deletions

View File

@ -9,26 +9,16 @@ import net.frozenorb.apiv3.unsorted.Permissions;
public final class UserActor implements Actor { public final class UserActor implements Actor {
@Getter private final User user; @Getter private final User user;
// We use Boolean here so we can have null = not calculated; private boolean authorized;
// Currently having this cached isn't important as we only check
// this once, but later on when we have non-logged in routes
// this will be important.
private Boolean cachedAuthorized = null;
public UserActor(User user) { public UserActor(User user, boolean authorized) {
this.user = user; this.user = user;
this.authorized = authorized;
} }
@Override @Override
public boolean isAuthorized() { public boolean isAuthorized() {
if (cachedAuthorized != null) { return authorized;
return cachedAuthorized;
} else {
// TODO: THIS IS KINDA BLOCKING
boolean authorized = user.hasPermissionAnywhere(Permissions.SIGN_API_REQUEST);
cachedAuthorized = authorized;
return authorized;
}
} }
@Override @Override

View File

@ -7,6 +7,7 @@ import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actor.actors.*; import net.frozenorb.apiv3.actor.actors.*;
import net.frozenorb.apiv3.model.Server; import net.frozenorb.apiv3.model.Server;
import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.Permissions;
import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.ErrorUtils;
import java.util.Base64; import java.util.Base64;
@ -47,7 +48,7 @@ public final class ActorAttributeHandler implements Handler<RoutingContext> {
} }
if (user != null && user.checkPassword(password)) { if (user != null && user.checkPassword(password)) {
ctx.put("actor", new UserActor(user)); ctx.put("actor", new UserActor(user, user.hasPermissionAnywhere(Permissions.SIGN_API_REQUEST))); // TODO: BLOCKING
ctx.next(); ctx.next();
} else { } else {
ErrorUtils.respondGeneric(ctx, 401, "Failed to authorize as " + username + "."); ErrorUtils.respondGeneric(ctx, 401, "Failed to authorize as " + username + ".");