基于逻辑运算的简单权限系统(原理,设计,实现) VBS 版
程序员文章站
2022-03-20 14:13:38
作者:slightboy看到好多同学权限判断都是用字符串 然后或分割或截取 其实对于 允许/不允许(true/false) 这种的权限,&nb...
作者:slightboy
看到好多同学权限判断都是用字符串 然后或分割或截取
其实对于 允许/不允许(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 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0
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 permissiontype
public read
public write
public delete
private sub class_initialize
read = 1
write = 2
delete = 4
end sub
end class
权限类
class permissionsetcomponent
private intvalue
public property get read()
read = getvalue(permission.read)
end property
public property let read(arg)
call setvalue(permission.read, arg)
end property
public property get write()
write = getvalue(permission.write)
end property
public property let write(arg)
call setvalue(permission.write, arg)
end property
public property get delete()
delete = getvalue(permission.delete)
end property
public property let delete(arg)
call setvalue(permission.delete, arg)
end property
public property get value()
value = intvalue
end property
public property let value(arg)
intvalue = arg
end property
public function getvalue(inttype)
getvalue = (value and inttype) = inttype
end function
public sub setvalue(inttype, boolvalue)
if (boolvalue) then
value = value or inttype
else
value = value and (not inttype)
end if
end sub
end class
运用示例代码:
dim permission : set permission = new permissiontype
dim permissionset : set permissionset = new permissionsetcomponent
permissionset.value = 0
w("read:")
permissionset.read = false
w(permissionset.value &" "& permissionset.read)
permissionset.read = true
w(permissionset.value &" "& permissionset.read)
w("write:")
permissionset.write = false
w(permissionset.value &" "& permissionset.write)
permissionset.write = true
w(permissionset.value &" "& permissionset.write)
w("delete:")
permissionset.delete = false
w(permissionset.value &" "& permissionset.delete)
permissionset.delete = true
w(permissionset.value &" "& permissionset.delete)
function w(o)
response.write("<br />"& o)
end function
今天的课程就到这里, 大家可以举一反三, 下课...
看到好多同学权限判断都是用字符串 然后或分割或截取
其实对于 允许/不允许(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 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0
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 permissiontype
public read
public write
public delete
private sub class_initialize
read = 1
write = 2
delete = 4
end sub
end class
权限类
class permissionsetcomponent
private intvalue
public property get read()
read = getvalue(permission.read)
end property
public property let read(arg)
call setvalue(permission.read, arg)
end property
public property get write()
write = getvalue(permission.write)
end property
public property let write(arg)
call setvalue(permission.write, arg)
end property
public property get delete()
delete = getvalue(permission.delete)
end property
public property let delete(arg)
call setvalue(permission.delete, arg)
end property
public property get value()
value = intvalue
end property
public property let value(arg)
intvalue = arg
end property
public function getvalue(inttype)
getvalue = (value and inttype) = inttype
end function
public sub setvalue(inttype, boolvalue)
if (boolvalue) then
value = value or inttype
else
value = value and (not inttype)
end if
end sub
end class
运用示例代码:
dim permission : set permission = new permissiontype
dim permissionset : set permissionset = new permissionsetcomponent
permissionset.value = 0
w("read:")
permissionset.read = false
w(permissionset.value &" "& permissionset.read)
permissionset.read = true
w(permissionset.value &" "& permissionset.read)
w("write:")
permissionset.write = false
w(permissionset.value &" "& permissionset.write)
permissionset.write = true
w(permissionset.value &" "& permissionset.write)
w("delete:")
permissionset.delete = false
w(permissionset.value &" "& permissionset.delete)
permissionset.delete = true
w(permissionset.value &" "& permissionset.delete)
function w(o)
response.write("<br />"& o)
end function
今天的课程就到这里, 大家可以举一反三, 下课...
上一篇: vbs中Empty和Null的区别
下一篇: 收集的一些经典的vbs脚本大全