All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.didiglobal.logi.security.service.impl.UserServiceImpl Maven / Gradle / Ivy

Go to download

logi-security 提供项目大多都需要的一些基础功能(用户、角色、权限、登录、注册、操作记录)

There is a newer version: 2.10.19
Show newest version
package com.didiglobal.logi.security.service.impl;

import static com.didiglobal.logi.security.common.enums.ResultCode.USER_ACCOUNT_ALREADY_EXIST;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_ACCOUNT_INSERT_FAIL;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_ACCOUNT_NOT_EXIST;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_ACCOUNT_UPDATE_FAIL;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_EMAIL_EXIST;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_EMAIL_FORMAT_ERROR;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_NAME_EXISTS;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_NAME_FORMAT_ERROR;
import static com.didiglobal.logi.security.common.enums.ResultCode.USER_PHONE_EXIST;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.didiglobal.logi.security.common.PagingData;
import com.didiglobal.logi.security.common.Result;
import com.didiglobal.logi.security.common.dto.user.UserBriefQueryDTO;
import com.didiglobal.logi.security.common.dto.user.UserDTO;
import com.didiglobal.logi.security.common.dto.user.UserQueryDTO;
import com.didiglobal.logi.security.common.entity.user.User;
import com.didiglobal.logi.security.common.entity.user.UserBrief;
import com.didiglobal.logi.security.common.enums.ResultCode;
import com.didiglobal.logi.security.common.enums.user.UserCheckType;
import com.didiglobal.logi.security.common.po.UserPO;
import com.didiglobal.logi.security.common.vo.project.ProjectBriefVO;
import com.didiglobal.logi.security.common.vo.role.AssignInfoVO;
import com.didiglobal.logi.security.common.vo.role.RoleBriefVO;
import com.didiglobal.logi.security.common.vo.user.UserBriefVO;
import com.didiglobal.logi.security.common.vo.user.UserVO;
import com.didiglobal.logi.security.dao.ProjectDao;
import com.didiglobal.logi.security.dao.UserDao;
import com.didiglobal.logi.security.dao.UserProjectDao;
import com.didiglobal.logi.security.exception.LogiSecurityException;
import com.didiglobal.logi.security.service.DeptService;
import com.didiglobal.logi.security.service.PermissionService;
import com.didiglobal.logi.security.service.RolePermissionService;
import com.didiglobal.logi.security.service.RoleService;
import com.didiglobal.logi.security.service.UserRoleService;
import com.didiglobal.logi.security.service.UserService;
import com.didiglobal.logi.security.util.CopyBeanUtil;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author cjm
 */
@Service("logiSecurityUserServiceImpl")
public class UserServiceImpl implements UserService {

    private static final Pattern P_USER_NAME    = Pattern.compile("^[0-9a-zA-Z_]{5,50}$");
    private static final Pattern P_USER_PHONE   = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
    private static final Pattern P_USER_MAIL    = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");

    @Autowired
    private UserDao userDao;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserProjectDao userProjectDao;
    
    @Autowired
    private ProjectDao projectDao;
    @Override
    public Result check(Integer type, String value) {
        if(UserCheckType.USER_NAME.getCode() == type){
            return userNameCheck(value);
        }else if(UserCheckType.USER_PHONE.getCode() == type){
            return userPhoneCheck(value);
        }else if(UserCheckType.USER_MAIL.getCode() == type){
            return userMailCheck(value);
        }

        return Result.fail("校验类型参数不正确");
    }

    @Override
    public PagingData getUserPage(UserQueryDTO queryDTO) {
        List userIdList = null;
        // 是否有角色id条件
        if(queryDTO.getRoleId() != null) {
            // 根据角色获取用户IdList
            userIdList = userRoleService.getUserIdListByRoleId(queryDTO.getRoleId());
            if(CollectionUtils.isEmpty(userIdList)){
                return new PagingData<>(new Page<>(queryDTO.getPage(), queryDTO.getSize()));
            }
        }

        IPage pageInfo = userDao.selectPageByUserIdList(queryDTO, userIdList);
        List userVOList = new ArrayList<>();
        List userList = pageInfo.getRecords();

        // 提前获取所有部门
        for (User user : userList) {
            UserVO userVo = CopyBeanUtil.copy(user, UserVO.class);
            // 设置角色信息
            userVo.setRoleList(roleService.getRoleBriefListByUserId(userVo.getId()));
            userVo.setUpdateTime(user.getUpdateTime());
            userVo.setCreateTime(user.getCreateTime());
            // 隐私信息处理
            privacyProcessing(userVo);
            userVOList.add(userVo);
            //设置应用信息
            final List projectBriefVOS = userProjectDao.selectProjectIdListByUserIdList(
                    Collections.singletonList(userVo.getId()))
                .stream()
                .map(projectDao::selectByProjectId)
                .map(project -> CopyBeanUtil.copy(project, ProjectBriefVO.class))
                .collect(Collectors.toList());
            userVo.setProjectList(projectBriefVOS);
        }
        return new PagingData<>(userVOList, pageInfo);
    }

