|
|
@@ -0,0 +1,173 @@
|
|
|
+package com.seecoder.service.impl;
|
|
|
+
|
|
|
+import cn.dev33.satoken.session.SaSessionCustomUtil;
|
|
|
+import cn.dev33.satoken.stp.SaTokenInfo;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import com.seecoder.convertor.UserConvertor;
|
|
|
+import com.seecoder.dao.*;
|
|
|
+import com.seecoder.entity.po.*;
|
|
|
+import com.seecoder.entity.vo.PlatformUserVO;
|
|
|
+import com.seecoder.enums.PermissionStatusEnum;
|
|
|
+import com.seecoder.enums.RoleStatusEnum;
|
|
|
+import com.seecoder.service.PlatformUserService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.transaction.Transactional;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class PlatformUserServiceImpl implements PlatformUserService {
|
|
|
+ private final PlatformUserDao platformUserDao;
|
|
|
+ private final UserPermissionDao userPermissionDao;
|
|
|
+ private final PermissionDao permissionDao;
|
|
|
+ private final UserRoleDao userRoleDao;
|
|
|
+ private final RolePermissionDao rolePermissionDao;
|
|
|
+ private final RoleDao roleDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PlatformUser getInfo(String username) {
|
|
|
+ return platformUserDao.findFirstByUsername(username);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public Long deleteUser(Long userId) {
|
|
|
+ // 删除用户与角色的绑定关系
|
|
|
+ userRoleDao.deleteByUserId(userId);
|
|
|
+ // 删除用户与权限的绑定关系
|
|
|
+ userPermissionDao.deleteByUserId(userId);
|
|
|
+ // 删除用户
|
|
|
+ platformUserDao.deleteById(userId);
|
|
|
+ return userId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public PlatformUser createUser(PlatformUser platformUser) {
|
|
|
+ platformUser.setCreateTime(LocalDateTime.now());
|
|
|
+ return platformUserDao.save(platformUser);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public PlatformUser updateUser(PlatformUserVO userVO) {
|
|
|
+ PlatformUser user = platformUserDao.findById(userVO.getId()).orElseThrow(()->new RuntimeException("找不到用户"));
|
|
|
+ if(userVO.getPhone() != null){
|
|
|
+ user.setPhone(userVO.getPhone());
|
|
|
+ }
|
|
|
+ if(userVO.getEmail()!=null){
|
|
|
+ user.setEmail(userVO.getEmail());
|
|
|
+ }
|
|
|
+ if(userVO.getUsername()!=null){
|
|
|
+ user.setUsername(userVO.getUsername());
|
|
|
+ }
|
|
|
+ if(userVO.getUid()!=null){
|
|
|
+ user.setUid(userVO.getUid());
|
|
|
+ }
|
|
|
+ user.setModifyTime(LocalDateTime.now());
|
|
|
+ return platformUserDao.save(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PlatformUser getUser(Long userId) {
|
|
|
+ return platformUserDao.findById(userId).orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<PlatformUser> getAllUsers() {
|
|
|
+ return platformUserDao.findAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public void resetPassword(Long userId, String originPassword, String newPassword) {
|
|
|
+ // 校验原密码
|
|
|
+ PlatformUser user = platformUserDao.findById(userId).orElseThrow(() -> new RuntimeException("找不到用户"));
|
|
|
+ if (!Objects.equals(user.getPassword(), originPassword)) {
|
|
|
+ throw new RuntimeException("原密码错误");
|
|
|
+ }
|
|
|
+ // 设置新密码
|
|
|
+ user.setPassword(newPassword);
|
|
|
+ platformUserDao.save(user);
|
|
|
+ // 注销登录
|
|
|
+ StpUtil.logout(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public void grantPermissionToUsers(List<Long> userIdList, List<Long> permissionIdList) {
|
|
|
+ if (CollectionUtils.isEmpty(userIdList) || CollectionUtils.isEmpty(permissionIdList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 授权给对应角色
|
|
|
+ List<UserPermission> userPermissionList = new ArrayList<>();
|
|
|
+ for (Long userId : userIdList) {
|
|
|
+ for (Long permissionId : permissionIdList) {
|
|
|
+ UserPermission up = new UserPermission();
|
|
|
+ up.setUserId(userId);
|
|
|
+ up.setPermissionId(permissionId);
|
|
|
+ userPermissionList.add(up);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userPermissionDao.saveAll(userPermissionList);
|
|
|
+ // 注销角色所对应的用户的登录状态(使其重新登录并获取新的角色绑定关系)
|
|
|
+ for (Long userId : userIdList) {
|
|
|
+ StpUtil.logout(userId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public SaTokenInfo login(String phone, String password) {
|
|
|
+ PlatformUser platformUser = platformUserDao.findFirstByPhone(phone);
|
|
|
+ if (platformUser == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (!password.equals(platformUser.getPassword())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 获取用户权限
|
|
|
+ Long userId = platformUser.getId();
|
|
|
+ List<String> permissionList = getUserPermissionList(userId);
|
|
|
+ List<String> roleList = getUserRoleList(userId);
|
|
|
+ StpUtil.login(userId);
|
|
|
+ StpUtil.getSession().set("permissions", String.join(",", permissionList));
|
|
|
+ StpUtil.getSession().set("roles", String.join(",", roleList));
|
|
|
+ return StpUtil.getTokenInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> getUserRoleList(Long userId) {
|
|
|
+ List<Long> roleIdList = userRoleDao.findByUserId(userId)
|
|
|
+ .stream().map(UserRole::getRoleId).distinct().collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isEmpty(roleIdList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ return roleDao.findByIdInAndStatusEquals(roleIdList, RoleStatusEnum.ON).stream().map(Role::getName).distinct().collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> getUserPermissionList(Long userId) {
|
|
|
+ // 获取用户的角色
|
|
|
+ List<Long> roleIdList = userRoleDao.findByUserId(userId)
|
|
|
+ .stream().map(UserRole::getRoleId).distinct().collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isEmpty(roleIdList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ // 获取启用的角色
|
|
|
+ List<Long> onRoleIdList = roleDao.findByIdInAndStatusEquals(roleIdList, RoleStatusEnum.ON).stream().map(Role::getId).distinct().collect(Collectors.toList());
|
|
|
+ // 获取角色对应的权限
|
|
|
+ List<Long> permissionIdList = rolePermissionDao.findByRoleIdIn(onRoleIdList)
|
|
|
+ .stream().map(RolePermission::getPermissionId).distinct().collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isEmpty(permissionIdList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ // 返回权限Code列表
|
|
|
+ return permissionDao.findByIdInAndStatusEquals(permissionIdList, PermissionStatusEnum.ON)
|
|
|
+ .stream().map(Permission::getCode).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+}
|