把你平时使用的权限系统的实现原理以及重要代码写下来,没写过,有的发过来看下啊

解决方案 »

  1.   

    仅供参考:
     public class LicenseItem
        {
            protected ProgramCodes.Codes m_id = ProgramCodes.Codes.Null;
            protected bool m_allowOpen = true, m_allowNew = false, m_allowEdit = false, m_allowDel = false, m_allowIO = true, m_allowQuery = true, m_allowReport = true, m_allowAudit = false;        public LicenseItem() { }
            public LicenseItem(ProgramCodes.Codes licenseID)
            {
                m_id = licenseID;            this.InitLicense();
            }
            public LicenseItem(string licenseID)
            {
                m_id = (ProgramCodes.Codes)System.Enum.Parse(typeof(ProgramCodes.Codes),licenseID);            this.InitLicense();
            }
            /// <summary>
            /// 获取或设置 权限编号
            /// </summary>
            public ProgramCodes.Codes LicenseID
            {
                get { return m_id; }
                set
                {
                    if (m_id == value) return;
                    m_id = value;                this.InitLicense();
                }
            }
    ///// <summary>
    ///// 获取或设置 当未获得指定的权限时,是否弹出对话框提示"未授权"
    ///// </summary>
    //        public Common.Common.DefaultBoolean ShowUnlicensedInfo
    //        { get { return m_popupMSGBOX; }
    //            set { m_popupMSGBOX = value; }
    //        }        protected void InitLicense()
            {
                if (Common.Common.get_CIMSReadOnly(false)) return;
                if (LicenseID == ProgramCodes.Codes.Null) return;            try
                {
                    //string paramName = "@CurrentUser";
                    string cmdText = string.Format("Select * From UserLicense Where {0} = @{0} And {1} = @{1}", Utils.COL_USER, Utils.COL_PROGRAMID);
                    SqlCommand cmd = Common.SqlHelper.AccountCommandProvider.CreateCommand(cmdText);
                    cmd.Parameters.Add("@" + Utils.COL_USER, SqlDbType.VarChar, 20).Value = Common.Common.CurrentUserName;
                    cmd.Parameters.Add("@" + Utils.COL_PROGRAMID, SqlDbType.VarChar, 50).Value = this.LicenseID.ToString();                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);                if (dr.Read())
                    {
                        this.m_allowOpen = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_ACCESS], true));
                        this.m_allowNew = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_ADD], false));
                        this.m_allowEdit = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_CHANGE], false));
                        this.m_allowDel = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_DELETE], false));
                        this.m_allowIO = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_IO], true));
                        this.m_allowQuery = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_QUERY], true));
                        this.m_allowReport = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_REPORT], true));
                        this.m_allowAudit = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_AUDITING], true));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    Common.SqlHelper.AccountProvider.Close();
                }
            }
            /// <summary>
            /// 获取是否拥有该模块的 访问 权限
            /// </summary>
            public bool AllowAccess
            {
                get
                {
                   // if (m_allowOpen == false && ShowUnlicensedInfo != CIMS.Common.Common.DefaultBoolean.False) LicenseProvider.UnlicensedInfo(LicenseType.Access, this.LicenseID.ToString());
                    return m_allowOpen;
                }
            }
            /// <summary>
            /// 获取是否拥有该模块的 新增 权限
            /// </summary>
            public bool AllowNew { get {// if (m_allowNew == false && ShowUnlicensedInfo == CIMS.Common.Common.DefaultBoolean.True) LicenseProvider.UnlicensedInfo(LicenseType.NewRow, this.LicenseID.ToString()); 
                return m_allowNew; } }
            /// <summary>
            /// 获取是否拥有该模块的 编辑 权限
            /// </summary>
            public bool AllowEdit { get { return m_allowEdit; } }
            /// <summary>
            /// 获取是否拥有该模块的 删除 权限
            /// </summary>
            public bool AllowDel { get { return m_allowDel; } }
            /// <summary>
            /// 获取是否拥有该模块的 数据导入/导出 权限
            /// </summary>
            public bool AllowIO { get { return m_allowIO; } }
            /// <summary>
            /// 获取是否拥有该模块的 查询 权限
            /// </summary>
            public bool AllowQuery { get { return m_allowQuery; } }
            /// <summary>
            /// 获取是否拥有该模块的 报表 权限
            /// </summary>
            public bool AllowReport { get { return m_allowReport; } }
            /// <summary>
            /// 获取是否拥有该模块的 审核 权限
            /// </summary>
            public bool AllowAudit { get { return m_allowAudit; } }        public override string ToString()
            {
                return string.Format("{0}:{1},{2},{3},{4},{5},{6},{7},{8}", LicenseID, AllowAccess, AllowNew, AllowEdit, AllowDel, AllowIO, AllowQuery, AllowReport, AllowAudit);
            }
            public override bool Equals(object obj)
            {
                if (obj == null) return false;
                if (obj == this) return true;
                if (obj is LicenseItem && ((LicenseItem)obj).LicenseID == this.LicenseID) return true;            return base.Equals(obj);
            }
        }
      

  2.   

    public class LicenseProvider
    {
    protected LicenseItem m_licenseItem = null;
            #region Init
            /// <summary>
            /// 实例化权限管理类,CurrentUserName,并使用默认程序权限
            /// </summary>
            public LicenseProvider()
            {
                m_licenseItem = new LicenseItem();
            } /// <summary>
    /// 实例化权限管理类
    /// </summary>
            /// <param name="ProgramCode">程序模块</param>
            public LicenseProvider(string ProgramCode)
    {
                m_licenseItem = new LicenseItem(ProgramCode);
    }        /// <summary>
            /// 实例化权限管理类
            /// </summary>
            /// <param name="ProgramCode">程序模块</param>
            public LicenseProvider(ProgramCodes.Codes ProgramCode)
            {
                m_licenseItem = new LicenseItem(ProgramCode);
            }        #endregion        #region Properties
            
    /// <summary>
    /// 获取或设置受当前权限管理的程序编码
    /// </summary>
    public ProgramCodes.Codes LicenseID
    {
    get{return this.m_licenseItem.LicenseID;}
                set { this.Provider.LicenseID = value; }
    }
            protected LicenseItem Provider { get { return m_licenseItem; } }
    /// <summary>
    /// 指定访问类型的字段
    /// </summary>
    /// <param name="type">受权类型</param>
    /// <returns></returns>
    public static string LicenseString(LicenseType type)
    {
    switch (type)
    {
    case LicenseType.Access :
    return Utils.COL_ACCESS;

    case LicenseType.NewRow:
                        return Utils.COL_ADD;

    case LicenseType.ModifyRow :
                        return Utils.COL_CHANGE;

    case LicenseType.DeleteRow :
                        return Utils.COL_DELETE;

    case LicenseType.Query :
                        return Utils.COL_QUERY;

    case LicenseType.IO :
                        return Utils.COL_IO;

    case LicenseType.Report :
                        return Utils.COL_REPORT; case LicenseType.Audit:
                        return Utils.COL_AUDITING; default:
                        return Utils.COL_ACCESS;
    }
            }        #endregion
     
      

  3.   


                 #region License
            /// <summary>
            /// 用户是否获得当前程序编号的许可证。
            /// </summary>
            /// <param name="type">受权类型</param>
            /// <returns></returns>
            public bool this[LicenseType type]
            {
                get
                {
                    return IsLicense(type);
                }
            }     /// <summary>
    /// 用户是否获得当前程序编号的访问许可证。
    /// 不提示未受权消息!
    /// </summary>
    /// <returns>return IsLicense(LicenseType.Access,true);</returns>
    public bool IsLicense()
    {
    return IsLicense(LicenseType.Access,true);
    } /// <summary>
    /// 用户是否获得当前程序编号的许可证,默认许可类型是:访问
    /// </summary>
    /// <param name="type">受权类型</param>
    /// <returns>if (type == LicenseType.Access ) bol =true; return IsLicense(type,bol);</returns>
    public bool IsLicense(LicenseType type)
    {
    bool bol=false;
    if (type == LicenseType.Access ) bol =true;//显示未受权消息
    return IsLicense(type,bol);
    } /// <summary>
    /// 用户是否获得当前程序编号的指定许可证
    /// </summary>
    /// <param name="type">许可类型</param>
    /// <param name="IsShowUnlicenseInfo">是否提示未授权消息</param>
    /// <returns>当前权限类型是否生效(无效则消息提示,一般为“访问权限”)</returns>
            public bool IsLicense(LicenseType type, bool IsShowUnlicenseInfo)
            {
                if (Common.Common.get_CIMSReadOnly(false))
                {
                    if (type == LicenseType.Access) return true;
                    return false;
                }            bool result = false;
                switch (type)
                {
                    case LicenseType.Access:
                        result = Provider.AllowAccess;
                        break;
                    case LicenseType.NewRow:
                        result = Provider.AllowNew;
                        break;
                    case LicenseType.ModifyRow:
                        result = Provider.AllowEdit;
                        break;
                    case LicenseType.DeleteRow:
                        result = Provider.AllowDel;
                        break;
                    case LicenseType.IO:
                        result = Provider.AllowIO;
                        break;
                    case LicenseType.Query:
                        result = Provider.AllowQuery;
                        break;
                    case LicenseType.Report:
                        result = Provider.AllowReport;
                        break;
                    case LicenseType.Audit:
                        result = Provider.AllowAudit;
                        break;
                    default:
                        throw new UnauthorizedAccessException("未指定的权限类型!");
                        break;
                }            if (result==false && IsShowUnlicenseInfo == true) UnlicensedInfo(type, "当前");            return result;
            }
           
    public static void UnlicensedInfo(LicenseType type,string ProgramCode)
    {
                if (Common.Common.get_CIMSReadOnly(false)) return;
                string formText = ProgramCodes.GetFormText(ProgramCode);
                if (string.IsNullOrEmpty(formText)) formText = type.ToString();
                Common.ShareMembers.UnlicensedInfo(Common.Common.GetMasks(formText) + " 的" + LicenseString(type));
    } internal static void LicenseingErrorInfo(LicenseType type,string ErrorMessage)
    {
                if (Common.Common.CIMSReadOnly) return; Common.Common.OnInfo("您没有 " + LicenseString(type) + " 权限!" + Common.ShareMembers.ConstString.LinkAdmin,InfoBase.InfoTypeEnum.Error);
                MessageBox.Show(Common.InfoProvider.LateInfo + "\n\n" + ErrorMessage, "权限错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
            #endregion
      

  4.   

    權限比較簡單,我看過ERP和HCP的權限,都差不多,只不過前2者分得比較細
    一般都只分到模組,但是HCP分到了模組上的按鈕,但是個人覺得沒必要那么做
    以上屬于個人意見