    @Override
    public PagingData getUserBriefPage(UserBriefQueryDTO queryDTO) {
        // 查找合适的部门idList
        List deptIdList = deptService.
                getDeptIdListByParentIdAndDeptName(queryDTO.getDeptId(), queryDTO.getDeptName());
        // 分页获取
        IPage pageInfo = userDao.selectBriefPageByDeptIdList(queryDTO, deptIdList);
        List userBriefVOList = CopyBeanUtil.copyList(pageInfo.getRecords(), UserBriefVO.class);
        return new PagingData<>(userBriefVOList, pageInfo);
    }

    @Override
    public UserVO getUserDetailByUserId(Integer userId) throws LogiSecurityException {
        User user = userDao.selectByUserId(userId);
        if (user == null) {
            throw new LogiSecurityException(ResultCode.USER_NOT_EXISTS);
        }
        UserVO userVo = CopyBeanUtil.copy(user, UserVO.class);

        // 根据用户id获取角色List
        List roleBriefVOList = roleService.getRoleBriefListByUserId(userId);
        // 设置角色信息
        userVo.setRoleList(roleBriefVOList);

        List roleIdList = roleBriefVOList.stream().map(RoleBriefVO::getId).collect(Collectors.toList());
        // 根据角色idList获取权限idList
        List hasPermissionIdList = rolePermissionService.getPermissionIdListByRoleIdList(roleIdList);
        // 构建权限树
        userVo.setPermissionTreeVO(permissionService.buildPermissionTreeWithHas(hasPermissionIdList));
        //设置应用信息
        final List projectBriefVOS = userProjectDao.selectProjectIdListByUserIdList(
                Collections.singletonList(userVo.getId()))
            .stream()
            .map(projectDao::selectByProjectId)
            .map(project -> CopyBeanUtil.copy(project, ProjectBriefVO.class))
            .collect(Collectors.toList());
        userVo.setProjectList(projectBriefVOS);
        userVo.setUpdateTime(user.getUpdateTime());
        userVo.setCreateTime(user.getCreateTime());
        return userVo;
    }
    
    @Override
    public Result> getUserDetailByUserIds(List ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            return Result.buildSucc(Lists.newArrayList());
        }
        final List userVOS = ids.stream()
            .distinct()
            .map(userDao::selectByUserId)
            .filter(Objects::nonNull)
            .map(user -> {
                final UserVO userVO = CopyBeanUtil.copy(user, UserVO.class);
                userVO.setUpdateTime(user.getUpdateTime());
                userVO.setCreateTime(user.getCreateTime());
                return userVO;
            })
            .collect(Collectors.toList());
        for (UserVO userVO : userVOS) {
            // 根据用户id获取角色List
            List roleBriefVOList = roleService.getRoleBriefListByUserId(
                userVO.getId());
            // 设置角色信息
            userVO.setRoleList(roleBriefVOList);
            
            List roleIdList = roleBriefVOList.stream().map(RoleBriefVO::getId)
                .collect(Collectors.toList());
            // 根据角色idList获取权限idList
            List hasPermissionIdList = rolePermissionService.getPermissionIdListByRoleIdList(
                roleIdList);
            // 构建权限树
            userVO.setPermissionTreeVO(
                permissionService.buildPermissionTreeWithHas(hasPermissionIdList));
            //设置应用信息
            final List projectBriefVOS = userProjectDao.selectProjectIdListByUserIdList(
                    Collections.singletonList(userVO.getId()))
                .stream()
                .map(projectDao::selectByProjectId)
                .map(project -> CopyBeanUtil.copy(project, ProjectBriefVO.class))
                .collect(Collectors.toList());
            userVO.setProjectList(projectBriefVOS);
        }
        
