123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | auth.php 后台管理 权限管理
- |--------------------------------------------------------------------------
- */
- namespace MazeApp\Manage;
- use Maze;
- use Maze\Session\Save;
- use Maze\Security\Internal;
- use Maze\Routing\Uri;
- class Auth
- {
- /**
- * 后台的session名
- *
- * @var string
- */
- private $name;
-
- /**
- * save
- *
- * @var Maze\Plad\Save
- */
- private $save;
-
- /**
- * __construct
- *
- * @return mixed
- */
- public function __construct()
- {
- $this->save = new Save(MAZE_PROJECT, 'cookie');
- $this->name = MAZE_PROJECT . '_manage';
- }
-
- /**
- * 获取角色
- *
- * @return mixed
- */
- public function role()
- {
- $data = Maze::load('/role-list', array('option_name' => '%'.Maze::input('term', 'test').'%'));
- if($data)
- {
- return $data;
- }
- return array
- (
- 0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
- );
- }
-
- /**
- * 获取当前登录的管理员信息
- *
- * @return mixed
- */
- public function info()
- {
- $info = $this->save->get($this->name);
-
- # 插件接管 暂时未实现
- //Maze::plugin($this);
-
- return $info;
- }
-
- /**
- * 获取公告
- *
- * @return mixed
- */
- public function notice()
- {
- $admin = $this->info();
- if($admin['config'] && isset($admin['config']['id']) && $admin['config']['id'] > 0)
- {
- $data = Maze::load('manage/notice-getAll', array('where_config' => $admin['config']['id']));
-
- return $data;
- }
-
- return array();
- }
-
- /**
- * 获取当前登录的管理员信息
- *
- * @return mixed
- */
- public function manage()
- {
- $admin = $this->info();
- if($admin && $admin['id'] == 1)
- {
- return '';
- }
- return 'display:none;';
- }
-
- /**
- * 退出登录
- *
- * @return mixed
- */
- public function quit()
- {
- if($this->info())
- {
- $this->save->un($this->name);
- }
-
- Maze::location('login');
- }
-
- /**
- * login
- *
- * @return mixed
- */
- public function login()
- {
- $param['where_username'] = Maze::input('username');
-
- $password = md5(Maze::input('password'));
- //print_r($param);die;
- $user = Maze::load('manage/admin-user', $param);
-
- if($user && $user['password'] == $password)
- {
- $this->save($user);
-
- $refer = Maze::input('refer');
-
- if($refer)
- {
- $url = parse_url(Internal::decode($refer));
- $url['path'] = preg_replace('/^\//', '', $url['path']);
- if(Uri::$type == '?')
- {
- /*
- Uri::$type = '';
- if(strpos($url['query'], '?') !== false)
- {
- Uri::$type = '?';
- }
- */
- $refer = Maze::url(str_replace($url['path'] . Uri::$type, '', $url['query']));
- }
- else
- {
- $refer = Maze::url($url['path'] . '?' . $url['query']);
- }
-
- Maze::out($refer);
- }
- else
- {
- Maze::out(Maze::url('home'));
- }
- }
- else
- {
- Maze::abert('登录失败');
- }
- }
-
- public function save(&$user)
- {
- if($user['config'])
- {
- $user['config'] = Maze::load('manage/config-one', array('where_id' => $user['config']));
- }
- if($user['id'] == 1)
- {
- $user['oper'] = 'all';
- $user['auth'] = 'all';
- $user['top'] = 'all';
- }
- else
- {
- $role = Maze::load('manage/role-get', array('where_id' => $user['role']));
- if($role)
- {
- $user['oper'] = array();
- $user['auth'] = '';
- $user['top'] = '';
- foreach($role as $k => $v)
- {
- if($v['oper'])
- {
- if(strpos($v['oper'], ',') !== false)
- {
- $user['oper'] += explode(',', $v['oper']);
- }
- else
- {
- $user['oper'][] = $v['oper'];
- }
- }
- if($v['auth'])
- {
- $user['auth'][] = $v['auth'];
- }
- if($v['top'])
- {
- $user['top'][] = $v['top'];
- }
- }
-
- $user['oper'] = implode(',', $user['oper']);
- $user['auth'] = implode(',', $user['auth']);
- $user['top'] = implode(',', $user['top']);
-
- if(strpos($user['oper'], 'all') !== false)
- {
- $user['oper'] = 'all';
- }
-
- if(strpos($user['auth'], 'all') !== false)
- {
- $user['auth'] = 'all';
- }
- if(strpos($user['top'], 'all') !== false)
- {
- $user['top'] = 'all';
- }
- }
- }
-
- $this->save->add($this->name, $user, 3600*24*7);
- }
-
- /**
- * set
- *
- * @return mixed
- */
- public function set()
- {
- $param['refer'] = Maze::input('refer');
-
- return $param;
- }
-
- /**
- * location_login
- *
- * @return mixed
- */
- public function location_login()
- {
- $refer = Internal::encode(Maze::url());
- return Maze::location('login?refer=' . $refer);
- }
-
- /**
- * init
- *
- * @return mixed
- */
- public function init()
- {
- //$this->check(Uri::$value);
- $admin = $this->info();
- if(!$admin)
- {
- return $this->location_login();
- }
- /*
- if(!Uri::$value)
- {
- return;
- }
- //print_r(Uri::$url);die;
- $param['where_project'] = Maze::input('key', MAZE_PROJECT_NAME);
- $param['where_table'] = Maze::input('table', 'other');
- $param['where_uri'] = Uri::$value;
- $data = Maze::load('manage/auth-get', $param);
-
- if(!$data)
- {
- $update['add_name'] = $this->table($param['where_table'], $param['where_project']) . $this->name($param['where_uri']);
- //print_r($update);die;
-
- $update['add_uri'] = $param['where_uri'];
- $update['add_type'] = $this->type($update['add_uri']);
- $update['add_project'] = $param['where_project'];
- $update['add_table'] = $param['where_table'];
- $data['id'] = Maze::load('manage/auth-insert', $update);
- }
- */
-
- # 第一个为超级管理员
- if($admin['id'] == 1)
- {
- return;
- }
-
- $menu = Maze::input('menu');
- $project = Maze::input('key');
- $table = Maze::input('table');
- $menu_id = Maze::input('menu_id');
- if($menu_id && $menu_id > 0)
- {
- $menu = Maze::load('manage/menu-info', $menu_id);
-
- if(!$menu)
- {
- Maze::abert('没有该权限');
- }
-
- //$this->save($admin);
- if($menu['key'])
- {
- if(isset($admin['auth']) && $admin['auth'])
- {
- if($admin['auth'] == 'all' || $admin['auth'] == '')
- {
- return;
- }
-
- $admin['auth'] = explode(',', $admin['auth']);
- if(!in_array($menu['key'], $admin['auth']))
- {
- Maze::abert('您没有操作权限');
- }
- }
- else
- {
- Maze::abert('您没有操作权限');
- }
- }
- else
- {
- Maze::abert('您没有操作权限');
- }
- }
- /*
- elseif(strpos(Uri::$url, 'project/list') === false && strpos(Uri::$url, 'log/list') === false)
- {
- return;
- }
- */
- else
- {
- //Maze::abert('您没有操作权限');
- }
- }
- # 得到当前管理员的权限
- public function admin()
- {
- $admin = $this->info();
- return $admin['auth'] == 'all' ? '' : explode(',', $admin['auth']);
- }
- # 得到当前头部菜单的权限
- public function top()
- {
- $admin = $this->info();
- return $admin['top'] == 'all' ? '' : explode(',', $admin['top']);
- }
- # 设置头部菜单的权限
- public function _setTop($info)
- {
- if($info)
- {
- $info['key'] = explode('_', $info['key']);
- $this->save->add($this->name . '_topgetv1_' . $info['key'][0], $info, 3600*24*365);
- }
- }
- # 得到当前头部菜单
- public function getTop($key)
- {
- $state = false;
- if(is_array($key))
- {
- $key = $key[0];
- $state = true;
- }
- $data = $this->save->get($this->name . '_topgetv1_' . $key);
- # 当数据不存在时,先从数据库里取出一个最新的
- if(!$data && $state == true)
- {
- $info = Maze::load('manage/top-key', array('where_key' => $key));
- if($info)
- {
- $data = Maze::load('manage/top-getOne', array('where_top_id' => $info['id']));
- }
- }
- return $data;
- }
- public function config()
- {
- $admin = $this->info();
- if($admin['config'] && $admin['config']['id'] > 0)
- {
- $admin['config'] = Maze::load('manage/config-info', array('where_id' => $admin['config']['id']));
- }
- $state = isset($admin['config']) && $admin['config'];
- $admin['config']['title'] = ($state && $admin['config']['title']) ? $admin['config']['title'] : Maze::$global['base']['name'] . ' 后台管理';
- $admin['config']['info'] = ($state && $admin['config']['info']) ? $admin['config']['info'] : Maze::$global['base']['name'] . ' 欢迎您';
- $admin['config']['content'] = ($state && $admin['config']['content']) ? $admin['config']['content'] : '欢迎您使用本系统';
- $admin['config']['template'] = ($state && $admin['config']['template']) ? $admin['config']['template'] : 1;
-
- return $admin['config'];
- }
-
- /**
- * oper的判断
- *
- * @param uri string
- * @return mixed
- */
- public function oper($type = 1)
- {
- $oper = '';
- $admin = $this->info();
- //$role['oper'] = '1,2,3,4,5';
- if($admin && isset($admin['oper']) && $admin['oper'] != 'all')
- {
- if(strpos(',' . $admin['oper'], ',' . $type) !== false)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return true;
- }
- }
- /**
- * 获取当前uri的类型
- *
- * @param uri string
- * @return mixed
- */
- private function table($table, $project)
- {
- if($table == 'other')
- {
- $table = '';
- }
- else
- {
- $path = Maze::load('manage/project.path', $project);
- $config = Maze::database(MAZE_PATH . $path . 'database/' . $table . '.php');
- $table = $config['lang'];
- }
- return $table;
- }
- /*
- private function name($name)
- {
- if($name == 'log/list')
- {
- $name = '日志列表';
- }
- if($name == 'project/list')
- {
- $name = '项目管理';
- }
- elseif($name == 'home')
- {
- $name = '首页';
- }
- elseif(strpos($name, 'list'))
- {
- $name = '列表';
- }
- elseif(strpos($name, 'update'))
- {
- $name = '更新';
- }
- elseif(strpos($name, 'delete'))
- {
- $name = '删除';
- }
-
- return $name;
- }
- */
- /**
- * check
- *
- * @return mixed
- */
- private function check($uri)
- {
- if(strpos($uri, '.html') !== false)
- {
- //Maze::abert('您没有操作权限');
- }
- }
- /**
- * 获取当前uri的类型
- *
- * @param uri string
- * @return mixed
- */
- public function type($uri)
- {
- if(strpos($uri, '.') !== false)
- {
- $type = 3;
- }
- elseif(strpos($uri, '-') !== false)
- {
- $type = 2;
- }
- else
- {
- $type = 1;
- }
- return $type;
- }
- /**
- * 获取所有权限列表,并进行统计处理
- *
- * @return mixed
- */
- public function get()
- {
- /*
- $auth = Maze::load('manage/auth-all');
- $project = Maze::load('manage/project.get');
- $result = array();
- $url = 'manage/';
- foreach($auth as $k => $v)
- {
- if(isset($project[$v['project']]['lang']))
- {
- $result[$v['project']]['child'][$k] = $v;
- $result[$v['project']]['name'] = $project[$v['project']]['lang'];
- $result[$v['project']]['url'] = $url;
- }
- }
- # 1为多维数组
- $result['state'] = 1;
- */
- $result = Maze::load('manage/menu.left', true);
- $result['state'] = 1;
- return $result;
- }
-
- /**
- * 修改当前管理员的密码
- *
- * @return mixed
- */
- public function password()
- {
- $admin = $this->info();
-
- $new = Maze::input('new');
- $old = Maze::input('old');
- if($admin && $admin['id'] > 0 && $new && $old && $new != $old && md5($old) == $admin['password'])
- {
- $param['set_password'] = $new;
- $param['where_id'] = $admin['id'];
- Maze::load('manage/admin-password', $param);
-
- $admin['password'] = md5($new);
- $this->save->add($this->name, $admin);
- return '修改成功';
- }
- else
- {
- return '修改失败';
- }
- }
- }
|