Auth.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. namespace Manage\Src;
  3. use Dever;
  4. use Dever\Routing\Uri;
  5. use Dever\Session\Oper;
  6. use Dever\String\Encrypt;
  7. class Auth
  8. {
  9. /**
  10. * 后台的session名
  11. *
  12. * @var string
  13. */
  14. private $name;
  15. /**
  16. * save
  17. *
  18. * @var Dever\Plad\Save
  19. */
  20. private $save;
  21. /**
  22. * __construct
  23. *
  24. * @return mixed
  25. */
  26. public function __construct()
  27. {
  28. $this->save = new Oper(DEVER_PROJECT, 'cookie');
  29. $this->name = DEVER_PROJECT . '_manage';
  30. }
  31. public function test()
  32. {
  33. return array('name' => 'test', 'check' => false, 'data' => array
  34. (
  35. 0 => array('name' => 'haha1'),
  36. 1 => array('name' => 'haha2'),
  37. ));
  38. }
  39. /**
  40. * 只获取中间的内容部分
  41. *
  42. * @return mixed
  43. */
  44. public function loading()
  45. {
  46. $set = Dever::input('loading');
  47. if ($set) {
  48. Dever::config('base')->url = 'loading=' . $set;
  49. }
  50. return $set;
  51. }
  52. /**
  53. * 获取角色
  54. *
  55. * @return mixed
  56. */
  57. public function role_api()
  58. {
  59. $data = Dever::db('manage/role')->alls(array
  60. (
  61. /* list和all可以自定义参数
  62. 'option' => array
  63. (
  64. 'name' => array('yes', 'like')
  65. ),
  66. */
  67. //'option_name' => '%' . Dever::input('term', 'test') . '%')
  68. 'option_name' => Dever::input('term', 'test'))
  69. );
  70. //Dever::debug($data);
  71. if ($data) {
  72. return $data;
  73. }
  74. return array
  75. (
  76. 0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
  77. );
  78. }
  79. public function blur()
  80. {
  81. $value = Dever::input('value');
  82. return 1;
  83. }
  84. /**
  85. * 获取当前登录的管理员信息
  86. *
  87. * @return mixed
  88. */
  89. public function info($state = true)
  90. {
  91. return $this->real($this->data(), $state);
  92. }
  93. /**
  94. * 实时读取信息
  95. *
  96. * @return mixed
  97. */
  98. public function real($info, $state = true)
  99. {
  100. if ($state == true && Dever::config('base')->getAdmin && $info && isset($info['id'])) {
  101. $admin = Dever::db('manage/admin')->one($info['id']);
  102. if ($admin['top']) {
  103. $info['top'] = $admin['top'];
  104. }
  105. }
  106. if (!$info) {
  107. $info = $this->auth();
  108. if ($info) {
  109. $this->save($info);
  110. Dever::location(Dever::url(''));
  111. }
  112. }
  113. if (isset($info['role']) && $info['role']) {
  114. $info['role_info'] = Dever::db('manage/role')->one($info['role']);
  115. $info['rolename'] = '未分组';
  116. if ($info['role_info']['name']) {
  117. $info['rolename'] = $info['role_info']['name'];
  118. }
  119. $info['self'] = $info['role_info']['self'];
  120. $info['col_update'] = $info['role_info']['col_update'];
  121. $info['col_insert'] = $info['role_info']['col_insert'];
  122. $info['col_select'] = $info['role_info']['col_select'];
  123. }
  124. if (isset($info['group']) && $info['group']) {
  125. $info['group_info'] = Dever::db('manage/group')->one($info['group']);
  126. }
  127. return $info;
  128. }
  129. /**
  130. * 获取当前登录的管理员信息
  131. *
  132. * @return mixed
  133. */
  134. public function data($state = true)
  135. {
  136. return $this->save->get($this->name);
  137. }
  138. private function auth()
  139. {
  140. # 此处可以接入当前的用户系统
  141. /*
  142. $user = \CondeAdminUser::GetAdminInfo('manage', '后台管理');
  143. $info = Dever::load('manage/admin-user', array('where_username' => $user['name']));
  144. //$info = Dever::load('manage/admin-user', array('where_email' => $user['email']));
  145. if(!$info)
  146. {
  147. $id = Dever::load('manage/admin-insert', array('add_role' => 1, 'add_username' => $user['name'], 'add_email' => $user['email']));
  148. $info = Dever::load('manage/admin-one', $id);
  149. }
  150. return $info;
  151. */
  152. return false;
  153. }
  154. /**
  155. * 获取公告
  156. *
  157. * @return mixed
  158. */
  159. public function notice()
  160. {
  161. $admin = $this->info();
  162. if ($admin && $admin['config'] && isset($admin['config']['id']) && $admin['config']['id'] > 0) {
  163. $data = Dever::db('manage/notice')->getAll(array('where_config' => $admin['config']['id']));
  164. return $data;
  165. }
  166. return array();
  167. }
  168. /**
  169. * 获取当前登录的管理员信息
  170. *
  171. * @return mixed
  172. */
  173. public function manage()
  174. {
  175. $admin = $this->info();
  176. if ($admin && $admin['id'] == 1) {
  177. return '';
  178. }
  179. return 'display:none;';
  180. }
  181. /**
  182. * 退出登录
  183. *
  184. * @return mixed
  185. */
  186. public function quit_api()
  187. {
  188. if ($this->info()) {
  189. $this->save->un($this->name);
  190. }
  191. Dever::location('login');
  192. }
  193. /**
  194. * login
  195. *
  196. * @return mixed
  197. */
  198. public function login_api()
  199. {
  200. //$param['where_username'] = Dever::input('username');
  201. $param['where_email'] = Dever::input('username');
  202. $password = hash('sha256', Dever::input('password'));
  203. //print_r($param);die;
  204. $user = Dever::db('manage/admin')->user($param);
  205. //print_r($user);die;
  206. if (!$user) {
  207. $total = Dever::db('manage/admin')->total();
  208. if ($total <= 0) {
  209. $insert['username'] = $param['where_email'];
  210. $insert['email'] = $param['where_email'];
  211. $insert['password'] = Dever::input('password');
  212. $insert['status'] = $insert['state'] = 1;
  213. Dever::db('manage/admin')->insert($insert);
  214. $param['time'] = 1;
  215. $user = Dever::db('manage/admin')->user($param);
  216. }
  217. }
  218. if ($user && $user['password'] == $password) {
  219. $this->save($user);
  220. $refer = Dever::input('refer');
  221. if ($refer) {
  222. $refer = Encrypt::decode($refer);
  223. /*
  224. $url = parse_url(Encrypt::decode($refer));
  225. $url['path'] = preg_replace('/^\//', '', $url['path']);
  226. if (empty($url['query'])) {
  227. $url['query'] = '';
  228. }
  229. if (Uri::$type == '?') {
  230. $refer = Dever::url(str_replace($url['path'] . Uri::$type, '', $url['query']));
  231. } else {
  232. $refer = Dever::url($url['path'] . '?' . $url['query']);
  233. }
  234. */
  235. Dever::out($refer);
  236. } else {
  237. Dever::out(Dever::url('home'));
  238. }
  239. } else {
  240. Dever::alert('登录失败');
  241. }
  242. }
  243. public function update($id)
  244. {
  245. $id = Dever::input('update_where_id');
  246. $admin = $this->info();
  247. if ($id > 0 && $id == $admin['id']) {
  248. $user = Dever::db('manage/admin')->one($id);
  249. $this->save($user);
  250. }
  251. }
  252. public function save(&$user)
  253. {
  254. if ($user['config']) {
  255. $user['config'] = Dever::db('manage/config')->one($user['config']);
  256. }
  257. if ($user['id'] == 1) {
  258. $user['oper'] = 'all';
  259. $user['auth'] = 'all';
  260. $user['top'] = 'all';
  261. } else {
  262. $role = Dever::db('manage/role')->get(array('where_id' => $user['role']));
  263. if ($role) {
  264. $user['oper'] = array();
  265. $user['auth'] = array();
  266. $user['self'] = 2;
  267. $top = array();
  268. foreach ($role as $k => $v) {
  269. if ($v['oper']) {
  270. if (strpos($v['oper'], ',') !== false) {
  271. $user['oper'] += explode(',', $v['oper']);
  272. } else {
  273. $user['oper'][] = $v['oper'];
  274. }
  275. }
  276. if ($v['auth']) {
  277. $user['auth'][] = $v['auth'];
  278. }
  279. if (!$user['top'] && $v['top']) {
  280. $top[] = $v['top'];
  281. }
  282. if ($v['self'] == 1) {
  283. $user['self'] = $v['self'];
  284. }
  285. }
  286. $user['oper'] = implode(',', $user['oper']);
  287. $user['auth'] = implode(',', $user['auth']);
  288. if (!$user['top'] && $top) {
  289. $user['top'] = implode(',', $top);
  290. }
  291. if (strpos($user['oper'], 'all') !== false) {
  292. $user['oper'] = 'all';
  293. }
  294. if (strpos($user['auth'], 'all') !== false) {
  295. $user['auth'] = 'all';
  296. }
  297. if (strpos($user['top'], 'all') !== false) {
  298. $user['top'] = 'all';
  299. }
  300. }
  301. }
  302. $this->save->add($this->name, $user, 3600 * 24 * 7);
  303. }
  304. /**
  305. * set
  306. *
  307. * @return mixed
  308. */
  309. public function set()
  310. {
  311. $param['refer'] = Dever::input('refer');
  312. return $param;
  313. }
  314. /**
  315. * location_login
  316. *
  317. * @return mixed
  318. */
  319. public function location_login()
  320. {
  321. $refer = Encrypt::encode(Dever::url());
  322. return Dever::location('login?refer=' . $refer);
  323. }
  324. /**
  325. * init
  326. *
  327. * @return mixed
  328. */
  329. public function init()
  330. {
  331. if (isset($this->load)) {
  332. return;
  333. }
  334. $this->load = true;
  335. $admin = $this->info();
  336. if (!$admin) {
  337. return $this->location_login();
  338. }
  339. if ($admin['id'] == 1) {
  340. return;
  341. }
  342. $menu = Dever::input('menu');
  343. $project = Dever::input('key');
  344. $table = Dever::input('table');
  345. $menu_id = Dever::input('menu_id');
  346. if ($menu_id && $menu_id > 0) {
  347. $menu = Dever::db('manage/menu')->info($menu_id);
  348. if (!$menu) {
  349. Dever::alert('没有该权限');
  350. }
  351. if ($menu['key']) {
  352. if (isset($admin['auth']) && $admin['auth']) {
  353. if ($admin['auth'] == 'all' || $admin['auth'] == '') {
  354. return;
  355. }
  356. $admin['auth'] = explode(',', $admin['auth']);
  357. if (!in_array($menu['key'], $admin['auth'])) {
  358. Dever::alert('您没有操作权限');
  359. }
  360. } else {
  361. Dever::alert('您没有操作权限');
  362. }
  363. } else {
  364. Dever::alert('您没有操作权限');
  365. }
  366. } else {
  367. //Dever::alert('您没有操作权限');
  368. }
  369. }
  370. # 得到当前管理员的权限
  371. public function admin()
  372. {
  373. $admin = $this->info();
  374. return $admin['auth'] == 'all' ? '' : explode(',', $admin['auth']);
  375. }
  376. # 得到当前头部菜单的权限
  377. public function top()
  378. {
  379. $admin = $this->info();
  380. return $admin['top'] == 'all' ? '' : $admin['top'];
  381. return $admin['top'] == 'all' ? '' : explode(',', $admin['top']);
  382. }
  383. # 设置头部菜单的权限
  384. public function _setTop($info)
  385. {
  386. if ($info) {
  387. $info['key'] = explode('_', $info['key']);
  388. $count = count($info['key']);
  389. if ($count > 2) {
  390. foreach ($info['key'] as $k => $v) {
  391. if ($k+1 < $count) {
  392. $key[] = $v;
  393. }
  394. }
  395. $key = implode('_', $key);
  396. } else {
  397. $key = $info['key'][0];
  398. }
  399. $this->save->add($this->name . '_topgetv1_' . $key, $info, 3600 * 24 * 365);
  400. }
  401. }
  402. # 得到当前头部菜单
  403. public function getTop($key)
  404. {
  405. $state = false;
  406. if (is_array($key)) {
  407. $key = $key[0];
  408. $state = true;
  409. }
  410. $data = $this->save->get($this->name . '_topgetv1_' . $key);
  411. # 当数据不存在时,先从数据库里取出一个最新的
  412. if (!$data && $state == true) {
  413. $info = Dever::db('manage/top')->key(array('where_key' => $key));
  414. if ($info) {
  415. $data = Dever::db('manage/top')->getOne(array('where_top_id' => $info['id']));
  416. }
  417. }
  418. return $data;
  419. }
  420. public function config()
  421. {
  422. $admin = $this->info();
  423. if (!$admin && $id = Dever::input('auth')) {
  424. $admin['config']['id'] = $id;
  425. }
  426. if ($admin['config'] && $admin['config']['id'] > 0) {
  427. $admin['config'] = Dever::db('manage/config')->info(array('where_id' => $admin['config']['id']));
  428. }
  429. $state = isset($admin['config']) && $admin['config'];
  430. $admin['config']['title'] = ($state && $admin['config']['title']) ? $admin['config']['title'] : Dever::config('base')->name . '';
  431. $admin['config']['info'] = ($state && $admin['config']['info']) ? $admin['config']['info'] : Dever::config('base')->name . ' 欢迎您';
  432. $admin['config']['content'] = ($state && $admin['config']['content']) ? $admin['config']['content'] : '欢迎您使用本系统';
  433. $admin['config']['template'] = ($state && $admin['config']['template']) ? $admin['config']['template'] : 1;
  434. return $admin['config'];
  435. }
  436. /**
  437. * oper的判断
  438. *
  439. * @param uri string
  440. * @return mixed
  441. */
  442. public function oper($type = 1)
  443. {
  444. $oper = '';
  445. $admin = $this->info();
  446. //$role['oper'] = '1,2,3,4,5';
  447. if ($admin && isset($admin['oper']) && $admin['oper'] != 'all') {
  448. if (strpos(',' . $admin['oper'], ',' . $type) !== false) {
  449. return true;
  450. } else {
  451. return false;
  452. }
  453. } else {
  454. return true;
  455. }
  456. }
  457. /**
  458. * 获取当前uri的类型
  459. *
  460. * @param uri string
  461. * @return mixed
  462. */
  463. private function table($table, $project)
  464. {
  465. if ($table == 'other') {
  466. $table = '';
  467. } else {
  468. $path = Dever::load('manage/src/project.path', $project);
  469. $config = Dever::database(DEVER_PATH . $path . 'database/' . $table . '.php');
  470. $table = $config['lang'];
  471. }
  472. return $table;
  473. }
  474. /**
  475. * check
  476. *
  477. * @return mixed
  478. */
  479. public function check($id)
  480. {
  481. $id = Dever::input('update_where_id');
  482. # 先验证email是否已经存在
  483. $email = Dever::input('update_email');
  484. $info = Dever::db('manage/admin')->one(array('option_email' => $email));
  485. if ($id > 0 && $info && $info['id'] != $id) {
  486. Dever::alert('该邮箱已经存在');
  487. } elseif ($id < 0 && $info) {
  488. Dever::alert('该邮箱已经存在');
  489. }
  490. }
  491. /**
  492. * 获取当前uri的类型
  493. *
  494. * @param uri string
  495. * @return mixed
  496. */
  497. public function type($uri)
  498. {
  499. if (strpos($uri, '.') !== false) {
  500. $type = 3;
  501. } elseif (strpos($uri, '-') !== false) {
  502. $type = 2;
  503. } else {
  504. $type = 1;
  505. }
  506. return $type;
  507. }
  508. /**
  509. * 获取所有权限列表,并进行统计处理
  510. *
  511. * @return mixed
  512. */
  513. public function get($state = true)
  514. {
  515. $result = Dever::load('manage/src/menu.left', $state);
  516. $result['state'] = 1;
  517. return $result;
  518. }
  519. /**
  520. * 修改当前管理员的密码
  521. *
  522. * @return mixed
  523. */
  524. public function password()
  525. {
  526. $admin = $this->info();
  527. $new = Dever::input('new');
  528. $old = Dever::input('old');
  529. if ($admin && $admin['id'] > 0 && $new && $old && $new != $old && hash('sha256', ($old)) == $admin['password']) {
  530. $param['set_password'] = $new;
  531. $param['where_id'] = $admin['id'];
  532. Dever::db('manage/admin')->password($param);
  533. $admin['password'] = hash('sha256', ($new));
  534. $this->save->add($this->name, $admin);
  535. return '修改成功';
  536. } else {
  537. return '修改失败';
  538. }
  539. }
  540. /**
  541. * 更新数据到数据库
  542. *
  543. * @return array
  544. */
  545. public function update_action($param = array())
  546. {
  547. if (isset($param['key'])) {
  548. $info = Dever::db('manage/auth')->key(array('where_key' => $param['key']));
  549. //print_r($info);die;
  550. if (!$info) {
  551. $update['add_project'] = $param['project'];
  552. $update['add_project_name'] = $param['project_name'];
  553. $update['add_key'] = $param['key'];
  554. $update['add_name'] = $param['name'];
  555. $update['add_auth_id'] = isset($param['auth']) ? $param['auth'] : -1;
  556. $update['add_value'] = $param['value'];
  557. $update['add_state'] = isset($param['state']) ? $param['state'] : 1;
  558. $info['id'] = Dever::db('manage/auth')->insert($update);
  559. } else {
  560. $update['set_project'] = $param['project'];
  561. $update['set_project_name'] = $param['project_name'];
  562. $update['set_name'] = $param['name'];
  563. $update['set_value'] = $param['value'];
  564. $update['set_state'] = isset($param['state']) ? $param['state'] : 1;
  565. $update['where_id'] = $info['id'];
  566. Dever::db('manage/auth')->update($update);
  567. }
  568. return $info['id'];
  569. }
  570. return false;
  571. }
  572. /**
  573. * 同步子权限更新到数据库
  574. *
  575. * @return array
  576. */
  577. public function sync($param = array())
  578. {
  579. if (isset($param[0]) && isset($param[1])) {
  580. $key = $param[1]['key'];
  581. $info = Dever::db('manage/auth')->key(array('where_key' => $key));
  582. if ($info) {
  583. $update['value'] = Dever::input('where_id', $param[0]);
  584. $update['name'] = Dever::input('name', '-u');
  585. $update['top'] = $info['id'];
  586. $update['key'] = $key . '_' . $update['value'];
  587. $update['state'] = Dever::input('state', '-u');
  588. $this->update_action($update);
  589. }
  590. }
  591. }
  592. /**
  593. * 获取所有的项目精细权限
  594. *
  595. * @return array
  596. */
  597. public function all()
  598. {
  599. $data = Dever::db('manage/auth')->main;
  600. if ($data) {
  601. $child = Dever::db('manage/auth')->child;
  602. foreach ($data as $t => $d) {
  603. foreach ($d as $k => $v) {
  604. if (isset($child[$v['id']])) {
  605. $c = 0;
  606. foreach ($child[$v['id']] as $i => $j) {
  607. $data[$t][$k]['child'][$i] = $j;
  608. $c++;
  609. }
  610. if (!isset($data[$t][$k]['child'])) {
  611. unset($data[$t][$k]);
  612. }
  613. }
  614. }
  615. $data[$t]['state'] = 1;
  616. }
  617. }
  618. return $data;
  619. }
  620. /**
  621. * opt push
  622. *
  623. * @return mixed
  624. */
  625. public function opt($param = false)
  626. {
  627. $opt = new Opt;
  628. $opt->push($param);
  629. }
  630. /**
  631. * opt push
  632. *
  633. * @return mixed
  634. */
  635. public function opt_api($param = false)
  636. {
  637. $opt = new Opt;
  638. $opt->push($param);
  639. }
  640. /**
  641. * opt push
  642. *
  643. * @return mixed
  644. */
  645. public function api($param = false)
  646. {
  647. $api = new Api;
  648. $api->push($param);
  649. }
  650. /**
  651. * opt push
  652. *
  653. * @return mixed
  654. */
  655. public function api_api($param = false)
  656. {
  657. $api = new Api;
  658. $api->push($param);
  659. }
  660. }