权限组件学习 转载于fswan的文章
转载于http://blog.csdn.net/fswan/archive/2004/11/02/164063.aspx **学习使用 ** 首先,设置三种要素:用户、群组、角色。用户为登录用,对应到人。群组对应为用户的集合,是一种特殊的用户。角色为一组权限项的集合,用户(群组)都有各自的角色。 权限的实现通过Permission类和Rule类来实现。Permission供外部调用,Rule为一个接口,为权限判断规则。 Permission是一个抽象类,有以下方法
public boolean hasPermission(User user,HashMap oldData,Input input);
public String getPermissionName();
public abstract Rule[] getDenyRule();
public abstract Rule[] getAcceptRule();
hasPermission方法供外部调用,已实现,实现方法为先根据getDenyRule()得到的规则判断权限是否被阻拦,再根据getAcceptRule来判断是否有权限。 而Rule接口的接品则由用户自行定义,随包附带了一个已实现的Rule,实现的功能如下: 先寻找User的所有角色,然后判断角色是否有权限,如果无权限则寻找其父级群组,再取父级群组的所有角色进行判断是否有权限,如果无权限则再往上级群组找,直到找最上一级还是无权限才判断为无权限。 现实现判断权限有无权限的方式已可以达成的有以下三种: 1、 是否有操作的权限。 2、 是否有操作的子操作的权限。 3、 在数据为某条件时有操作(子操作)的权限。
在进行程序开发时,第一步,编写User,Group,Role的实现类,已提供了一套XML的实现类。 第二步,写配置文件,进行权限项的配置。 第三步,在程序中要进行权限判断的地方调用Permission.hasPermission方法即可. 首先,我定义了一个用户接口,可以从其中取出其的各种属性.代码如后面所示.用户除了各种不同的属性以外还必须设置其角色以及所属的群组. 然后定义一个AbstractUser把User共性的东西进行处理.所有的取属性的方法都已实现.用户只要根据实现情况继承AbstractUser把自己要定义的属性进行处理即可.(因为每个系统的用户都会有不同的属性,所以留成抽象类供使用者自己扩展). 只要初始化变量description, name ,group ,id, prop,role,propMap即可. 最后定义了一个类UserImpl来实现具体的User作随包的一个User供一般使用者使用.不建议直接使用UserImpl,因为以后的版本可能会重写该类.如果要使用的话可以把该类改名为自己的类即可.
这部分涉及到了一个ResourceLib类,该类中存是我自己定义的存储各种资源配置用的类,可以直接使用.
======================User.java===============================
package org.fswan.permission;
import org.fswan.Identity;
import java.util.Properties;
import org.fswan.workflow.exception.CantFoundIDException;
public interface User
{
/**
* 获取用户名
* @return 用户名
*/
public String getName();
/**
* 获取用户描述
* @return 描述
*/
public String getDescription();
/**
* 获取用户标识
* @return 用户标识
*/
public Identity getId();
/**
* 获取属性
* @return 属性值
*/
public Properties getProperties();
/**
* 获取所属组
* @return 组列表
*/
public Group[] getGroups() throws CantFoundIDException;
/**
* 获取所有角色
* @return 角色数组
*/
public Role[] getRoles() throws CantFoundIDException;
/**
* 获取用户用作权限判断的属性
* @param prop 权限属性
* @return 属性值
*/
public Object getPermissionProp(String prop);
}
=======================AbstractUser===================================
package org.fswan.permission;
import java.util.HashMap;
import java.util.Properties;
import org.fswan.Identity;
import org.fswan.workflow.exception.IdentityMappingError;
/**
*
* 实现AbstractUser的类构造必须做的一些事
* 初始化变量description,name,group,id,prop,role,propMap
*/
public class AbstractUser implements User
{
/**
* 描述
*/
protected String description;
/**
* 名称
*/
protected String name;
/**
* 所属的组的标识
*/
protected Identity[] group;
/**
* 用户标识
*/
protected Identity id;
/**
* 属性
*/
protected Properties prop;
/**
* 角色
*/
protected Identity[] role;
/**
* 权限属性存储的位置
*/
protected HashMap propMap;
/* (non-Javadoc)
* @see org.fswan.permission.User#getDescription()
*/
public String getDescription()
{
return description;
}
/* (non-Javadoc)
* @see org.fswan.permission.User#getGroups()
*/
public Group[] getGroups()
{
Group[] groups = new Group[group.length];
for (int i = 0; i < groups.length; i++)
{
try
{
groups[i] = (Group) group[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return groups;
}
/* (non-Javadoc)
* @see org.fswan.permission.User#getId()
*/
public Identity getId()
{
return id;
}
/* (non-Javadoc)
* @see org.fswan.permission.User#getName()
*/
public String getName()
{
return name;
}
/* (non-Javadoc)
* @see org.fswan.permission.User#getProperties()
*/
public Properties getProperties()
{
return prop;
}
/* (non-Javadoc)
* @see org.fswan.permission.User#getRoles()
*/
public Role[] getRoles()
{
Role[] roles = new Role[role.length];
for (int i = 0; i < roles.length; i++)
{
try
{
roles[i] = (Role) role[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return roles;
}
public String toString()
{
String retStr = id.getIdName();
retStr += ":" + name;
retStr += " " + description + "\n";
retStr += "Group:";
Group[] groups = this.getGroups();
if (groups != null)
{
for (int i = 0; i < groups.length; i++)
{
if (groups[i] != null)
retStr += groups[i].getId().getIdName() + "\t";
}
}
Properties p = getProperties();
Object[] obj = p.keySet().toArray();
for (int i = 0; i < obj.length; i++)
{
retStr+=obj[i]+":"+p.getProperty(obj[i].toString())+"\n";
}
return retStr;
}
/* (non-Javadoc)
* @see org.fswan.permission.User#getPermissionProp(java.lang.String)
*/
public Object getPermissionProp(String prop)
{
return propMap.get(prop);
}
}
==============================UserImpl.java===========================
package org.fswan.permission;
import java.util.ArrayList;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilderFactory;
import org.fswan.Identity;
import org.fswan.IdentityImpl;
import org.fswan.ImplementIdentity;
import org.fswan.ResourceLib;
import org.fswan.workflow.exception.IdentityMappingError;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XMLUser extends AbstractUser implements ImplementIdentity
{
/**
* XML中定义用户的标签
*/
public static final String USER = "User";
public Object newInstance(Identity id)
{
ArrayList sources = ResourceLib.getXmlResource();
for (int i = 0; i < sources.size(); i++)
{
try
{
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(sources.get(i).toString());
NodeList nl = doc.getElementsByTagName(USER);
for (int j = 0; j < nl.getLength(); j++)
{
Element tempEl = (Element) nl.item(j);
String idStr = tempEl.getAttribute("id");
if (idStr != null && !idStr.equals(""))
{
if (idStr.equals(id.getIdName()))
{
this.id = new IdentityImpl(Identity.SWITCH, Identity.XMLUSER, idStr);
NodeList tempNl = tempEl.getElementsByTagName("Name");
name = tempNl.item(0).getChildNodes().item(0).getNodeValue();
tempNl = tempEl.getElementsByTagName("Description");
description = tempNl.item(0).getChildNodes().item(0).getNodeValue();
tempNl = tempEl.getElementsByTagName("ParentGroup");
Identity[] groups = new Identity[tempnl.getlength()];
for (int iGroup = 0; iGroup < groups.length; iGroup++)
{
Element tempEl2 = (Element) tempNl.item(iGroup);
String subType = tempEl.getAttribute("subType");
if(subType==null || subType.equals(""))subType = Identity.XMLGROUP;
groups[igroup] = new IdentityImpl(Identity.GROUP, subType, tempEl2.getAttribute("id"));
}
this.group = groups;
tempNl = tempEl.getElementsByTagName("Role");
Identity[] roles = new Identity[tempnl.getlength()];
for (int iRole = 0; iRole < tempNl.getLength(); iRole++)
{
Element tempEl2 = (Element) tempNl.item(iRole);
String subType = tempEl.getAttribute("subType");
if(subType==null || subType.equals(""))subType = Identity.XMLROLE;
roles[irole] = new IdentityImpl(Identity.ROLE, subType, tempEl2.getAttribute("id"));
}
this.role = roles;
this.prop = new Properties();
tempNl = tempEl.getElementsByTagName("Property");
if (tempNl != null)
for (int k = 0; k < tempNl.getLength(); k++)
{
Element tempElement = (Element) tempNl.item(k);
System.out.println(tempElement.getAttribute("name"));
this.prop.setProperty(tempElement.getAttribute("name"), tempElement.getAttribute("value"));
}
return this;
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
public static void main(String[] args)
{
try
{
ResourceLib.addResource("D:\\eclipse\\workspace\\workflow\\workflow.xml");
User u = (User) new IdentityImpl(Identity.USER, Identity.XMLUSER, "U01").newInstance();
System.out.println(u);
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
}
========================XML格式=====================
<User id="U01">
<Name>Swan</Name>
<Description>方志文</Description>
<ParentGroup id="G01"/>
<Property name="SEX" value="male"/>
<Property name="AGE" value="20"/>
</User>
========================XML描述文件==================
<xs:complexType name="User">
<xs:annotation>
<xs:documentation>用户名</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Name"/>
<xs:element name="Description"/>
<xs:element name="ParentGroup" type="Identity" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="Role" type="Identity" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="Property" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" use="required"/>
<xs:attribute name="value" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" use="required"/>
</xs:complexType>
权限组件之二(群组)收藏 首先,我定义了一个群组接口,除了继承用户的方法以外还有两个方法,getUsers,getSubGroup.代码如后面所示.用户除了各种不同的属性以外还必须设置其角色以及所属的群组.
然后定义一个AbstractGroup,他继承了Group以及AbstractUser,并实现了Group接口定义的两个方法.用户只要根据实现情况继承AbstractGroup把自己要定义的属性进行处理即可.(因为每个系统的用户都会有不同的属性,所以留成抽象类供使用者自己扩展). 只要初始化变量description, name ,group ,id, prop,role,propMap,subGroup,user即可.
最后定义了一个类XMLGroup来实现具体的Group作随包的一个Group供一般使用者使用.不建议直接使用XMLGroup,因为以后的版本可能会重写该类.如果要使用的话可以把该类改名为自己的类即可.
这部分涉及到了一个ResourceLib类,该类中存是我自己定义的存储各种资源配置用的类,可以直接使用.
======================Group.java===============================
package org.fswan.permission;
public interface Group extends User
{
/**
* 获取组下所有的用户
* @return 用户数组
*/
public User[] getUsers();
/**
* 获取组下属的组
* @return 组的数组
*/
public Group[] getSubGroup();
}
=======================AbstractGroup==================================
package org.fswan.permission;
import org.fswan.Identity;
import org.fswan.workflow.exception.IdentityMappingError;
public class AbstractGroup extends AbstractUser implements Group
{
protected Identity[] subGroup;
protected Identity[] user;
/* (non-Javadoc)
* @see org.fswan.permission.Group#getSubGroup()
*/
public Group[] getSubGroup()
{
Group[] groups = new Group[subgroup.length];
for (int i = 0; i < groups.length; i++)
{
try
{
groups[i] = (Group)subGroup[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return groups;
}
/* (non-Javadoc)
* @see org.fswan.permission.Group#getUsers()
*/
public User[] getUsers()
{
User[] users = new User[user.length];
for (int i = 0; i < users.length; i++)
{
try
{
users[i] = (User)user[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return users;
}
public String toString()
{
String retStr = id.getIdName();
retStr +=":"+name;
retStr += " "+description;
return retStr;
}
}
==============================XMLGroup.java===========================
package org.fswan.permission;
import java.util.ArrayList;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilderFactory;
import org.fswan.Identity;
import org.fswan.IdentityImpl;
import org.fswan.ImplementIdentity;
import org.fswan.ResourceLib;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XMLGroup extends AbstractGroup implements ImplementIdentity
{
/**
* XML中定义群组的标签
*/
public static final String GROUP = "Group";
public Object newInstance(Identity id)
{
ArrayList sources = ResourceLib.getXmlResource();
for (int i = 0; i < sources.size(); i++)
{
try
{
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(sources.get(i).toString());
NodeList nl = doc.getElementsByTagName(GROUP);
for (int j = 0; j < nl.getLength(); j++)
{
Element tempEl = (Element)nl.item(j);
String idStr = tempEl.getAttribute("id");
if(idStr!=null && !idStr.equals(""))
{
if(idStr.equals(id.getIdName()))
{
this.id = new IdentityImpl(Identity.SWITCH,Identity.XMLGROUP,idStr);
//加载名称
NodeList tempNl = tempEl.getElementsByTagName("Name");
name = tempNl.item(0).getChildNodes().item(0).getNodeValue();
//加载描述
tempNl = tempEl.getElementsByTagName("Description");
description = tempNl.item(0).getChildNodes().item(0).getNodeValue();
//加载父群组
tempNl = tempEl.getElementsByTagName("ParentGroup");
Identity[] groups = new Identity[tempnl.getlength()];
for (int iGroup=0; iGroup < tempNl.getLength(); iGroup++)
{
tempEl = (Element)tempNl.item(iGroup);
groups[igroup] = new IdentityImpl(Identity.GROUP,Identity.XMLGROUP,tempEl.getAttribute("id"));
}
this.subGroup = groups;
//加载角色
tempNl = tempEl.getElementsByTagName("Role");
Identity[] roles = new Identity[tempnl.getlength()];
for (int iRole=0; iRole < tempNl.getLength(); iRole++)
{
tempEl = (Element)tempNl.item(iRole);
roles[irole] = new IdentityImpl(Identity.ROLE,Identity.XMLROLE,tempEl.getAttribute("id"));
}
this.role = roles;
//加载属性
this.prop = new Properties();
tempNl = tempEl.getElementsByTagName("Property");
if(tempNl!=null)
for (int k = 0; k < tempNl.getLength(); k++)
{
Element tempElement = (Element)tempNl.item(k);
this.prop.setProperty(tempElement.getAttribute("name"),tempElement.getAttribute("value"));
}
//加载子群组
tempNl = tempEl.getElementsByTagName("SubGroup");
if (tempNl != null)
{
subGroup = new Identity[tempnl.getlength()];
for (int k = 0; k < subGroup.length; k++)
{
Element tempElement = (Element) tempNl.item(k);
subGroup[k] = new IdentityImpl(Identity.GROUP,Identity.XMLGROUP,tempElement.getAttribute("id"));
}
}
//加载用户
tempNl = tempEl.getElementsByTagName("User");
if (tempNl != null)
{
user = new Identity[tempnl.getlength()];
for (int k = 0; k < subGroup.length; k++)
{
Element tempElement = (Element) tempNl.item(k);
user[k] = new IdentityImpl(Identity.USER,Identity.XMLUSER,tempElement.getAttribute("id"));
}
}
return this;
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
}
========================XML格式=====================
<Group id="G01">
<Name>系统部</Name>
<Description>系统部</Description>
</Group>
========================XML描述文件==================
<xs:complexType name="Group">
<xs:annotation>
<xs:documentation>群组</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="User">
<xs:sequence>
<xs:element name="SubGroup" type="Identity" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="User" type="Identity" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
首先,我定义了一个角色接口,可以从其中取出其的各种属性.代码如后面所示.
然后定义一个AbstractRole把Role共性的东西进行处理.所有的取属性的方法都已实现.用户只要根据实现情况继承AbstractRole把自己要定义的属性进行处理即可.(因为每个系统的用户都会有不同的属性,所以留成抽象类供使用者自己扩展). 只要初始化变量description, name ,id, prop,users(可能是群组), permissionMap即可.
最后定义了一个类XMLRole来实现具体的Role作随包的一个Role供一般使用者使用.不建议直接使用XMLRole,因为以后的版本可能会重写该类.如果要使用的话可以把该类改名为自己的类即可.
这部分涉及到了一个ResourceLib类,该类中存是我自己定义的存储各种资源配置用的类,可以直接使用.
======================Role.java===============================
package org.fswan.permission;
import org.fswan.Identity;
import java.util.Properties;
import org.fswan.workflow.exception.CantFoundIDException;
public interface Role
{
/**
* 获取角色ID
* @return
*/
public Identity getId();
/**
* 获取角色的名称
* @return 名称
*/
public String getName();
/**
* 获取角色的描述
* @return 描述
*/
public String getDescription();
/**
* 获取角色的所有的用户(组)
* @return 用户
*/
public User[] getUsers() throws CantFoundIDException;
/**
* 角色的属性
* @return 属性值
*/
public Properties getProperties();
/**
* 获取用户用作权限判断的属性
* @param prop 权限属性
* @return 属性值
*/
public Object getPermissionProp(String prop);
}
=======================AbstractRole===================================
package org.fswan.permission;
import java.util.HashMap;
import java.util.Properties;
import org.fswan.Identity;
import org.fswan.workflow.exception.IdentityMappingError;
public class AbstractRole implements Role
{
/**
* 描述
*/
protected String description ;
/**
* 名称
*/
protected String name;
/**
* 标识
*/
protected Identity id;
/**
* 属性
*/
protected Properties prop;
/**
* 该角色所有用户的标识
*/
protected Identity[] users;
/**
* 权限属性表
*/
protected HashMap permissionMap;
/* (non-Javadoc)
* @see org.fswan.permission.Role#getDescription()
*/
public String getDescription()
{
return description;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getId()
*/
public Identity getId()
{
return id;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getName()
*/
public String getName()
{
return name;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getProperties()
*/
public Properties getProperties()
{
return prop;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getUsers()
*/
public User[] getUsers()
{
User[] retUser = new User[users.length];
for (int i = 0; i < retUser.length; i++)
{
try
{
retUser[i] = (User)users[i].newInstance();
} catch (IdentityMappingError e)
{
e.printStackTrace();
}
}
return retUser;
}
public String toString()
{
String retStr = id.getIdName();
retStr +=":"+name;
retStr += " "+description;
return retStr;
}
/* (non-Javadoc)
* @see org.fswan.permission.Role#getPermissionProp(java.lang.String)
*/
public Object getPermissionProp(String prop)
{
return permissionMap.get(prop);
}
}
==============================XMLRole.java===========================
/*
* Created on 2004-4-21
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.fswan.permission;
import java.util.ArrayList;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilderFactory;
import org.fswan.Identity;
import org.fswan.IdentityImpl;
import org.fswan.ImplementIdentity;
import org.fswan.ResourceLib;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XMLRole extends AbstractRole implements ImplementIdentity
{
/**
* XML标签用的字符串
*/
public static final String ROLE = "Role";
public Object newInstance(Identity id)
{
ArrayList sources = ResourceLib.getXmlResource();
for (int i = 0; i < sources.size(); i++)
{
try
{
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(sources.get(i).toString());
NodeList nl = doc.getElementsByTagName(ROLE);
for (int j = 0; j < nl.getLength(); j++)
{
Element tempEl = (Element)nl.item(j);
String idStr = tempEl.getAttribute("id");
if(idStr!=null && !idStr.equals(""))
{
if(idStr.equals(id.getIdName()))
{
this.id = new IdentityImpl(Identity.SWITCH,Identity.XMLROLE,idStr);
NodeList tempNl = tempEl.getElementsByTagName("Name");
name = tempNl.item(0).getChildNodes().item(0).getNodeValue();
tempNl = tempEl.getElementsByTagName("Description");
description = tempNl.item(0).getChildNodes().item(0).getNodeValue();
this.prop = new Properties();
tempNl = tempEl.getElementsByTagName("Property");
if(tempNl!=null)
for (int k = 0; k < tempNl.getLength(); k++)
{
Element tempElement = (Element)tempNl.item(k);
this.prop.setProperty(tempElement.getAttribute("name"),tempElement.getAttribute("value"));
}
return this;
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
}
========================XML格式=====================
<Role id="R01">
<Name>系统管理员</Name>
<Description>系统管理员</Description>
</Role>
========================XML描述文件==================
<xs:complexType name="Role">
<xs:annotation>
<xs:documentation>角色名</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Name"/>
<xs:element name="Description"/>
<xs:element name="Property" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" use="required"/>
</xs:complexType>