        return Result.buildSucc(userVOS);
    }
    @Override
    public Result deleteByUserId(Integer userId) {
        if(userId == null) {
            return Result.fail("userId is null!");
        }

        boolean success = userDao.deleteByUserId(userId);
        if(success){
            userRoleService.deleteByUserIdOrRoleId(userId, null);
        }

        return success ? Result.success() : Result.fail();
    }

    @Override
    public UserBriefVO getUserBriefByUserName(String userName) {
        if(StringUtils.isEmpty(userName)) {
            return null;
        }
        User user = userDao.selectByUsername(userName);
        return CopyBeanUtil.copy(user, UserBriefVO.class);
    }

    @Override
    public User getUserByUserName(String userName){
        return userDao.selectByUsername(userName);
    }

    @Override
    public List getUserBriefListByUserIdList(List userIdList) {
        if(CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>();
        }
        List userBriefList = userDao.selectBriefListByUserIdList(userIdList);
        final List userBriefVOS = CopyBeanUtil.copyList(userBriefList,
            UserBriefVO.class);
        for (UserBriefVO userBriefVO : userBriefVOS) {
            final User user = userDao.selectByUserId(userBriefVO.getId());
            userBriefVO.setEmail(user.getEmail());
            userBriefVO.setPhone(user.getPhone());
            final List roles = roleService.getRoleBriefListByUserId(
                    userBriefVO.getId())
                .stream()
                .map(RoleBriefVO::getRoleName)
                .collect(Collectors.toList());
            userBriefVO.setRoleList(roles);
        }
        return userBriefVOS;
    }

    @Override
    public List getUserBriefListByUsernameOrRealName(String name) {
        List userBriefList = userDao.selectBriefListByNameAndDescOrderByCreateTime(name);
        return CopyBeanUtil.copyList(userBriefList, UserBriefVO.class);
    }

    @Override
    public List getAllUserBriefListOrderByCreateTime(boolean isAsc) {
        List userBriefList = userDao.selectBriefListOrderByCreateTime(isAsc);
        return CopyBeanUtil.copyList(userBriefList, UserBriefVO.class);
    }

    @Override
    public List getUserIdListByUsernameOrRealName(String name) {
        return userDao.selectUserIdListByUsernameOrRealName(name);
    }

    @Override
    public List getAllUserBriefList() {
        List userBriefList = userDao.selectAllBriefList();
        return CopyBeanUtil.copyList(userBriefList, UserBriefVO.class);
    }

    @Override
    public List getUserBriefListByDeptId(Integer deptId) {
        // 根据部门id查找用户,该部门的子部门的用户都属于该部门
        List deptIdList = deptService.getDeptIdListByParentId(deptId);
        List userBriefList = userDao.selectBriefListByDeptIdList(deptIdList);
        return CopyBeanUtil.copyList(userBriefList, UserBriefVO.class);
    }

    @Override
    public List getAssignDataByUserId(Integer userId) throws LogiSecurityException {
        if(userId == null) {
            throw new LogiSecurityException(ResultCode.USER_ID_CANNOT_BE_NULL);
        }
        // 获取所有角色
        List roleBriefVOList = roleService.getAllRoleBriefList();
        // 获取该用户拥有的角色
        Set hasRoleIdSet = new HashSet<>(userRoleService.getRoleIdListByUserId(userId));

        List list = new ArrayList<>();
        for(RoleBriefVO roleBriefVO : roleBriefVOList) {
            AssignInfoVO data = new AssignInfoVO();
            data.setName(roleBriefVO.getRoleName());
            data.setId(roleBriefVO.getId());
            data.setHas(hasRoleIdSet.contains(roleBriefVO.getId()));
            list.add(data);
        }
        return list;
    }

    @Override
    public List getUserBriefListByRoleId(Integer roleId) {
        // 先获取拥有该角色的用户id
        List userIdList = userRoleService.getUserIdListByRoleId(roleId);
        List userBriefList = userDao.selectBriefListByUserIdList(userIdList);
        return CopyBeanUtil.copyList(userBriefList, UserBriefVO.class);
    }

    @Override
    public Result addUser(UserDTO userDTO, String operator) {
        if(null != userDao.selectByUsername(userDTO.getUserName())){
            return Result.fail(USER_ACCOUNT_ALREADY_EXIST);
        }

        try {
            UserPO userPO = CopyBeanUtil.copy(userDTO, UserPO.class);
            if(userDao.addUser(userPO) > 0){
                userRoleService.updateUserRoleByUserId(userPO.getId(), userDTO.getRoleIds());
            }
        } catch (Exception e) {
            return Result.fail(USER_ACCOUNT_INSERT_FAIL);
        }

        return Result.success();
    }

    @Override
    public Result editUser(UserDTO userDTO, String operator) {
        User user = userDao.selectByUsername(userDTO.getUserName());
        if(null == user){
            return Result.fail(USER_ACCOUNT_NOT_EXIST);
        }

        try {
            UserPO userPO = CopyBeanUtil.copy(userDTO, UserPO.class);
            userPO.setId(user.getId());
            if(userDao.editUser(userPO) > 0){
                userRoleService.updateUserRoleByUserId(userPO.getId(), userDTO.getRoleIds());
            }
        } catch (Exception e) {
            return Result.fail(USER_ACCOUNT_UPDATE_FAIL);
        }

        return Result.success();
    }

    /**
     * 隐私处理
     *
     * @param userVo 返回给页面的用户信息
     */
    private void privacyProcessing(UserVO userVo) {
        String phone = userVo.getPhone();
        userVo.setPhone(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
    }

    private Result userNameCheck(String userName){
        if(!P_USER_NAME.matcher(userName).matches()){
            return Result.fail(USER_NAME_FORMAT_ERROR);
        }

        if(null != userDao.selectByUsername(userName)){
            return Result.fail(USER_NAME_EXISTS);
        }

        return Result.success();
    }

    private Result userPhoneCheck(String userPhone){
        if(!P_USER_PHONE.matcher(userPhone).matches()){
            return Result.fail(USER_NAME_FORMAT_ERROR);
        }

        if(null != userDao.selectByUserPhone(userPhone)){
            return Result.fail(USER_PHONE_EXIST);
        }

        return Result.success();
    }

    private Result userMailCheck(String userMail){
        if(!P_USER_MAIL.matcher(userMail).matches()){
            return Result.fail(USER_EMAIL_FORMAT_ERROR);
        }

        if(null != userDao.selectByUserMail(userMail)){
            return Result.fail(USER_EMAIL_EXIST);
        }

        return Result.success();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy