 package org.sky.system.handler;


 import jakarta.servlet.http.HttpServletRequest;
 import org.sky.core.secure.KdUser;
 import org.sky.core.secure.handler.IPermissionHandler;
 import org.sky.core.secure.utils.AuthUtil;
 import org.sky.core.tool.utils.WebUtil;

 import java.util.List;

 import static org.sky.system.cache.ApiScopeCache.*;

/**
 * 接口权限校验类
 *
  */
public class ApiScopePermissionHandler implements IPermissionHandler {

	@Override
	public boolean permissionAll() {
		HttpServletRequest request = WebUtil.getRequest();
		KdUser user = AuthUtil.getUser();
		if (request == null || user == null) {
			return false;
		}
		String uri = request.getRequestURI();
		List<String> paths = permissionPath(user.getRoleId());
		if (paths == null || paths.isEmpty()) {
			return false;
		}
		return paths.stream().anyMatch(uri::contains);
	}

	@Override
	public boolean hasPermission(String permission) {
		HttpServletRequest request = WebUtil.getRequest();
		KdUser user = AuthUtil.getUser();
		if (request == null || user == null) {
			return false;
		}
		List<String> codes = permissionCode(permission, user.getRoleId());
		return codes != null && !codes.isEmpty();
	}

	@Override
	public boolean hasMenu(String permission) {
		HttpServletRequest request = WebUtil.getRequest();
		KdUser user = AuthUtil.getUser();
		if (request == null || user == null) {
			return false;
		}
		List<String> codes = permissionMenu(permission, user.getRoleId());
		return codes != null && !codes.isEmpty();
	}

}
