php 用户授权访问控制,用户权限访问控制功能

摘要:<?php

namespace app\admins\controller;

use think\Controller;

use Util\SysDb;

class Home extends Base{

public function index(){

&nbs<?php

namespace app\admins\controller;

use think\Controller;

use Util\SysDb;

class Home extends Base{

public function index(){

$role = $this->db->table('admin_groups')->where(array('gid'=>$this->_admin['gid']))->item();

if($role){

$role['rights'] = $role['rights']?json_decode($role['rights'],true):[];

}

if($role['rights']){

$where = 'mid in('.implode(',', $role['rights']).') and ishidden=0 and status=0';

$menus = $this->db->table('admin_menus')->where($where)->cates('mid');

$menus && $menus = $this->gettreeitems($menus);

}

$data['menus'] = $menus;

$data['role'] = $role;

return $this->fetch('',$data);

}

public function welcome(){

return $this->fetch();

}

private function gettreeitems($items){

$tree = [];

foreach ($items as $item) {

if(isset($items[$item['pid']])){

$items[$item['pid']]['children'][] = &$items[$item['mid']];

}else{

$tree[] = &$items[$item['mid']];

}

}

return $tree;

}

}html>

欢迎

body{margin: 0px;}

.header{width: 100%;height: 50px;line-height: 50px;background: #01AAED;color:#ffffff;}

.title{margin-left: 20px;font-size: 20px;}

.userinfo{float: right;margin-right: 10px;}

.userinfo a{text-decoration: none;color: #ffffff;}

.menu{width:200px;background: #333744;position: absolute;}

.main{position: absolute;left: 200px;right: 0px;}

.layui-collapse{border: none;}

.layui-colla-item{border-top: none;}

.layui-colla-title{background: #42485b;color: #ffffff;}

.layui-colla-content{border-top: none;padding: 0px;}

后台管理系统

{$admin.username}【{$role.title}】退出

{volist name="$menus" id="vo"}

{$vo.title}

{volist name="vo.children" id="cvo"}

{$cvo.title}

{/volist}

{/volist}

layui.use(['element','layer'], function(){

var element = layui.element;

$ = layui.jquery;

layer = layui.layer;

resetMenuHeight();

});

// 重新设置页面高度

function resetMenuHeight(){

var height = document.documentElement.clientHeight - 50;

$('#menu').height(height);

}

// 重新设置主操作区高度

function resetMainHeight(obj){

var height = parent.document.documentElement.clientHeight - 53;

$(obj).parent('div').height(height);

}

// 菜单点击

function menufire(obj){

// 获取url

var src = $(obj).attr('src');

// 设置iframe的src

$('iframe').attr('src',src);

}

// 退出登录

function logout(){

// 退出前确认

layer.confirm('确定要退出吗?',{

icon:3,

btn:['确定','取消']

},function(){

$.get('/index.php/admins/account/logout',function(res){

if(res.code>0){

layer.msg(res.msg,{'icon':2});

}else{

layer.msg(res.msg,{'icon':1});

setTimeout(function(){window.location.href='/index.php/admins/account/login';},1000);

}

},'json');

});

}

namespace app\admins\controller;

use think\Controller;

use Util\SysDb;

class Base extends Controller{

public function __construct(){

parent::__construct();

$this->_admin = session('admin');

if(!$this->_admin){

header('Location:/index.php/admins/account/login');

exit;

}

$this->assign('admin',$this->_admin);

$this->db = new SysDb;

// 判断用户是否有权限

$group = $this->db->table('admin_groups')->where(array('gid'=>$this->_admin['gid']))->item();

if(!$group){

$this->request_error('对不起,您没有权限');

}

$rights = json_decode($group['rights']);

// 当前访问的菜单

$controller = request()->controller();

$method = request()->action();

$res = $this->db->table('admin_menus')->where(array('controller'=>$controller,'method'=>$method))->item();

if(!$res){

$this->request_error('对不起,您访问的功能不存在');

}

if($res['status'] == 1){

$this->request_error('对不起,该功能已禁止使用');

}

if(!in_array($res['mid'],$rights)){

$this->request_error('对不起,您没有权限');

}

}

private function request_error($msg){

if(request()->isAjax()){

exit(json_encode(array('code'=>1,'msg'=>$msg)));

}

exit($msg);

}

}

批改老师:欧阳批改时间:2019-06-10 09:17:18

老师总结:完成的不错,后台cms管理系统,最重要的就是权限。继续加油。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值