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

com.github.rexsheng.springboot.faster.system.auth.domain.UserDomainService Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.auth.domain;

import com.github.rexsheng.springboot.faster.system.user.application.UserService;
import com.github.rexsheng.springboot.faster.system.user.application.dto.*;
import com.github.rexsheng.springboot.faster.util.DateUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@ConditionalOnClass(UserDetails.class)
public class UserDomainService {

    private static final Logger logger= LoggerFactory.getLogger(UserDomainService.class);

    @Resource
    private UserService userService;

    public SysUserDetail queryByAccount(SysUserDetail user){
        QueryUserRequest request=new QueryUserRequest();
        request.setLoginAccount(user.getAccount());
        List userList=userService.queryList(request);
        if(ObjectUtils.isEmpty(userList)){
            return null;
        }
        SysUserDetail sysUserDetail=map(userList.get(0));
//        if(!sysUserDetail.isEnabled()){
//            logger.warn("账号已禁用: {}",sysUserDetail.getUserId());
//            return null;
//        }
        return sysUserDetail;
    }

    public SysUserDetail queryById(Long userId){
        UserDetailResponse userResponse=userService.getAllInfo(userId);
        if(Boolean.TRUE.equals(userResponse.getDel())){
            logger.warn("账号已删除: {}",userResponse.getUserId());
            return null;
        }
        SysUserDetail sysUserDetail = map(userResponse);
//        if(!sysUserDetail.isEnabled()){
//            logger.warn("账号已禁用: {}",sysUserDetail.getUserId());
//            return null;
//        }
        return sysUserDetail;
    }

    @Async
    public void updateLoginTime(Long userId,LocalDateTime loginTime) {
        UpdateUserRequest request=new UpdateUserRequest();
        request.setUserId(userId);
        request.setLastLoginTime(loginTime);
        userService.updateLoginTime(request);
    }

    public SysUserDetail queryByApiToken(String tokenName,String tokenValue){
        QueryTokenRequest request=new QueryTokenRequest();
        request.setTokenName(tokenName);
        request.setTokenValue(tokenValue);
        List tokenList=userService.queryTokens(request);
        if(ObjectUtils.isEmpty(tokenList)){
            return null;
        }
        UserTokenDetailResponse tokenDetailResponse=tokenList.get(0);
        //validate token
        if(!UserTokenDetailResponse.tokenValidated(tokenDetailResponse)){
            logger.warn("无效的token, name: {}, id: {}",tokenDetailResponse.getTokenName(),tokenDetailResponse.getTokenId());
            return null;
        }
        SysUserDetail sysUserDetail = this.queryById(tokenDetailResponse.getUserId());
        if(sysUserDetail==null){
            return null;
        }
        sysUserDetail.setTokenId(tokenDetailResponse.getTokenId());
        sysUserDetail.setTokenBusinessKey(tokenDetailResponse.getBusinessKey());
        //取token与账号过期时间最早值
        sysUserDetail.setAccountExpiredTime(DateUtil.toDate(DateUtil.min(tokenDetailResponse.getExpireTime(),DateUtil.toLocalDateTime(sysUserDetail.getAccountExpiredTime()))));
        return sysUserDetail;
    }

    public List queryList(List userIds){
        ViewUserRequest request=new ViewUserRequest();
        request.setFetchDeleted(true);
        request.setUserIds(userIds.toArray(new Long[userIds.size()]));
        List userList=userService.viewUserByCondition(request).getDataList();
        if(ObjectUtils.isEmpty(userList)){
            return List.of();
        }
        return userList.stream().map(this::map).collect(Collectors.toList());
    }

    private SysUserDetail map(UserDetailResponse user){
        SysUserDetail result=new SysUserDetail();
        result.setUserId(user.getUserId());
        result.setAccount(user.getLoginAccount());
        result.setPassword(user.getLoginPassword());
        result.setUserName(user.getUserName());
        result.setMail(user.getMail());
        result.setPhone(user.getPhone());
        result.setDeptId(user.getDeptId());
        result.setStatus(user.getStatus());
        result.setLastLoginTime(DateUtil.toDate(user.getLastLoginTime()));
        result.setLastPasswordTime(DateUtil.toDate(user.getLastPasswordTime()));
        result.setAccountExpiredTime(DateUtil.toDate(user.getAccountExpiredTime()));
        result.setAccountLockedTime(DateUtil.toDate(user.getAccountLockedTime()));
        result.setCredentialsExpiredTime(DateUtil.toDate(user.getPasswordExpiredTime()));
        return result;
    }

    public void changePassword(Long userId,String password){
        ChangePasswordRequest request=new ChangePasswordRequest();
        request.setUserId(userId);
        request.setNewPassword(password);
        userService.changePassword(request);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy