📄 dbdrivenmethoddefinitionsource.java
字号:
package org.artemis.manager.auth;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.ConfigAttributeEditor;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.intercept.method.AbstractMethodDefinitionSource;
import org.springframework.util.Assert;
import org.artemis.manager.auth.cache.info.FunctionByNameCache;
import org.artemis.manager.common.Constants;
import org.artemis.right.RightDelegateIF;
import org.artemis.right.model.Function;
import org.artemis.right.model.Role;
import com.gsta.eshore.framework.auth.cache.AuthorityBasedFunctionCache;
import com.gsta.eshore.framework.exeception.ClientException;
import com.gsta.eshore.framework.jcf.ErrorCode;
import com.gsta.eshore.framework.jcf.IState;
import com.gsta.eshore.framework.jcf.StateImpl;
import com.gsta.eshore.framework.service.impl.ContextServiceLocator;
public class DBDrivenMethodDefinitionSource extends AbstractMethodDefinitionSource {
private AuthorityBasedFunctionCache cache;
private FunctionByNameCache functionCache;
public FunctionByNameCache getFunctionCache() {
return functionCache;
}
public void setFunctionCache(FunctionByNameCache functionCache) {
this.functionCache = functionCache;
}
/**
* 从数据库中查找名为protectFunction的Function以及拥有这个function的角色
*/
protected ConfigAttributeDefinition lookupAttributes(Method mi) {
Assert.notNull(mi,"lookupAttrubutes in the DBDrivenMethodDefinitionSource is null");
String secureObjectName=mi.getDeclaringClass().getName() +"."+ mi.getName();
//logger.debug("secureObjectName = "+secureObjectName);
Function secureObject=functionCache.getFunctionByCache(secureObjectName);
if(secureObject==null)//if secure object not exist in database
{
//logger.debug("Function is null ,get Function from Database");
IState state=new StateImpl();
//protectfunction是唯一的
secureObject=(Function)this.getFunctionfromDatabase(secureObjectName,state);
if(!state.isOK()){
throw new ClientException("read rights from database error!",ErrorCode.DB_OPERATOR_ERROR);
}
functionCache.putFunctionInCache(secureObject);
}
if(secureObject==null)
Assert.notNull(secureObject,"secureObject(Function) not found in db");
//retrieving roles associated with this secure object
Collection roles = null;
GrantedAuthority[] grantedAuthoritys = cache.getAuthorityFromCache(secureObject.getName());
if(grantedAuthoritys == null){
List rolesList = secureObject.getRoles();
Iterator it = rolesList.iterator();
List list = new ArrayList();
while(it.hasNext()){
Role role = (Role)it.next();
GrantedAuthority g = new GrantedAuthorityImpl(role.getName());
list.add(g);
}
grantedAuthoritys = (GrantedAuthority[])list.toArray(new GrantedAuthority[0]);
cache.putAuthorityInCache(secureObject.getName(),grantedAuthoritys);
roles = Arrays.asList(grantedAuthoritys);
}else{
roles = Arrays.asList(grantedAuthoritys);
}
if(!roles.isEmpty()){
ConfigAttributeEditor configAttrEditor=new ConfigAttributeEditor();
StringBuffer rolesStr=new StringBuffer();
for(Iterator it = roles.iterator();it.hasNext();){
GrantedAuthority role=(GrantedAuthority)it.next();
rolesStr.append(role.getAuthority()).append(",");
}
configAttrEditor.setAsText( rolesStr.toString().substring(0,rolesStr.length()-1) );
ConfigAttributeDefinition configAttrDef=(ConfigAttributeDefinition)configAttrEditor.getValue();
return configAttrDef;
}
Assert.notEmpty(roles,"collection of roles is null or empty");
return null;
}
/*
* 这个方法Function必须要有以下Role才可以继续执行下去
* 从数据库中查找名为protectFunction的Function以及拥有这个function的角色
*/
private Function getFunctionfromDatabase(String secureObjectName,IState state) {
return getBiz().findFunctionFromDataBase(secureObjectName, state);
}
public RightDelegateIF getBiz() {
return ((RightDelegateIF)ContextServiceLocator.getInstance().getBean(Constants.BizDelegateWithOutMethodControl));
}
/*
private Function getFunctionfromDatabase(String secureObjectName) {
//构建一个Function
Function fc=new Function();
fc.setId("1");
fc.setName("com.gsta.bnet.bizservice.BizDelegateIF.findContactPersonList");
fc.setProtectFunction("com.gsta.bnet.bizservice.BizDelegateIF.findContactPersonList");
List role = new ArrayList();
Role r = new Role();
r.setEnabled("1");
r.setName("ROLE_SUPERVISOR");
r.setRoleType("ROLE_SUPERVISOR");
Role r1 = new Role();
r1.setEnabled("1");
r1.setName("ROLE_USER");
r1.setRoleType("ROLE_USER");
role.add(r);
role.add(r1);
fc.setRoles(role);
return fc;
}
*/
public Iterator getConfigAttributeDefinitions() {
return null;
}
public AuthorityBasedFunctionCache getCache() {
return cache;
}
public void setCache(AuthorityBasedFunctionCache cache) {
this.cache = cache;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -