Просмотр исходного кода

account-service 中的 UserAccount 添加一个 roles 字段, 其内容为 "1,2,3" 字符串格式, 表示帐号拥有的角色, 对应到 UserAccountRole, UserAccount 默认拥有一个 ROLE_USER 角色

reghao 7 месяцев назад
Родитель
Сommit
46fe4eae44

+ 22 - 0
account/account-api/src/main/java/cn/reghao/tnb/account/api/dto/ExamAccount.java

@@ -0,0 +1,22 @@
+package cn.reghao.tnb.account.api.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+/**
+ * 可访问 exam 系统的帐号
+ * @author reghao
+ * @date 2025-08-13 16:29:09
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Getter
+public class ExamAccount implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private boolean exist;
+    private boolean examAdmin;
+}

+ 2 - 4
account/account-api/src/main/java/cn/reghao/tnb/account/api/iface/AccountQuery.java

@@ -1,9 +1,6 @@
 package cn.reghao.tnb.account.api.iface;
 
-import cn.reghao.tnb.account.api.dto.AccountAvatar;
-import cn.reghao.tnb.account.api.dto.AccountInfo;
-import cn.reghao.tnb.account.api.dto.AuthedAccount;
-import cn.reghao.tnb.account.api.dto.CrawledUser;
+import cn.reghao.tnb.account.api.dto.*;
 
 import java.util.List;
 
@@ -24,4 +21,5 @@ public interface AccountQuery {
     void updateAvatar(long userId, String avatarUrl);
     String getUserIdStr(long userIdLong);
     long getUserIdLong(String userIdStr);
+    ExamAccount getExamAccount(long userId);
 }

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

@@ -0,0 +1,16 @@
+package cn.reghao.tnb.account.app.db.mapper;
+
+import cn.reghao.jutil.jdk.db.BaseMapper;
+import cn.reghao.tnb.account.app.model.po.UserAccountRole;
+import org.apache.ibatis.annotations.Mapper;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-08-13 16:32:45
+ */
+@Mapper
+public interface UserAccountRoleMapper extends BaseMapper<UserAccountRole> {
+    List<UserAccountRole> findByIds(List<Integer> list);
+}

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

@@ -4,9 +4,11 @@ import cn.reghao.jutil.jdk.db.Page;
 import cn.reghao.tnb.account.api.dto.AccountAvatar;
 import cn.reghao.tnb.account.api.dto.AccountInfo;
 import cn.reghao.tnb.account.app.db.mapper.UserAccountMapper;
+import cn.reghao.tnb.account.app.db.mapper.UserAccountRoleMapper;
 import cn.reghao.tnb.account.app.db.mapper.UserRegistryMapper;
 import cn.reghao.tnb.account.app.model.dto.AccountDetail;
 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.UserRegistry;
 import cn.reghao.tnb.message.api.constant.NotifyType;
 import org.springframework.cache.annotation.CacheEvict;
