国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

二進制交叉權限微型php類分享

2024-05-04 23:20:49
字體:
來源:轉載
供稿:網友

靚點:
1、多對多交叉場景分配;
2、php新特性-閉包使用、批處理函數array_walk/array_flip使用;
3、位運算&使用案例;

使用場景:
1、常見增刪改查(2的N次方值入庫);
2、認證、第三方賬號綁定、多分類選擇;

復制代碼 代碼如下:


<?php

class s_allow{

//聲明使用場景,任務清單
public $scene,$case_list=array();

//聲明指定用戶角色、允許值、允許清單
public $allow_value=0,$allow_list=array();


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

if ($allow_value) {
$this->allow_value=$allow_value;
$this->allow_list=$this->allow_list($allow_value,true);
}
}


//取得全部任務清單
function case_list($mode=false){
$case_list=config($this->scene,'allow');
return $mode ? $case_list : array_keys($case_list);
}


//取得用戶任務清單
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);
}


//計算允許值
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);
}


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

復制代碼 代碼如下:


[access]
1=add
2=del
3=read
4=list
5=mod
6=detail
7=pub
8=collect
9=like
10=send


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


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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德令哈市| 平凉市| 错那县| 盖州市| 阳江市| 沐川县| 隆昌县| 兴义市| 永宁县| 和顺县| 明水县| 刚察县| 乌苏市| 秀山| 梓潼县| 清徐县| 桓台县| 突泉县| 通河县| 南昌县| 洞口县| 冷水江市| 乌苏市| 任丘市| 温州市| 南康市| 陆良县| 潮安县| 张家川| 油尖旺区| 金沙县| 图片| 富源县| 仁寿县| 鹰潭市| 和田市| 东阿县| 榆树市| 襄城县| 呼伦贝尔市| 商都县|