Heim > php教程 > php手册 > Hauptteil

用户权力详解

WBOY
Freigeben: 2016-06-13 10:25:53
Original
1060 Leute haben es durchsucht

有耐心就看完(还是算法重要)
用户初始权值:Enable = 0;
假如一个用户有第一项权力Select,则用户权值:Enable = Enable + 1;
假如一个用户有第二项权力Insert,则用户权值:Enable = Enable + 2;
假如一个用户有第三项权力Update,则用户权值:Enable = Enable + 4;
假如一个用户有第四项权力Delete,则用户权值:Enable = Enable + 8;
为什么依次+1、+2、+4、+8,而不是+1、+2、+3、+4呢?
假如是+1、+2、+3、+4:
假如一个用户有第一项权力Select,则用户权值:Enable = Enable + 1;
假如一个用户有第二项权力Insert,则用户权值:Enable = Enable + 2;
假如一个用户有第三项权力Update,则用户权值:Enable = Enable + 3;
假如一个用户有第四项权力Delete,则用户权值:Enable = Enable + 4;
那么当用户权值:Enable = 3时,你就无法判断用户到底是同时拥有
第一项权力Select和第二项权力Insert两项权利,还是仅拥有第三项权力Update,
前一种权值算法就可以避免这种情况。
但是当采用前一种权值算法时,怎样由用户的权值Enable快速知道用户拥有哪几项权
力呢?假如是用户权值Enable = 5,用户拥有第一项权力Select和第三项权力Update,
注意不拥有第二项权力Insert。
下面我将用户权力列表:
权力 拥有权力用户的权值Enable
SelectItem:( 1, 3, 5, 7, 9, 11, 13, 15 )
InsertItem:( 2, 3, 6, 7, 10, 11, 14, 15 )
UpdateItem:( 4, 5, 6, 7, 12, 13, 14, 15 )


DeleteItem:( 8, 9, 10, 11, 12, 13, 14, 15 )
可见并不是权值Enable大的用户就拥有“更多”的权力,
假如 :用户权值Enable = 3,用户拥有第一项权力(Select)和第二项权力(Insert),共两项权利,
用户权值Enable = 4,用户拥有第三项权力(Update),仅一项权利。
仔细观察“用户权力列表”:
SelectItem:( 1, 3, 5, 7, 9, 11, 13, 15 ): 1 = 0 + 2 的0次幂;3 = 2 的1次幂 + 2 的0次幂;5 = 2 的2次幂 + 2 的0次幂;
InsertItem:( 2, 3, 6, 7, 10, 11, 14, 15 ): 2 = 0 + 2 的1次幂;3 = 2 的0次幂 + 2 的1次幂;6 = 2 的2次幂 + 2 的1次幂;
UpdateItem:( 4, 5, 6, 7, 12, 13, 14, 15 ): 4 = 0 + 2 的2次幂;5 = 2 的0次幂 + 2 的2次幂;6 = 2 的2次幂 + 2 的2次幂;
DeleteItem:( 8, 9, 10, 11, 12, 13, 14, 15 ):8 = 0 + 2 的3次幂;9 = 2 的0次幂 + 2 的3次幂 10 = 2 的1次幂 + 2 的3次幂;
规律总结如下:
拥有第一项权力Select用户的权值:Enable = ? + 2 的0次幂;(其中?亦必须拆成2 的N次幂相加的情况)
拥有第二项权力Insert用户的权值:Enable = ? + 2 的1次幂;(其中?亦必须拆成2 的N次幂相加的情况)
拥有第三项权力Update用户的权值:Enable = ? + 2 的2次幂;(其中?亦必须拆成2 的N次幂相加的情况)
拥有第四项权力Delete用户的权值:Enable = ? + 2 的3次幂;(其中?亦必须拆成2 的N次幂相加的情况)
到此已经很清楚了:
只要将用户的权值Enable拆成2 的N次幂相加的情况,
如果其中有N=0,则拥有第一项权力Select,
如果其中有N=1,则拥有第二项权力Insert,
如果其中有N=2,则拥有第三项权力Update,
如果其中有N=3,则拥有第四项权力Delete,
那么怎样将用户权值Enable快拆成2 的N次幂相加的情况呢?
呵呵!只要将Enable转化为二进制,从右向左依次取基,若基为的位则拥有相应权利,
举例:
(11)10=(1011)2,即拥有第一、二、四项权力,和“用户权力列表”相符;
(12)10=(1100)2,即拥有第三、四项权力,和“用户权力列表”相符;
(15)10=(1111)2,即拥有第一、二、三、四项权力,和“用户权力列表”相符;
当权利等级极其复杂时,该算法可以很快知道用户权力:
例如:
共8个等级,用户权值Enable=67;(67)10=(1000011)2,即拥有第一、六、七项权力,
用户权值Enable=67;(159)10=(10011111)2,即拥有第一、四、五、六、七、八项权力。
到此结束,希望没有浪费您的时间,对您有所启发。
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!