| | |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import org.dromara.common.core.constant.Constants; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.constant.TenantConstants; |
| | | import org.dromara.common.core.domain.R; |
| | | import org.dromara.common.core.domain.dto.RoleDTO; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.enums.LoginType; |
| | | import org.dromara.common.core.enums.TenantStatus; |
| | | import org.dromara.common.core.enums.UserStatus; |
| | | import org.dromara.common.core.exception.user.UserException; |
| | | import org.dromara.common.core.utils.DateUtils; |
| | | import org.dromara.common.core.utils.MessageUtils; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.*; |
| | | import org.dromara.common.log.event.LogininforEvent; |
| | | import org.dromara.common.mybatis.helper.DataPermissionHelper; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.common.tenant.exception.TenantException; |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.dromara.system.domain.SysUser; |
| | | import org.dromara.system.domain.bo.SysSocialBo; |
| | | import org.dromara.system.domain.vo.SysSocialVo; |
| | | import org.dromara.system.domain.vo.SysTenantVo; |
| | | import org.dromara.system.domain.vo.SysUserVo; |
| | | import org.dromara.system.mapper.SysUserMapper; |
| | | import org.dromara.system.service.ISysPermissionService; |
| | | import org.dromara.system.service.ISysSocialService; |
| | | import org.dromara.system.service.ISysTenantService; |
| | | import org.dromara.web.domain.vo.LoginVo; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | * @param authUserData 授权响应实体 |
| | | * @return 统一响应实体 |
| | | */ |
| | | public R<LoginVo> sociaRegister(AuthUser authUserData) { |
| | | SysSocialBo bo = new SysSocialBo(); |
| | | public void socialRegister(AuthUser authUserData) { |
| | | String authId = authUserData.getSource() + authUserData.getUuid(); |
| | | // 第三方用户信息 |
| | | SysSocialBo bo = BeanUtil.toBean(authUserData, SysSocialBo.class); |
| | | BeanUtil.copyProperties(authUserData.getToken(), bo); |
| | | bo.setUserId(LoginHelper.getUserId()); |
| | | bo.setAuthId(authUserData.getSource() + authUserData.getUuid()); |
| | | bo.setAuthId(authId); |
| | | bo.setOpenId(authUserData.getUuid()); |
| | | bo.setUserName(authUserData.getUsername()); |
| | | BeanUtils.copyProperties(authUserData, bo); |
| | | BeanUtils.copyProperties(authUserData.getToken(), bo); |
| | | sysSocialService.insertByBo(bo); |
| | | return R.ok(); |
| | | bo.setNickName(authUserData.getNickname()); |
| | | // 查询是否已经绑定用户 |
| | | List<SysSocialVo> list = sysSocialService.selectByAuthId(authId); |
| | | if (CollUtil.isEmpty(list)) { |
| | | // 没有绑定用户, 新增用户信息 |
| | | sysSocialService.insertByBo(bo); |
| | | } else { |
| | | // 更新用户信息 |
| | | bo.setId(list.get(0).getId()); |
| | | sysSocialService.updateByBo(bo); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | public void logout() { |
| | | try { |
| | | LoginUser loginUser = LoginHelper.getLoginUser(); |
| | | if (ObjectUtil.isNull(loginUser)) { |
| | | return; |
| | | } |
| | | if (TenantHelper.isEnable() && LoginHelper.isSuperAdmin()) { |
| | | // 超级管理员 登出清除动态租户 |
| | | TenantHelper.clearDynamic(); |
| | |
| | | } |
| | | |
| | | |
| | | private SysUserVo loadUserByUsername(String tenantId, String username) { |
| | | SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() |
| | | .select(SysUser::getUserName, SysUser::getStatus) |
| | | .eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId) |
| | | .eq(SysUser::getUserName, username)); |
| | | if (ObjectUtil.isNull(user)) { |
| | | log.info("登录用户:{} 不存在.", username); |
| | | throw new UserException("user.not.exists", username); |
| | | } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) { |
| | | log.info("登录用户:{} 已被停用.", username); |
| | | throw new UserException("user.blocked", username); |
| | | } |
| | | if (TenantHelper.isEnable()) { |
| | | return userMapper.selectTenantUserByUserName(username, tenantId); |
| | | } |
| | | return userMapper.selectUserByUserName(username); |
| | | } |
| | | |
| | | /** |
| | | * 构建登录用户 |
| | | */ |
| | |
| | | loginUser.setUserId(user.getUserId()); |
| | | loginUser.setDeptId(user.getDeptId()); |
| | | loginUser.setUsername(user.getUserName()); |
| | | loginUser.setNickname(user.getNickName()); |
| | | loginUser.setUserType(user.getUserType()); |
| | | loginUser.setMenuPermission(permissionService.getMenuPermission(user.getUserId())); |
| | | loginUser.setRolePermission(permissionService.getRolePermission(user.getUserId())); |
| | |
| | | * |
| | | * @param userId 用户ID |
| | | */ |
| | | public void recordLoginInfo(Long userId) { |
| | | public void recordLoginInfo(Long userId, String ip) { |
| | | SysUser sysUser = new SysUser(); |
| | | sysUser.setUserId(userId); |
| | | sysUser.setLoginIp(ServletUtils.getClientIP()); |
| | | sysUser.setLoginIp(ip); |
| | | sysUser.setLoginDate(DateUtils.getNowDate()); |
| | | sysUser.setUpdateBy(userId); |
| | | userMapper.updateById(sysUser); |
| | | DataPermissionHelper.ignore(() -> userMapper.updateById(sysUser)); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (TenantConstants.DEFAULT_TENANT_ID.equals(tenantId)) { |
| | | return; |
| | | } |
| | | if (StringUtils.isBlank(tenantId)) { |
| | | throw new TenantException("tenant.number.not.blank"); |
| | | } |
| | | SysTenantVo tenant = tenantService.queryByTenantId(tenantId); |
| | | if (ObjectUtil.isNull(tenant)) { |
| | | log.info("登录租户:{} 不存在.", tenantId); |