温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

thinkPHP5中怎么使用 ACL用户权限模块

发布时间:2021-06-23 15:48:57 阅读:104 作者:Leah 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇文章给大家分享的是有关thinkPHP5中怎么使用 ACL用户权限模块,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

数据库:

role数据库表:

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20NOT NULL COMMENT '角色名称',
`pid` smallint(6DEFAULT NULL COMMENT '父角色ID',
`rule_name` text COMMENT '规则唯一英文标识,全小写',
`type` varchar(50DEFAULT '' COMMENT '权限规则分类,请加应用前缀,如admin_',
`status` tinyint(1) unsigned DEFAULT NULL COMMENT '状态',
`remark` varchar(255DEFAULT NULL COMMENT '备注',
`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
`listorder` int(3NOT NULL DEFAULT '0' COMMENT '排序字段',

auth_rule数据库表:

`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '规则id,自增主键',
`modulevarchar(20NOT NULL COMMENT '规则所属module',
`type` varchar(30NOT NULL DEFAULT '1' COMMENT '权限规则分类,请加应用前缀,如admin_',
`name` varchar(255NOT NULL DEFAULT '' COMMENT '规则唯一英文标识,全小写',
`param` varchar(255DEFAULT NULL COMMENT '额外url参数',
`title` varchar(20NOT NULL DEFAULT '' COMMENT '规则中文描述',
`status` tinyint(1NOT NULL DEFAULT '1' COMMENT '是否有效(0:无效,1:有效)',
`conditionvarchar(300NOT NULL DEFAULT '' COMMENT '规则附加条件',

用户表里面增加:

`pools` varchar(20DEFAULT '' COMMENT '权限池',
`roleId` smallint(5NOT 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');

以上就是thinkPHP5中怎么使用 ACL用户权限模块,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×