Auth.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. $data = $this->data();
  400. $key = str_replace('/', '-', $key);
  401. $this->save->add($this->name . '_topgetv1_' . $key . '_b' . $data['id'], $info, 3600 * 24 * 365);
  402. }
  403. }
  404. # 得到当前头部菜单
  405. public function getTop($key)
  406. {
  407. $state = false;
  408. if (is_array($key)) {
  409. $key = $key[0];
  410. $state = true;
  411. }
  412. $data = $this->data();
  413. $key = str_replace('/', '-', $key);
  414. $data = $this->save->get($this->name . '_topgetv1_' . $key . '_b' . $data['id']);
  415. # 当数据不存在时,先从数据库里取出一个最新的
  416. if (!$data && $state == true) {
  417. $info = Dever::db('manage/top')->key(array('where_key' => $key));
  418. if ($info) {
  419. $data = Dever::db('manage/top')->getOne(array('where_top_id' => $info['id']));
  420. }
  421. }
  422. return $data;
  423. }
  424. public function config()
  425. {
  426. $admin = $this->info();
  427. if (!$admin && $id = Dever::input('auth')) {
  428. $admin['config']['id'] = $id;
  429. }
  430. if ($admin['config'] && $admin['config']['id'] > 0) {
  431. $admin['config'] = Dever::db('manage/config')->info(array('where_id' => $admin['config']['id']));
  432. }
  433. $state = isset($admin['config']) && $admin['config'];
  434. $admin['config']['title'] = ($state && $admin['config']['title']) ? $admin['config']['title'] : Dever::config('base')->name . '';
  435. $admin['config']['info'] = ($state && $admin['config']['info']) ? $admin['config']['info'] : Dever::config('base')->name . ' 欢迎您';
  436. $admin['config']['content'] = ($state && $admin['config']['content']) ? $admin['config']['content'] : '欢迎您使用本系统';
  437. $admin['config']['template'] = ($state && $admin['config']['template']) ? $admin['config']['template'] : 1;
  438. return $admin['config'];
  439. }
  440. /**
  441. * oper的判断
  442. *
  443. * @param uri string
  444. * @return mixed
  445. */
  446. public function oper($type = 1)
  447. {
  448. $oper = '';
  449. $admin = $this->info();
  450. //$role['oper'] = '1,2,3,4,5';
  451. if ($admin && isset($admin['oper']) && $admin['oper'] != 'all') {
  452. if (strpos(',' . $admin['oper'], ',' . $type) !== false) {
  453. return true;
  454. } else {
  455. return false;
  456. }
  457. } else {
  458. return true;
  459. }
  460. }
  461. /**
  462. * 获取当前uri的类型
  463. *
  464. * @param uri string
  465. * @return mixed
  466. */
  467. private function table($table, $project)
  468. {
  469. if ($table == 'other') {
  470. $table = '';
  471. } else {
  472. $path = Dever::load('manage/src/project.path', $project);
  473. $config = Dever::database(DEVER_PATH . $path . 'database/' . $table . '.php');
  474. $table = $config['lang'];
  475. }
  476. return $table;
  477. }
  478. /**
  479. * check
  480. *
  481. * @return mixed
  482. */
  483. public function check($id)
  484. {
  485. $id = Dever::input('update_where_id');
  486. # 先验证email是否已经存在
  487. $email = Dever::input('update_email');
  488. $info = Dever::db('manage/admin')->one(array('option_email' => $email));
  489. if ($id > 0 && $info && $info['id'] != $id) {
  490. Dever::alert('该邮箱已经存在');
  491. } elseif ($id < 0 && $info) {
  492. Dever::alert('该邮箱已经存在');
  493. }
  494. }
  495. /**
  496. * 获取当前uri的类型
  497. *
  498. * @param uri string
  499. * @return mixed
  500. */
  501. public function type($uri)
  502. {
  503. if (strpos($uri, '.') !== false) {
  504. $type = 3;
  505. } elseif (strpos($uri, '-') !== false) {
  506. $type = 2;
  507. } else {
  508. $type = 1;
  509. }
  510. return $type;
  511. }
  512. /**
  513. * 获取所有权限列表,并进行统计处理
  514. *
  515. * @return mixed
  516. */
  517. public function get($state = true)
  518. {
  519. $result = Dever::load('manage/src/menu.left', $state);
  520. $result['state'] = 1;
  521. return $result;
  522. }
  523. /**
  524. * 修改当前管理员的密码
  525. *
  526. * @return mixed
  527. */
  528. public function password()
  529. {
  530. $admin = $this->info();
  531. $new = Dever::input('new');
  532. $old = Dever::input('old');
  533. if ($admin && $admin['id'] > 0 && $new && $old && $new != $old && hash('sha256', ($old)) == $admin['password']) {
  534. $param['set_password'] = $new;
  535. $param['where_id'] = $admin['id'];
  536. Dever::db('manage/admin')->password($param);
  537. $admin['password'] = hash('sha256', ($new));
  538. $this->save->add($this->name, $admin);
  539. return '修改成功';
  540. } else {
  541. return '修改失败';
  542. }
  543. }
  544. /**
  545. * 更新数据到数据库
  546. *
  547. * @return array
  548. */
  549. public function update_action($param = array())
  550. {
  551. if (isset($param['key'])) {
  552. $info = Dever::db('manage/auth')->key(array('where_key' => $param['key']));
  553. //print_r($info);die;
  554. if (!$info) {
  555. $update['add_project'] = $param['project'];
  556. $update['add_project_name'] = $param['project_name'];
  557. $update['add_key'] = $param['key'];
  558. $update['add_name'] = $param['name'];
  559. $update['add_auth_id'] = isset($param['auth']) ? $param['auth'] : -1;
  560. $update['add_value'] = $param['value'];
  561. $update['add_state'] = isset($param['state']) ? $param['state'] : 1;
  562. $info['id'] = Dever::db('manage/auth')->insert($update);
  563. } else {
  564. $update['set_project'] = $param['project'];
  565. $update['set_project_name'] = $param['project_name'];
  566. $update['set_name'] = $param['name'];
  567. $update['set_value'] = $param['value'];
  568. $update['set_state'] = isset($param['state']) ? $param['state'] : 1;
  569. $update['where_id'] = $info['id'];
  570. Dever::db('manage/auth')->update($update);
  571. }
  572. return $info['id'];
  573. }
  574. return false;
  575. }
  576. /**
  577. * 同步子权限更新到数据库
  578. *
  579. * @return array
  580. */
  581. public function sync($param = array())
  582. {
  583. if (isset($param[0]) && isset($param[1])) {
  584. $key = $param[1]['key'];
  585. $info = Dever::db('manage/auth')->key(array('where_key' => $key));
  586. if ($info) {
  587. $update['value'] = Dever::input('where_id', $param[0]);
  588. $update['name'] = Dever::input('name', '-u');
  589. $update['top'] = $info['id'];
  590. $update['key'] = $key . '_' . $update['value'];
  591. $update['state'] = Dever::input('state', '-u');
  592. $this->update_action($update);
  593. }
  594. }
  595. }
  596. /**
  597. * 获取所有的项目精细权限
  598. *
  599. * @return array
  600. */
  601. public function all()
  602. {
  603. $data = Dever::db('manage/auth')->main;
  604. if ($data) {
  605. $child = Dever::db('manage/auth')->child;
  606. foreach ($data as $t => $d) {
  607. foreach ($d as $k => $v) {
  608. if (isset($child[$v['id']])) {
  609. $c = 0;
  610. foreach ($child[$v['id']] as $i => $j) {
  611. $data[$t][$k]['child'][$i] = $j;
  612. $c++;
  613. }
  614. if (!isset($data[$t][$k]['child'])) {
  615. unset($data[$t][$k]);
  616. }
  617. }
  618. }
  619. $data[$t]['state'] = 1;
  620. }
  621. }
  622. return $data;
  623. }
  624. /**
  625. * opt push
  626. *
  627. * @return mixed
  628. */
  629. public function opt($param = false)
  630. {
  631. $opt = new Opt;
  632. $opt->push($param);
  633. }
  634. /**
  635. * opt push
  636. *
  637. * @return mixed
  638. */
  639. public function opt_api($param = false)
  640. {
  641. $opt = new Opt;
  642. $opt->push($param);
  643. }
  644. /**
  645. * opt push
  646. *
  647. * @return mixed
  648. */
  649. public function api($param = false)
  650. {
  651. $api = new Api;
  652. $api->push($param);
  653. }
  654. /**
  655. * opt push
  656. *
  657. * @return mixed
  658. */
  659. public function api_api($param = false)
  660. {
  661. $api = new Api;
  662. $api->push($param);
  663. }
  664. }