日期:2014-05-18 浏览次数:20777 次
package com.zjlolife.sysIm.util.interceptor;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.zjlolife.sysIm.role.domain.Function;
import com.zjlolife.sysIm.userManager.domain.User;
import com.zjlolife.sysIm.util.ActionPrivilege;
import com.zjlolife.sysIm.util.ActionPrivilegesReader;
import com.zjlolife.sysIm.util.BeanFactory;
import com.zjlolife.sysIm.util.privilege.service.PrivilegeService;
public class CheckPrivilegeInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
User user = (User) ServletActionContext.getRequest().getSession()
.getAttribute("loginUser");
ActionContext context = invocation.getInvocationContext();
String actionName = context.getName();
if(actionName.equals("loginPro")) {
return invocation.invoke();
}
//读取配置文件XML该action(请求)的对应的权限值
List<ActionPrivilege> actionPrivileges = ActionPrivilegesReader
.getInstance().getPrivilegesList();
PrivilegeService privilegeService = (PrivilegeService) BeanFactory
.getInstance().getBean(PrivilegeService.class);
//获取数据库每个用户对应的权限
List<Function> functions = privilegeService.getFunctions(user.getId());
String functionId = null;
for (ActionPrivilege actionPrivilege : actionPrivileges) {
if (actionPrivilege.getActionName().equals(actionName)) {
functionId = actionPrivilege.getValue();
break;
}
}
if(functionId==null) {
return invocation.invoke();
}
Function function = new Function();
function.setFunctionId(functionId);
if (functions.contains(function)) {
return invocation.invoke();
} else {
return "checkFailed";
}
}
}