MIS系统的权限设计

http://www.hahasoft.cn/Blog/article.asp?id=17

大部分MIS系统中,都存在多用户权限系统,下面给出权限设计的思路

例如现在系统的功能如下:
人员管理,数据备份,公文管理(公文管理里面又按类别分为 添加,删除,评论)

此时我们可以利用 序列化来写一个权限类,然后把它序列化成xml字符串,存储在用户数据库的一个字段里面.
为了实现"树型权限", PermissionItemNews 实现了 ICollection 接口

代码如下:

using System;
using System.IO;
using System.Collections;
using System.Xml.Serialization;
using System.Web.UI ;
using System.ComponentModel ;
using System.Web.Caching ;

namespace HSSite.Util
{
 public class Permission
 {

  public static Permission Get(int UserID)
  {
   Permission p = null ;
   string sql = "Select Permission From Account Where ID=" + UserID.ToString() ;
   string strPm = string.Empty ;
   System.Data.DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.ConnectionString ,System.Data.CommandType.Text ,sql ).Tables[0] ;
   if (dt.Rows.Count > 0)
   {
    strPm = Convert.ToString(dt.Rows[0]["Permission"]) ;
    if (strPm.Trim()==string.Empty )
    {
     p = new Permission() ;
     p.UserID = UserID ;
     p.SaveToDatabase() ;
    }
    else
    {
     System.IO.StringReader r = new StringReader(strPm) ;
     System.Xml.Serialization.XmlSerializer xml = new XmlSerializer(typeof(Permission)) ;
     p = (Permission) xml.Deserialize(r) ;
     r.Close() ;
    }
   }

   return p ;
  }

  // 从缓存中读取权限 . 缓存 Key : UserPermission23
  public static Permission GetFromCache(System.Web.HttpCookieCollection cookie)
  {
   try
   {
    int UserID = Int32.Parse(cookie["UserID"].Value) ;

    if ( System.Web.HttpRuntime.Cache["UserPermission" + UserID.ToString()] == null )
    {
     Permission p = null ;
     p = Permission.Get(UserID) ;
     if ( p!=null)
      System.Web.HttpRuntime.Cache.Insert("UserPermission" + UserID.ToString() , p , null , DateTime.Now.AddMinutes(20), TimeSpan.Zero) ;
     return p ;
    }
    else
     return (Permission)System.Web.HttpRuntime.Cache["UserPermission" + UserID.ToString()] ;
   }
   catch ( Exception ex)
   {
    throw ex  ;
   }
  }

  public int UserID = 0 ;

  public bool PersonManage = false ;
  public bool Backup = false ;
  public PermissionItemNews News = new PermissionItemNews() ;

  public void SaveToDatabase()
  {
   System.Text.StringBuilder sb = new System.Text.StringBuilder() ;
   System.IO.StringWriter w = new StringWriter(sb) ;

   // 如果权限改变则清除缓存
   System.Web.HttpRuntime.Cache.Remove("UserPermission" + this.UserID.ToString()) ;

   try
   {
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Permission)) ;
    x.Serialize(w,this) ;

    string sql = "Update Account Set Permission='" + sb.ToString() + "' Where ID=" + this.UserID.ToString() ;
    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString ,System.Data.CommandType.Text ,sql ) ;
   }
   catch(Exception ex)
   {
    throw ex ;
   }
   finally
   {
    w.Close() ;
   }
  }
 }

 public class PermissionItemNews:System.Collections.ICollection
 {

  private ArrayList al = new ArrayList();

  public PermissionItemNewsItem this[int index]
  {
   get{return (PermissionItemNewsItem) al[index];}
  }

  public bool Exists(int CategoryID)
  {
   if ( this[CategoryID.ToString()] == null )
    return false ;
   else
    return true ;
  }

  public PermissionItemNewsItem this[string CategoryID]
  {
   get
   {
    PermissionItemNewsItem tn = null ;
    foreach (PermissionItemNewsItem n in al)
    {
     if (n!=null)
      if (Convert.ToInt32(n.CategoryID) == Convert.ToInt32(CategoryID) )
      {
       tn = n ;
       break ;
      }
    }
    return tn ;
   }
  }
   
  public void CopyTo(Array a, int index)
  {
   al.CopyTo(a, index);
  }
  public int Count
  {
   get{return al.Count;}
  }
  public object SyncRoot
  {
   get{return this;}
  }
  public bool IsSynchronized
  {
   get{return false;}
  }
  public IEnumerator GetEnumerator()
  {
   return al.GetEnumerator();
  }

  public void Add(PermissionItemNewsItem n)
  {
   al.Add(n);
  }

  public void Clear()
  {
   al.Clear() ;
  }

  public void Remove(int CategoryID)
  {
   foreach ( PermissionItemNewsItem n in al)
   {
    if(n.CategoryID == CategoryID )
    {
     al.Remove(n) ;
     break ;
    }
   }
  }

  public string GetListIDs()
  {
   string ids = string.Empty ;
   foreach ( PermissionItemNewsItem itm in al )
   {
    if ( itm.List )
     ids = ids + itm.CategoryID.ToString() + "," ;
   }
   return ids + "0" ;
  }

  public bool CanUpdate(int catId)
  {
   bool can = false ;
   foreach ( PermissionItemNewsItem itm in al)
   {
    if (catId == itm.CategoryID )
    {
     can = itm.Update ;
     break ;
    }
   }

   return can ;
  }

  public bool CanAdult(int catId)
  {
   bool can = false ;
   foreach ( PermissionItemNewsItem itm in al)
   {
    if (catId == itm.CategoryID )
    {
     can = itm.Aduit  ;
     break ;
    }
   }

   return can ;
  }
 
  public bool CanInsert(int catId)
  {
   bool can = false ;
   foreach ( PermissionItemNewsItem itm in al)
   {
    if (catId == itm.CategoryID )
    {
     can = itm.Insert ;
     break ;
    }
   }

   return can ;
  }

  public bool CanDelete(int catId)
  {
   bool can = false ;
   foreach ( PermissionItemNewsItem itm in al)
   {
    if (catId == itm.CategoryID )
    {
     can = itm.Delete ;
     break ;
    }
   }

   return can ;
  }

 }

 public class PermissionItemNewsItem
 {
  public int CategoryID = 0 ;
  public bool List = false ;
  public bool Insert = false ;
  public bool Update = false ;
  public bool Delete = false ;
  public bool Aduit = false ;
  public bool Comment = false ;
 }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值