CSDN博客

img blade_sea

权限组件之一(用户)

发表于2004/12/31 13:19:00  474人阅读

首先,我定义了一个用户接口,可以从其中取出其的各种属性.代码如后面所示.用户除了各种不同的属性以外还必须设置其角色以及所属的群组.

    然后定义一个AbstractUserUser共性的东西进行处理.所有的取属性的方法都已实现.用户只要根据实现情况继承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>

0 0

相关博文

我的热门文章

img
取 消
img即使是一小步
也想与你分享
打开
img