Browse Source

update account-service

reghao 6 tháng trước cách đây
mục cha
commit
b1774688ae

+ 3 - 0
account/account-api/src/main/java/cn/reghao/tnb/account/api/dto/AuthedAccount.java

@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
 import lombok.Getter;
 
 import java.io.Serializable;
+import java.util.Set;
 
 /**
  * @author reghao
@@ -15,4 +16,6 @@ public class AuthedAccount implements Serializable {
     private static final long serialVersionUID = 1L;
 
     private long userId;
+    private String userIdStr;
+    private Set<String> roles;
 }

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

@@ -33,11 +33,9 @@ public interface UserAccountMapper extends BaseMapper<UserAccount> {
     UserAccount findByMobile(String mobile);
     Long findUserIdByUsername(String username);
     UserAccount findByUserId(long userId);
-    int count();
     AccountInfo findAccountInfo(long userId);
     List<AccountInfo> findAccountInfos(List<Long> list);
     AccountInfo findAccountInfoByMobile(String mobile);
     AccountInfo findAccountInfoByEmail(String email);
     AccountInfo findAccountInfoByUsername(String username);
-    List<AccountInfo> findAccountInfoByPage(Page page);
 }

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

@@ -13,5 +13,4 @@ import java.util.List;
 @Mapper
 public interface UserAccountRoleMapper extends BaseMapper<UserAccountRole> {
     List<UserAccountRole> findByUserId(long userId);
-    List<UserAccountRole> findByIds(List<Integer> list);
 }

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

@@ -6,13 +6,16 @@ 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.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 org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -34,7 +37,13 @@ public class AccountRepository {
 
     @Transactional(rollbackFor = Exception.class)
     public void saveAccount(UserAccount userAccount) {
+        long userId = userAccount.getUserId();
+        Set<UserAccountRole> set = userAccount.getAuthorities().stream()
+                .map(role -> new UserAccountRole(userId, role.getAuthority()))
+                .collect(Collectors.toSet());
+
         userAccountMapper.save(userAccount);
+        userAccountRoleMapper.saveAll(new ArrayList<>(set));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -76,11 +85,6 @@ public class AccountRepository {
         return userRegistryMapper.findAll().get(0);
     }
 
-    public List<String> getRoles(List<Integer> roleIds) {
-        List<UserAccountRole> roles = userAccountRoleMapper.findByIds(roleIds);
-        return roles.stream().map(UserAccountRole::getName).collect(Collectors.toList());
-    }
-
     public long getUserIdByUsername(String username) {
         return userAccountMapper.findUserIdByUsername(username);
     }
@@ -104,7 +108,13 @@ public class AccountRepository {
     }
 
     public UserAccount getUserAccountByUsername(String username) {
-        return userAccountMapper.findByUsername(username);
+        UserAccount userAccount = userAccountMapper.findByUsername(username);
+        Set<UserAuthority> set = userAccountRoleMapper.findByUserId(userAccount.getUserId()).stream()
+                .map(userAccountRole -> new UserAuthority(userAccountRole.getName()))
+                .collect(Collectors.toSet());
+        userAccount.setAuthorities(set);
+
+        return userAccount;
     }
 
     public UserAccount getUserAccountByEmail(String email) {

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

@@ -31,8 +31,6 @@ import java.util.stream.Collectors;
 public class UserAccount extends BaseObject<Integer> implements UserDetails {
     @NotNull
     private Long userId;
-    private Integer accountType;
-    @NotNull
     @NotBlank
     @Length(min = 4, max = 16, message = "用户名仅可包含数字,大小写字母和 '-' 等字符, 长度为 4 ~ 16 个字符")
     private String username;
@@ -42,72 +40,57 @@ public class UserAccount extends BaseObject<Integer> implements UserDetails {
     private String encodedPassword;
     private String salt;
     private LocalDateTime createAt;
-    private Boolean notify;
-    @Deprecated
-    private String roles;
+    private String screenName;
+    private String avatarUrl;
 
     private transient int loginType;
     private transient int plat;
     // Spring Security 使用的字段
-    private transient Set<UserAuthority> authorities = new HashSet<>();
-    @Deprecated
-    private String role;
+    private transient Set<UserAuthority> authorities;
     private Boolean enabled;
     private Boolean locked;
 
-    private String screenName;
-    private String avatarUrl;
-    private String signature;
-    private Integer gender;
-
     public UserAccount(long userId, String email, String mobile, String avatarUrl) {
         this.userId = userId;
-        this.accountType = AccountType.tnb.getValue();
         this.username = String.format("tnb_%s", userId);
         this.email = email;
         this.mobile = mobile;
         this.encodedPassword = "";
         this.salt = "";
         this.createAt = LocalDateTime.now();
-        this.notify = false;
-        //this.role = AccountRole.user.getValue();
         this.enabled = true;
         this.locked = false;
         this.screenName = this.username;
         this.avatarUrl = avatarUrl;
-        this.gender = 2;
+        this.authorities = Set.of(new UserAuthority());
     }
 
     public UserAccount(long userId, String email, String mobile, String encodedPassword, String salt, String avatarUrl) {
         this.userId = userId;
-        this.accountType = AccountType.tnb.getValue();
         this.username = String.format("tnb_%s", userId);
         this.mobile = mobile;
         this.email = email;
         this.encodedPassword = encodedPassword;
         this.salt = salt;
         this.createAt = LocalDateTime.now();
-        this.notify = false;
-        //this.role = AccountRole.user.getValue();
         this.enabled = true;
         this.locked = false;
         this.screenName = this.username;
         this.avatarUrl = avatarUrl;
-        this.gender = 2;
+        this.authorities = Set.of(new UserAuthority());
     }
 
     @Deprecated
     public UserAccount(long userId, String email, String username, String screenName, String avatarUrl) {
         this.userId = userId;
-        this.accountType = AccountType.bilibili.getValue();
         this.username = username;
         this.email = email;
         this.createAt = LocalDateTime.now();
-        //this.role = AccountRole.user.getValue();
         this.enabled = true;
         this.locked = false;
         this.screenName = screenName;
         this.avatarUrl = avatarUrl;
+        this.authorities = Set.of(new UserAuthority());
     }
 
     @Override

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

@@ -2,13 +2,20 @@ package cn.reghao.tnb.account.app.model.po;
 
 import cn.reghao.jutil.jdk.db.BaseObject;
 import lombok.Getter;
+import lombok.NoArgsConstructor;
 
 /**
  * @author reghao
  * @date 2025-08-13 16:32:07
  */