@@ -16,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author reghao
@@ -26,10 +29,13 @@ public class AccountRepository {
     private final int pageSize = 100;
     private final UserAccountMapper userAccountMapper;
     private final UserRegistryMapper userRegistryMapper;
+    private UserAccountRoleMapper userAccountRoleMapper;
 
-    public AccountRepository(UserAccountMapper userAccountMapper, UserRegistryMapper userRegistryMapper) {
+    public AccountRepository(UserAccountMapper userAccountMapper, UserRegistryMapper userRegistryMapper,
+                             UserAccountRoleMapper userAccountRoleMapper) {
         this.userAccountMapper = userAccountMapper;
         this.userRegistryMapper = userRegistryMapper;
+        this.userAccountRoleMapper = userAccountRoleMapper;
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -132,4 +138,9 @@ public class AccountRepository {
         UserRegistry userRegistry = getUserRegistry();
         userRegistryMapper.update(userRegistry);
     }
+
+    public List<String> getRoles(List<Integer> roleIds) {
+        List<UserAccountRole> roles = userAccountRoleMapper.findByIds(roleIds);
+        return roles.stream().map(UserAccountRole::getName).collect(Collectors.toList());
+    }
 }

+ 1 - 0
account/account-service/src/main/java/cn/reghao/tnb/account/app/model/po/UserAccount.java

@@ -41,6 +41,7 @@ public class UserAccount extends BaseObject<Integer> {
     private String salt;
     private LocalDateTime createAt;
     private Boolean notify;
+    private String roles;
 
     // Spring Security 使用的字段
     private String role;

+ 13 - 0
account/account-service/src/main/java/cn/reghao/tnb/account/app/model/po/UserAccountRole.java

@@ -0,0 +1,13 @@
+package cn.reghao.tnb.account.app.model.po;
+
+import cn.reghao.jutil.jdk.db.BaseObject;
+import lombok.Getter;
+
+/**
+ * @author reghao
+ * @date 2025-08-13 16:32:07
+ */
+@Getter
+public class UserAccountRole extends BaseObject<Integer> {
+    private String name;
+}

+ 31 - 5
account/account-service/src/main/java/cn/reghao/tnb/account/app/rpc/AccountQueryImpl.java

@@ -1,20 +1,20 @@
 package cn.reghao.tnb.account.app.rpc;
 
 import cn.reghao.jutil.jdk.string.IDObfuscation;
-import cn.reghao.tnb.account.api.dto.AccountAvatar;
-import cn.reghao.tnb.account.api.dto.AccountInfo;
-import cn.reghao.tnb.account.api.dto.AuthedAccount;
-import cn.reghao.tnb.account.api.dto.CrawledUser;
+import cn.reghao.tnb.account.api.dto.*;
 import cn.reghao.tnb.account.api.iface.AccountQuery;
 import cn.reghao.tnb.account.app.db.repository.AccountRepository;
 import cn.reghao.tnb.account.app.model.po.UserAccount;
-import cn.reghao.tnb.account.app.security.form.AccountAuthToken;
 import cn.reghao.tnb.account.app.service.AccountTokenService;
 import cn.reghao.tnb.account.app.service.impl.AccountRegistryServiceImpl;
 import org.apache.dubbo.config.annotation.DubboService;
 import org.springframework.stereotype.Service;
 
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * @author reghao
@@ -127,4 +127,30 @@ public class AccountQueryImpl implements AccountQuery {
     public long getUserIdLong(String userIdStr) {
         return userIdObfuscation.restore(userIdStr);
     }
+
+    /**
+     * 判断帐号是否可访问 exam 系统
+     *
+     * @param
+     * @return
+     * @date 2025-08-13 16:46:05
+     */
+    @Override
+    public ExamAccount getExamAccount(long userId) {
+        UserAccount userAccount = accountRepository.getUserAccount(userId);
+        List<Integer> roleIds = Arrays.stream(userAccount.getRoles().split(","))
+                .map(Integer::parseInt)
+                .collect(Collectors.toList());
+        Set<String> roles = new HashSet<>(accountRepository.getRoles(roleIds));
+
+        String examAdmin = "ROLE_EXAM_ADMIN";
+        String examUser = "ROLE_EXAM_USER";
+        if (roles.contains(examAdmin)) {
+            return new ExamAccount(true, true);
+        } else if (roles.contains(examUser)) {
+            return new ExamAccount(true, false);
+        }
+
+        return new ExamAccount(false, false);
+    }
 }

+ 4 - 4
account/account-service/src/main/resources/mapper/UserAccountMapper.xml

@@ -4,16 +4,16 @@
 <mapper namespace="cn.reghao.tnb.account.app.db.mapper.UserAccountMapper">
     <insert id="save">
         insert into user_account
-        (`user_id`,`account_type`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`role`,`enabled`,`locked`,`screen_name`,`avatar_url`,`notify`)
+        (`user_id`,`account_type`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`role`,`enabled`,`locked`,`screen_name`,`avatar_url`,`notify`,`roles`)
         values
-        (#{userId},#{accountType},#{username},#{mobile},#{email},#{encodedPassword},#{salt},#{createAt},#{role},#{enabled},#{locked},#{screenName},#{avatarUrl},#{notify})
+        (#{userId},#{accountType},#{username},#{mobile},#{email},#{encodedPassword},#{salt},#{createAt},#{role},#{enabled},#{locked},#{screenName},#{avatarUrl},#{notify},#{roles})
     </insert>
     <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
         insert into user_account
-        (`user_id`,`account_type`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`role`,`enabled`,`locked`,`screen_name`,`avatar_url`,`notify`))
+        (`user_id`,`account_type`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`role`,`enabled`,`locked`,`screen_name`,`avatar_url`,`notify`,`roles`)
         values
         <foreach collection="list" item="item" index="index" separator=",">
-            (#{item.userId},#{item.accountType},#{item.username},#{item.mobile},#{item.email},#{item.encodedPassword},#{item.salt},#{item.createAt},#{item.role},#{item.enabled},#{item.locked},#{item.screenName},#{item.avatarUrl},#{item.notify})
+            (#{item.userId},#{item.accountType},#{item.username},#{item.mobile},#{item.email},#{item.encodedPassword},#{item.salt},#{item.createAt},#{item.role},#{item.enabled},#{item.locked},#{item.screenName},#{item.avatarUrl},#{item.notify},#{item.roles})
         </foreach>
     </insert>
 

+ 13 - 0
account/account-service/src/main/resources/mapper/UserAccountRoleMapper.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="cn.reghao.tnb.account.app.db.mapper.UserAccountRoleMapper">
+    <select id="findByIds" resultType="cn.reghao.tnb.account.app.model.po.UserAccountRole">
+        select *
+        from user_account_role
+        where id in
+        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
+            #{id}
+        </foreach>
+    </select>
+</mapper>