| | |
| | | package com.ruoyi.common.satoken.utils; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.stp.SaLoginModel; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.json.JSONObject; |
| | | 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 lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * 登录鉴权助手 |
| | | * |
| | | * <p> |
| | | * user_type 为 用户类型 同一个用户表 可以有多种用户类型 例如 pc,app |
| | | * deivce 为 设备类型 同一个用户类型 可以有 多种设备类型 例如 web,ios |
| | | * 可以组成 用户类型与设备类型多对多的 权限灵活控制 |
| | | * |
| | | * <p> |
| | | * 多用户体系 针对 多种用户类型 但权限控制不一致 |
| | | * 可以组成 多用户类型表与多设备类型 分别控制权限 |
| | | * |
| | |
| | | */ |
| | | public static void login(LoginUser loginUser) { |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | StpUtil.login(loginUser.getLoginId()); |
| | | setLoginUser(loginUser); |
| | | StpUtil.login(loginUser.getLoginId(), new SaLoginModel().setExtra(LOGIN_USER_KEY, 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) { |
| | | StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser); |
| | | StpUtil.login(loginUser.getLoginId(), new SaLoginModel() |
| | | .setDevice(deviceType.getDevice()) |
| | | .setExtra(LOGIN_USER_KEY, loginUser)); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (loginUser != null) { |
| | | return loginUser; |
| | | } |
| | | loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY); |
| | | loginUser = ((JSONObject) StpUtil.getExtra(LOGIN_USER_KEY)).toBean(LoginUser.class); |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | return loginUser; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取租户ID |
| | | */ |
| | | public static String getTenantId() { |
| | | LoginUser loginUser; |
| | | try { |
| | | loginUser = getLoginUser(); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return loginUser.getTenantId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取部门ID |
| | | */ |
| | | public static Long getDeptId() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 是否为管理员 |
| | | * 是否为超级管理员 |
| | | * |
| | | * @param userId 用户ID |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isAdmin(Long userId) { |
| | | return UserConstants.ADMIN_ID.equals(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()); |
| | | } |
| | | |
| | | } |