UserConfigServiceImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * DBSyncer Copyright 2020-2024 All Rights Reserved.
  3. */
  4. package org.dbsyncer.biz.impl;
  5. import org.dbsyncer.biz.BizException;
  6. import org.dbsyncer.biz.UserConfigService;
  7. import org.dbsyncer.biz.checker.impl.user.UserConfigChecker;
  8. import org.dbsyncer.biz.enums.UserRoleEnum;
  9. import org.dbsyncer.biz.vo.UserInfoVo;
  10. import org.dbsyncer.common.util.SHA1Util;
  11. import org.dbsyncer.common.util.StringUtil;
  12. import org.dbsyncer.parser.ProfileComponent;
  13. import org.dbsyncer.parser.LogService;
  14. import org.dbsyncer.parser.LogType;
  15. import org.dbsyncer.parser.model.UserConfig;
  16. import org.dbsyncer.parser.model.UserInfo;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.util.Assert;
  20. import javax.annotation.Resource;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.stream.Collectors;
  26. /**
  27. * @author AE86
  28. * @version 1.0.0
  29. * @date 2022/11/17 0:16
  30. */
  31. @Service
  32. public class UserConfigServiceImpl implements UserConfigService {
  33. private static final String DEFAULT_USERNAME = "admin";
  34. private static final String DEFAULT_PASSWORD = "0DPiKuNIrrVmD8IUCuw1hQxNqZc=";
  35. @Resource
  36. private ProfileComponent profileComponent;
  37. @Resource
  38. private UserConfigChecker userConfigChecker;
  39. @Resource
  40. private LogService logService;
  41. @Override
  42. public synchronized String add(Map<String, String> params) {
  43. String username = params.get("username");
  44. Assert.hasText(username, "The username is null.");
  45. String nickname = params.get("nickname");
  46. Assert.hasText(nickname, "The nickname is null.");
  47. String password = params.get("password");
  48. Assert.hasText(password, "The password is null.");
  49. String mail = params.get("mail");
  50. String phone = params.get("phone");
  51. // 验证当前登录用户合法身份(必须是管理员操作)
  52. UserConfig userConfig = getUserConfig();
  53. UserInfo currentUser = userConfig.getUserInfo(params.get(UserConfigService.CURRENT_USER_NAME));
  54. Assert.isTrue(null == currentUser || UserRoleEnum.isAdmin(currentUser.getRoleCode()), "No permission.");
  55. // 新用户合法性(用户不能重复)
  56. Assert.isNull(userConfig.getUserInfo(username), "用户已存在,请换个账号");
  57. // 注册新用户
  58. userConfig.getUserInfoList().add(new UserInfo(username, nickname, SHA1Util.b64_sha1(password), UserRoleEnum.USER.getCode(), mail, phone));
  59. logService.log(LogType.UserLog.INSERT, String.format("[%s]添加[%s]账号成功", currentUser.getUsername(), username));
  60. return profileComponent.editConfigModel(userConfig);
  61. }
  62. @Override
  63. public synchronized String edit(Map<String, String> params) {
  64. String username = params.get("username");
  65. Assert.hasText(username, "The username is null.");
  66. String nickname = params.get("nickname");
  67. Assert.hasText(nickname, "The nickname is null.");
  68. String newPwd = params.get("newPwd");
  69. String mail = params.get("mail");
  70. String phone = params.get("phone");
  71. // 验证当前登录用户合法身份(管理员或本人操作)
  72. UserConfig userConfig = getUserConfig();
  73. UserInfo currentUser = userConfig.getUserInfo(params.get(UserConfigService.CURRENT_USER_NAME));
  74. boolean admin = null != currentUser && UserRoleEnum.isAdmin(currentUser.getRoleCode());
  75. boolean self = null != currentUser && StringUtil.equals(currentUser.getUsername(), username);
  76. Assert.isTrue(admin || self, "No permission.");
  77. // 修改自己或其他用户信息
  78. UserInfo updateUser = self ? currentUser : userConfig.getUserInfo(username);
  79. Assert.notNull(updateUser, "用户不存在");
  80. // 用户昵称
  81. updateUser.setNickname(nickname);
  82. updateUser.setMail(mail);
  83. updateUser.setPhone(phone);
  84. // 修改密码
  85. if (StringUtil.isNotBlank(newPwd)) {
  86. // 修改自己的密码需要验证
  87. if (self) {
  88. String oldPwd = params.get("oldPwd");
  89. Assert.hasText(oldPwd, "旧密码不能为空.");
  90. if (!StringUtil.equals(SHA1Util.b64_sha1(oldPwd), updateUser.getPassword())) {
  91. logService.log(LogType.SystemLog.ERROR, String.format("[%s]修改密码失败", username));
  92. throw new BizException("修改密码失败.");
  93. }
  94. }
  95. newPwd = SHA1Util.b64_sha1(newPwd);
  96. Assert.isTrue(!StringUtil.equals(newPwd, updateUser.getPassword()), "新旧密码不能完全一样.");
  97. updateUser.setPassword(newPwd);
  98. logService.log(LogType.UserLog.UPDATE, String.format("[%s]修改[%s]账号密码成功", currentUser.getUsername(), username));
  99. }
  100. return profileComponent.editConfigModel(userConfig);
  101. }
  102. @Override
  103. public synchronized String remove(Map<String, String> params) {
  104. String username = params.get("username");
  105. Assert.hasText(username, "The username is null.");
  106. // 验证当前登录用户合法身份(必须是管理员操作)
  107. UserConfig userConfig = getUserConfig();
  108. UserInfo currentUser = userConfig.getUserInfo(params.get(UserConfigService.CURRENT_USER_NAME));
  109. Assert.isTrue(UserRoleEnum.isAdmin(currentUser.getRoleCode()), "No permission.");
  110. // 不能删除自己
  111. Assert.isTrue(!StringUtil.equals(currentUser.getUsername(), username), "不能删除自己.");
  112. // 删除用户
  113. UserInfo deleteUser = userConfig.getUserInfo(username);
  114. Assert.notNull(deleteUser, "用户已删除.");
  115. userConfig.removeUserInfo(username);
  116. profileComponent.editConfigModel(userConfig);
  117. logService.log(LogType.UserLog.DELETE, String.format("[%s]删除[%s]账号成功", currentUser.getUsername(), username));
  118. return "删除用户成功!";
  119. }
  120. @Override
  121. public UserInfo getUserInfo(String currentUserName) {
  122. return getUserConfig().getUserInfo(currentUserName);
  123. }
  124. @Override
  125. public UserInfoVo getUserInfoVo(String currentUserName, String username) {
  126. // 管理员可以查看所有用户,普通用户只能查看自己
  127. UserConfig userConfig = getUserConfig();
  128. UserInfo currentUser = userConfig.getUserInfo(currentUserName);
  129. boolean admin = null != currentUser && UserRoleEnum.isAdmin(currentUser.getRoleCode());
  130. boolean self = null != currentUser && StringUtil.equals(currentUser.getUsername(), username);
  131. Assert.isTrue(admin || self, "No permission.");
  132. UserInfo userInfo = getUserConfig().getUserInfo(username);
  133. return convertUserInfo2Vo(userInfo);
  134. }
  135. @Override
  136. public List<UserInfoVo> getUserInfoAll(String currentUserName) {
  137. // 系统管理员可以查看所有用户,其他用户只能查看自己
  138. UserConfig userConfig = getUserConfig();
  139. UserInfo currentUser = userConfig.getUserInfo(currentUserName);
  140. boolean admin = null != currentUser && UserRoleEnum.isAdmin(currentUser.getRoleCode());
  141. if (admin) {
  142. return getUserConfig().getUserInfoList().stream().map(user -> convertUserInfo2Vo(user)).collect(Collectors.toList());
  143. }
  144. List<UserInfoVo> list = new ArrayList<>();
  145. UserInfo userInfo = userConfig.getUserInfo(currentUserName);
  146. list.add(convertUserInfo2Vo(userInfo));
  147. return list;
  148. }
  149. @Override
  150. public UserConfig getUserConfig() {
  151. UserConfig config = profileComponent.getUserConfig();
  152. if (null != config) {
  153. return config;
  154. }
  155. synchronized (this) {
  156. config = profileComponent.getUserConfig();
  157. if (null == config) {
  158. config = (UserConfig) userConfigChecker.checkAddConfigModel(new HashMap<>());
  159. UserRoleEnum admin = UserRoleEnum.ADMIN;
  160. config.getUserInfoList().add(new UserInfo(DEFAULT_USERNAME, DEFAULT_USERNAME, DEFAULT_PASSWORD, admin.getCode(), StringUtil.EMPTY, StringUtil.EMPTY));
  161. profileComponent.addConfigModel(config);
  162. }
  163. return config;
  164. }
  165. }
  166. private UserInfoVo convertUserInfo2Vo(UserInfo userInfo) {
  167. UserInfoVo userInfoVo = new UserInfoVo();
  168. if (null != userInfo) {
  169. BeanUtils.copyProperties(userInfo, userInfoVo);
  170. // 避免密码直接暴露
  171. userInfoVo.setPassword("***");
  172. userInfoVo.setRoleName(UserRoleEnum.getNameByCode(userInfo.getRoleCode()));
  173. }
  174. return userInfoVo;
  175. }
  176. }