栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

thinkPHP5框架auth权限控制类与用法示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

thinkPHP5框架auth权限控制类与用法示例

本文实例讲述了thinkPHP5框架auth权限控制类。分享给大家供大家参考,具体如下:

这个是比较简单的用法:

直接把类贴出来,这里我改了,我没有用uid,因为我建的表是admin表,所以代码里对应查询改成了aid

还有表名,我都去掉了前缀

 
// +----------------------------------------------------------------------
namespace auth;

//数据库

class Auth{
  //默认配置
  protected $_config = array(
    'auth_on'      => true,    // 认证开关
    'auth_type'     => 1,      // 认证方式,1为实时认证;2为登录认证。
    'auth_group'    => 'auth_group',    // 用户组数据表名
    'auth_group_access' => 'group_access', // 用户-用户组关系表
    'auth_rule'     => 'auth_rule',     // 权限规则表
    'auth_user'     => 'admin'// 用户信息表
  );
  public function __construct() {
    if (config('auth_config')) {
      //可设置配置项 auth_config, 此配置项为数组。
      $this->_config = array_merge($this->_config, config('auth_config'));
    }
  }
  
  public function check($name, $uid, $type=1, $mode='url', $relation='or') {
    if (!$this->_config['auth_on'])
      return true;
    $authList = $this->getAuthList($uid,$type); //获取用户需要验证的所有有效规则列表
    if (is_string($name)) {
      $name = strtolower($name);
      if (strpos($name, ',') !== false) {
 $name = explode(',', $name);
      } else {
 $name = array($name);
      }
    }
    $list = array(); //保存验证通过的规则名
    if ($mode=='url') {
      $REQUEST = unserialize( strtolower(serialize($_REQUEST)) );
    }
    foreach ( $authList as $auth ) {
      $query = preg_replace('/^.+?/U','',$auth);
      if ($mode=='url' && $query!=$auth ) {
 parse_str($query,$param); //解析规则中的param
 $intersect = array_intersect_assoc($REQUEST,$param);
 $auth = preg_replace('/?.*$/U','',$auth);
 if ( in_array($auth,$name) && $intersect==$param ) { //如果节点相符且url参数满足
   $list[] = $auth ;
 }
      }else if (in_array($auth , $name)){
 $list[] = $auth ;
      }
    }
    if ($relation == 'or' and !empty($list)) {
      return true;
    }
    $diff = array_diff($name, $list);
    if ($relation == 'and' and empty($diff)) {
      return true;
    }
    return false;
  }
  
  public function getGroups($uid) {
    static $groups = array();
    if (isset($groups[$uid]))
      return $groups[$uid];
    $user_groups = thinkDb::name($this->_config['auth_group_access'])
      ->alias('a')
      ->join($this->_config['auth_group']." g", "g.id=a.group_id")
      ->where("a.aid='$uid' and g.status='1'")
      ->field('aid,group_id,title,rules')->select();
    $groups[$uid] = $user_groups ? $user_groups : array();
    return $groups[$uid];
  }
  
  protected function getAuthList($uid,$type) {
    static $_authList = array(); //保存用户验证通过的权限列表
    $t = implode(',',(array)$type);
    if (isset($_authList[$uid.$t])) {
      return $_authList[$uid.$t];
    }
    if( $this->_config['auth_type']==2 && isset($_SESSION['_auth_list_'.$uid.$t])){
      return $_SESSION['_auth_list_'.$uid.$t];
    }
    //读取用户所属用户组
    $groups = $this->getGroups($uid);
    $ids = array();//保存用户所属用户组设置的所有权限规则id
    foreach ($groups as $g) {
      $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
    }
    $ids = array_unique($ids);
    if (empty($ids)) {
      $_authList[$uid.$t] = array();
      return array();
    }
    $map=array(
      'id'=>array('in',$ids),
      'type'=>$type,
      'status'=>1,
    );
    //读取用户组所有权限规则
    $rules = thinkDb::name($this->_config['auth_rule'])->where($map)->field('condition,name')->select();
    //循环规则,判断结果。
    $authList = array();  //
    foreach ($rules as $rule) {
      if (!empty($rule['condition'])) { //根据condition进行验证
 $user = $this->getUserInfo($uid);//获取用户信息,一维数组
 $command = preg_replace('/{(w*?)}/', '$user['\1']', $rule['condition']);
 //dump($command);//debug
 @(eval('$condition=(' . $command . ');'));
 if ($condition) {
   $authList[] = strtolower($rule['name']);
 }
      } else {
 //只要存在就记录
 $authList[] = strtolower($rule['name']);
      }
    }
    $_authList[$uid.$t] = $authList;
    if($this->_config['auth_type']==2){
      //规则列表结果保存到session
      $_SESSION['_auth_list_'.$uid.$t]=$authList;
    }
    return array_unique($authList);
  }
  
  protected function getUserInfo($uid) {
    static $userinfo=array();
    if(!isset($userinfo[$uid])){
$userinfo[$uid]=thinkDb::name($this->_config['auth_user'])->where(array('aid'=>$uid))->find();
    }
    return $userinfo[$uid];
  }
}

Auth.php放在extend下面的auth目录里,命名空间为auth

然后在使用的控制器中使用构造方法,或者继承一个使用了构造方法的控制器,构造方法如下:

public function _initialize()
{
    $aid = 1;
    $auth = new authAuth();
    $request = Request::instance();
    $au = $auth->check($request->module() . '/' . $request->controller() . '/' . $request->action(), $aid);
    if (!$au) {// 第一个参数是规则名称,第二个参数是用户UID
      
      $this->error('你没有权限');
    }
}

上面的$aid应该是用户登录以后获取的,正常情况下应该是session()获取的用户id

相关业务逻辑可以自行判断,剩下的就是添加全县,用户组添加啊,等等系列增删改查了。

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend frameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/39727.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号