位运算权限改进
权限枚举类
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(); } } }