+@NoArgsConstructor
 @Getter
 public class UserAccountRole extends BaseObject<Integer> {
     private Long userId;
     private String name;
+
+    public UserAccountRole(long userId, String name) {
+        this.userId = userId;
+        this.name = name;
+    }
 }

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

@@ -8,6 +8,7 @@ import cn.reghao.tnb.account.app.model.po.UserAccount;
 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.security.core.GrantedAuthority;
 import org.springframework.stereotype.Service;
 
 import java.util.Arrays;
@@ -41,11 +42,13 @@ public class AccountQueryImpl implements AccountQuery {
     public AuthedAccount getAuthedAccount(int type, String sessId) {
         AccountInfo accountInfo = accountTokenService.getAccountInfo(type, sessId);
         if (accountInfo != null) {
-            //long userId = userIdObfuscation.restore(userIdStr);
             long userId = accountInfo.getUserId();
             UserAccount userAccount = accountRepository.getUserAccountByUserId(userId);
-            //String role = userAccount.getRole();
-            return new AuthedAccount(userAccount.getUserId());
+            String userIdStr = userIdObfuscation.obfuscate(userId);
+            Set<String> roles = userAccount.getAuthorities().stream()
+                    .map(GrantedAuthority::getAuthority)
+                    .collect(Collectors.toSet());
+            return new AuthedAccount(userId, userIdStr, roles);
         }
 
         return null;
@@ -131,7 +134,7 @@ public class AccountQueryImpl implements AccountQuery {
      */
     @Override
     public ExamAccount getExamAccount(long userId) {
-        UserAccount userAccount = accountRepository.getUserAccountByUserId(userId);
+        /*UserAccount userAccount = accountRepository.getUserAccountByUserId(userId);
         List<Integer> roleIds = Arrays.stream(userAccount.getRoles().split(","))
                 .map(Integer::parseInt)
                 .collect(Collectors.toList());
@@ -143,7 +146,7 @@ public class AccountQueryImpl implements AccountQuery {
             return new ExamAccount(true, true);
         } else if (roles.contains(examUser)) {
             return new ExamAccount(true, false);
-        }
+        }*/
 
         return new ExamAccount(false, false);
     }

+ 1 - 7
account/account-service/src/main/java/cn/reghao/tnb/account/app/rpc/AdminAccountServiceImpl.java

@@ -49,13 +49,7 @@ public class AdminAccountServiceImpl implements AdminAccountService {
 
     @Override
     public PageList<AccountInfo> getByScreenName(String screenName, int pageNumber, int pageSize) {
-        /*int total = accountRepository.countByScreenName(screenName);
-        List<AccountInfo> list = accountRepository.getPageByScreenName(screenName, page);*/
-        log.info("screenName -> {}", screenName);
-        int total = userAccountMapper.count();
-        Page page = new Page(pageNumber, pageSize);
-        List<AccountInfo> list = userAccountMapper.findAccountInfoByPage(page);
-        return PageList.pageList(pageNumber, pageSize, total, list);
+        return PageList.empty();
     }
 
     @Override

+ 2 - 2
account/account-service/src/main/java/cn/reghao/tnb/account/app/security/handler/AuthSuccessHandlerImpl.java

@@ -70,7 +70,7 @@ public class AuthSuccessHandlerImpl implements AuthenticationSuccessHandler {
         boolean rememberMe = authToken.isRememberMe();
         String redirectPath = getRedirectPath();
 
-        UserAccount userAccount = accountRepository.getUserAccountByUserId(userId);
+        /*UserAccount userAccount = accountRepository.getUserAccountByUserId(userId);
         if (userAccount.getNotify()) {
             int notifyType = NotifyType.email.getValue();
             String receiver = userAccount.getEmail();
@@ -86,7 +86,7 @@ public class AuthSuccessHandlerImpl implements AuthenticationSuccessHandler {
             String loginAt = DateTimeConverter.format(localDateTime);
             LoginMessage loginMessage = new LoginMessage(notifyType, receiver, publicIp, userAgent, loginAt);
             rabbitProducer.sendLoginMessage(loginMessage);
-        }
+        }*/
 
         long timeout = rememberMe ? 3600*24*30 : 0;
         String uri = ServletUtil.getRequest().getRequestURI();

+ 4 - 12
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`,`roles`)
+        (`user_id`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`enabled`,`locked`,`screen_name`,`avatar_url`)
         values
-        (#{userId},#{accountType},#{username},#{mobile},#{email},#{encodedPassword},#{salt},#{createAt},#{role},#{enabled},#{locked},#{screenName},#{avatarUrl},#{notify},#{roles})
+        (#{userId},#{username},#{mobile},#{email},#{encodedPassword},#{salt},#{createAt},#{enabled},#{locked},#{screenName},#{avatarUrl})
     </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`,`roles`)
+        (`user_id`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`enabled`,`locked`,`screen_name`,`avatar_url`)
         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.roles})
+            (#{item.userId},#{item.username},#{item.mobile},#{item.email},#{item.encodedPassword},#{item.salt},#{item.createAt},#{item.enabled},#{item.locked},#{item.screenName},#{item.avatarUrl})
         </foreach>
     </insert>
 
@@ -120,10 +120,6 @@
         </foreach>
     </select>
 
-    <select id="count" resultType="java.lang.Integer">
-        select count(*)
-        from user_account
-    </select>
     <select id="findAccountInfoByMobile" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
         select user_id,screen_name,avatar_url,username,mobile,email
         from user_account
@@ -150,8 +146,4 @@
         from user_account
         where screen_name like concat('%',#{screenName},'%')
     </select>
-    <select id="findAccountInfoByPage" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
-        select user_id,screen_name,avatar_url,username,mobile,email
-        from user_account
-    </select>
 </mapper>

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

@@ -22,12 +22,4 @@
         from user_account_role
         where user_id=#{userId}
     </select>
-    <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>