thinkphp的RBAC

sql

--用户表
CREATE TABLE IF NOT EXISTS `sw_manager` (
  `mg_id` int NOT NULL AUTO_INCREMENT,
  `mg_name` varchar(20) NOT NULL comment '名称',
  `mg_pwd` varchar(32) NOT NULL comment '密码',
  `mg_time` int unsigned NOT NULL comment '时间',
  `mg_role_id` tinyint(1) unsigned not null default 0 comment '角色id',
  PRIMARY KEY (`mg_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

--权限表
CREATE TABLE IF NOT EXISTS `sw_auth` (
  `auth_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `auth_name` varchar(20) NOT NULL comment '名称',
  `auth_pid` smallint(6) unsigned NOT NULL comment '父id',
  `auth_c` varchar(32) not null default '' comment '模块',
  `auth_a` varchar(32) not null default '' comment '操作方法',
  `auth_path` varchar(32) NOT NULL comment '全路径',
  `auth_level` tinyint not null default 0 comment '权限级别012',
  PRIMARY KEY (`auth_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

--角色表
CREATE TABLE IF NOT EXISTS `sw_role` (
  `role_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `role_name` varchar(20) NOT NULL comment '角色名称',
  `role_auth_ids` varchar(128) not null default '' comment '权限ids,1,2,5',
  `role_auth_ac` text comment '模块-操作',
  PRIMARY KEY (`role_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

`role_auth_ac`=”Company-show,Cat-mag,Product-list”

角色:
    董事长
    总监
    高级经理
    经理
    项目经理
    业务主管
	客服
	技术支持
	美工
    员工
    

CREATE TABLE IF NOT EXISTS `sw_auth` (
  `auth_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `auth_name` varchar(20) NOT NULL comment '名称',
  `auth_pid` smallint(6) unsigned NOT NULL comment '父id',
  `auth_c` varchar(32) not null default '' comment '模块',
  `auth_a` varchar(32) not null default '' comment '操作方法',
  `auth_path` varchar(32) NOT NULL comment '全路径父级全路径与本身id做连接,如果没有父级,全路径就是本身id值,用于排序',
  PRIMARY KEY (`auth_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

insert into sw_auth values (null,'商品管理',0,'','','1');
insert into sw_auth values (null,'商品列表',1,'Goods','showlist','1-2');
insert into sw_auth values (null,'添加商品',1,'Goods','add','1-3');
insert into sw_auth values (null,'用户评论',1,'User','pinglun','1-4');

insert into sw_auth values (null,'订单管理',0,'','','5');
insert into sw_auth values (null,'订单列表',5,'Order','showlist','5-6');
insert into sw_auth values (null,'订单查询',5,'Order','view','5-7');

insert into sw_auth values (null,'文章管理',0,'','','8');
insert into sw_auth values (null,'文章列表',8,'Article','showlist','8-9');

insert into sw_auth values (null,'权限管理',0,'','','10');
insert into sw_auth values (null,'管理员列表',10,'Manager','showlist','10-11');
insert into sw_auth values (null,'角色管理',10,'Role','showlist','10-12');
insert into sw_auth values (null,'权限管理',10,'Auth','showlist','10-13');

CREATE TABLE IF NOT EXISTS `sw_role` (
  `role_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `role_name` varchar(20) NOT NULL comment '角色名称',
  `role_auth_ids` varchar(128) not null default '' comment '权限ids,1,2,5',
  `role_auth_ac` text comment '模块-操作',
  PRIMARY KEY (`role_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

insert into sw_role values (null,'经理','5,6,7','Order-showlist,Order-view');
insert into sw_role values (null,'员工','1,2,3,4','Goods-showlist,Goods-add,User-pinglun');

alter table sw_manager add mg_time int UNSIGNED not null comment '时间';
alter table sw_manager add mg_role_id tinyint UNSIGNED not null default 0 comment '角色id';
    

RoleAction

<?php

import("@.components.AdminAction");
class RoleAction extends AdminAction{
    function showlist(){
        $info = D("Role")->select();
        
        $this -> assign('info',$info);
        $this -> display();
    }
    
    //分配权限方法
    function distribute($role_id){
        
        //表单数据收集
        if(!empty($_POST)){
            //给角色分配具体权限
            $rst = D("Role")->distributeAuth($_POST['auth_name'],$role_id);  //普通模型方法
            if($rst){
                $this -> success("分配权限成功",U("Role/showlist"));
            }
        }else {
            //获得全部权限信息
            $p_auth = D("Auth")->where("auth_level=0")->select();
            $s_auth = D("Auth")->where("auth_level=1")->select();
            $t_auth = D("Auth")->where("auth_level=2")->select();

            //根据$role_id查询对应角色名称
            $role_info = D("Role")->getByRole_id($role_id);
            $role_name = $role_info['role_name'];  //角色名称
            $role_auth_ids = $role_info['role_auth_ids'];  //权限id值
            $this -> assign('role_name', $role_name);
            $this -> assign('role_auth_ids', explode(',',$role_auth_ids));

            $this -> assign('p_auth',$p_auth);
            $this -> assign('s_auth',$s_auth);
            $this -> assign('t_auth',$t_auth);

            $this -> display();
        }
    }
}

IndexAction

<?php

//后台主架构控制器

class IndexAction extends Action{
    //默认调用index方法
    function index(){
        $this -> display();
    }
    
    //"品"字头部
    function head(){
        //查看系统有哪些常量可以使用
        
        //获得全部常量信息,true,常量根据类型进行分类显示
        //var_dump(get_defined_constants(true));
        $this -> display();
    }
    
    //"品"字左边
    function left(){
        //用户--角色--权限
        //给左边传递数据,可以直接使用
        //$_SESSION['mg_id']
        //manager    role    auth
        $model = M();
        
        $sql = "select b.role_auth_ids from sw_manager a join sw_role b on a.mg_role_id=b.role_id where a.mg_id=".$_SESSION['mg_id'];
        
        $info = $model -> query($sql);
        $auth_ids = $info[0]['role_auth_ids'];
        
        //查询具体权限
        //查询父权限
        $sql = "select * from sw_auth where auth_level=0";
        if($_SESSION['mg_id'] != 1){
            $sql .=" and auth_id in ($auth_ids)";
        }
        $p_auth_info = $model -> query($sql);
        //查询子权限
        $sql = "select * from sw_auth where auth_level=1";
        if($_SESSION['mg_id'] != 1){
            $sql .=" and auth_id in ($auth_ids)";
        }
        $s_auth_info = $model -> query($sql);

        $this -> assign('p_auth', $p_auth_info);
        $this -> assign('s_auth', $s_auth_info);
        
        $this -> display();
    }
    
    function right(){
        $this -> display();
    }
}

ManagerAction

<?php

//后台管理员控制器

class ManagerAction extends Action{
    //登录系统
    function login(){
        //读取语言变量信息
        //L(名称)  读取指定语言信息
        //L()     把全部语言以数组形式给我们返回
        //show_bug(L());
        
        
        
        if(!empty($_POST)){
            //判断验证码是否正确
            //$_SESSION['verify']
            //让用户提交过来的验证码与session的做比较
            if(md5($_POST['captcha']) == $_SESSION['verify']){
                //用户名和密码校验
                //在数据model模型里边,自定义一个方法校验用户名和密码
                $manager_model = D("Manager");
                $user_info = $manager_model -> checkNamePwd($_POST['mg_name'],$_POST['mg_pwd']);
                //如果$user_info不等于false,就说明用户名和密码是正确的
                if($user_info !== false){
                    //持久化用户信息(id和名字)
                    session("mg_name",$user_info['mg_name']);
                    session("mg_id",$user_info['mg_id']);
                    $this -> redirect("Index/index");
                } else {
                    echo "用户名或密码错误!";
                }
            } else {
                echo "验证码不正确";
            }
        }
        
        $this -> assign('language',L());
        $this -> display();
    }
    
    //退出系统
    function logout(){
        //删除session信息
        session(null);
        $this -> redirect("Manager/login");
    }
    
    //生成验证码
    function verifyImg1(){
        //手动加载对应的类文件  include()引入
        //echo Image::buildImageVerify();
        
        //ThinkPHP/Common/common.php
//        show_bug(class_exists('World'));
//        //shop/Lib/hello/world.class.php
//        import("@.hello.world");  
//        show_bug(class_exists("World"));
//        
//        show_bug(class_exists('Orange'));
//        import("@.apple.orange");
//        show_bug(class_exists('Orange'));
        
        //引入框架核心类文件
//        show_bug(class_exists('Driver'));
//        import("think.car.driver");
//        show_bug(class_exists('Driver'));
        
        //第三方类库文件引入
//        show_bug(class_exists('Pink'));
//        import("ORG.red.pink");
//        show_bug(class_exists('Pink'));
        
//        import("ORG.Util.Image");
//        echo Image::buildImageVerify();
        
        //引入特殊类文件
//        show_bug(class_exists('Banana'));
//        //shop/Lib/apple/banana/good/fresh.class.php
//        import("@.apple.banana#good#fresh");
//        show_bug(class_exists('Banana'));
        
    }
    
    //生成验证码
    function verifyImg(){
        import("ORG.Util.Image");
        echo Image::buildImageVerify();
    }
    
    function showlist(){
        //获得全部管理员信息
        $info = D("Manager")->select();
        
        //获得角色信息
        $role = D("Role")->select();
        //把角色变为一维数组信息 array(id值->名称,id值->名称...)
        $role_info = array();
        foreach($role as $k => $v){
            $role_info[$v['role_id']] = $v['role_name'];
        }
        $this -> assign('role_info', $role_info);
        
        
        $this -> assign("info", $info);
        $this -> display();
    }
    //添加管理员
    function add(){
        //判断form提交
        if(!empty($_POST)){
            $manager_model = D("Manager");
            //给manager存入数据
            $_POST['mg_pwd'] = "123456";
            $_POST['mg_time'] = time();
            $manager_model -> create();
            $rst = $manager_model -> add();
            if($rst){
                $this -> success("添加管理员成功",U("Manager/showlist"));
            }
        }else {
            //获得角色信息
            $role = D("Role")->select();
            //把角色变为一维数组信息 array(id值->名称,id值->名称...)
            $role_info = array();
            foreach($role as $k => $v){
                $role_info[$v['role_id']] = $v['role_name'];
            }
            $this -> assign('role_info', $role_info);

            $this -> display();
        }
    }
}

AuthAction

<?php

//权限控制器

class AuthAction extends Action{
    function showlist(){
        
        //获得全部权限
        $info = D("Auth")->order("auth_path")->select();
        //权限父子级有缩进关系
        foreach($info as $k => $v){
            $info[$k]['auth_name'] = str_repeat("-/",$v['auth_level']).$info[$k]['auth_name'];
        }
        $this -> assign("info",$info);
        
        $this -> display();
    }
    
    function add(){
        //判断表单是否提交数据
        if(!empty($_POST)){
//            /show_bug($_POST);
            //在model模型里边制作一个方法处理权限添加
            $rst = D("Auth")->saveAuth($_POST);
            if($rst){
                $this -> success("添加权限成功",U("Auth/showlist"));
            }
        } else {
            $info = D("Auth")->where('auth_level<2')->order("auth_path")->select();
            //权限父子级有缩进关系
            foreach($info as $k => $v){
                $info[$k]['auth_name'] = str_repeat("-/",$v['auth_level']).$info[$k]['auth_name'];
            }
            //show_bug($info);
            $authinfo = array(); //array(1=>商品管理,2=>商品列表...)
            foreach($info as $kk => $vv){
                $authinfo[$vv['auth_id']] = $vv['auth_name'];
            }
            $this -> assign("authinfo",$authinfo);
            $this -> display();
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值