mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 10:29:19 +08:00
101 lines
3.0 KiB
Java
101 lines
3.0 KiB
Java
package top.xyquan.ecomdmbbackend.utils;
|
|
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.stereotype.Component;
|
|
import top.xyquan.ecomdmbbackend.modules.sys.permission.service.PermissionCheckService;
|
|
|
|
/**
|
|
* 权限控制工具类
|
|
* 提供静态方法进行权限检查
|
|
*/
|
|
@Component
|
|
public class PermissionUtils implements ApplicationContextAware {
|
|
|
|
private static ApplicationContext applicationContext;
|
|
|
|
@Override
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
PermissionUtils.applicationContext = applicationContext;
|
|
}
|
|
|
|
/**
|
|
* 检查当前用户是否有指定权限
|
|
*
|
|
* @param permissionCode 权限码
|
|
* @return 是否有权限
|
|
*/
|
|
public static boolean hasPermission(String permissionCode) {
|
|
try {
|
|
PermissionCheckService permissionCheckService = applicationContext.getBean(PermissionCheckService.class);
|
|
CurrentUserContext currentUserContext = applicationContext.getBean(CurrentUserContext.class);
|
|
|
|
String role = currentUserContext.getCurrentUserPayload().getRole();
|
|
Long positionId = currentUserContext.getCurrentUserPayload().getPositionId();
|
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
|
|
|
return permissionCheckService.hasPermissionByRole(role, permissionCode, positionId, companyId);
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查当前用户是否有指定角色
|
|
*
|
|
* @param role 角色
|
|
* @return 是否有角色
|
|
*/
|
|
public static boolean hasRole(String role) {
|
|
try {
|
|
CurrentUserContext currentUserContext = applicationContext.getBean(CurrentUserContext.class);
|
|
String currentRole = currentUserContext.getCurrentUserPayload().getRole();
|
|
return role.equals(currentRole);
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查当前用户是否是超级管理员
|
|
*
|
|
* @return 是否是超级管理员
|
|
*/
|
|
public static boolean isSuper() {
|
|
return hasRole("super");
|
|
}
|
|
|
|
/**
|
|
* 检查当前用户是否是公司管理员
|
|
*
|
|
* @return 是否是公司管理员
|
|
*/
|
|
public static boolean isCompany() {
|
|
return hasRole("company");
|
|
}
|
|
|
|
/**
|
|
* 检查当前用户是否是普通用户
|
|
*
|
|
* @return 是否是普通用户
|
|
*/
|
|
public static boolean isUser() {
|
|
return hasRole("user");
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户角色
|
|
*
|
|
* @return 用户角色
|
|
*/
|
|
public static String getCurrentRole() {
|
|
try {
|
|
CurrentUserContext currentUserContext = applicationContext.getBean(CurrentUserContext.class);
|
|
return currentUserContext.getCurrentUserPayload().getRole();
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|