利用列舉來完成權限設定

  • 2747
  • 0
  • C#
  • 2009-11-20

利用列舉來完成權限設定

在參考下列文章後

[C#] enum 與 [Flags] 屬性

想到之前網頁表單權限都是利用資料庫欄位來設定,當權限一多

資料庫欄位勢必要增加或者更改;未來想用列舉方式來完成,將

所有權限用一個欄位紀錄,取出後再用列舉的方式分析,並且利

用正則表示式來檢核使用者,最後使用XML讀取設定權限檔

先設計雛型一下~

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;

namespace DateTest
{
    [Flags]
    //使用者權限:無,查詢,新增,更新,刪除
    public enum DbPrivilege
    {
        None = 0,
        Select = 2,
        Insert = 4,
        Update = 8,
        Delete = 16,
        All = Select | Insert | Update | Delete
    };
    public class UserInfo
    {
        private string _Id;       
        public string Id
        {
            set { _Id = value; }
            get { return _Id; }

        }
        /// <summary>
        /// Checks the id.
        /// </summary>
        /// <param name="InputId">The input id.</param>
        /// <returns></returns>
        public bool CheckId(string InputId)
        {
            return (InputId != string.Empty && !Regex.IsMatch(InputId, "[^0-9]")) ? true : false;
        }
        public DbPrivilege UsrPrivilege;
        /// <summary>
        /// Initializes a new instance of the <see cref="UserInfo"/> class.
        /// </summary>
        /// <param name="UserPrivilege">The user privilege.</param>
        /// <param name="ID">The ID.</param>
        public UserInfo(DbPrivilege UserPrivilege,string ID)
        {
            if (!CheckId(ID))
            {
                Console.WriteLine("Wrong!");
                
            }
            else
            {
                this.UsrPrivilege = UserPrivilege;
                this.Id = ID;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UserInfo"/> class.
        /// </summary>
        /// <param name="ID">The ID.</param>
        public UserInfo( string ID)
        {
            if (!CheckId(ID))
            {
                Console.WriteLine("Wrong!");

            }
            else
            {
                this.UsrPrivilege = DbPrivilege.None;
                this.Id = ID;
            }
        }


        /// <summary>
        /// Adds the privilege.
        /// </summary>
        /// <param name="Input">The input.</param>
        /// <param name="ID">The ID.</param>
        public void AddPrivilege(DbPrivilege Input,string ID)
        {
            if (CheckId(ID) && string.Compare(this.Id,ID) == 0 )
            {
                DbPrivilege result = this.UsrPrivilege | Input;
                this.UsrPrivilege = result;
            }
        }
        /// <summary>
        /// Removes the privilege.
        /// </summary>
        /// <param name="Input">The input.</param>
        /// <param name="ID">The ID.</param>
        public void RemovePrivilege(DbPrivilege Input, string ID)
        {
            if (CheckId(ID))
            {
                DbPrivilege InputTest = this.UsrPrivilege & Input;
                if (InputTest == Input && string.Compare(this.Id, ID) == 0)
                {
                    DbPrivilege result = this.UsrPrivilege ^ Input;
                    this.UsrPrivilege = result;
                }
            }
        }
        /// <summary>
        /// Modifies the privilege.
        /// </summary>
        /// <param name="Input">The input.</param>
        /// <param name="ID">The ID.</param>
        public void ModifyPrivilege(DbPrivilege Input, string ID)
        {
            if (CheckId(ID) && string.Compare(this.Id, ID) == 0)
            {
                DbPrivilege result = this.UsrPrivilege ^ Input;
                this.UsrPrivilege = result;
            }
        }
        /// <summary>
        /// Resets the privilege.
        /// </summary>
        /// <param name="ID">The ID.</param>
        public void ResetPrivilege(string ID)
        {
            if (CheckId(ID) && string.Compare(this.Id, ID) == 0)
            {

                this.UsrPrivilege = DbPrivilege.None;
            }
        }
        /// <summary>
        /// Displays the privilege.
        /// </summary>
        public void DisplayPrivilege()
        {
            Console.WriteLine("Privilege is : " + this.UsrPrivilege);
            Console.WriteLine("(int)Privilege is : " + (int)this.UsrPrivilege);
        }
    }

    class Program
    {        
        static void Main(string[] args)
        {            
           
           string id = "123";
           UserInfo a = new UserInfo(id);
           a.DisplayPrivilege();
           a.AddPrivilege(DbPrivilege.All,"444");
           a.DisplayPrivilege();
           a.AddPrivilege(DbPrivilege.Select, id);
           a.DisplayPrivilege();
           a.AddPrivilege(DbPrivilege.Insert, id);
           a.DisplayPrivilege();
           a.AddPrivilege(DbPrivilege.Delete, id);
           a.DisplayPrivilege();
           a.AddPrivilege(DbPrivilege.Update, id);
           a.DisplayPrivilege();
           a.RemovePrivilege(DbPrivilege.Select,id);
           a.DisplayPrivilege();
           a.ResetPrivilege("321");
           a.DisplayPrivilege();
           a.ResetPrivilege(id);
           a.DisplayPrivilege();
           a.ModifyPrivilege(DbPrivilege.Delete,id);
           a.DisplayPrivilege();
           a.ModifyPrivilege(DbPrivilege.Delete,id);
           a.DisplayPrivilege(); 
           Console.Read();
        }

        
    }
}

輸出畫面:

output

文件簽核 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace FlowEnumTest
{
    [Flags]
    //文件簽核流程:退回,待簽核,組長放行,科長放行,完成
    public enum FlowStatus
    {
        Reject = -1,
        WaitForCheck = 1,
        LeaderChecked = 2,
        HeadChecked = 3,
        Complete = 4,
        
    };

    public class UserInfo
    {
        private string _Id;
        public string Id
        {
            set { _Id = value; }
            get { return _Id; }

        }
        /// <summary>
        /// Checks the id.
        /// </summary>
        /// <param name="InputId">The input id.</param>
        /// <returns></returns>
        public bool CheckId(string InputId)
        {
            return (InputId != string.Empty && !Regex.IsMatch(InputId, "[^0-9]")) ? true : false;
        }
        public FlowStatus UsrFlowStatus;
        /// <summary>
        /// Initializes a new instance of the <see cref="UserInfo"/> class.
        /// </summary>
        /// <param name="UserFlowStatus">The user FlowStatus.</param>
        /// <param name="ID">The ID.</param>
        public UserInfo(FlowStatus UserFlowStatus, string ID)
        {
            if (!CheckId(ID))
            {
                Console.WriteLine("Wrong!");

            }
            else
            {
                this.UsrFlowStatus = UserFlowStatus;
                this.Id = ID;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UserInfo"/> class.
        /// </summary>
        /// <param name="ID">The ID.</param>
        public UserInfo(string ID)
        {
            if (!CheckId(ID))
            {
                Console.WriteLine("Wrong!");

            }
            else
            {
                this.UsrFlowStatus = FlowStatus.WaitForCheck;
                this.Id = ID;
            }
        }


        /// <summary>
        /// ToNext the FlowStatus.
        /// </summary>
        /// <param name="ID">The ID.</param>
        public void ToNextFlowStatus(string ID)
        {
            if (CheckId(ID) && string.Compare(this.Id, ID) == 0 && this.UsrFlowStatus != FlowStatus.Complete)
            {
                int result = (int)this.UsrFlowStatus +1 ;
                this.UsrFlowStatus =(FlowStatus) result;
            }
        }
        /// <summary>
        /// RollBack the FlowStatus.
        /// </summary>
        /// <param name="ID">The ID.</param>
        public void RollBackFlowStatus(string ID)
        {
            if (CheckId(ID) &&  this.UsrFlowStatus != FlowStatus.Reject && this.UsrFlowStatus != FlowStatus.Complete )
            {
                int result = (int)this.UsrFlowStatus -1;
                this.UsrFlowStatus = (FlowStatus)result;
            }
        }
        /// <summary>
        /// Complete the FlowStatus.
        /// </summary>
        /// <param name="ID">The ID.</param>
        public void CompleteFlowStatus(string ID)
        {
            if (CheckId(ID) && string.Compare(this.Id, ID) == 0)
            {
                this.UsrFlowStatus = FlowStatus.Complete;
            }
        }
        /// <summary>
        /// Rejects the FlowStatus.
        /// </summary>
        /// <param name="ID">The ID.</param>
        public void RejectFlowStatus(string ID)
        {
            if (CheckId(ID) && string.Compare(this.Id, ID) == 0)
            {

                this.UsrFlowStatus = FlowStatus.Reject;
            }
        }
        /// <summary>
        /// Displays the FlowStatus.
        /// </summary>
        public void DisplayFlowStatus()
        {
            Console.WriteLine("FlowStatus is : " + this.UsrFlowStatus);
            Console.WriteLine("(int)FlowStatus is : " + (int)this.UsrFlowStatus);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            string id = "123";
            UserInfo a = new UserInfo(id);
            a.DisplayFlowStatus();
            a.ToNextFlowStatus(id);
            a.DisplayFlowStatus();
            a.RollBackFlowStatus(id);
            a.DisplayFlowStatus();
            a.CompleteFlowStatus(id);
            a.DisplayFlowStatus();
            a.RejectFlowStatus(id);
            a.DisplayFlowStatus();
            Console.Read();
        }


    }
}

輸出畫面:

aaacc