From 4a84af6a2991352ac2d86ab2a3f75aeb432b099f Mon Sep 17 00:00:00 2001 From: Liu Rui Date: Tue, 8 Oct 2024 11:40:05 +0800 Subject: [PATCH] =?UTF-8?q?feat-IAuthAbility-=E5=8A=A0=E5=85=A5=E8=8E=B7?= =?UTF-8?q?=E5=8F=96getApiRequest=E7=9A=84=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/net/ximatai/muyun/MuYunConst.java | 6 ++++++ .../java/net/ximatai/muyun/ability/IAuthAbility.java | 8 ++++++-- .../java/net/ximatai/muyun/ability/IRuntimeAbility.java | 9 ++++----- .../java/net/ximatai/muyun/http/AuthorizationFilter.java | 3 ++- .../main/java/net/ximatai/muyun/model/ApiRequest.java | 7 +++++++ 5 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 muyun-core/src/main/java/net/ximatai/muyun/MuYunConst.java diff --git a/muyun-core/src/main/java/net/ximatai/muyun/MuYunConst.java b/muyun-core/src/main/java/net/ximatai/muyun/MuYunConst.java new file mode 100644 index 00000000..3732c6e5 --- /dev/null +++ b/muyun-core/src/main/java/net/ximatai/muyun/MuYunConst.java @@ -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"; +} diff --git a/muyun-core/src/main/java/net/ximatai/muyun/ability/IAuthAbility.java b/muyun-core/src/main/java/net/ximatai/muyun/ability/IAuthAbility.java index 7b87e1ce..3805589a 100644 --- a/muyun-core/src/main/java/net/ximatai/muyun/ability/IAuthAbility.java +++ b/muyun-core/src/main/java/net/ximatai/muyun/ability/IAuthAbility.java @@ -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; @@ -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 actions() { @@ -20,8 +25,7 @@ default List actions() { } String userID = getUser().getId(); - ApiRequest request = getRoutingContext().get("apiRequest"); - String module = request.getModule(); + String module = getApiRequest().getModule(); return authorizationService.getAllowedActions(userID, module); } diff --git a/muyun-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java b/muyun-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java index 867927fa..cc0b7ed8 100644 --- a/muyun-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java +++ b/muyun-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java @@ -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() { diff --git a/muyun-core/src/main/java/net/ximatai/muyun/http/AuthorizationFilter.java b/muyun-core/src/main/java/net/ximatai/muyun/http/AuthorizationFilter.java index b2e202bd..4b6069c1 100644 --- a/muyun-core/src/main/java/net/ximatai/muyun/http/AuthorizationFilter.java +++ b/muyun-core/src/main/java/net/ximatai/muyun/http/AuthorizationFilter.java @@ -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; @@ -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(); diff --git a/muyun-core/src/main/java/net/ximatai/muyun/model/ApiRequest.java b/muyun-core/src/main/java/net/ximatai/muyun/model/ApiRequest.java index d089e1fb..7d4585c9 100644 --- a/muyun-core/src/main/java/net/ximatai/muyun/model/ApiRequest.java +++ b/muyun-core/src/main/java/net/ximatai/muyun/model/ApiRequest.java @@ -4,6 +4,7 @@ public class ApiRequest { + private final String path; private String module; private String action; private String dataID; @@ -14,6 +15,7 @@ public class ApiRequest { public ApiRequest(IRuntimeUser user, String path) { this.user = user; + this.path = path; String[] urlBlock = path.split("/"); @@ -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; + } }