Auth.php 20 KB

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