Move permissions check from UserActor to ActorAttributeHandler to make the async conversion easier
This commit is contained in:
parent
fddb61b00e
commit
a0188e2d8c
@ -9,27 +9,17 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
|
@ -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 + ".");
|
||||
|
Loading…
Reference in New Issue
Block a user