Skip to content

Commit

Permalink
feat-IAuthAbility-加入获取getApiRequest的能力
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 8, 2024
1 parent 74bc0ad commit 4a84af6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions muyun-core/src/main/java/net/ximatai/muyun/MuYunConst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.ximatai.muyun;

public class MuYunConst {
public static final String API_REQUEST_CONTEXT_KEY = "apiRequest";
public static final String SESSION_USER_KEY = "user";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import net.ximatai.muyun.MuYunConst;
import net.ximatai.muyun.model.ApiRequest;
import net.ximatai.muyun.service.IAuthorizationService;

Expand All @@ -11,6 +12,10 @@ public interface IAuthAbility extends IRuntimeAbility {

IAuthorizationService getAuthorizationService();

default ApiRequest getApiRequest() {
return getRoutingContext().get(MuYunConst.API_REQUEST_CONTEXT_KEY);
}

@GET
@Path("/actions")
default List<String> actions() {
Expand All @@ -20,8 +25,7 @@ default List<String> actions() {
}

String userID = getUser().getId();
ApiRequest request = getRoutingContext().get("apiRequest");
String module = request.getModule();
String module = getApiRequest().getModule();

return authorizationService.getAllowedActions(userID, module);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.Session;
import net.ximatai.muyun.MuYunConst;
import net.ximatai.muyun.model.IRuntimeUser;

public interface IRuntimeAbility {

String SESSION_USER_KEY = "user";

RoutingContext getRoutingContext();

default IRuntimeUser getUser() {
Session session = getRoutingContext().session();
if (session != null && session.get(SESSION_USER_KEY) != null) {
return session.get(SESSION_USER_KEY);
if (session != null && session.get(MuYunConst.SESSION_USER_KEY) != null) {
return session.get(MuYunConst.SESSION_USER_KEY);
} else {
return IRuntimeUser.WHITE;
}
}

default void setUser(IRuntimeUser user) {
getRoutingContext().session().put(SESSION_USER_KEY, user);
getRoutingContext().session().put(MuYunConst.SESSION_USER_KEY, user);
}

default void destroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import net.ximatai.muyun.MuYunConst;
import net.ximatai.muyun.RouterFilterPriority;
import net.ximatai.muyun.ability.IRuntimeAbility;
import net.ximatai.muyun.model.ApiRequest;
Expand Down Expand Up @@ -36,7 +37,7 @@ void filter(RoutingContext context) {
throw apiRequest.getError();
}

context.put("apiRequest", apiRequest);
context.put(MuYunConst.API_REQUEST_CONTEXT_KEY, apiRequest);
}

context.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public class ApiRequest {

private final String path;
private String module;
private String action;
private String dataID;
Expand All @@ -14,6 +15,7 @@ public class ApiRequest {

public ApiRequest(IRuntimeUser user, String path) {
this.user = user;
this.path = path;

String[] urlBlock = path.split("/");

Expand Down Expand Up @@ -65,4 +67,9 @@ public boolean isSkip() {
public RuntimeException getError() {
return error;
}

@Override
public String toString() {
return "path:" + path + ", module:" + module + ", action:" + action + ", dataID:" + dataID + ", userID" + getUser().getId() + ", isSkip:" + isSkip;
}
}

0 comments on commit 4a84af6

Please sign in to comment.