Jelajahi Sumber

AccountRole 中添加一个 ROLE_OSS 角色

reghao 5 bulan lalu
induk
melakukan
9d6c7ccf52

+ 2 - 1
account/account-service/src/main/java/cn/reghao/tnb/account/app/db/mapper/UserAccountRoleMapper.java

@@ -12,5 +12,6 @@ import java.util.List;
  */
 @Mapper
 public interface UserAccountRoleMapper extends BaseMapper<UserAccountRole> {
-    List<UserAccountRole> findByUserId(long userId);
+    List<UserAccountRole> findRolesByUserId(long userId);
+    List<Long> findUserIdsByRole(String role);
 }

+ 19 - 2
account/account-service/src/main/java/cn/reghao/tnb/account/app/db/repository/AccountRepository.java

@@ -8,6 +8,7 @@ import cn.reghao.tnb.account.app.model.po.UserAccount;
 import cn.reghao.tnb.account.app.model.po.UserAccountRole;
 import cn.reghao.tnb.account.app.model.po.UserAuthority;
 import cn.reghao.tnb.account.app.model.po.UserRegistry;
+import cn.reghao.tnb.common.auth.AccountRole;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Repository;
@@ -89,12 +90,28 @@ public class AccountRepository {
         return accountInfo;
     }
 
+    /**
+     * 获取某个 user 拥有的所有 role
+     *
+     * @param
+     * @return
+     * @date 2025-10-17 11:30:05
+     */
     public Set<String> getUserRoles(long userId) {
-        return userAccountRoleMapper.findByUserId(userId).stream()
+        return userAccountRoleMapper.findRolesByUserId(userId).stream()
                 .map(UserAccountRole::getName)
                 .collect(Collectors.toSet());
     }
 
+    public List<Long> getRoleUsers(String role) {
+        for (AccountRole accountRole : AccountRole.values()) {
+            String role1 = accountRole.getValue();
+        }
+
+        List<Long> userIds = userAccountRoleMapper.findUserIdsByRole(role);
+        return userIds;
+    }
+
     public UserRegistry getUserRegistry() {
         List<UserRegistry> list = userRegistryMapper.findAll();
         if (list.isEmpty()) {
@@ -143,7 +160,7 @@ public class AccountRepository {
     }
 
     private UserAccount setAccountAuthorities(UserAccount userAccount) {
-        Set<UserAuthority> set = userAccountRoleMapper.findByUserId(userAccount.getUserId()).stream()
+        Set<UserAuthority> set = userAccountRoleMapper.findRolesByUserId(userAccount.getUserId()).stream()
                 .map(userAccountRole -> new UserAuthority(userAccountRole.getName()))
                 .collect(Collectors.toSet());
         userAccount.setAuthorities(set);

+ 8 - 1
account/account-service/src/main/resources/mapper/UserAccountRoleMapper.xml

@@ -17,9 +17,16 @@
         </foreach>
     </insert>
 
-    <select id="findByUserId" resultType="cn.reghao.tnb.account.app.model.po.UserAccountRole">
+    <select id="findRolesByUserId" resultType="cn.reghao.tnb.account.app.model.po.UserAccountRole">
         select *
         from user_account_role
         where user_id=#{userId}
     </select>
+    <select id="findUserIdsByRoleName" resultType="java.lang.Long">
+        select user_id
+        from user_account_role
+        where `name`=#{role}
+        order by user_id
+        limit 1000
+    </select>
 </mapper>

+ 1 - 0
common/src/main/java/cn/reghao/tnb/common/auth/AccountRole.java

@@ -5,6 +5,7 @@ package cn.reghao.tnb.common.auth;
  * @date 2024-02-14 19:13:18
  */
 public enum AccountRole {
+    oss("ROLE_OSS"),
     examUser("ROLE_EXAM_USER"),
     examAdmin("ROLE_EXAM_ADMIN"),
     disk("ROLE_DISK"),