This article mainly introduces the binary cross-Permission micro php class. For more information, see
This article mainly introduces the binary cross-Permission micro php class. For more information, see
Highlights:
1. Multi-to-Multi-cross scenario allocation;
2. New Features of php-Use of closures and use of the batch processing function array_walk/array_flip;
3. bitwise Operations & use cases;
Use Cases:
1. Common addition, deletion, modification, and query (the n value of 2 is stored in the database );
2. Authentication, third-party account binding, and multi-category selection;
The Code is as follows:
Class s_allow {
// Declare the application scenario and task list
Public $ scene, $ case_list = array ();
// Declare the specified user role, allowed value, and allowed list
Public $ allow_value = 0, $ allow_list = array ();
// Initialization scenario 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 );
}
}
// Obtain the list of all tasks
Function case_list ($ mode = false ){
$ Case_list = config ($ this-> scene, 'allow ');
Return $ mode? $ Case_list: array_keys ($ case_list );
}
// Obtain 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 or not to allow
Function is_allow ($ case ){
$ Case_list = array_flip ($ this-> case_list );
Return (bool) ($ this-> allow_value & pow (2, $ case_list [$ case]-1 ));
}
}
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
,