CSDN博客

img syma7788

C# 实现 在 WinForm 和 WebForm 中的权限控制

发表于2004/10/29 9:39:00  2154人阅读

using System;
using System.Data;
using System.Collections;

namespace XSTBD.Common.Right
{
 /// <summary>
 /// Action 的摘要说明。
 /// </summary>
 public class Action
 {
  private string m_ActionName;
  private bool   m_ActionEnabled = true;
  private DataSet m_dsRights;
  private Hashtable m_ActionObjHT;
  private ArrayList m_ActionList;

  /// <summary>
  /// 操作
  /// </summary>
  public Action()
  {
   ResetAction();
  }

  public Action(string actionName,bool actionEnabled)
  {
   this.m_ActionName = actionName;
   this.m_ActionEnabled = actionEnabled;
   ResetAction();
  }

  public Action(string actionName)
  {
   this.m_ActionName = actionName;
   this.m_ActionEnabled = false;
   ResetAction();
  }
  /// <summary>
  /// 操作名称
  /// </summary>
  public string ActionName
  {
   get
   {
    return this.m_ActionName;
   }
   set
   {
    this.m_ActionName = value;
   }
  }

  /// <summary>
  /// 操作使能状态
  /// </summary>
  public bool ActionEnabled
  {
   get
   {
    return this.m_ActionEnabled;
   }
   set
   {
    this.m_ActionEnabled = value;
   }
  }

  /// <summary>
  /// Action 对应操作对象表
  /// </summary>
  public Hashtable ActionObjHT
  {
   get
   {
    return this.m_ActionObjHT;
   }
   set
   {
    this.m_ActionObjHT = value;
   }
  }
  /// <summary>
  /// Action 中包含的子Action
  /// </summary>
  public ArrayList ActionList
  {
   get
   {
    return this.m_ActionList;
   }
   set
   {
    this.m_ActionList = value;
   }
  }

  /// <summary>
  /// 权限集和
  /// </summary>
  public DataSet DsRights
  {
   get
   {
    return this.m_dsRights;
   }
   set
   {
    this.m_dsRights = value;
   }
  }
  public void AddAction(Action action)
  {
   m_ActionList.Add(action);
  }
  /// <summary>
  /// 重新设置Action
  /// </summary>
  private void ResetAction()
  {
   m_ActionObjHT = new Hashtable();
   m_ActionList = new ArrayList();

  }
  /// <summary>
  /// 添加操作对象
  /// </summary>
  /// <param name="obj">操作对象</param>
  /// <param name="objType">对象类型</param>
  public void AddActionObj(Object obj,Type objType)
  {
   m_ActionObjHT.Add(obj,objType);
  }

  /// <summary>
  /// 设置Action的Enabled状态,同时通过Action的Enabled状态设置控件
  /// </summary>
  public void SetActionEnabled()
  {
   if(this.DsRights.Tables[0].Rows.Count > 0)
   {
    if(this.DsRights.Tables[0].Rows[0]["ActionName"].ToString() == "AllRight")//设置所有权限
    {
     SetAllActionEnabled();
     return;
    }
    for(int i = 0; i < this.DsRights.Tables[0].Rows.Count; i++)
    {
     string actionName = this.DsRights.Tables[0].Rows[i]["ActionName"].ToString();
     for(int j = 0; j < m_ActionList.Count; j++)
     {
      if(((Action)m_ActionList[j]).ActionName == actionName)
      {
       ((Action)m_ActionList[j]).ActionEnabled = true;
       break;
      }
     }
    }
   }
   SetActionObjEnabled();
  }

  private void SetAllActionEnabled()
  {
   for(int i = 0; i < this.ActionList.Count; i++)
   {
    ((Action)ActionList[i]).ActionEnabled = true;
   }
   SetActionObjEnabled();
  }
  /// <summary>
  ///  设置控件Enabled
  /// </summary>
  private void SetActionObjEnabled()
  {
   for(int i = 0; i < this.ActionList.Count; i++)
   {
    Action action = ((Action)ActionList[i]);
    IDictionaryEnumerator actionList = action.ActionObjHT.GetEnumerator();
    while(actionList.MoveNext())
    {
     Type  type = ((Type)actionList.Value);
     Object obj = ((Object)actionList.Key);
     if(type.FullName == "System.Windows.Forms.MenuItem")
     {
      bool enable = ((System.Windows.Forms.MenuItem)obj).Enabled;
      ((System.Windows.Forms.MenuItem)obj).Enabled = enable && action.ActionEnabled;
     }
     else if(type.FullName == "System.Windows.Forms.Control")
     {
      bool enable = ((System.Windows.Forms.Control)obj).Enabled;
      ((System.Windows.Forms.Control)obj).Enabled = enable && action.ActionEnabled;
     }
     else if(type.BaseType.FullName == "System.Web.UI.WebControls.WebControl")
     {
      bool enable = ((System.Web.UI.WebControls.WebControl)obj).Enabled;
      ((System.Web.UI.WebControls.WebControl)obj).Enabled = enable && action.ActionEnabled;
     }
    }
   }
  }

 }
}

以上只是简单实现,还有很多不够完善的地方,谢谢提出更好的方法和意见!

阅读全文
0 0

相关文章推荐

img
取 消
img