Skip to content

Commit

Permalink
feat-IAuthorizationService-优化获取用户拥有角色的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 13, 2024
1 parent a727459 commit 24e13b0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class AuthorizationService implements IAuthorizationService {
.expireAfterWrite(5, TimeUnit.MINUTES)
.build(this::loadAction);

private final LoadingCache<String, List<String>> userToRoles = Caffeine.newBuilder()
private final LoadingCache<String, Set<String>> userToRoles = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.build(this::loadRoles);

Expand Down Expand Up @@ -82,14 +82,14 @@ private Map<String, Object> loadAction(String actionAtModule) { // 形如 view@
return db.row("select * from platform.app_module_action where id_at_app_module = ? and v_alias = ?", moduleID, action);
}

public List<String> loadRoles(String userID) {
public Set<String> loadRoles(String userID) {
List<Map<String, Object>> rows = db.query("""
select id_at_auth_role
from platform.auth_user_role
where id_at_auth_user = ?
and exists(select * from platform.auth_role where auth_role.id = id_at_auth_role)
""", userID);
return rows.stream().map(it -> it.get("id_at_auth_role").toString()).toList();
return rows.stream().map(it -> it.get("id_at_auth_role").toString()).collect(Collectors.toSet());
}

public List<Map<String, Object>> loadOrgAndDept(String type) {
Expand Down Expand Up @@ -153,7 +153,7 @@ public boolean isAuthorized(String userID, String module, String action) {
return true;
}

List<String> roles = getUserAvailableRoles(userID);
Set<String> roles = getUserAvailableRoles(userID);

if (roles.isEmpty()) {
return false;
Expand All @@ -176,7 +176,7 @@ public boolean isDataAuthorized(String userID, String module, String action, Str
return true;
}

List<String> roles = getUserAvailableRoles(userID);
Set<String> roles = getUserAvailableRoles(userID);

if (roles.isEmpty()) {
return false;
Expand Down Expand Up @@ -208,7 +208,7 @@ public String getAuthCondition(String userID, String module, String action) {
return "and 1=1";
}

List<String> roles = getUserAvailableRoles(userID);
Set<String> roles = getUserAvailableRoles(userID);

if (roles.isEmpty()) {
return "and 1=2";
Expand Down Expand Up @@ -365,7 +365,7 @@ public List<String> getAllowedActions(String userID, String module) {
return result.stream().map(it -> it.get("v_alias").toString()).collect(Collectors.toList());
}

List<String> roles = getUserAvailableRoles(userID);
Set<String> roles = getUserAvailableRoles(userID);

if (roles.isEmpty()) {
return List.of();
Expand Down Expand Up @@ -396,7 +396,7 @@ public Map<String, Set<String>> getAuthorizedResources(String userID) {
""";
params = new Object[]{}; // 超级用户不需要参数
} else {
List<String> roles = getUserAvailableRoles(userID);
Set<String> roles = getUserAvailableRoles(userID);

if (roles.isEmpty()) {
return Map.of();
Expand Down Expand Up @@ -424,7 +424,7 @@ where id_at_auth_role in (%s)
}

@Override
public List<String> getUserAvailableRoles(String userID) {
public Set<String> getUserAvailableRoles(String userID) {
if (config.debug()) {
return loadRoles(userID);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void setUp() {

@Test
void testGetUserAvailableRoles() {
List<String> roles = authService.getUserAvailableRoles(userID);
Set<String> roles = authService.getUserAvailableRoles(userID);
assertEquals(2, roles.size());
assertTrue(roles.contains(role1));
assertTrue(roles.contains(role2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public interface IAuthorizationService {
* @param userID
* @return
*/
List<String> getUserAvailableRoles(String userID);
Set<String> getUserAvailableRoles(String userID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.List;
import java.util.Map;
import java.util.Set;

import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH;

Expand Down Expand Up @@ -85,7 +86,7 @@ public String schemaForUser(@QueryParam("userID") String userID, @QueryParam("te
if (terminalType == null) {
terminalType = "web";
}
List<String> roles = authorizationService.getUserAvailableRoles(userID);
Set<String> roles = authorizationService.getUserAvailableRoles(userID);

Map row = getDB().row("""
select * from platform.app_menu_schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH;

Expand Down Expand Up @@ -170,7 +171,7 @@ public List<QueryItem> queryItemList() {
@GET
@Path("/roles/{userID}")
@Operation(summary = "获取用户拥有的角色")
public List<String> roles(@PathParam("userID") String userID) {
public Set<String> roles(@PathParam("userID") String userID) {
return authorizationService.getUserAvailableRoles(userID);
}

Expand Down

0 comments on commit 24e13b0

Please sign in to comment.