位运算权限改进

权限枚举类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AuthorityByte
{
    public enum Authority
    {
        logisticsBuy_show = 1,
        logsticsBuy_add = 2,
        logsticsBuy_del = 3,
        logsticsBuy_update = 4,
        config_show = 5,
        config_add = 6,
        config_update = 7,
        config_del = 8,
        logisticsSell_show = 9,
        logisticsSell_add = 10,
        logisticsSell_del = 11,
        logisticsSell_update = 12,
        logisticsSell_check = 13,
        contract_keepArch = 14,
        contract_keepArch_Add = 15,
        contract_edit = 16,
        contract_del = 17
    }
}

权限工具类AuthorityUtil

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AuthorityByte
{
    class AuthorityUtil
    {

        /// <summary>
        /// 添加权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要添加的权限枚举数组</param>
        /// <returns></returns>
        public static String AddAuthority(String MyAuthorityStr, params Authority[] maskList)
        {
            foreach (Authority t in maskList)
            {
                MyAuthorityStr = AddAuthority(MyAuthorityStr, t);
            }

            return MyAuthorityStr;
        }

        /// <summary>
        /// 对外提供的添加权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要添加的权限枚举</param>
        /// <returns></returns>
        public static String AddAuthority(String MyAuthorityStr, Authority AuthorityIndex)
        {
            if (MyAuthorityStr == null)
                MyAuthorityStr = "0";

            List<Byte> list = StringToByte(MyAuthorityStr);

            int byteIndex = (int)AuthorityIndex / 8;   //因为1Byte占8位,所以byteIndex表示AuthorityIndex处于第几个Byte,
            int byteOffset = (int)AuthorityIndex % 8;  //byteOffset表示所在Byte8位中的具体哪一位
            int listLength = list.Count;               //暂存list.Count,因为后面list.Insert(0, 0)会改变Count值

            if (list.Count * 8 < (int)AuthorityIndex)    //假如AuthorityIndex长度大于list.Count * 8,则需在list前面面补0
                for (int i = 0; i < byteIndex + 1 - listLength; i++)
                {
                    list.Insert(0,0);
                }

            if (byteOffset==0)
                list[list.Count - byteIndex] = (Byte)(list[list.Count-byteIndex] | 1 << 7);
            else
                list[list.Count - byteIndex - 1] = (Byte)(list[list.Count - byteIndex - 1] | (1 << (byteOffset - 1)));

            return BytesToString(list);
        }

        /// <summary>
        /// 删除权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要删除的权限枚举数组</param>
        /// <returns></returns>
        public static String RemoveAuthority(String MyAuthorityStr, params Authority[] maskList)
        {
            foreach (Authority t in maskList)
            {
                MyAuthorityStr = RemoveAuthority(MyAuthorityStr, t);
            }

            return MyAuthorityStr;
        }

        /// <summary>
        /// 对外提供的删除权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要删除的权限枚举</param>
        /// <returns></returns>
        public static String RemoveAuthority(String MyAuthorityStr, Authority AuthorityIndex)
        {
            if (MyAuthorityStr == null)
                MyAuthorityStr = "0";

            List<Byte> list = StringToByte(MyAuthorityStr);

            int byteIndex = (int)AuthorityIndex / 8;   //因为1Byte占8位,所以byteIndex表示AuthorityIndex处于第几个Byte,
            int byteOffset = (int)AuthorityIndex % 8;  //byteOffset表示所在Byte8位中的具体哪一位
            int listLength = list.Count;               //暂存list.Count,因为后面list.Insert(0, 0)会改变Count值

            if (list.Count * 8 < (int)AuthorityIndex)    //假如AuthorityIndex长度大于list.Count * 8,则需在list前面面补0
                for (int i = 0; i < byteIndex + 1 - listLength; i++)
                {
                    list.Insert(0, 0);
                }

            if (byteOffset == 0)
                list[list.Count - byteIndex] = (Byte)(list[list.Count - byteIndex] & ~(1 << 7));
            else
                list[list.Count - byteIndex - 1] = (Byte)(list[list.Count - byteIndex - 1] & ~(1 << (byteOffset - 1)));

            return BytesToString(list);
        }

        /// <summary>
        /// 检验权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要检验的权限枚举</param>
        /// <returns></returns>
        public static Boolean CheckAuthority(String MyAuthorityStr, params Authority[] maskList)
        {
            foreach (Authority t in maskList)
            {
                if(CheckAuthority(MyAuthorityStr, t)==false)
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 对外提供的检验权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要检验的权限枚举</param>
        /// <returns></returns>
        public static Boolean CheckAuthority(String MyAuthorityStr, Authority AuthorityIndex)
        {
            Boolean flag = false;
            if (MyAuthorityStr == null)
                MyAuthorityStr = "0";

            List<Byte> list = StringToByte(MyAuthorityStr);

            int byteIndex = (int)AuthorityIndex / 8;   //因为1Byte占8位,所以byteIndex表示AuthorityIndex处于第几个Byte,
            int byteOffset = (int)AuthorityIndex % 8;  //byteOffset表示所在Byte8位中的具体哪一位
            int listLength = list.Count;               //暂存list.Count,因为后面list.Insert(0, 0)会改变Count值

            if (list.Count * 8 < (int)AuthorityIndex)    //假如AuthorityIndex长度大于list.Count * 8,则需在list前面面补0
                for (int i = 0; i < byteIndex + 1 - listLength; i++)
                {
                    list.Insert(0, 0);
                }

            if (byteOffset == 0)
            {
                if ((1 << 7) == (Byte)(list[list.Count - byteIndex] & (1 << 7)))
                    flag = true;
            }
            else
            {
                if ((1 << (byteOffset - 1)) == (Byte)(list[list.Count - byteIndex - 1] & (1 << (byteOffset - 1))))
                    flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 内部方法,将Byte数组转成字符串
        /// </summary>
        /// <param name="Authority"></param>
        /// <returns></returns>
        private static String BytesToString(List<Byte> list)
        {
            String Str = "";
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i]>16)
                    Str += Convert.ToString(list[i], 16);
                else
                    Str += "0" + Convert.ToString(list[i], 16);
            }
            return Str;
        }

        /// <summary>
        /// 内部方法,将字符串转成Byte数组
        /// </summary>
        /// <param name="AuthorityStr"></param>
        /// <returns></returns>
        private static List<Byte> StringToByte(String AuthorityStr)
        {
            if (AuthorityStr.Length % 2 == 1)
                AuthorityStr = "0" + AuthorityStr; //确保AuthorityStr为2的整数倍

            Char[] CharArray = AuthorityStr.ToCharArray();

            List<Byte> StrByte = new List<Byte>();

            for (int i = 0; i < CharArray.Length; i += 2)
            {
                StrByte.Add(Convert.ToByte(AuthorityStr.Substring(i, 2), 16));  //1Byte为8位,0-255,刚好表示两位16进制数
            }
            return StrByte;
        }
    }
}

user类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AuthorityByte
{
    public class User
    {
        public String name { get; set; }
        public int age { get; set; }
        public String Authority { get; set; }

        public User(String name, int age)
        {
            this.name = name;
            this.age = age;
        }
    }
}

 

测试类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AuthorityByte
{
    class Program
    {
        static void Main(string[] args)
        {
            User user = new User("Jack", 20);

            //user.Authority = AuthorityUtil.AddAuthority(user.Authority, Authority.logisticsBuy_show);        //{              1 };
            //user.Authority = AuthorityUtil.AddAuthority(user.Authority, Authority.config_update);            //{       100 0000 }
            //user.Authority = AuthorityUtil.AddAuthority(user.Authority, Authority.config_del);               //{      1000 0000 };
            //user.Authority = AuthorityUtil.AddAuthority(user.Authority, Authority.logisticsSell_show);       //{    1 0000 0000 };
            //user.Authority = AuthorityUtil.AddAuthority(user.Authority, Authority.logisticsSell_update);     //{ 1000 0000 0000 };

            user.Authority = AuthorityUtil.AddAuthority(user.Authority,
                Authority.logisticsBuy_show,
                Authority.logsticsBuy_add,
                Authority.logsticsBuy_del,
                Authority.logsticsBuy_update,
                Authority.config_show,
                Authority.config_add,
                Authority.config_update,
                Authority.config_del,
                Authority.logisticsSell_show,
                Authority.logisticsSell_add,
                Authority.logisticsSell_del,
                Authority.logisticsSell_update,
                Authority.logisticsSell_check,
                Authority.contract_keepArch,
                Authority.contract_keepArch_Add,
                Authority.contract_edit,
                Authority.contract_del
                );
            Console.WriteLine(user.Authority); //预计是"01ffff"

            //user.Authority = AuthorityUtil.RemoveAuthority(user.Authority,
            //    Authority.logisticsBuy_show,
            //    Authority.config_update,
            //    Authority.config_del,
            //    Authority.logisticsSell_show,
            //    Authority.logisticsSell_update
            //    );
            //Console.WriteLine(user.Authority); //预计是"000"

            AuthorityUtil.CheckAuthority("0", Authority.contract_edit);

            Console.WriteLine(AuthorityUtil.CheckAuthority(user.Authority,
                Authority.logisticsBuy_show,
                Authority.config_update,
                Authority.config_del,
                Authority.logisticsSell_show,
                Authority.logisticsSell_update
                )); //预计是true

            Console.WriteLine(AuthorityUtil.CheckAuthority(user.Authority,
               Authority.logisticsBuy_show,
                Authority.config_update,
                Authority.config_del,
                Authority.logisticsSell_del,
                Authority.logisticsSell_check
                )); //预计是false

            Console.ReadKey();
        }
    }
}

转载于:https://www.cnblogs.com/xiayangqiushi/p/3381027.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值