07a7373a51d6dba2df1281b7bd0a75466f849fc1.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | auth.php 后台管理 权限管理
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace MazeApp\Manage;
  8. use Maze;
  9. use Maze\Session\Save;
  10. use Maze\Security\Internal;
  11. use Maze\Routing\Uri;
  12. class Auth
  13. {
  14. /**
  15. * NAME 后台的session名
  16. *
  17. * @var string
  18. */
  19. const NAME = 'admin';
  20. /**
  21. * save
  22. *
  23. * @var Maze\Plad\Save
  24. */
  25. private $save;
  26. /**
  27. * __construct
  28. *
  29. * @return mixed
  30. */
  31. public function __construct()
  32. {
  33. $this->save = new Save(false, 'cookie');
  34. }
  35. /**
  36. * 获取角色
  37. *
  38. * @return mixed
  39. */
  40. public function role()
  41. {
  42. $data = Maze::load('/role-list', array('option_name' => '%'.Maze::input('term', 'test').'%'));
  43. if($data)
  44. {
  45. return $data;
  46. }
  47. return array
  48. (
  49. 0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
  50. );
  51. }
  52. /**
  53. * 获取当前登录的管理员信息
  54. *
  55. * @return mixed
  56. */
  57. public function info()
  58. {
  59. $info = $this->save->get(self::NAME);
  60. # 插件接管 暂时未实现
  61. //Maze::plugin($this);
  62. return $info;
  63. }
  64. /**
  65. * 获取公告
  66. *
  67. * @return mixed
  68. */
  69. public function notice()
  70. {
  71. $admin = $this->info();
  72. if($admin['config'] && isset($admin['config']['id']) && $admin['config']['id'] > 0)
  73. {
  74. $data = Maze::load('manage/notice-getAll', array('where_config' => $admin['config']['id']));
  75. return $data;
  76. }
  77. return array();
  78. }
  79. /**
  80. * 获取当前登录的管理员信息
  81. *
  82. * @return mixed
  83. */
  84. public function manage()
  85. {
  86. $admin = $this->info();
  87. if($admin && $admin['id'] == 1)
  88. {
  89. return '';
  90. }
  91. return 'display:none;';
  92. }
  93. /**
  94. * 退出登录
  95. *
  96. * @return mixed
  97. */
  98. public function quit()
  99. {
  100. if($this->info())
  101. {
  102. $this->save->un(self::NAME);
  103. }
  104. Maze::location('login');
  105. }
  106. /**
  107. * login
  108. *
  109. * @return mixed
  110. */
  111. public function login()
  112. {
  113. $param['where_username'] = Maze::input('username');
  114. $password = md5(Maze::input('password'));
  115. //print_r($param);die;
  116. $user = Maze::load('manage/admin-user', $param);
  117. if($user && $user['password'] == $password)
  118. {
  119. $this->save($user);
  120. $refer = Maze::input('refer');
  121. if($refer)
  122. {
  123. $url = parse_url(Internal::decode($refer));
  124. $url['path'] = preg_replace('/^\//', '', $url['path']);
  125. if(Uri::$type == '?')
  126. {
  127. /*
  128. Uri::$type = '';
  129. if(strpos($url['query'], '?') !== false)
  130. {
  131. Uri::$type = '?';
  132. }
  133. */
  134. $refer = Maze::url(str_replace($url['path'] . Uri::$type, '', $url['query']));
  135. }
  136. else
  137. {
  138. $refer = Maze::url($url['path'] . '?' . $url['query']);
  139. }
  140. Maze::out($refer);
  141. }
  142. else
  143. {
  144. Maze::out(Maze::url('home'));
  145. }
  146. }
  147. else
  148. {
  149. Maze::abert('登录失败');
  150. }
  151. }
  152. public function save(&$user)
  153. {
  154. if($user['config'])
  155. {
  156. $user['config'] = Maze::load('manage/config-one', array('where_id' => $user['config']));
  157. }
  158. if($user['id'] == 1)
  159. {
  160. $user['oper'] = 'all';
  161. $user['auth'] = 'all';
  162. $user['top'] = 'all';
  163. }
  164. else
  165. {
  166. $role = Maze::load('manage/role-get', array('where_id' => $user['role']));
  167. if($role)
  168. {
  169. $user['oper'] = array();
  170. $user['auth'] = '';
  171. $user['top'] = '';
  172. foreach($role as $k => $v)
  173. {
  174. if($v['oper'])
  175. {
  176. if(strpos($v['oper'], ',') !== false)
  177. {
  178. $user['oper'] += explode(',', $v['oper']);
  179. }
  180. else
  181. {
  182. $user['oper'][] = $v['oper'];
  183. }
  184. }
  185. if($v['auth'])
  186. {
  187. $user['auth'][] = $v['auth'];
  188. }
  189. if($v['top'])
  190. {
  191. $user['top'][] = $v['top'];
  192. }
  193. }
  194. $user['oper'] = implode(',', $user['oper']);
  195. $user['auth'] = implode(',', $user['auth']);
  196. $user['top'] = implode(',', $user['top']);
  197. if(strpos($user['oper'], 'all') !== false)
  198. {
  199. $user['oper'] = 'all';
  200. }
  201. if(strpos($user['auth'], 'all') !== false)
  202. {
  203. $user['auth'] = 'all';
  204. }
  205. if(strpos($user['top'], 'all') !== false)
  206. {
  207. $user['top'] = 'all';
  208. }
  209. }
  210. }
  211. $this->save->add(self::NAME, $user, 3600*24*7);
  212. }
  213. /**
  214. * set
  215. *
  216. * @return mixed
  217. */
  218. public function set()
  219. {
  220. $param['refer'] = Maze::input('refer');
  221. return $param;
  222. }
  223. /**
  224. * location_login
  225. *
  226. * @return mixed
  227. */
  228. public function location_login()
  229. {
  230. $refer = Internal::encode(Maze::url());
  231. return Maze::location('login?refer=' . $refer);
  232. }
  233. /**
  234. * init
  235. *
  236. * @return mixed
  237. */
  238. public function init()
  239. {
  240. //$this->check(Uri::$value);
  241. $admin = $this->info();
  242. if(!$admin)
  243. {
  244. return $this->location_login();
  245. }
  246. /*
  247. if(!Uri::$value)
  248. {
  249. return;
  250. }
  251. //print_r(Uri::$url);die;
  252. $param['where_project'] = Maze::input('key', MAZE_PROJECT_NAME);
  253. $param['where_table'] = Maze::input('table', 'other');
  254. $param['where_uri'] = Uri::$value;
  255. $data = Maze::load('manage/auth-get', $param);
  256. if(!$data)
  257. {
  258. $update['add_name'] = $this->table($param['where_table'], $param['where_project']) . $this->name($param['where_uri']);
  259. //print_r($update);die;
  260. $update['add_uri'] = $param['where_uri'];
  261. $update['add_type'] = $this->type($update['add_uri']);
  262. $update['add_project'] = $param['where_project'];
  263. $update['add_table'] = $param['where_table'];
  264. $data['id'] = Maze::load('manage/auth-insert', $update);
  265. }
  266. */
  267. # 第一个为超级管理员
  268. if($admin['id'] == 1)
  269. {
  270. return;
  271. }
  272. $menu = Maze::input('menu');
  273. $project = Maze::input('key');
  274. $table = Maze::input('table');
  275. $menu_id = Maze::input('menu_id');
  276. if($menu_id && $menu_id > 0)
  277. {
  278. $menu = Maze::load('manage/menu-info', $menu_id);
  279. if(!$menu)
  280. {
  281. Maze::abert('没有该权限');
  282. }
  283. //$this->save($admin);
  284. if($menu['key'])
  285. {
  286. if(isset($admin['auth']) && $admin['auth'])
  287. {
  288. if($admin['auth'] == 'all' || $admin['auth'] == '')
  289. {
  290. return;
  291. }
  292. $admin['auth'] = explode(',', $admin['auth']);
  293. if(!in_array($menu['key'], $admin['auth']))
  294. {
  295. Maze::abert('您没有操作权限');
  296. }
  297. }
  298. else
  299. {
  300. Maze::abert('您没有操作权限');
  301. }
  302. }
  303. else
  304. {
  305. Maze::abert('您没有操作权限');
  306. }
  307. }
  308. /*
  309. elseif(strpos(Uri::$url, 'project/list') === false && strpos(Uri::$url, 'log/list') === false)
  310. {
  311. return;
  312. }
  313. */
  314. else
  315. {
  316. //Maze::abert('您没有操作权限');
  317. }
  318. }
  319. # 得到当前管理员的权限
  320. public function admin()
  321. {
  322. $admin = $this->info();
  323. return $admin['auth'] == 'all' ? '' : explode(',', $admin['auth']);
  324. }
  325. # 得到当前头部菜单的权限
  326. public function top()
  327. {
  328. $admin = $this->info();
  329. return $admin['top'] == 'all' ? '' : explode(',', $admin['top']);
  330. }
  331. # 设置头部菜单的权限
  332. public function _setTop($info)
  333. {
  334. if($info)
  335. {
  336. $info['key'] = explode('_', $info['key']);
  337. $this->save->add(self::NAME . '_topget_' . $info['key'][0], $info, 3600*24*365);
  338. }
  339. }
  340. # 得到当前头部菜单
  341. public function getTop($key)
  342. {
  343. $state = false;
  344. if(is_array($key))
  345. {
  346. $key = $key[0];
  347. $state = true;
  348. }
  349. $data = $this->save->get(self::NAME . '_topget_' . $key);
  350. # 当数据不存在时,先从数据库里取出一个最新的
  351. if(!$data && $state == true)
  352. {
  353. $info = Maze::load('manage/top-key', array('where_key' => $key));
  354. if($info)
  355. {
  356. $data = Maze::load('manage/top-getOne', array('where_top_id' => $info['id']));
  357. }
  358. }
  359. return $data;
  360. }
  361. public function config()
  362. {
  363. $admin = $this->info();
  364. if($admin['config'] && $admin['config']['id'] > 0)
  365. {
  366. $admin['config'] = Maze::load('manage/config-info', array('where_id' => $admin['config']['id']));
  367. }
  368. $state = isset($admin['config']) && $admin['config'];
  369. $admin['config']['title'] = ($state && $admin['config']['title']) ? $admin['config']['title'] : Maze::$global['base']['name'] . ' 后台管理';
  370. $admin['config']['info'] = ($state && $admin['config']['info']) ? $admin['config']['info'] : Maze::$global['base']['name'] . ' 欢迎您';
  371. $admin['config']['content'] = ($state && $admin['config']['content']) ? $admin['config']['content'] : '欢迎您使用本系统';
  372. $admin['config']['template'] = ($state && $admin['config']['template']) ? $admin['config']['template'] : 1;
  373. return $admin['config'];
  374. }
  375. /**
  376. * oper的判断
  377. *
  378. * @param uri string
  379. * @return mixed
  380. */
  381. public function oper($type = 1)
  382. {
  383. $oper = '';
  384. $admin = $this->info();
  385. //$role['oper'] = '1,2,3,4,5';
  386. if($admin && isset($admin['oper']) && $admin['oper'] != 'all')
  387. {
  388. if(strpos(',' . $admin['oper'], ',' . $type) !== false)
  389. {
  390. return true;
  391. }
  392. else
  393. {
  394. return false;
  395. }
  396. }
  397. else
  398. {
  399. return true;
  400. }
  401. }
  402. /**
  403. * 获取当前uri的类型
  404. *
  405. * @param uri string
  406. * @return mixed
  407. */
  408. private function table($table, $project)
  409. {
  410. if($table == 'other')
  411. {
  412. $table = '';
  413. }
  414. else
  415. {
  416. $path = Maze::load('manage/project.path', $project);
  417. $config = Maze::database(MAZE_PATH . $path . 'database/' . $table . '.php');
  418. $table = $config['lang'];
  419. }
  420. return $table;
  421. }
  422. /*
  423. private function name($name)
  424. {
  425. if($name == 'log/list')
  426. {
  427. $name = '日志列表';
  428. }
  429. if($name == 'project/list')
  430. {
  431. $name = '项目管理';
  432. }
  433. elseif($name == 'home')
  434. {
  435. $name = '首页';
  436. }
  437. elseif(strpos($name, 'list'))
  438. {
  439. $name = '列表';
  440. }
  441. elseif(strpos($name, 'update'))
  442. {
  443. $name = '更新';
  444. }
  445. elseif(strpos($name, 'delete'))
  446. {
  447. $name = '删除';
  448. }
  449. return $name;
  450. }
  451. */
  452. /**
  453. * check
  454. *
  455. * @return mixed
  456. */
  457. private function check($uri)
  458. {
  459. if(strpos($uri, '.html') !== false)
  460. {
  461. //Maze::abert('您没有操作权限');
  462. }
  463. }
  464. /**
  465. * 获取当前uri的类型
  466. *
  467. * @param uri string
  468. * @return mixed
  469. */
  470. public function type($uri)
  471. {
  472. if(strpos($uri, '.') !== false)
  473. {
  474. $type = 3;
  475. }
  476. elseif(strpos($uri, '-') !== false)
  477. {
  478. $type = 2;
  479. }
  480. else
  481. {
  482. $type = 1;
  483. }
  484. return $type;
  485. }
  486. /**
  487. * 获取所有权限列表,并进行统计处理
  488. *
  489. * @return mixed
  490. */
  491. public function get()
  492. {
  493. /*
  494. $auth = Maze::load('manage/auth-all');
  495. $project = Maze::load('manage/project.get');
  496. $result = array();
  497. $url = 'manage/';
  498. foreach($auth as $k => $v)
  499. {
  500. if(isset($project[$v['project']]['lang']))
  501. {
  502. $result[$v['project']]['child'][$k] = $v;
  503. $result[$v['project']]['name'] = $project[$v['project']]['lang'];
  504. $result[$v['project']]['url'] = $url;
  505. }
  506. }
  507. # 1为多维数组
  508. $result['state'] = 1;
  509. */
  510. $result = Maze::load('manage/menu.left', true);
  511. $result['state'] = 1;
  512. return $result;
  513. }
  514. /**
  515. * 修改当前管理员的密码
  516. *
  517. * @return mixed
  518. */
  519. public function password()
  520. {
  521. $admin = $this->info();
  522. $new = Maze::input('new');
  523. $old = Maze::input('old');
  524. if($admin && $admin['id'] > 0 && $new && $old && $new != $old && md5($old) == $admin['password'])
  525. {
  526. $param['set_password'] = $new;
  527. $param['where_id'] = $admin['id'];
  528. Maze::load('manage/admin-password', $param);
  529. $admin['password'] = md5($new);
  530. $this->save->add(self::NAME, $admin);
  531. return '修改成功';
  532. }
  533. else
  534. {
  535. return '修改失败';
  536. }
  537. }
  538. }