惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
Y
Y Combinator Blog
The Cloudflare Blog
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
T
Tailwind CSS Blog
P
Privacy International News Feed
S
Securelist
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tor Project blog
P
Proofpoint News Feed
Project Zero
Project Zero
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Help Net Security
美团技术团队
The GitHub Blog
The GitHub Blog
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Latest
Security Latest
NISL@THU
NISL@THU
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Secure Thoughts
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
IT之家
IT之家
Last Week in AI
Last Week in AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Privacy & Cybersecurity Law Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
S
Schneier on Security
L
Lohrmann on Cybersecurity
S
Security Affairs
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG

博客园 - lwjacky

SQL提高查询效益之in、not in、between、like等条件讲述 sqlserver查询数据的所有表名和行数 Extjs4---Cannot read property 'addCls' of null ExtJs4.1实现数据缓存 Sql 脚本文件太大 系统操作日志设计 ExtJs 常用方法 属性 C#农历类 通过C#使用googleAPI SQL SERVER 如何批量修改表和存储过程的架构 HTML特殊转义字符列表 - lwjacky - 博客园 extjs3.0 修正 ie 与 firefox字体显示差异的补丁 C#实现QQ接口软件--QQ的HTTP接口协议探究 二次开发WinWebMail邮件系统接口 C#实现汉字转换为拼音缩写的代码 C#实现将汉字转化为拼音的代码 extjs IE8下datefield的width问题 ExtJS中如何给Label添加click事件 自己的工具箱(Toolkit)
一种简单的直观的高效的权限设计
lwjacky · 2012-03-11 · via 博客园 - lwjacky

    大部分系统都有权限系统。一般来说,它能管控人员对某个否页面的访问;对某些字段、控件可见或者不可见。对gridview中的数据是否可删除、可添加、可新增等等。大部分人都把权限作为一个子系统独立出来。但是这里我不是想设计一个权限管理系统,网上的设计方案太多了,可以说每个开发人员都有自己的开发权限管理系统的想法和思路。

   在这篇文章中,我先用简单的C#代码模仿一个用户的权限,再使用sql去模拟。这是一种很简单,很直观,很高效的方式去判定用户的权限。

C#:

   好吧,先从最简单开始,定义一个用户(User)类,如下。

1 class User 
2 {
3     bool CanDelete;
4     bool CanRead;
5     bool CanWrite;
6     bool CanModify;
7     bool CanCreate;
8 }

    这里设计5个属性来管控用户的权限。我发现这样虽然很直观,但是不宜扩张。我们将权限独立出来,在看下面代码:

 1     enum PermissionTypes : int 
 2     {
 3         None = 0,
 4         Read = 1,
 5         Write = 2,
 6         Modify = 4,
 7         Delete = 8,
 8         Create = 16,
 9         All = Read | Write | Modify | Delete | Create
10     }
11     class User 
12     {
13        public PermissionTypes Permissions = PermissionTypes.None;
14     }

    我们先试用一下,你就能感觉到神奇之处:

 1 //创建一个用户
 2 User admin = new User();
 3 admin.Permissions = PermissionTypes.Read
 4     | PermissionTypes.Write
 5     | PermissionTypes.Delete;
 6 
 7 //验证权限
 8 bool canRead = ((PermissionTypes.Read & admin.Permissions) == PermissionTypes.Read);
 9 bool canWrite = ((PermissionTypes.Write & admin.Permissions) == PermissionTypes.Write);
10 bool canCreate = ((PermissionTypes.Create & admin.Permissions) == PermissionTypes.Create);
11 
12 //查看结果
13 Console.WriteLine(canRead); //true
14 Console.WriteLine(canWrite); //true
15 Console.WriteLine(canCreate); //false
16 

    利用了'|'和'&'两个操作。但是这样看起来很是很别捏,初始化权限和验证权限用了一长串'|'和'&'运算的代码。很不直观。我在System.Enum中扩展一些方法供你调用,代码如下。

 1   //是否存在权限
 2         public static bool Has<T>(this System.Enum type, T value)
 3         {
 4             try
 5             {
 6                 return (((int)(object)type & (int)(object)value) == (int)(object)value);
 7             }
 8             catch
 9             {
10                 return false;
11             }
12         }
13         //判断权限
14         public static bool Is<T>(this System.Enum type, T value)
15         {
16             try
17             {
18                 return (int)(object)type == (int)(object)value;
19             }
20             catch
21             {
22                 return false;
23             }
24         }
25         //添加权限
26         public static T Add<T>(this System.Enum type, T value)
27         {
28             try
29             {
30                 return (T)(object)(((int)(object)type | (int)(object)value));
31             }
32             catch (Exception ex)
33             {
34                 throw new ArgumentException(
35                     string.Format(
36                         "不能添加类型 '{0}'",
37                         typeof(T).Name
38                         ), ex);
39             }
40         }
41 
42         //移除权限
43         public static T Remove<T>(this System.Enum type, T value)
44         {
45             try
46             {
47                 return (T)(object)(((int)(object)type & ~(int)(object)value));
48             }
49             catch (Exception ex)
50             {
51                 throw new ArgumentException(
52                     string.Format(
53                         "不能移除类型 '{0}'",
54                         typeof(T).Name
55                         ), ex);
56             }
57         }

使用一下:

 1            //创建一个用户
 2             User admin = new User();
 3             PermissionTypes permissions = new PermissionTypes();
 4             admin.Permissions = permissions;
 5             //添加权限
 6             admin.Permissions = admin.Permissions.Add(PermissionTypes.Create);
 7             admin.Permissions = admin.Permissions.Add(PermissionTypes.Read);
 8             admin.Permissions = admin.Permissions.Add(PermissionTypes.Write);
 9            //判断权限
10             bool canRead = admin.Permissions.Has(PermissionTypes.Read); //true
11             bool canWrite = admin.Permissions.Has(PermissionTypes.Write); //true
12             bool canDelete = admin.Permissions.Has(PermissionTypes.Delete); //false
13             bool canCreate = admin.Permissions.Has(PermissionTypes.Create); //true
14 
15             Console.WriteLine(canRead); //true
16             Console.WriteLine(canWrite); //true
17             Console.WriteLine(canDelete); //false
18             Console.WriteLine(canCreate); //true
19             Console.Read();

SQL:

    大部分权限管理都是数据库的操作,好依照上面的思路,我在sqlserver里面模拟一下以上的操作,在sql中与或运算是很高效的。先设计两张表User和Permission。

1、获取有Read权限的所有用户:

1 select * from [User] where PermissionTypes&1 =1 

Result:

2、获取有Delete权限的所有用户:

1 select * from [User] where PermissionTypes&8 =8

Result:

3、判断麒麟是否有有Delete权限

1 if  exists (select * from [User] where Name='qilin' and  PermissionTypes&8 =8)
2     print 'true'
3 else
4     print 'flase'

Result: flase