本文實例講述了thinkPHP5 ACL用戶權限模塊用法。分享給大家供大家參考,具體如下:
最近學習thinkphp5,和以前3.X版本是完全不是一個概念。學習thinkphp5的朋友要注意命名空間思想。
最近做的一個項目,一個檢測管理系統,由于為了以后做APP需要,才切換到thinkphp5作為以后的擴展API用的。今天完成的是用戶權限控制模塊。我把這個mark下來
數據庫:
role數據庫表:
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(20) NOT NULL COMMENT '角色名稱',`pid` smallint(6) DEFAULT NULL COMMENT '父角色ID',`rule_name` text COMMENT '規則唯一英文標識,全小寫',`type` varchar(50) DEFAULT '' COMMENT '權限規則分類,請加應用前綴,如admin_',`status` tinyint(1) unsigned DEFAULT NULL COMMENT '狀態',`remark` varchar(255) DEFAULT NULL COMMENT '備注',`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '創建時間',`update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新時間',`listorder` int(3) NOT NULL DEFAULT '0' COMMENT '排序字段',
auth_rule數據庫表:
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '規則id,自增主鍵',`module` varchar(20) NOT NULL COMMENT '規則所屬module',`type` varchar(30) NOT NULL DEFAULT '1' COMMENT '權限規則分類,請加應用前綴,如admin_',`name` varchar(255) NOT NULL DEFAULT '' COMMENT '規則唯一英文標識,全小寫',`param` varchar(255) DEFAULT NULL COMMENT '額外url參數',`title` varchar(20) NOT NULL DEFAULT '' COMMENT '規則中文描述',`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否有效(0:無效,1:有效)',`condition` varchar(300) NOT NULL DEFAULT '' COMMENT '規則附加條件',
用戶表里面增加:
`pools` varchar(20) DEFAULT '' COMMENT '權限池',`roleId` smallint(5) NOT NULL DEFAULT '0' COMMENT '權限id',
代碼如下:
iAuth.php 權限認證的公共庫文件
class iAuth{  public $user = null;  //默認配置  protected $_config = array(  );  public function __construct() {  }  /**   * 檢查權限   * @param name string|array 需要驗證的規則列表,支持逗號分隔的權限規則或索引數組   * @param uid int      認證用戶的id   * @param relation string  如果為 'or' 表示滿足任一條規則即通過驗證;如果為 'and'則表示需滿足所有規則才能通過驗證   * @return boolean      通過驗證返回true;失敗返回false   */  public function check($uid,$name,$relation='or') {    if(empty($uid)){      return false;    }    if($uid==1){      return true;    }    if (is_string($name)) {      $name = strtolower($name);      if (strpos($name, ',') !== false) {        $name = explode(',', $name);      } else {        $name = array($name);      }    }    $list = array(); //保存驗證通過的規則名    //獲取用戶信息    $this->getUserInfo($uid);//獲取用戶信息,一維數組    $groups= $this->user['roleId'];    if(in_array(1, $groups)){      return true;    }    if(empty($groups)){      return false;    }    $rules = self::get_rules($this->user['roleId']);    if(in_array($name,$rules))    {      return true;    }    return false;  }  /**   * 獲得用戶資料   */  private function getUserInfo(&$uid) {    if(!isset($this->user)){      $user = new Users($uid);      $this->user = $user->fields;    }    return $this->user;  }  /**   * 獲取驗證規則   * @param int $id   */  public static function get_rules($id)  {    if(empty($id)) return false;    $rules = Cache::get(self::$cache_prefix . $id);    if(empty($rules))    {      $model = Db::name('role');      $model->where('id',$id);      $rules = $model->find();      $rules['rule_name'] = explode(',',strtolower($rules['rule_name']));      //設置緩存      Cache::set(self::$cache_prefix,$rules);    }    return $rules;  }}Common.php 通用函數類庫
/** * 檢測用戶id * @param name string|array 需要驗證的規則列表,支持逗號分隔的權限規則或索引數組 * @param uid int      認證用戶的id */function sp_auth_check($uid, $name=null){  if(empty($uid)) return false;  if(empty($name)){    $name=strtolower(MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME);  }  $iAuth_obj = new /app/Common/Lib/iAuth();  return $iAuth_obj->check($uid);}AdminbaseController.php 后臺管理的父控制器類
class AdminbaseController extends Controller{  public $uid = 0;  //用戶實例  public $userObj = null;  /**   * 構造函數   * Adminbase constructor.   */  public function __construct()  {    parent::__construct();  }  public function _initialize()  {    $this->uid = Session::read('AdminId');    if(!empty($this->uid ))    {      //檢測過已經登錄了      $this->userObj = Db::name('users')->where('uid',$this->uid)->find();      if(!$this->check_access($this->uid))      {        $this->error("您沒有訪問權限!",Url::build('admin/index/login'));        exit();      }      $this->assign('admin',$this->userObj);    }    else    {      //沒有登錄的      $this->error("您還沒有登錄!",Url::build('admin/index/login'));      exit();    }  }  /**   * 檢測權限   * @param $uid   */  private function check_access(&$uid)  {    if($uid == 1)    {      //超級管理員      return true;    }    $request = Request::instance();    //如果不是這個應用池的賬戶也不通過    $pools = explode(',',$this->userObj['pools']);    if(!in_array(strtolower($request->module()), $pools))  return false;    $rule = $request->module() . '_' . $request->controller() . '_' . $request->action() ;    $no_need_check_rules = Config::get('inc_auth.no_need_check_rules');    if(!in_array(strtolower($rule),$no_need_check_rules))    {      //驗證權限      return sp_auth_check($uid);    }    else    {      return true;    }  }}inc_auth.php 認證配置文件
$config['no_need_check_rules'] = array('admin_index_index','admin_index_login');希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選