二進(jìn)制交叉權(quán)限微型php類分享
靚點(diǎn):
1、多對(duì)多交叉場(chǎng)景分配;
2、php新特性-閉包使用、批處理函數(shù)array_walk/array_flip使用;
3、位運(yùn)算&使用案例;
使用場(chǎng)景:
1、常見(jiàn)增刪改查(2的N次方值入庫(kù));
2、認(rèn)證、第三方賬號(hào)綁定、多分類選擇;
<?php
class s_allow{
//聲明使用場(chǎng)景,任務(wù)清單
public $scene,$case_list=array();
//聲明指定用戶角色、允許值、允許清單
public $allow_value=0,$allow_list=array();
//初始化場(chǎng)景、角色
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);
}
}
//取得全部任務(wù)清單
function case_list($mode=false){
$case_list=config($this->scene,'allow');
return $mode ? $case_list : array_keys($case_list);
}
//取得用戶任務(wù)清單
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);
}
//計(jì)算允許值
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
相關(guān)文章
Laravel 5.5 的自定義驗(yàn)證對(duì)象/類示例代碼詳解
Laravel 5.5 將提供一個(gè)全新的自定義驗(yàn)證規(guī)則的對(duì)象,以作為原來(lái)的 Validator::extend 方法的替代。接下來(lái)通過(guò)示例代碼給大家介紹laravel 自定義驗(yàn)證對(duì)象和類,需要的朋友參考下吧2017-08-08
PHP實(shí)現(xiàn)WebService的簡(jiǎn)單示例和實(shí)現(xiàn)步驟
這篇文章主要介紹了PHP實(shí)現(xiàn)WebService的簡(jiǎn)單示例和實(shí)現(xiàn)步驟,本文直接給出示例代碼并分步驟講解,需要的朋友可以參考下2015-03-03
PHP中的socket_read和socket_recv區(qū)別詳解
這篇文章主要介紹了PHP中的socket_read和socket_recv區(qū)別詳解,本文從源碼上分析了這兩個(gè)函數(shù)的不同之處,需要的朋友可以參考下2015-02-02
laravel框架創(chuàng)建授權(quán)策略實(shí)例分析
這篇文章主要介紹了laravel框架創(chuàng)建授權(quán)策略,結(jié)合實(shí)例形式分析了laravel框架創(chuàng)建授權(quán)策略的相關(guān)步驟、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11
php判斷是否連接上網(wǎng)絡(luò)的方法實(shí)例詳解
本文主要介紹PHP判斷是否連接上網(wǎng)絡(luò)的方法,通過(guò)php判斷能否打開(kāi)百度頁(yè)面來(lái)判斷是否連接上網(wǎng)絡(luò)的功能,需要的朋友可以參考下2016-12-12
Laravel如何使用數(shù)據(jù)庫(kù)事務(wù)及捕獲事務(wù)失敗后的異常詳解
最近在學(xué)習(xí)Laravel,在學(xué)習(xí)中遇到了不少的問(wèn)題,所以想著總結(jié)記錄下來(lái),所以下面這篇文章主要給大家介紹了關(guān)于Laravel如何使用數(shù)據(jù)庫(kù)事務(wù)及捕獲事務(wù)失敗后異常的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10

