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 {
@Getter private final User user;
// We use Boolean here so we can have null = not calculated;
// 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;
private boolean authorized;
public UserActor(User user) {
public UserActor(User user, boolean authorized) {
this.user = user;
this.authorized = authorized;
}
@Override
public boolean isAuthorized() {
if (cachedAuthorized != null) {
return cachedAuthorized;
} else {
// TODO: THIS IS KINDA BLOCKING
boolean authorized = user.hasPermissionAnywhere(Permissions.SIGN_API_REQUEST);
cachedAuthorized = authorized;
return authorized;
}
return authorized;
}
@Override

View File

@ -7,6 +7,7 @@ import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actor.actors.*;
import net.frozenorb.apiv3.model.Server;
import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.unsorted.Permissions;
import net.frozenorb.apiv3.util.ErrorUtils;
import java.util.Base64;
@ -47,7 +48,7 @@ public final class ActorAttributeHandler implements Handler<RoutingContext> {
}
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();
} else {
ErrorUtils.respondGeneric(ctx, 401, "Failed to authorize as " + username + ".");