PHP   发布时间:2019-11-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了二进制交叉权限微型php类分享大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

靓点:
1、多对多交叉场景分配;
2、php新特性-闭包使用、批处理函数array_walk/array_flip使用;
3、位运算&使用案例;@H_489_4@

使用场景:
1、常见增删改查(2的N次方值入库);
2、认证、第三方账号绑定、多分类选择;@H_489_4@

title"> 代码如下:
php@H_489_4@

class s_allow{@H_489_4@

//声明使用场景,任务清单
public $scene,$case_list=array();@H_489_4@

//声明指定用户角色、允许值、允许清单
public $allow_value=0,$allow_list=array();@H_489_4@


//初始化场景、角色
function __construct($scene,$allow_value=0){
$this->scene=$scene;
$this->case_list=$this->case_list(true);@H_489_4@

if ($allow_value) {
$this->allow_value=$allow_value;
$this->allow_list=$this->allow_list($allow_value,truE);
}
}@H_489_4@


//取得全部任务清单
function case_list($mode=falsE){
$case_list=config($this->scene,'allow');
return $mode ? $case_list : array_keys($case_list);
}@H_489_4@


//取得用户任务清单
function allow_list($allow_value=0,$mode=falsE){
$buffer=array();
foreach ($this->case_list as $key=>$value) {
if ($allow_value & pow(2,$key-1)) {
$buffer[$key]=$value;
}
}
return $mode ? $buffer : array_keys($buffer);
}@H_489_4@


//计算允许值
function allow_value($allow_list=null){
if ($allow_list==null) $allow_list=$this->allow_list;
array_walk($allow_list,function($value,$key) use(&$allow_list){
$allow_list[$key]=pow(2,$value-1);
});
return array_sum($allow_list);
}@H_489_4@


//判断是否允许
function is_allow($casE){
$case_list=array_flip($this->case_list);
return (bool)($this->allow_value & pow(2,$case_list[$case]-1));
}
}

@H_489_4@

title"> 代码如下:
[access]
1=add
2=del
3=read
4=list
5=mod
6=detail
7=pub
8=collect
9=like
10=send@H_489_4@


[cert]
1=email
2=tel
3=qq
4=identity_card
5=real_name
6=business_license@H_489_4@


[bind]
1=qq
2=weibo
3=taobao
4=alipay
5=renren
6=weichat
7=baidu

@H_489_4@

大佬总结

以上是大佬教程为你收集整理的二进制交叉权限微型php类分享全部内容,希望文章能够帮你解决二进制交叉权限微型php类分享所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。