⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authdaoimpl.java

📁 Java开发的权限管理的例子
💻 JAVA
字号:
package org.artemis.manager.auth;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.acegisecurity.GrantedAuthority;
import org.artemis.manager.auth.cache.info.RoleByNameCache;
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.AuthDao;
import com.gsta.eshore.framework.auth.GrantedFunction;
import com.gsta.eshore.framework.auth.GrantedFunctionImpl;
import com.gsta.eshore.framework.auth.cache.FunctionCache;
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 AuthDaoImpl  implements AuthDao {
    private FunctionCache cache;
    private RoleByNameCache roleCache;
    
    
	public RoleByNameCache getRoleCache() {
		return roleCache;
	}

	public void setRoleCache(RoleByNameCache roleCache) {
		this.roleCache = roleCache;
	}

	public FunctionCache getCache() {
		return cache;
	}

	public void setCache(FunctionCache cache) {
		this.cache = cache;
	}
    /**
     * 得到角色的功能点
     */
	public Collection getFunctionsByRoles(Collection granted) {
		Set set = new HashSet();
		if(null == granted) throw new IllegalArgumentException("Granted Roles cannot be null");
        
		for(Iterator it = granted.iterator();it.hasNext();){
            
            GrantedAuthority grantedAuthority = (GrantedAuthority)it.next();
            //logger.debug("grantedAuthority = "+grantedAuthority.getAuthority());
            Role  role = roleCache.getRoleByRoleNameCache(grantedAuthority.getAuthority()); //
            if(role == null){
            	IState state=new StateImpl();
            	role = (Role)getgrantedAuthorityFromDataBase(grantedAuthority.getAuthority(),state);
            	if(!state.isOK()){
            		throw new ClientException("read roles from database error!",ErrorCode.DB_OPERATOR_ERROR);
            	}
            	roleCache.putRoleInCache(role);
            }
            GrantedFunction[] grantedFunctions = cache.getFunctionFromCache(role.getName());
            
            if(grantedFunctions == null){
            	
            	List functions = role.getFunctions();
    			for(Iterator it2 = functions.iterator();it2.hasNext();){	
                    Function function = (Function)it2.next();
                    GrantedFunction grantedFunction = new GrantedFunctionImpl(function.getName());
    				set.add(  grantedFunction  ); 
    			}
                 
    			grantedFunctions = (GrantedFunction[]) set.toArray(new GrantedFunction[0]);
    			cache.putFuncitonInCache(role.getName(),grantedFunctions);
            }
            
            for(int i = 0 ; i < grantedFunctions.length; i++){
            	 GrantedFunction grantedFunction = grantedFunctions[i];
            	 set.add(grantedFunction);
            }
		}
       
		return set;
	}

	/**
	 * 从数据库中查找该角色Role和所拥有的Function
	 * @param authority
	 * @param state
	 * @return
	 */
	private Role getgrantedAuthorityFromDataBase(String authority,IState state) {
		return getBiz().findgrantedAuthorityFromDataBase(authority, state);
	}
	
	public RightDelegateIF getBiz() {
		return ((RightDelegateIF)ContextServiceLocator.getInstance().getBean(Constants.BizDelegateWithOutMethodControl));
	}
	
	/* 直接构造,不用数据库,测试用。
	private Role getgrantedAuthorityFromDataBase(String authority,IState state) {
		logger.debug("authority = "+authority);
		List users =new ArrayList();
		User user=new User();
		
		List functions=new ArrayList();
		//构建一个Function
		Function fc=new Function();
		fc.setId("1");
		if(authority.endsWith("ROLE_USER")){
			user.setName("user");
			user.setPassword("user");
			fc.setName("func_user");
			fc.setProtectFunction("func_user");
		}else if(authority.endsWith("ROLE_SUPERVISOR")){
			user.setName("root");
			user.setPassword("root");
			fc.setName("com.gsta.bnet.bizservice.BizDelegateIF.findContactPersonList");
			fc.setProtectFunction("com.gsta.bnet.bizservice.BizDelegateIF.findContactPersonList");
		}
		users.add(user);
		functions.add(fc);
		Role role=new Role();
		role.setName(authority);
		role.setUsers(users);
		role.setFunctions(functions);
		role.setEnabled("1");
		role.setRoleType(authority);
		
		return role;
	}
   */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -