Thinkphp--Auth权限控制

权限介绍 


ThinkPHP 提供了一个内置的 Auth.class.php 类来实现权限控 制,这个类提供了三个表:

think_auth_rule(认证规则表)、think_auth_group(用户组 表)、think_auth_group_access(用户和组对应关系表)。

Auth.class.php中有提供三个表结构,

当然还要结合自己创建的用户 表进行对应即可。

二、简单登录

首先进入后台首页 IndexControll.class.php


正常输出 

然后

公共目录Common创建Controller文件夹  建立AuthController.class.php  

namespace Common\Controller;
 use Think\Controller;
 use Think\Auth;
class AuthController extends Controller { 
protected function _initialize() {
 $auth = new Auth();

if(!$auth->check()) {
 $this->error('没有权限'); }

}
} 

IndexController.class.php:

namespace Admin\Controller; 
use Common\Controller\AuthController;
 //继承公共目录的控制类
class IndexController extends AuthController { public function index() { echo '后台首页!'; } } 

再访问后台首页的时候,已经没有权限了。



admin/Controller目录创建

LoginController.class.php:  


<?php  
  
namespace Admin\Controller;  
  
use Think\Controller;  
  
class LoginController extends Controller {  
  
    public function index() {  
        if (IS_POST) {  
            $login = array();  
            switch (I('user', 'null', FALSE)) {  
                case 'admin':  
                    $login['u_id'] = 1;  
                    $login['user'] = 'admin';  
                    break;  
                case 'vip':  
                    $login['u_id'] = 2;  
                    $login['user'] = 'vip';  
                    break;  
                case 'guest':  
                    $login['u_id'] = 3;  
                    $login['user'] = 'guest';  
                    break;  
                default :  
                    $this->error('您输入的用户不存在!');  
            }  
        }if (count($login)) {  
            session('auth', $login);  
            $this->success('登陆成功', U('Index/index'));  
        } else {  
            $this->display();  
        }  
    }  
  
    public function logout() {  
        session('[destroy]');  
        $this->success('退出成功', U('Login/index'));  
    }  
  
}  


view/Login  index.html


<!DOCTYPE html>  
<html>  
    <head>  
        <title>后台页面</title>  
        <meta charset="UTF-8">  
        <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    </head>  
    <body>  
        <form method="post" action="{:U('Login/index')}">  
            <p><font color='red'>admin用户为管理员,可以进入后台页面</font></p>  
            <p><font color='purple'>vip用户为会员,可以进入后台页面</font></p>  
            <p><font color='blue'>guest用户游客,不可以进入后台页面</font></p>  
            <p>用户:<input type="text" name="user"/></p>  
            <p><input type="submit" value="提交"/></p>  
        </form>  
    </body>  
</html>  


完善 AuthController 类的权限验证过程。


 class AuthController extends Controller {

 protected function _initialize() {

 $sess_auth = session('auth'); 

if (!$sess_auth) { 

$this->error('非法访问!正在跳转登录页面!', U('Login/index'));

    } 

if ($sess_auth['uid'] == 1) { return true;

 } $auth = new Auth();

 if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/' .ACTION_NAME, $sess_auth['uid'])){ 

$this->error('没有权限', U('Login/logout')); 

       } 

   } 


之前要导入数据 

表的结构 `think_auth_group`
--


CREATE TABLE IF NOT EXISTS `think_auth_group` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `title` char(100) NOT NULL DEFAULT '',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `rules` char(80) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;


--
-- 转存表中的数据 `think_auth_group`
--


INSERT INTO `think_auth_group` (`id`, `title`, `status`, `rules`) VALUES
(1, '默认管理组', 1, '1,2,3');


-- --------------------------------------------------------


--
-- 表的结构 `think_auth_group_access`
--


CREATE TABLE IF NOT EXISTS `think_auth_group_access` (
  `uid` mediumint(8) unsigned NOT NULL,
  `group_id` mediumint(8) unsigned NOT NULL,
  UNIQUE KEY `uid_group_id` (`uid`,`group_id`),
  KEY `uid` (`uid`),
  KEY `group_id` (`group_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


--
-- 转存表中的数据 `think_auth_group_access`
--


INSERT INTO `think_auth_group_access` (`uid`, `group_id`) VALUES
(2, 1);


-- --------------------------------------------------------


--
-- 表的结构 `think_auth_rule`
--


CREATE TABLE IF NOT EXISTS `think_auth_rule` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `name` char(80) NOT NULL DEFAULT '',
  `title` char(20) NOT NULL DEFAULT '',
  `type` tinyint(1) NOT NULL DEFAULT '1',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `condition` char(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;


--
-- 转存表中的数据 `think_auth_rule`
--


INSERT INTO `think_auth_rule` (`id`, `name`, `title`, `type`, `status`, `condition`) VALUES
(1, 'Admin/Index/index', '后台首页', 1, 1, '');


-- 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值