业务场景,用于查询某个用户在某个组织下有无特定的权限。
例如,打开菜单时进行权限校验、点击功能按钮时进行权限校验等。
import com.kingdee.bos.Context;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.base.permission.UserOrgPermFactory;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
/**
* 权限校验相关
* @author 作者
*
*/
public class CoreUtil {
/**
* 查询用户是否具有某个组织下的特定权限,用于前端
* @param userId 用户主键
* @param orgId 业务组织
* @param permItemId 权限主键
*
*/
public static boolean hasPerm(String userId, String orgId, String permItemId) {
boolean flag = false;
FilterInfo filter = new FilterInfo();
filter.getFilterItems().add(new FilterItemInfo("owner.id", userId));
filter.getFilterItems().add(new FilterItemInfo("org.id", orgId));
filter.getFilterItems().add(new FilterItemInfo("permItem.id", permItemId));
try {
flag = UserOrgPermFactory.getRemoteInstance().exists(filter);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 查询用户是否具有某个组织下的特定权限,用于前端
* @param user 用户
* @param orgId 业务组织
* @param permItemId 权限主键
*
*/
public static boolean hasPerm(UserInfo user, BOSUuid orgId, String permItemId){
boolean flag = false;
FilterInfo filter = new FilterInfo();
filter.getFilterItems().add(new FilterItemInfo("owner.id", user.getId()));
filter.getFilterItems().add(new FilterItemInfo("org.id", orgId));
filter.getFilterItems().add(new FilterItemInfo("permItem.id", permItemId));
try {
flag = UserOrgPermFactory.getRemoteInstance().exists(filter);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 查询用户是否具有某个组织下的特定权限,用于后台
* @param ctx 上下文
* @param userId 用户主键
* @param orgId 业务组织
* @param permItemId 权限主键
*
*/
public static boolean hasPerm(Context ctx, String userId, String orgId, String permItemId){
boolean flag = false;
FilterInfo filter = new FilterInfo();
filter.getFilterItems().add(new FilterItemInfo("owner.id", userId));
filter.getFilterItems().add(new FilterItemInfo("org.id", orgId));
filter.getFilterItems().add(new FilterItemInfo("permItem.id", permItemId));
try {
flag = UserOrgPermFactory.getLocalInstance(ctx).exists(filter);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 查询用户是否具有某个组织下的特定权限,用于后台
* @param ctx 上下文
* @param user 用户
* @param orgId 业务组织
* @param permItemId 权限主键
*
*/
public static boolean hasPerm(Context ctx, UserInfo user, BOSUuid orgId, String permItemId) {
boolean flag = false;
FilterInfo filter = new FilterInfo();
filter.getFilterItems().add(new FilterItemInfo("owner.id", user.getId()));
filter.getFilterItems().add(new FilterItemInfo("org.id", orgId));
filter.getFilterItems().add(new FilterItemInfo("permItem.id", permItemId));
try {
flag = UserOrgPermFactory.getLocalInstance(ctx).exists(filter);
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}