Auth.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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(DEVER_PROJECT, 'cookie');
  30. $this->name = DEVER_PROJECT . '_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. }
  297. }
  298. }
  299. $this->save->add($this->name, $user, 3600 * 24 * 7);
  300. }
  301. /**
  302. * set
  303. *
  304. * @return mixed
  305. */
  306. public function set()
  307. {
  308. $param['refer'] = Dever::input('refer');
  309. return $param;
  310. }
  311. /**
  312. * location_login
  313. *
  314. * @return mixed
  315. */
  316. public function location_login()
  317. {
  318. $refer = Encrypt::encode(Dever::url());
  319. return Dever::location('login?refer=' . $refer);
  320. }
  321. /**
  322. * init
  323. *
  324. * @return mixed
  325. */
  326. public function init()
  327. {
  328. if (isset($this->load)) {
  329. return;
  330. }
  331. $this->load = true;
  332. $admin = $this->info();
  333. if (!$admin) {
  334. return $this->location_login();
  335. }
  336. if ($admin['id'] == 1) {
  337. return;
  338. }
  339. $menu = Dever::input('menu');
  340. $project = Dever::input('key');
  341. $table = Dever::input('table');
  342. $menu_id = Dever::input('menu_id');
  343. if ($menu_id && $menu_id > 0) {
  344. $menu = Dever::db('manage/menu')->info($menu_id);
  345. if (!$menu) {
  346. Dever::alert('没有该权限');
  347. }
  348. if ($menu['key']) {
  349. if (isset($admin['auth']) && $admin['auth']) {
  350. if ($admin['auth'] == 'all' || $admin['auth'] == '') {
  351. return;
  352. }
  353. $admin['auth'] = explode(',', $admin['auth']);
  354. if (!in_array($menu['key'], $admin['auth'])) {
  355. Dever::alert('您没有操作权限');
  356. }
  357. } else {
  358. Dever::alert('您没有操作权限');
  359. }
  360. } else {
  361. Dever::alert('您没有操作权限');
  362. }
  363. } else {
  364. //Dever::alert('您没有操作权限');
  365. }
  366. }
  367. # 得到当前管理员的权限
  368. public function admin()
  369. {
  370. $admin = $this->info();
  371. return $admin['auth'] == 'all' ? '' : explode(',', $admin['auth']);
  372. }
  373. # 得到当前头部菜单的权限
  374. public function top()
  375. {
  376. $admin = $this->info();
  377. return $admin['top'] == 'all' ? '' : $admin['top'];
  378. return $admin['top'] == 'all' ? '' : explode(',', $admin['top']);
  379. }
  380. # 设置头部菜单的权限
  381. public function _setTop($info)
  382. {
  383. if ($info) {
  384. $info['key'] = explode('_', $info['key']);
  385. $count = count($info['key']);
  386. if ($count > 2) {
  387. foreach ($info['key'] as $k => $v) {
  388. if ($k+1 < $count) {
  389. $key[] = $v;
  390. }
  391. }
  392. $key = implode('_', $key);
  393. } else {
  394. $key = $info['key'][0];
  395. }
  396. $data = $this->data();
  397. $key = str_replace('/', '-', $key);
  398. $this->top = $info;
  399. $this->save->add($this->name . '_topgetv1_' . $key . '_a' . $data['id'], $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->data();
  411. $key = str_replace('/', '-', $key);
  412. $data = $this->save->get($this->name . '_topgetv1_' . $key . '_a' . $data['id']);
  413. $top = Dever::input('top');
  414. if ($top) {
  415. $data = Dever::db('manage/top')->one($top);
  416. }
  417. //print_r($data);die;
  418. # 当数据不存在时,先从数据库里取出一个最新的
  419. if (!$data && $state == true) {
  420. $info = Dever::db('manage/top')->key(array('where_key' => $key));
  421. if ($info) {
  422. $data = Dever::db('manage/top')->getOne(array('where_top_id' => $info['id']));
  423. }
  424. } elseif (!$data && $this->top) {
  425. $data = $this->top;
  426. }
  427. return $data;
  428. }
  429. public function config()
  430. {
  431. $admin = $this->info();
  432. if (!$admin && $id = Dever::input('auth')) {
  433. $admin['config']['id'] = $id;
  434. }
  435. if ($admin['config'] && $admin['config']['id'] > 0) {
  436. $admin['config'] = Dever::db('manage/config')->info(array('where_id' => $admin['config']['id']));
  437. }
  438. $state = isset($admin['config']) && $admin['config'];
  439. $admin['config']['title'] = ($state && $admin['config']['title']) ? $admin['config']['title'] : Dever::config('base')->name . '';
  440. $admin['config']['info'] = ($state && $admin['config']['info']) ? $admin['config']['info'] : Dever::config('base')->name . ' 欢迎您';
  441. $admin['config']['content'] = ($state && $admin['config']['content']) ? $admin['config']['content'] : '欢迎您使用本系统';
  442. $admin['config']['template'] = ($state && $admin['config']['template']) ? $admin['config']['template'] : 1;
  443. return $admin['config'];
  444. }
  445. /**
  446. * oper的判断
  447. *
  448. * @param uri string
  449. * @return mixed
  450. */
  451. public function oper($type = 1)
  452. {
  453. $oper = '';
  454. $admin = $this->info();
  455. //$role['oper'] = '1,2,3,4,5';
  456. if ($admin && isset($admin['oper']) && $admin['oper'] != 'all') {
  457. if (strpos(',' . $admin['oper'], ',' . $type) !== false) {
  458. return true;
  459. } else {
  460. return false;
  461. }
  462. } else {
  463. return true;
  464. }
  465. }
  466. /**
  467. * 获取当前uri的类型
  468. *
  469. * @param uri string
  470. * @return mixed
  471. */
  472. private function table($table, $project)
  473. {
  474. if ($table == 'other') {
  475. $table = '';
  476. } else {
  477. $path = Dever::load('manage/src/project.path', $project);
  478. $config = Dever::database(DEVER_PATH . $path . 'database/' . $table . '.php');
  479. $table = $config['lang'];
  480. }
  481. return $table;
  482. }
  483. /**
  484. * check
  485. *
  486. * @return mixed
  487. */
  488. public function check($id)
  489. {
  490. $id = Dever::input('update_where_id');
  491. # 先验证email是否已经存在
  492. $email = Dever::input('update_email');
  493. $info = Dever::db('manage/admin')->one(array('option_email' => $email));
  494. if ($id > 0 && $info && $info['id'] != $id) {
  495. Dever::alert('该邮箱已经存在');
  496. } elseif ($id < 0 && $info) {
  497. Dever::alert('该邮箱已经存在');
  498. }
  499. }
  500. /**
  501. * 获取当前uri的类型
  502. *
  503. * @param uri string
  504. * @return mixed
  505. */
  506. public function type($uri)
  507. {
  508. if (strpos($uri, '.') !== false) {
  509. $type = 3;
  510. } elseif (strpos($uri, '-') !== false) {
  511. $type = 2;
  512. } else {
  513. $type = 1;
  514. }
  515. return $type;
  516. }
  517. /**
  518. * 获取所有权限列表,并进行统计处理
  519. *
  520. * @return mixed
  521. */
  522. public function get($state = true)
  523. {
  524. $result = Dever::load('manage/src/menu.left', $state);
  525. $result['state'] = 1;
  526. return $result;
  527. }
  528. /**
  529. * 修改当前管理员的密码
  530. *
  531. * @return mixed
  532. */
  533. public function password()
  534. {
  535. $admin = $this->info();
  536. $new = Dever::input('new');
  537. $old = Dever::input('old');
  538. if ($admin && $admin['id'] > 0 && $new && $old && $new != $old && hash('sha256', ($old)) == $admin['password']) {
  539. $param['set_password'] = $new;
  540. $param['where_id'] = $admin['id'];
  541. Dever::db('manage/admin')->password($param);
  542. $admin['password'] = hash('sha256', ($new));
  543. $this->save->add($this->name, $admin);
  544. return '修改成功';
  545. } else {
  546. return '修改失败';
  547. }
  548. }
  549. /**
  550. * 更新数据到数据库
  551. *
  552. * @return array
  553. */
  554. public function update_action($param = array())
  555. {
  556. if (isset($param['key'])) {
  557. $info = Dever::db('manage/auth')->key(array('where_key' => $param['key']));
  558. //print_r($info);die;
  559. if (!$info) {
  560. $update['add_project'] = $param['project'];
  561. $update['add_project_name'] = $param['project_name'];
  562. $update['add_key'] = $param['key'];
  563. $update['add_name'] = $param['name'];
  564. $update['add_auth_id'] = isset($param['auth']) ? $param['auth'] : -1;
  565. $update['add_value'] = $param['value'];
  566. $update['add_state'] = isset($param['state']) ? $param['state'] : 1;
  567. $info['id'] = Dever::db('manage/auth')->insert($update);
  568. } else {
  569. $update['set_project'] = $param['project'];
  570. $update['set_project_name'] = $param['project_name'];
  571. $update['set_name'] = $param['name'];
  572. $update['set_value'] = $param['value'];
  573. $update['set_state'] = isset($param['state']) ? $param['state'] : 1;
  574. $update['where_id'] = $info['id'];
  575. Dever::db('manage/auth')->update($update);
  576. }
  577. return $info['id'];
  578. }
  579. return false;
  580. }
  581. /**
  582. * 同步子权限更新到数据库
  583. *
  584. * @return array
  585. */
  586. public function sync($param = array())
  587. {
  588. if (isset($param[0]) && isset($param[1])) {
  589. $key = $param[1]['key'];
  590. $info = Dever::db('manage/auth')->key(array('where_key' => $key));
  591. if ($info) {
  592. $update['value'] = Dever::input('where_id', $param[0]);
  593. $update['name'] = Dever::input('name', '-u');
  594. $update['top'] = $info['id'];
  595. $update['key'] = $key . '_' . $update['value'];
  596. $update['state'] = Dever::input('state', '-u');
  597. $this->update_action($update);
  598. }
  599. }
  600. }
  601. /**
  602. * 获取所有的项目精细权限
  603. *
  604. * @return array
  605. */
  606. public function all()
  607. {
  608. $data = Dever::db('manage/auth')->main;
  609. if ($data) {
  610. $child = Dever::db('manage/auth')->child;
  611. foreach ($data as $t => $d) {
  612. foreach ($d as $k => $v) {
  613. if (isset($child[$v['id']])) {
  614. $c = 0;
  615. foreach ($child[$v['id']] as $i => $j) {
  616. $data[$t][$k]['child'][$i] = $j;
  617. $c++;
  618. }
  619. if (!isset($data[$t][$k]['child'])) {
  620. unset($data[$t][$k]);
  621. }
  622. }
  623. }
  624. $data[$t]['state'] = 1;
  625. }
  626. }
  627. return $data;
  628. }
  629. /**
  630. * opt push
  631. *
  632. * @return mixed
  633. */
  634. public function opt($param = false)
  635. {
  636. $opt = new Opt;
  637. $opt->push($param);
  638. }
  639. /**
  640. * opt push
  641. *
  642. * @return mixed
  643. */
  644. public function opt_api($param = false)
  645. {
  646. $opt = new Opt;
  647. $opt->push($param);
  648. }
  649. /**
  650. * opt push
  651. *
  652. * @return mixed
  653. */
  654. public function api($param = false)
  655. {
  656. $api = new Api;
  657. $api->push($param);
  658. }
  659. /**
  660. * opt push
  661. *
  662. * @return mixed
  663. */
  664. public function api_api($param = false)
  665. {
  666. $api = new Api;
  667. $api->push($param);
  668. }
  669. }