<?php namespace Common\Controller; use Think\Controller; class CommonController extends Controller { public function _initialize() { $access = $this->checkAccess(); if($access == 0){ echo "<script>alert('Sorry, you do not currently have permission to do this...')</script>"; } } //检测session值是否存在 public function checkSession() { } //检测变量是否存在二维数组中 public function deep_in_array($value, $array) { foreach($array as $item) { if(!is_array($item)) { if ($item == $value) { return true; } else { continue; } } if(in_array($value, $item)) { return true; } else if($this->deep_in_array($value, $item)) { return true; } } return false; } //检测权限 public function checkAccess() { $controller = CONTROLLER_NAME; $action = ACTION_NAME; $access = $this->getAccess(); $con = $this->deep_in_array($controller,$access); $act = $this->deep_in_array($action,$access); if($con){ if($act){ return 1; }else{ return 0; } }else{ return 0; } } //获取权限 public function getAccess() { $sql = "select * from admin where id = 2"; $model = M(); $admin = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sql); $rid = $admin[0]["rid"]; $sqls = "select * from role where id in($rid)"; $role = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sqls); foreach($role as $k=>$v){ $jid[] = explode(",",$v['jid']); } $jid = array_unique($jid); $jid = implode(",",$jid[0]); $sqlsd = "select * from jurisdiction where id in($jid)"; $jurisdiction = $model->db(1,"mysql://root:root@127.0.0.1:3306/kd")->query($sqlsd); return $jurisdiction; } }
TP 3.2.3 权限控制源码
最新推荐文章于 2024-06-16 23:59:28 发布