Home  >  Article  >  Backend Development  >  Binary Cross Permission Mini PHP Class Sharing_PHP Tutorial

Binary Cross Permission Mini PHP Class Sharing_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:29897browse

Highlights:
1. Many-to-many cross-scenario allocation;
2. PHP new features - use of closures, use of batch functions array_walk/array_flip;
3. Bit operations & use cases;

Usage scenarios:
1. Common additions, deletions and modifications (2 to the Nth power value is stored in the database);
2. Authentication, third-party account binding, multi-category selection;

Copy code The code is as follows:

class s_allow{

//Declare usage scenarios, task list
public $scene,$case_list=array();

//Declare the specified user role, allowed value, and allowed list
public $allow_value=0,$allow_list=array();


//Initialize scene and role
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);
}
}


//Get all task lists
function case_list($mode=false){
$case_list=config($this->scene,'allow');
return $mode ? $case_list : array_keys($case_list);
}


//Get the user task 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);
}


//Calculate the allowed value
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);
}


//Determine whether it is allowed
function is_allow($case){
$case_list=array_flip($this->case_list);
return (bool)($this-> allow_value & pow(2,$case_list[$case]-1));
}
}

Copy code The code is as follows:

[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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/730061.htmlTechArticleHighlights: 1. Many-to-many cross-scenario allocation; 2. PHP new features - closure usage, batch processing Function array_walk/array_flip is used; 3. Bit operations
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn