| | |
| | | package com.ruoyi.common.satoken.utils; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.context.model.SaStorage; |
| | | import cn.dev33.satoken.stp.SaLoginModel; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.ruoyi.common.core.constant.TenantConstants; |
| | | import com.ruoyi.common.core.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.core.enums.DeviceType; |
| | | import com.ruoyi.common.core.enums.UserType; |
| | | import com.ruoyi.common.core.exception.UtilException; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * 登录鉴权助手 |
| | |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class LoginHelper { |
| | | |
| | | public static final String JOIN_CODE = ":"; |
| | | public static final String LOGIN_USER_KEY = "loginUser"; |
| | | public static final String TENANT_KEY = "tenantId"; |
| | | public static final String USER_KEY = "userId"; |
| | | |
| | | /** |
| | | * 登录系统 |
| | |
| | | * @param loginUser 登录用户信息 |
| | | */ |
| | | public static void login(LoginUser loginUser) { |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | StpUtil.login(loginUser.getLoginId()); |
| | | setLoginUser(loginUser); |
| | | loginByDevice(loginUser, null); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param loginUser 登录用户信息 |
| | | */ |
| | | public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) { |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | StpUtil.login(loginUser.getLoginId(), deviceType.getDevice()); |
| | | setLoginUser(loginUser); |
| | | } |
| | | |
| | | /** |
| | | * 设置用户数据(多级缓存) |
| | | */ |
| | | public static void setLoginUser(LoginUser loginUser) { |
| | | SaStorage storage = SaHolder.getStorage(); |
| | | storage.set(LOGIN_USER_KEY, loginUser); |
| | | storage.set(TENANT_KEY, loginUser.getTenantId()); |
| | | storage.set(USER_KEY, loginUser.getUserId()); |
| | | SaLoginModel model = new SaLoginModel(); |
| | | if (ObjectUtil.isNotNull(deviceType)) { |
| | | model.setDevice(deviceType.getDevice()); |
| | | } |
| | | StpUtil.login(loginUser.getLoginId(), |
| | | model.setExtra(TENANT_KEY, loginUser.getTenantId()) |
| | | .setExtra(USER_KEY, loginUser.getUserId())); |
| | | StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取用户基于token |
| | | */ |
| | | public static LoginUser getLoginUser(String token) { |
| | | return (LoginUser) StpUtil.getTokenSessionByToken(token).get(LOGIN_USER_KEY); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户id |
| | | */ |
| | | public static Long getUserId() { |
| | | LoginUser loginUser = getLoginUser(); |
| | | if (ObjectUtil.isNull(loginUser)) { |
| | | String loginId = StpUtil.getLoginIdAsString(); |
| | | String userId = null; |
| | | for (UserType value : UserType.values()) { |
| | | if (StringUtils.contains(loginId, value.getUserType())) { |
| | | String[] strs = StringUtils.split(loginId, JOIN_CODE); |
| | | // 用户id在总是在最后 |
| | | userId = strs[strs.length - 1]; |
| | | } |
| | | Long userId; |
| | | try { |
| | | userId = Convert.toLong(SaHolder.getStorage().get(USER_KEY)); |
| | | if (ObjectUtil.isNull(userId)) { |
| | | userId = Convert.toLong(StpUtil.getExtra(USER_KEY)); |
| | | SaHolder.getStorage().set(USER_KEY, userId); |
| | | } |
| | | if (StringUtils.isBlank(userId)) { |
| | | throw new UtilException("登录用户: LoginId异常 => " + loginId); |
| | | } |
| | | return Long.parseLong(userId); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return loginUser.getUserId(); |
| | | return userId; |
| | | } |
| | | |
| | | /** |
| | | * 获取租户ID |
| | | */ |
| | | public static String getTenantId() { |
| | | String tenantId; |
| | | try { |
| | | tenantId =(String) SaHolder.getStorage().get(TENANT_KEY); |
| | | if (ObjectUtil.isNull(tenantId)) { |
| | | tenantId = (String) StpUtil.getExtra(TENANT_KEY); |
| | | SaHolder.getStorage().set(TENANT_KEY, tenantId); |
| | | } |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return tenantId; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 是否为管理员 |
| | | * 是否为超级管理员 |
| | | * |
| | | * @param userId 用户ID |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isAdmin(Long userId) { |
| | | public static boolean isSuperAdmin(Long userId) { |
| | | return UserConstants.SUPER_ADMIN_ID.equals(userId); |
| | | } |
| | | |
| | | public static boolean isAdmin() { |
| | | return isAdmin(getUserId()); |
| | | public static boolean isSuperAdmin() { |
| | | return isSuperAdmin(getUserId()); |
| | | } |
| | | |
| | | /** |
| | | * 是否为超级管理员 |
| | | * |
| | | * @param rolePermission 角色权限标识组 |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isTenantAdmin(Set<String> rolePermission) { |
| | | return rolePermission.contains(TenantConstants.TENANT_ADMIN_ROLE_KEY); |
| | | } |
| | | |
| | | public static boolean isTenantAdmin() { |
| | | return isTenantAdmin(getLoginUser().getRolePermission()); |
| | | } |
| | | |
| | | } |