zoukankan      html  css  js  c++  java
  • thinphp中auth认证方法使用

    一、获取Auth类
    1、ThinkPHP3.1.3完整版:http://www.thinkphp.cn/down/338.html
    2、OneThink1.0正式版:https://github.com/liu21st/onethink

    二、建立需要的数据表
    打开Auth.class.php,将其中有关数据库的注释部分复制后,
    到phpmyadmin中执行,
    可得到三个表:think_auth_rule、think_auth_group、think_auth_group_access,
    并按实际情况修改前缀。
    实际上,这三个表的名称不是规定死的,可通过配置来进行更改,
    如何更改,请看本文后面使用心得部分。
    注意:OneThink附带和3.1.3中附带的的auth_rule表稍有不同(不影响使用的改变)。

    三、建立用户表
    要有自增id,其他随意。

    四、添加认证规则
    数据表:think_auth_rule

    其中name是最重要的字段:分组/控制器/方法(3.1.3)模块/控制器/方法(3.2)。
    顺序不能改变,大小写随意。Auth类中会统一转成小写。
    不过,haran建议还是和项目中的命名规范统一。
    其次是type。一般情况下设定为1。
    (最后存入用户权限的就是type为1的规则)

    五、添加用户组(角色)
    数据表:think_auth_group

    其中rules字段最为重要,就是控制哪个组有哪些权限。

    六、添加用户和组对应关系

    此表实际上就是member和auth_group的中间表,
    规定哪个用户(uid)属于哪个组(group_id)

    七、新建基础控制器并写入初始化方法
    3.1.3
    在项目的Lib/Action中新建CommonAction.class.php

    1. <?php
    2.  class CommonAction extends Action{
    3.     public function _initialize(){
    4.        //类库位置应该位于ThinkPHPExtendLibraryORGUtil
    5.        import('ORG.Util.Auth');//加载类库
    6.        $auth=new Auth();
    7.        if(!$auth->check(GROUP_NAME . '/' . MODULE_NAME.'/'.ACTION_NAME,session('uid'))){
    8.             $this->error('你没有权限');
    9.        }
    10.     }
    11.  }

    在Application/Common/Controller中新建CommonController.class.php

     

    1. <?php
    2.  namespace CommonController;
    3.  use ThinkController;
    4.  class CommonController extends Controller {
    5.     public function _initialize () {
    6.         $AUTH = new ThinkAuth();
    7.         //类库位置应该位于ThinkPHPLibraryThink
    8.         if(!$AUTH->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, session('uid'))){
    9.             $this->error('没有权限');
    10.         }
    11.     }
    12.  }

    八、新建后台首页

    1. <?php
    2.  class IndexAction extends CommonAction {
    3.     public function index () {
    4.         echo '后台首页';
    5.     }
    6.  }

     

     

     

    1. <?php
    2.  namespace AdminController;
    3.  use CommonControllerCommonController;
    4.  class IndexController extends CommonController {
    5.     
    6.     public function index () {
    7.         echo '后台首页';
    8.     }
    9.  }

     

    注意命名应和auth_rule表中的name完全对应。

    使用心得:
    一、使用Auth认证,只要表命名正确,甚至不必在config.php中定义任何一项即可使用。
    二、如何进行自定义?
    在项目的配置文件中定义:

    1. //Auth权限设置
    2.     'AUTH_CONFIG' => array(
    3.         'AUTH_ON' => true,  // 认证开关
    4.         'AUTH_TYPE' => 1, // 认证方式,1为实时认证;2为登录认证。
    5.         'AUTH_GROUP' => 'auth_group', // 用户组数据表名
    6.         'AUTH_GROUP_ACCESS' => 'auth_group_access', // 用户-用户组关系表
    7.         'AUTH_RULE' => 'auth_rule', // 权限规则表
    8.         'AUTH_USER' => 'member', // 用户信息表
    9.     ),

     

    其中AUTH_TYPE设为2后,在SESSION中就会有类似数组

     

    1. [_AUTH_LIST_31] => Array
    2.         (
    3.             [0] => admin/index/index
    4.             [1] => admin/base/index
    5.             [2] => admin/rbac/role
    6.             [3] => admin/rbac/member
    7.             [4] => admin/rbac/node
    8.         )

    改变以上的'AUTH_GROUP','AUTH_GROUP_ACCESS','AUTH_RULE','AUTH_USER'配置项,即可自定义Auth认证所需四张表,但注意加上表前缀。

     

     

     

  • 相关阅读:
    Python异常处理
    奇异值分解(SVD)详解及其应用
    上楼梯问题
    Python面向对象(特殊成员)
    Best Time to Buy and Sell Stock II
    String to Integer (atoi)
    Gas Station
    N-Queens II
    Letter Combinations of a Phone Number
    N-Queens
  • 原文地址:https://www.cnblogs.com/chinalorin/p/5844560.html
Copyright © 2011-2022 走看看