zoukankan      html  css  js  c++  java
  • 二进制管理权限

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using Model.Enum;
    
    /// <summary>
    ///UserPurview 的摘要说明:用户权限
    /// </summary>
    public class UserPurview : System.Web.UI.Page
    {
    
        #region 获取用户是否拥有权限
        /// <summary>
        /// 获取用户是否拥有权限,传入权限枚举   (与运算)
        /// </summary>
        /// <param name="_EnumUserPurview">权限枚举</param>
        /// <param name="userPurview">用户所拥有的权限</param>
        /// <returns></returns>
        public static bool GetUserPurview(E_UserPurview _EnumUserPurview, string userPurview = null)
        {
            int num = Convert.ToInt32(Convert.ToString(Convert.ToInt32(_EnumUserPurview), 2));
            
            if (string.IsNullOrEmpty(userPurview))
            {
                //获取当前用户权限
                userPurview = GetUserPurview(Convert.ToInt32(HttpContext.Current.Session["uid"] ?? "0"));
            }
    
            if (!string.IsNullOrEmpty(userPurview))
            {
                int User_Purview = Convert.ToInt32(userPurview);
    
                if ((User_Purview & num) != 0)
                {
                    return true;
                }
                return false;
            }
            return false;
        }
        #endregion
    
        
        
        #region 添加权限
        /// <summary>
        /// 添加权限     (或运算)
        /// </summary>
        /// <param name="_E_UserPurview">权限枚举</param>
        /// <param name="uid">修要修改权限用户的uid</param>
        /// <returns></returns>
        public static bool AddUserPurview(E_UserPurview _E_UserPurview, int uid)
        {
            string oldUserPurview = GetUserPurview(uid);
            int UserPurview = Convert.ToInt32(oldUserPurview);
            UserPurview = UserPurview | Convert.ToInt32((Convert.ToString(Convert.ToInt32(_E_UserPurview), 2)));
            bool s = SetUserPurview(UserPurview.ToString(), uid);
            return s;
        }
        #endregion
    
        #region 删除权限
        /// <summary>
        /// 删除权限   (求补、与运算)
        /// </summary>
        /// <param name="_E_UserPurview">需要删除的权限</param>
        /// /// <param name="uid">修要修改权限用户的uid</param>
        /// <returns></returns>
        public static bool DeleteUserPurview(E_UserPurview _E_UserPurview, int uid)
        {
            string oldUserPurview = GetUserPurview(uid);
            int UserPurview = Convert.ToInt32(oldUserPurview);
            UserPurview = UserPurview & (~Convert.ToInt32((Convert.ToString(Convert.ToInt32(_E_UserPurview), 2))));
            bool s = SetUserPurview(UserPurview.ToString(), uid);
            return s;
        }
        #endregion
    
    
        #region 将用户权限写入数据库
        /// <summary>
        /// 将用户权限写入数据库
        /// </summary>
        /// <param name="userPuerview"></param>
        /// <returns></returns>
        public static bool SetUserPurview(string userPuerview, int uid)
        {
            try
            {
                int parentId = Convert.ToInt32(HttpContext.Current.Session["uid"] ?? "0");
                string sql = "UPDATE User SET UserPurview=@userPuerview WHERE UID =@uid and (parentid=@parentId or uid=@parentId)";
                SqlParameter[] param = new SqlParameter[] { 
                    new SqlParameter("@userPuerview",userPuerview),
                    new SqlParameter("@uid",uid),
                    new SqlParameter("@parentId",parentId)
                };
                CDataAccess.ExecSql(sql, param);
                return true;
            }
            catch 
            {
                return false;
            }
        }
        #endregion
    
    
        #region 根据用户uid查询用户权限
        /// <summary>
        /// 根据用户uid查询用户权限
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static string GetUserPurview(int uid)
        {
            try
            {
                int parentId = Convert.ToInt32(HttpContext.Current.Session["uid"] ?? "0");
                string sql = "select User from User where uid =@uid and (ParentID=@parentid or uid=@parentid)";
                SqlParameter[] param = new SqlParameter[] { 
                    new SqlParameter("@uid",uid),
                    new SqlParameter("@parentid",parentId)
                };
                string userPurview = CDataAccess.ExecuteScalar(sql, param).ToString();
                return userPurview;
            }
            catch 
            {
                return "0";
            }
        }
    
        #endregion
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Model.Enum
    {
        public enum E_UserPurview
        {
            /// <summary>
            /// 能否看到价格 
            /// </summary>
            price_enable = 1,
            /// <summary>
            /// 支付权限
            /// </summary>
            checkout_enable = 2,
            /// <summary>
            /// 修改查看自己账户的权限
            /// </summary>
            account_enable = 4
        }
    }

     注意:枚举值类型只能是1,2,4,8,....

    如果用3 二进制是11,意味着有两种权限,所以这里舍弃不用(位数代表权限)

    参考:http://www.cnblogs.com/qfcndtt/archive/2012/08/03/2621713.html

  • 相关阅读:
    DAOFactory复用代码
    WebUtils复用代码【request2Bean、UUID】
    过滤器复用代码【中文乱码、HTML转义】
    数据库复用代码【c3p0配置文件、数据库连接池】
    分页复用代码【Page类、JSP显示页面】
    AJAX应用【股票案例】
    JavaScript中的for in循环
    JSON【介绍、语法、解析JSON】
    javaScript【创建对象、创建类、成员变量、方法、公有和私有、静态】
    DOM【介绍、HTML中的DOM、XML中的DOM】
  • 原文地址:https://www.cnblogs.com/wugang/p/3160337.html
Copyright © 2011-2022 走看看