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

com.addplus.server.security.service.service.UserBaseService Maven / Gradle / Ivy

The newest version!
package com.addplus.server.security.service.service;

import com.addplus.server.core.exception.ErrorCodeBase;
import com.addplus.server.core.exception.ErrorException;
import com.addplus.server.core.model.authority.data.SysUser;
import com.addplus.server.core.utils.DataUtils;
import com.addplus.server.security.service.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.util.ByteSource;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class UserBaseService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired(required = false)
    private DefaultHashService defaultHashService;

    public SysUser selectByUsername(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        SysUser sysUser = new SysUser();
        sysUser.setAccount(account);
        sysUser.setIsDeleted(0);
        SysUser sysUserOne = sysUserMapper.selectOne(new QueryWrapper(sysUser));
        return sysUserOne;
    }

    public DefaultHashService returnHashService(){
        return this.defaultHashService;
    }

    public Boolean updateLoginUser(SysUser sysUser, String userAccount) throws Exception {
        if (DataUtils.isEmpty(userAccount)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysUser sysUserUpdate = new SysUser();
        sysUserUpdate.setId(sysUser.getId());
        LocalDateTime localDateTime = LocalDateTime.now();
        sysUserUpdate.setGmtModified(localDateTime);
        sysUserUpdate.setModifyUser(userAccount);
        int count = sysUserMapper.updateById(sysUserUpdate);
        if (count > 0) {
            return true;
        }
        return false;
    }

    public Boolean addUser(SysUser sysUser, String userAccount) throws ErrorException {
        if (sysUser == null) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        } else {
            if (DataUtils.isEmpty(sysUser.getRoles(), sysUser.getAccount(), sysUser.getPassword(), sysUser.getNickname(), sysUser.getPhone(), userAccount)) {
                throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
            }
        }
        sysUser.setGmtCreate(LocalDateTime.now());
        sysUser.setIsDeleted(0);
        ObjectId objectId = new ObjectId();
        sysUser.setPasswordSalt(objectId.toString());
        sysUser.setModifyUser(userAccount);
        //生成加密后的密码
        HashRequest request = new HashRequest.Builder()
                .setAlgorithmName("md5").setSource(sysUser.getPassword())
                .setSalt(ByteSource.Util.bytes(sysUser.getPasswordSalt())).setIterations(2).build();
        String hexPassword = defaultHashService.computeHash(request).toHex();
        sysUser.setPassword(hexPassword);
        int count = sysUserMapper.insert(sysUser);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public Boolean deleteUserById(Long id, String userAccount) throws Exception {
        if (id == null) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        } else if (id <= 0) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        if (DataUtils.isEmpty(userAccount)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setModifyUser(userAccount);
        sysUserMapper.updateById(sysUser);
        int count = sysUserMapper.deleteById(id);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public SysUser selectUserById(Long id) throws Exception {
        if (id == null || id <= 0) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setIsDeleted(0);
        SysUser sysUserNew = sysUserMapper.selectOne(new QueryWrapper(sysUser));
        if (sysUserNew == null) {
            throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_NULL_DATA);
        } else {
            if (sysUserNew.getStatus() == 1) {
                //抛出冻结异常码
                throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_LOGIN_MEMBER_DISABLE);
            }
        }
        return sysUserNew;
    }

    public SysUser modifyUserGetInfoById(Long id) throws Exception {
        if (id == null || id <= 0) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setIsDeleted(0);
        SysUser sysUserNew = sysUserMapper.selectOne(new QueryWrapper(sysUser));
        if (sysUserNew == null) {
            throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_NULL_DATA);
        }
        return sysUserNew;
    }


    public Boolean updateUser(SysUser sysUser, String userAccount) throws Exception {
        if (sysUser == null || sysUser.getId() == null || DataUtils.isEmpty(userAccount)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        sysUser.setGmtModified(LocalDateTime.now());
        sysUser.setModifyUser(userAccount);
        if (StringUtils.isNotBlank(sysUser.getPassword())) {
            if (StringUtils.isBlank(sysUser.getAccount())) {
                throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
            }
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", sysUser.getId());
            queryWrapper.select("id", "password_salt");
            SysUser sysUserOld = sysUserMapper.selectOne(queryWrapper);
            sysUser.setPasswordSalt(sysUserOld.getPasswordSalt());
            sysUser.setPassword(encryptString(sysUser));
        }
        int count = sysUserMapper.updateById(sysUser);
        return count > 0;
    }

    public IPage getAllUsers(Integer pageNo, Integer pageSize) throws Exception {
        if (DataUtils.EmptyOrNegative(pageNo, pageSize)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        Page page = new Page(pageNo, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        IPage sysUserIPage = sysUserMapper.selectPage(page, queryWrapper);
        if (sysUserIPage == null || sysUserIPage.getRecords() == null || sysUserIPage.getRecords().isEmpty()) {
            throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_NULL_DATA);
        }
        return sysUserIPage;
    }

    public SysUser getByUser(Long userId) throws Exception {
        if (DataUtils.EmptyOrNegative(userId)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        return sysUserMapper.selectOne(new QueryWrapper(sysUser));

    }

    public Integer getUserNameCount(String account) throws Exception {
        if (StringUtils.isBlank(account)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        int res = sysUserMapper.getSysCountNum(account);
        return res;
    }

    public String encryptString(SysUser sysUser) throws Exception {
        if (sysUser == null) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        //生成加密后的密码
        HashRequest request = new HashRequest.Builder()
                .setAlgorithmName("md5").setSource(sysUser.getPassword())
                .setSalt(ByteSource.Util.bytes(sysUser.getPasswordSalt())).setIterations(2).build();
        String hexPassword = defaultHashService.computeHash(request).toHex();
        return hexPassword;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy