SysUserServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2016-2019 人人开源 All rights reserved.
  3. * <p>
  4. * https://www.renren.io
  5. * <p>
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.modules.sys.service.impl;
  9. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  10. import com.baomidou.mybatisplus.core.metadata.IPage;
  11. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  12. import io.renren.common.exception.RRException;
  13. import io.renren.common.utils.Constant;
  14. import io.renren.common.utils.PageUtils;
  15. import io.renren.common.utils.Query;
  16. import io.renren.modules.sys.dao.SysUserDao;
  17. import io.renren.modules.sys.entity.SysDeptEntity;
  18. import io.renren.modules.sys.entity.SysUserEntity;
  19. import io.renren.modules.sys.service.*;
  20. import org.apache.commons.lang.RandomStringUtils;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.shiro.crypto.hash.Sha256Hash;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.context.annotation.Lazy;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import java.util.Arrays;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.Map;
  31. /**
  32. * 系统用户
  33. *
  34. * @author Mark 735032128@qq.com
  35. */
  36. @Service("sysUserService")
  37. public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
  38. @Autowired
  39. private SysUserRoleService sysUserRoleService;
  40. @Autowired
  41. private SysUserPlantService sysUserPlantService;
  42. @Lazy // 延迟注入,破坏循环依赖
  43. @Autowired
  44. private SysRoleService sysRoleService;
  45. @Autowired
  46. private SysDeptService sysDeptService;
  47. @Override
  48. public PageUtils queryPage(Map<String, Object> params) {
  49. String username = (String) params.get("username");
  50. Long createUserId = (Long) params.get("createUserId");
  51. IPage<SysUserEntity> page = this.page(
  52. new Query<SysUserEntity>().getPage(params),
  53. new QueryWrapper<SysUserEntity>()
  54. .like(StringUtils.isNotBlank(username), "username", username)
  55. .eq(createUserId != null, "create_user_id", createUserId)
  56. );
  57. for (SysUserEntity sysUserEntity : page.getRecords()) {
  58. SysDeptEntity sysDeptEntity = sysDeptService.getById(sysUserEntity.getDeptId());
  59. sysUserEntity.setDeptName(sysDeptEntity.getName());
  60. }
  61. return new PageUtils(page);
  62. }
  63. @Override
  64. public List<String> queryAllPerms(Long userId) {
  65. return baseMapper.queryAllPerms(userId);
  66. }
  67. @Override
  68. public List<Long> queryAllMenuId(Long userId) {
  69. return baseMapper.queryAllMenuId(userId);
  70. }
  71. @Override
  72. public SysUserEntity queryByUserName(String username) {
  73. return baseMapper.queryByUserName(username);
  74. }
  75. @Override
  76. public SysUserEntity queryByMail(String mail) {
  77. return baseMapper.queryByMail(mail);
  78. }
  79. @Override
  80. public SysUserEntity queryByUserId(Long userId) { return baseMapper.queryByUserId(userId); }
  81. @Override
  82. @Transactional
  83. public void saveUser(SysUserEntity user) {
  84. user.setCreateTime(new Date());
  85. //sha256加密
  86. String salt = RandomStringUtils.randomAlphanumeric(20);
  87. user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
  88. user.setSalt(salt);
  89. this.save(user);
  90. //检查角色是否越权
  91. checkRole(user);
  92. //保存用户与角色关系
  93. sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
  94. //保存用户与装置关系
  95. sysUserPlantService.saveOrUpdate(user.getUserId(), user.getPlantIdList());
  96. }
  97. @Override
  98. @Transactional
  99. public void update(SysUserEntity user) {
  100. if (StringUtils.isBlank(user.getPassword())) {
  101. user.setPassword(null);
  102. } else {
  103. user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
  104. }
  105. this.updateById(user);
  106. //检查角色是否越权
  107. checkRole(user);
  108. //保存用户与角色关系
  109. sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
  110. //保存用户与装置关系
  111. sysUserPlantService.saveOrUpdate(user.getUserId(), user.getPlantIdList());
  112. }
  113. @Override
  114. public void deleteBatch(Long[] userId) {
  115. this.removeByIds(Arrays.asList(userId));
  116. }
  117. @Override
  118. public boolean updatePassword(Long userId, String password, String newPassword) {
  119. SysUserEntity userEntity = new SysUserEntity();
  120. userEntity.setPassword(newPassword);
  121. return this.update(userEntity,
  122. new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
  123. }
  124. @Override
  125. public boolean forgetPassword(String username, String newPassword) {
  126. SysUserEntity userEntity = new SysUserEntity();
  127. userEntity.setPassword(newPassword);
  128. return this.update(userEntity,
  129. new QueryWrapper<SysUserEntity>().eq("USERNAME", username));
  130. }
  131. /**
  132. * 检查角色是否越权
  133. */
  134. private void checkRole(SysUserEntity user) {
  135. if (user.getRoleIdList() == null || user.getRoleIdList().size() == 0) {
  136. return;
  137. }
  138. //如果不是超级管理员,则需要判断用户的角色是否自己创建
  139. if (user.getCreateUserId() == Constant.SUPER_ADMIN) {
  140. return;
  141. }
  142. //查询用户创建的角色列表
  143. List<Long> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());
  144. //判断是否越权
  145. if (!roleIdList.containsAll(user.getRoleIdList())) {
  146. throw new RRException("新增用户所选角色,不是本人创建");
  147. }
  148. }
  149. }