二進位交叉許可權微型php類詳解

來源:互聯網
上載者:User

 這篇文章主要介紹了二進位交叉許可權微型php類,需要的朋友可以參考下

靚點: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=add2=del3=read4=list5=mod6=detail7=pub8=collect9=like10=send  [cert]1=email2=tel3=qq4=identity_card5=real_name6=business_license  [bind]1=qq2=weibo3=taobao4=alipay5=renren6=weichat7=baidu
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.