基于逻辑运算的简单权限系统(原理,设计,实现) VBS 版
程序员文章站
2022-06-16 20:02:11
作者: slightboy, 时间: 2006-10-14
首发于: http://cs.alienwave.cn/topic/3...
作者: slightboy, 时间: 2006-10-14
首发于: http://cs.alienwave.cn/topic/329.aspx
看到好多同学权限判断都是用字符串 然后或分割或截取
其实对于 允许/不允许(true/false) 这种的权限, 用逻辑运算再恰当不过了
声明下: 本文针对入门和为掌握的同学, 如果已经懂了那可以无视了
可能意思表达的不是很清楚, 敬请原谅.
逻辑运算符介绍:
and: 逻辑与
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
or: 逻辑或
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1
xor: 异或
0 xor 0 = 1
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 1
not: 逻辑非
not 1 = 0
not 0 = 1
表达方式介绍:
1 表示 ture, 0 表示 false
举二位为例
第一位 表示 read 的权限, 第二位 表示 write 的权限, 可以表示一下四种权限
00 read(false) write(false)
01 read(true) write(false)
10 read(false) write(true)
11 read(true) write(true)
运算方式介绍:
还是继续上面的例子
read = 01(1), write = 10(2)
00(0) and read = 0
01(1) and read = read
10(2) and read = 0
11(3) and read = read
00(0) and write = 0
01(1) and write = 0
10(2) and write = write
11(3) and write = write
下面给出示例代码:
权限定义类(要有枚举类型该多好啊...)
class permissiontypepublic readpublic writepublic deleteprivate sub class_initializeread = 1write = 2delete = 4end subend class
权限类
class permissionsetcomponentprivate intvaluepublic property get read()read = getvalue(permission.read)end propertypublic property let read(arg)call setvalue(permission.read, arg)end propertypublic property get write()write = getvalue(permission.write)end propertypublic property let write(arg)call setvalue(permission.write, arg)end propertypublic property get delete()delete = getvalue(permission.delete)end propertypublic property let delete(arg)call setvalue(permission.delete, arg)end propertypublic property get value()value = intvalueend propertypublic property let value(arg)intvalue = argend propertypublic function getvalue(inttype)getvalue = (value and inttype) = inttypeend functionpublic sub setvalue(inttype, boolvalue)if (boolvalue) thenvalue = value or inttypeelsevalue = value and (not inttype)end ifend subend class
运用示例代码:
dim permission : set permission = new permissiontypedim permissionset : set permissionset = new permissionsetcomponentpermissionset.value = 0w("read:")permissionset.read = falsew(permissionset.value &" "& permissionset.read)permissionset.read = truew(permissionset.value &" "& permissionset.read)w("write:")permissionset.write = falsew(permissionset.value &" "& permissionset.write)permissionset.write = truew(permissionset.value &" "& permissionset.write)w("delete:")permissionset.delete = falsew(permissionset.value &" "& permissionset.delete)permissionset.delete = truew(permissionset.value &" "& permissionset.delete)function w(o)response.write("<br />"& o)end function
今天的课程就到这里, 大家可以举一反三, 下课...
首发于: http://cs.alienwave.cn/topic/329.aspx
看到好多同学权限判断都是用字符串 然后或分割或截取
其实对于 允许/不允许(true/false) 这种的权限, 用逻辑运算再恰当不过了
声明下: 本文针对入门和为掌握的同学, 如果已经懂了那可以无视了
可能意思表达的不是很清楚, 敬请原谅.
逻辑运算符介绍:
and: 逻辑与
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
or: 逻辑或
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1
xor: 异或
0 xor 0 = 1
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 1
not: 逻辑非
not 1 = 0
not 0 = 1
表达方式介绍:
1 表示 ture, 0 表示 false
举二位为例
第一位 表示 read 的权限, 第二位 表示 write 的权限, 可以表示一下四种权限
00 read(false) write(false)
01 read(true) write(false)
10 read(false) write(true)
11 read(true) write(true)
运算方式介绍:
还是继续上面的例子
read = 01(1), write = 10(2)
00(0) and read = 0
01(1) and read = read
10(2) and read = 0
11(3) and read = read
00(0) and write = 0
01(1) and write = 0
10(2) and write = write
11(3) and write = write
下面给出示例代码:
权限定义类(要有枚举类型该多好啊...)
class permissiontypepublic readpublic writepublic deleteprivate sub class_initializeread = 1write = 2delete = 4end subend class
权限类
class permissionsetcomponentprivate intvaluepublic property get read()read = getvalue(permission.read)end propertypublic property let read(arg)call setvalue(permission.read, arg)end propertypublic property get write()write = getvalue(permission.write)end propertypublic property let write(arg)call setvalue(permission.write, arg)end propertypublic property get delete()delete = getvalue(permission.delete)end propertypublic property let delete(arg)call setvalue(permission.delete, arg)end propertypublic property get value()value = intvalueend propertypublic property let value(arg)intvalue = argend propertypublic function getvalue(inttype)getvalue = (value and inttype) = inttypeend functionpublic sub setvalue(inttype, boolvalue)if (boolvalue) thenvalue = value or inttypeelsevalue = value and (not inttype)end ifend subend class
运用示例代码:
dim permission : set permission = new permissiontypedim permissionset : set permissionset = new permissionsetcomponentpermissionset.value = 0w("read:")permissionset.read = falsew(permissionset.value &" "& permissionset.read)permissionset.read = truew(permissionset.value &" "& permissionset.read)w("write:")permissionset.write = falsew(permissionset.value &" "& permissionset.write)permissionset.write = truew(permissionset.value &" "& permissionset.write)w("delete:")permissionset.delete = falsew(permissionset.value &" "& permissionset.delete)permissionset.delete = truew(permissionset.value &" "& permissionset.delete)function w(o)response.write("<br />"& o)end function
今天的课程就到这里, 大家可以举一反三, 下课...