update 优化 重构 LoginHelper 将本地存储代码操作封装
This commit is contained in:
parent
40ec6584c5
commit
96ab48396c
@ -15,6 +15,7 @@ import org.dromara.common.core.domain.model.LoginUser;
|
|||||||
import org.dromara.common.core.enums.UserType;
|
import org.dromara.common.core.enums.UserType;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录鉴权助手
|
* 登录鉴权助手
|
||||||
@ -36,6 +37,7 @@ public class LoginHelper {
|
|||||||
public static final String USER_KEY = "userId";
|
public static final String USER_KEY = "userId";
|
||||||
public static final String DEPT_KEY = "deptId";
|
public static final String DEPT_KEY = "deptId";
|
||||||
public static final String CLIENT_KEY = "clientid";
|
public static final String CLIENT_KEY = "clientid";
|
||||||
|
public static final String TENANT_ADMIN_KEY = "isTenantAdmin";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录系统 基于 设备类型
|
* 登录系统 基于 设备类型
|
||||||
@ -62,17 +64,13 @@ public class LoginHelper {
|
|||||||
* 获取用户(多级缓存)
|
* 获取用户(多级缓存)
|
||||||
*/
|
*/
|
||||||
public static LoginUser getLoginUser() {
|
public static LoginUser getLoginUser() {
|
||||||
LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
|
return (LoginUser) getStorageIfAbsentSet(LOGIN_USER_KEY, () -> {
|
||||||
if (loginUser != null) {
|
SaSession session = StpUtil.getSession();
|
||||||
return loginUser;
|
if (ObjectUtil.isNull(session)) {
|
||||||
}
|
return null;
|
||||||
SaSession session = StpUtil.getSession();
|
}
|
||||||
if (ObjectUtil.isNull(session)) {
|
return session.get(LOGIN_USER_KEY);
|
||||||
return null;
|
});
|
||||||
}
|
|
||||||
loginUser = (LoginUser) session.get(LOGIN_USER_KEY);
|
|
||||||
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
|
|
||||||
return loginUser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,17 +107,7 @@ public class LoginHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Object getExtra(String key) {
|
private static Object getExtra(String key) {
|
||||||
Object obj;
|
return getStorageIfAbsentSet(key, () -> StpUtil.getExtra(key));
|
||||||
try {
|
|
||||||
obj = SaHolder.getStorage().get(key);
|
|
||||||
if (ObjectUtil.isNull(obj)) {
|
|
||||||
obj = StpUtil.getExtra(key);
|
|
||||||
SaHolder.getStorage().set(key, obj);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -162,7 +150,26 @@ public class LoginHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isTenantAdmin() {
|
public static boolean isTenantAdmin() {
|
||||||
return isTenantAdmin(getLoginUser().getRolePermission());
|
Object value = getStorageIfAbsentSet(TENANT_ADMIN_KEY, () -> {
|
||||||
|
return isTenantAdmin(getLoginUser().getRolePermission());
|
||||||
|
});
|
||||||
|
return Convert.toBool(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isLogin() {
|
||||||
|
return getLoginUser() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object getStorageIfAbsentSet(String key, Supplier<Object> handle) {
|
||||||
|
try {
|
||||||
|
Object obj = SaHolder.getStorage().get(key);
|
||||||
|
if (ObjectUtil.isNull(obj)) {
|
||||||
|
obj = handle.get();
|
||||||
|
SaHolder.getStorage().set(key, obj);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user