123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php namespace Manage\Lib;
- use Dever;
- class Auth
- {
- protected $login = true;
- public $uid;
- protected $user;
- protected $system;
- protected $info;
- protected $func;
- public $data = array();
- public function __construct()
- {
- $info = Dever::load('common', 'manage')->auth();
- if (!$info && $this->login) {
- $info['uid'] = 1;
- $info['extend']['system_id'] = 'no';
- $info['extend']['system_id'] = 1;
- $info['extend']['info_id'] = 1;
- $info['extend']['module_id'] = 1;
- $info['extend']['data_id'] = 1;
- #Dever::error('请先登录');
- }
- $this->system = Dever::db('system', 'manage')->find($info['extend']['system_id']);
- if (!$this->system) {
- Dever::error('当前系统不存在');
- }
- $this->system_info = Dever::db($this->system['info_table'])->find($info['extend']['info_id']);
- if (!$this->system) {
- Dever::error('当前系统设置错误');
- }
- $this->uid = $info['uid'];
- $this->user = Dever::db($this->system['user_table'])->find($this->uid);
- if (!$this->user) {
- Dever::error('请先登录');
- }
- $this->user['table'] = $this->system['user_table'];
- $this->user['auth'] = array('module' => '', 'menu' => '', 'func' => '');
- if ($this->user['role']) {
- $role = Dever::db($this->system['role_table'])->select(array('id' => array('in', $this->user['role'])));
- foreach ($role as $k => $v) {
- $this->user['auth']['module'] .= $v['module'] . ',';
- $this->user['auth']['menu'] .= $v['menu'] . ',';
- $this->user['auth']['func'] .= $v['auth'] . ',';
- }
- }
- if ($this->user['auth']['module']) {
- $this->user['auth']['module'] = rtrim($this->user['auth']['module'], ',');
- }
- if ($this->user['auth']['menu']) {
- $this->user['auth']['menu'] = rtrim($this->user['auth']['menu'], ',');
- }
- if ($this->user['auth']['func']) {
- $this->user['auth']['func'] = ',' . $this->user['auth']['func'];
- }
- $this->user['select'] = $info['extend'] ?? false;
- if (!$this->user['select']) {
- # 分别为系统id,系统基本信息id,模块id,模块数据id
- $this->user['select'] = array('partition' => 'no', 'system_id' => 1, 'info_id' => 1, 'module_id' => 1, 'data_id' => 1);
- }
- $this->checkModule($this->user['select']['module_id']);
- }
- # 设置功能权限
- public function getFunc($key, $name, $sort = 1, $param = '')
- {
- if (!$key) {
- $key = md5(base64_encode($name));
- }
- /*
- if ($param) {
- if (is_array($param)) {
- $param = Dever::json_encode($name);
- }
- $key = $key . '_' . md5($param);
- }*/
- if (!$this->menu) {
- return false;
- }
- $data['menu_id'] = $this->menu['id'];
- $data['key'] = $key;
- $key = $key . $data['menu_id'];
- if (isset($this->func[$key]['id'])) {
- return $this->func[$key]['id'];
- }
- $this->func[$key] = Dever::db('menu_func', 'manage')->find($data);
- $name = $this->menu['name'] . '-' . $name;
- if (!$this->func[$key]) {
- $data['name'] = $name;
- $data['sort'] = $sort;
- $id = Dever::db('menu_func', 'manage')->insert($data);
- Dever::db('menu', 'manage')->update($this->menu['id'], array('func' => 1));
- } else {
- /*
- if ($info['name'] != $name) {
- $data['name'] = $name;
- $data['sort'] = $sort;
- Dever::db('menu_func', 'manage')->update($info['id'], $data);
- Dever::db('menu', 'manage')->update($this->menu['id'], array('func' => 1));
- }*/
- $id = $this->func[$key]['id'];
- }
- if ($this->user['id'] == 1) {
- return $id;
- }
- if ($this->user['auth']['func'] && strstr($this->user['auth']['func'], ',' . $id . ',')) {
- return $id;
- }
- return false;
- }
- # 检测系统模块权限
- protected function checkModule($module_id)
- {
- if ($this->user['id'] == 1) {
- return;
- }
- if ($this->user['auth']['module'] && !Dever::check($this->user['auth']['module'], $module_id)) {
- Dever::error('无系统权限');
- }
- }
- # 检测菜单权限
- protected function checkMenu($menu, $result = true)
- {
- if ($this->user['id'] == 1) {
- if ($result) {
- return false;
- }
- return;
- }
- if ($this->user['auth']['menu'] && !Dever::check($this->user['auth']['menu'], $menu)) {
- if ($result) {
- return true;
- }
- Dever::error('无菜单访问权限');
- }
- if ($result) {
- return false;
- }
- }
- # 检测功能权限
- protected function checkFunc()
- {
- $id = Dever::input('func');
- if (!$id) {
- return false;
- }
- if ($this->user['id'] == 1) {
- return $id;
- }
- if ($this->user['auth']['func'] && strstr($this->user['auth']['func'], ',' . $id . ',')) {
- return $id;
- }
- if (isset($this->menu) && $this->menu && $this->menu['show'] != 1) {
- return $id;
- }
- Dever::error('无操作权限');
- }
- }
|