Auth.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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, 'cookie');
  30. $this->name = md5('manage_' . Dever::config('host')->host);
  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. public function check($name, $key, $func = false)
  42. {
  43. if (strstr($name, '回收站') || strstr($name, '返回上一页') || strstr($name, '删除')) {
  44. return true;
  45. }
  46. $config = array
  47. (
  48. '列表' => 1,
  49. '搜索' => 2,
  50. '编辑' => 3,
  51. '新增' => 4,
  52. '删除' => 5,
  53. //'查看' => 6,
  54. //'操作' => 7,
  55. );
  56. $reorder = 100;
  57. if (isset($config[$name])) {
  58. $state = $this->oper($config[$name]);
  59. if (!$state) {
  60. return false;
  61. }
  62. $reorder = $config[$name];
  63. }
  64. return $this->checkFunc($key, $func, $name, $reorder);
  65. }
  66. # 检测功能
  67. public function checkFunc($key, $func, $name, $reorder = 1)
  68. {
  69. if (is_numeric($key)) {
  70. $menu = Dever::db('manage/menu')->one(array('id' => $key));
  71. } else {
  72. $menu = Dever::db('manage/menu')->one(array('key' => $key));
  73. }
  74. if ($menu) {
  75. if (!$func) {
  76. $func = md5(base64_encode($name));
  77. }
  78. $info = Dever::db('manage/menu_func')->one(array('clear' => true, 'key' => $func, 'menu_id' => $menu['id']));
  79. if (!$info) {
  80. $func_id = Dever::db('manage/menu_func')->insert(array('name' => $name, 'key' => $func, 'menu_id' => $menu['id'], 'reorder' => $reorder));
  81. } else {
  82. if ($info['name'] != $name) {
  83. Dever::db('manage/menu_func')->update(array('name' => $name, 'reorder' => $reorder, 'where_id' => $info['id']));
  84. }
  85. $func_id = $info['id'];
  86. }
  87. $data = $this->info();
  88. if ($data && isset($data['auth'])) {
  89. if ($data['auth'] == 'all') {
  90. return true;
  91. } elseif (strstr($data['auth'], 'f_' . $func_id)) {
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * 只获取中间的内容部分
  100. *
  101. * @return mixed
  102. */
  103. public function loading()
  104. {
  105. $set = Dever::input('loading');
  106. if ($set) {
  107. Dever::config('base')->url = 'loading=' . $set;
  108. }
  109. return $set;
  110. }
  111. /**
  112. * 获取角色
  113. *
  114. * @return mixed
  115. */
  116. public function role_api()
  117. {
  118. $data = Dever::db('manage/role')->alls(array
  119. (
  120. /* list和all可以自定义参数
  121. 'option' => array
  122. (
  123. 'name' => array('yes', 'like')
  124. ),
  125. */
  126. //'option_name' => '%' . Dever::input('term', 'test') . '%')
  127. 'option_name' => Dever::input('term', 'test'))
  128. );
  129. //Dever::debug($data);
  130. if ($data) {
  131. return $data;
  132. }
  133. return array
  134. (
  135. 0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
  136. );
  137. }
  138. public function blur()
  139. {
  140. $value = Dever::input('value');
  141. return 'test';
  142. }
  143. /**
  144. * 获取当前登录的管理员信息
  145. *
  146. * @return mixed
  147. */
  148. public function info($state = true)
  149. {
  150. return $this->real($this->data(), $state);
  151. }
  152. /**
  153. * 获取当前登录的管理员信息
  154. *
  155. * @return mixed
  156. */
  157. public function authData($state = true)
  158. {
  159. $data = $this->info($state);
  160. if (isset($data['role_info']['auth_data'])) {
  161. return $data['role_info']['auth_data'];
  162. }
  163. return '';
  164. }
  165. /**
  166. * 实时读取信息
  167. *
  168. * @return mixed
  169. */
  170. public function real($info, $state = true)
  171. {
  172. if (!$info) {
  173. return;
  174. }
  175. if (!$info) {
  176. $info = $this->auth();
  177. if ($info) {
  178. $this->save($info);
  179. Dever::location(Dever::url(''));
  180. }
  181. }
  182. $info = $this->role($info);
  183. $state = true;
  184. Dever::config('base')->getAdmin = 1;
  185. if ($state == true && Dever::config('base')->getAdmin && $info && isset($info['id'])) {
  186. $admin = Dever::db('manage/admin')->one($info['id']);
  187. if (!$admin) {
  188. $this->save->un($this->name);
  189. echo '管理账户已被封禁';die;
  190. }
  191. if ($admin['status'] != 1) {
  192. $this->save->un($this->name);
  193. echo '管理账户已被封禁';die;
  194. }
  195. if ($admin['auth'] && $admin['auth'] != '-1') {
  196. $admin['auth'] = explode(',', $admin['auth']);
  197. $info['auth'] = explode(',', $info['auth']);
  198. $info['auth'] = array_unique(array_merge($info['auth'], $admin['auth']));
  199. $info['auth'] = implode(',', $info['auth']);
  200. }
  201. if ($admin['top']) {
  202. $info['top'] = $admin['top'];
  203. }
  204. $info['company'] = false;
  205. if ($admin['company']) {
  206. $info['company'] = $admin['company'];
  207. }
  208. $info['username'] = $admin['username'];
  209. }
  210. if (isset($info['role']) && $info['role']) {
  211. $info['role_info'] = Dever::db('manage/role')->one($info['role']);
  212. $info['rolename'] = '未分组';
  213. if ($info['role_info']['name']) {
  214. $info['rolename'] = $info['role_info']['name'];
  215. }
  216. $info['self'] = $info['role_info']['self'];
  217. $info['col_update'] = $info['role_info']['col_update'];
  218. $info['col_insert'] = $info['role_info']['col_insert'];
  219. $info['col_select'] = $info['role_info']['col_select'];
  220. }
  221. if (isset($info['group']) && $info['group']) {
  222. $info['group_info'] = Dever::db('manage/group')->one($info['group']);
  223. }
  224. return $info;
  225. }
  226. /**
  227. * 获取当前登录的管理员信息
  228. *
  229. * @return mixed
  230. */
  231. public function data($state = true)
  232. {
  233. return $this->save->get($this->name);
  234. }
  235. private function auth()
  236. {
  237. # 此处可以接入当前的用户系统
  238. /*
  239. $user = \CondeAdminUser::GetAdminInfo('manage', '后台管理');
  240. $info = Dever::load('manage/admin-user', array('where_username' => $user['name']));
  241. //$info = Dever::load('manage/admin-user', array('where_email' => $user['email']));
  242. if(!$info)
  243. {
  244. $id = Dever::load('manage/admin-insert', array('add_role' => 1, 'add_username' => $user['name'], 'add_email' => $user['email']));
  245. $info = Dever::load('manage/admin-one', $id);
  246. }
  247. return $info;
  248. */
  249. return false;
  250. }
  251. /**
  252. * 获取公告
  253. *
  254. * @return mixed
  255. */
  256. public function notice()
  257. {
  258. $admin = $this->info();
  259. if ($admin && $admin['config'] && isset($admin['config']['id']) && $admin['config']['id'] > 0) {
  260. $data = Dever::db('manage/notice')->getAll(array('where_config' => $admin['config']['id']));
  261. return $data;
  262. }
  263. return array();
  264. }
  265. /**
  266. * 获取当前登录的管理员信息
  267. *
  268. * @return mixed
  269. */
  270. public function manage()
  271. {
  272. $admin = $this->info();
  273. if ($admin && $admin['id'] == 1) {
  274. return '';
  275. }
  276. return 'display:none;';
  277. }
  278. /**
  279. * 退出登录
  280. *
  281. * @return mixed
  282. */
  283. public function quit_api()
  284. {
  285. if ($this->info()) {
  286. $this->save->un($this->name);
  287. }
  288. Dever::location('login');
  289. }
  290. /**
  291. * login
  292. *
  293. * @return mixed
  294. */
  295. public function login_api()
  296. {
  297. //$param['where_username'] = Dever::input('username');
  298. $username = Dever::input('username');
  299. if (strstr($username, '@')) {
  300. $param['where_email'] = $username;
  301. $method = 'email';
  302. } else {
  303. $param['where_mobile'] = $username;
  304. $method = 'mobile';
  305. }
  306. $password = hash('sha256', Dever::input('password'));
  307. $user = Dever::db('manage/admin')->$method($param);
  308. if (!$user) {
  309. $total = Dever::db('manage/admin')->total();
  310. if ($total <= 0) {
  311. $insert['username'] = $username;
  312. $insert[$method] = $username;
  313. $insert['password'] = Dever::input('password');
  314. $insert['status'] = $insert['state'] = 1;
  315. $insert['role'] = 1;
  316. Dever::db('manage/admin')->insert($insert);
  317. $param['time'] = 1;
  318. $user = Dever::db('manage/admin')->$method($param);
  319. } else {
  320. Dever::alert('登录失败');
  321. }
  322. }
  323. if ($user && $user['password'] == $password) {
  324. $user = $this->real($user);
  325. $this->save($user);
  326. $refer = Dever::input('refer');
  327. if ($refer) {
  328. $refer = Encrypt::decode($refer);
  329. Dever::out($refer);
  330. } else {
  331. Dever::out(Dever::url('home'));
  332. }
  333. } else {
  334. Dever::alert('登录失败');
  335. }
  336. }
  337. public function update($id, $name, $data)
  338. {
  339. $admin = $this->info();
  340. if ($id > 0 && $id == $admin['id']) {
  341. $user = Dever::db('manage/admin')->one($id);
  342. $this->save($user);
  343. }
  344. $group = Dever::param('group', $data);
  345. if ($group) {
  346. $company = Dever::db('manage/group')->getCompanyIds(array('ids' => $group));
  347. if ($company) {
  348. $update['where_id'] = $id;
  349. $update['company'] = implode(',', array_keys($company));
  350. Dever::db('manage/admin')->update($update);
  351. }
  352. }
  353. }
  354. public function save(&$user)
  355. {
  356. if ($user['config']) {
  357. $user['config'] = Dever::db('manage/config')->one($user['config']);
  358. }
  359. if ($user['id'] == 1) {
  360. $user['oper'] = 'all';
  361. $user['auth'] = 'all';
  362. $user['auth_data'] = 'all';
  363. $user['top'] = 'all';
  364. } else {
  365. $user = $this->role($user);
  366. }
  367. $this->save->add($this->name, $user, 3600 * 24 * 7);
  368. }
  369. /**
  370. * get_role
  371. *
  372. * @return mixed
  373. */
  374. private function role($user)
  375. {
  376. $role = Dever::db('manage/role')->get(array('where_id' => $user['role']));
  377. if ($role) {
  378. $user['oper'] = array();
  379. $user['auth'] = array();
  380. $user['auth_data'] = array();
  381. $user['self'] = 2;
  382. $top = array();
  383. foreach ($role as $k => $v) {
  384. if ($v['oper']) {
  385. if (strpos($v['oper'], ',') !== false) {
  386. $user['oper'] += explode(',', $v['oper']);
  387. } else {
  388. $user['oper'][] = $v['oper'];
  389. }
  390. }
  391. if ($v['auth']) {
  392. $user['auth'][] = $v['auth'];
  393. }
  394. if ($v['auth_data']) {
  395. $user['auth_data'][] = $v['auth_data'];
  396. }
  397. if (!$user['top'] && $v['top']) {
  398. $top[] = $v['top'];
  399. }
  400. if ($v['self'] == 1) {
  401. $user['self'] = $v['self'];
  402. }
  403. }
  404. $user['oper'] = implode(',', $user['oper']);
  405. $user['auth'] = implode(',', $user['auth']);
  406. $user['auth_data'] = implode(',', $user['auth_data']);
  407. if (!$user['top'] && $top) {
  408. $user['top'] = implode(',', $top);
  409. }
  410. $user['oper'] = $this->super($user['oper']);
  411. $user['auth'] = $this->super($user['auth']);
  412. $user['auth_data'] = $this->super($user['auth_data']);
  413. $user['top'] = $this->super($user['top']);
  414. if ($user['top'] != 'all') {
  415. # 这块暂时不用
  416. //$top = explode(',', $user['top']);
  417. //Dever::load('manage/top.update_action', $top[0]);
  418. }
  419. /*
  420. if (strpos($user['oper'], 'all') !== false) {
  421. $user['oper'] = 'all';
  422. }
  423. if (strpos($user['auth'], 'all') !== false) {
  424. $user['auth'] = 'all';
  425. }
  426. if (strpos($user['auth_data'], 'all') !== false) {
  427. $user['auth_data'] = 'all';
  428. }
  429. if (strpos($user['top'], 'all') !== false) {
  430. $user['top'] = 'all';
  431. } elseif ($user['top']) {
  432. # 这块暂时不用
  433. //$top = explode(',', $user['top']);
  434. //Dever::load('manage/top.update_action', $top[0]);
  435. }
  436. */
  437. }
  438. return $user;
  439. }
  440. private function super($auth)
  441. {
  442. return $auth;
  443. if ($auth == 'all' || strpos($auth, 'all,') !== false) {
  444. $auth = 'all';
  445. }
  446. return $auth;
  447. }
  448. /**
  449. * location_login
  450. *
  451. * @return mixed
  452. */
  453. public function location_login()
  454. {
  455. $refer = Encrypt::encode(Dever::url());
  456. return Dever::location('manage/login?refer=' . $refer);
  457. }
  458. /**
  459. * init
  460. *
  461. * @return mixed
  462. */
  463. public function init()
  464. {
  465. if (isset($this->load)) {
  466. return;
  467. }
  468. $this->load = true;
  469. $admin = $this->info();
  470. if (!$admin) {
  471. return $this->location_login();
  472. }
  473. if ($admin['id'] == 1) {
  474. return;
  475. }
  476. $menu = Dever::input('menu');
  477. $project = Dever::input('key');
  478. $table = Dever::input('table');
  479. $menu_id = Dever::input('menu_id');
  480. if ($menu_id && $menu_id > 0) {
  481. $menu = Dever::db('manage/menu')->info($menu_id);
  482. if (!$menu) {
  483. Dever::alert('没有该权限');
  484. }
  485. if ($menu) {
  486. if (isset($admin['auth']) && $admin['auth']) {
  487. if ($admin['auth'] == 'all' || $admin['auth'] == '') {
  488. return;
  489. }
  490. $admin['auth'] = explode(',', $admin['auth']);
  491. if (!in_array($menu['id'], $admin['auth'])) {
  492. Dever::alert('您没有操作权限');
  493. }
  494. } else {
  495. Dever::alert('您没有操作权限');
  496. }
  497. } else {
  498. Dever::alert('您没有操作权限');
  499. }
  500. } else {
  501. //Dever::alert('您没有操作权限');
  502. }
  503. }
  504. # 得到当前管理员的权限
  505. public function admin()
  506. {
  507. $admin = $this->info();
  508. return $admin['auth'] == 'all' ? '' : explode(',', $admin['auth']);
  509. }
  510. # 得到当前头部菜单的权限
  511. public function top()
  512. {
  513. $admin = $this->info();
  514. return $admin['top'] == 'all' ? '' : $admin['top'];
  515. return $admin['top'] == 'all' ? '' : explode(',', $admin['top']);
  516. }
  517. # 设置头部菜单的权限
  518. public function _setTop($info)
  519. {
  520. if ($info) {
  521. $info['key'] = explode('_', $info['key']);
  522. $count = count($info['key']);
  523. if ($count > 2) {
  524. foreach ($info['key'] as $k => $v) {
  525. if ($k+1 < $count) {
  526. $key[] = $v;
  527. }
  528. }
  529. $key = implode('_', $key);
  530. } else {
  531. $key = $info['key'][0];
  532. }
  533. $data = $this->data();
  534. $key = str_replace('/', '-', $key);
  535. $this->top = $info;
  536. $this->save->add($this->name . '_topgetv1_' . $key . '_a' . $data['id'], $info, 3600 * 24 * 365);
  537. }
  538. }
  539. # 得到当前头部菜单
  540. public function getTop($key)
  541. {
  542. $state = false;
  543. if (is_array($key)) {
  544. $key = $key[0];
  545. $state = true;
  546. }
  547. $data = $this->data();
  548. $key = str_replace('/', '-', $key);
  549. $data = $this->save->get($this->name . '_topgetv1_' . $key . '_a' . $data['id']);
  550. $top = Dever::input('top');
  551. if ($top) {
  552. $data = Dever::db('manage/top')->one($top);
  553. }
  554. //print_r($data);die;
  555. # 当数据不存在时,先从数据库里取出一个最新的
  556. if (!$data && $state == true) {
  557. $info = Dever::db('manage/top')->key(array('where_key' => $key));
  558. if ($info) {
  559. $data = Dever::db('manage/top')->getOne(array('where_top_id' => $info['id']));
  560. }
  561. } elseif (!$data && $this->top) {
  562. $data = $this->top;
  563. }
  564. return $data;
  565. }
  566. public function config()
  567. {
  568. $admin = $this->info();
  569. if (!$admin && $id = Dever::input('auth', 1)) {
  570. $admin['config']['id'] = $id;
  571. }
  572. if ($admin['config'] && $admin['config']['id'] > 0) {
  573. $admin['config'] = Dever::db('manage/config')->info(array('where_id' => $admin['config']['id']));
  574. }
  575. $state = isset($admin['config']) && $admin['config'];
  576. $admin['config']['title'] = ($state && $admin['config']['title']) ? $admin['config']['title'] : Dever::config('base')->name . '';
  577. $admin['config']['info'] = ($state && $admin['config']['info']) ? $admin['config']['info'] : Dever::config('base')->name . ' 欢迎您';
  578. $admin['config']['content'] = ($state && $admin['config']['content']) ? $admin['config']['content'] : '欢迎您使用' . $admin['config']['title'];
  579. $admin['config']['template'] = ($state && $admin['config']['template']) ? $admin['config']['template'] : 1;
  580. $admin['config']['front_url'] = Dever::config('base')->host;
  581. $admin['config']['front_name'] = '访问' . Dever::config('base')->name;
  582. $admin['config']['front_display'] = $admin['config']['front_url'] ? 'display:' : 'display:none';
  583. $admin['config']['version'] = Dever::config('base')->version;
  584. $admin['config']['refer'] = Dever::input('refer');
  585. $admin['config']['login_url'] = Dever::url("auth.login");
  586. $admin['config']['copyright'] = Dever::config("base")->copyright;
  587. return $admin['config'];
  588. }
  589. /**
  590. * oper的判断
  591. *
  592. * @param uri string
  593. * @return mixed
  594. */
  595. public function oper($type = 1)
  596. {
  597. $oper = '';
  598. $admin = $this->info();
  599. //$role['oper'] = '1,2,3,4,5';
  600. if ($admin && isset($admin['oper']) && $admin['oper'] != 'all') {
  601. if (strpos(',' . $admin['oper'], ',' . $type) !== false) {
  602. return true;
  603. } else {
  604. return false;
  605. }
  606. } else {
  607. return true;
  608. }
  609. }
  610. /**
  611. * 获取当前uri的类型
  612. *
  613. * @param uri string
  614. * @return mixed
  615. */
  616. private function table($table, $project)
  617. {
  618. if ($table == 'other') {
  619. $table = '';
  620. } else {
  621. $path = Dever::load('manage/src/project.path', $project);
  622. $config = Dever::database(DEVER_PATH . $path . 'database/' . $table . '.php');
  623. $table = $config['lang'];
  624. }
  625. return $table;
  626. }
  627. /**
  628. * checkEmail
  629. *
  630. * @return mixed
  631. */
  632. public function checkEmail($id)
  633. {
  634. $id = Dever::input('update_where_id');
  635. # 先验证email是否已经存在
  636. $email = Dever::input('update_email');
  637. $info = Dever::db('manage/admin')->one(array('option_email' => $email));
  638. if ($id > 0 && $info && $info['id'] != $id) {
  639. Dever::alert('该邮箱已经存在');
  640. } elseif ($id < 0 && $info) {
  641. Dever::alert('该邮箱已经存在');
  642. }
  643. }
  644. /**
  645. * 获取当前uri的类型
  646. *
  647. * @param uri string
  648. * @return mixed
  649. */
  650. public function type($uri)
  651. {
  652. if (strpos($uri, '.') !== false) {
  653. $type = 3;
  654. } elseif (strpos($uri, '-') !== false) {
  655. $type = 2;
  656. } else {
  657. $type = 1;
  658. }
  659. return $type;
  660. }
  661. /**
  662. * 获取所有权限列表,并进行统计处理
  663. *
  664. * @return mixed
  665. */
  666. public function get($state = true)
  667. {
  668. $result = Dever::load('manage/src/menu.left', $state);
  669. $result['state'] = 1;
  670. return $result;
  671. }
  672. /**
  673. * 获取所有数据权限列表,并进行统计处理
  674. *
  675. * @return mixed
  676. */
  677. public function getByData()
  678. {
  679. $key = Dever::config('base')->manageAuthData;
  680. $result = array();
  681. if ($key) {
  682. $result = Dever::load($key);
  683. $result['state'] = 1;
  684. }
  685. return $result;
  686. }
  687. /**
  688. * 修改当前管理员的密码
  689. *
  690. * @return mixed
  691. */
  692. public function password()
  693. {
  694. $admin = $this->info();
  695. $new = Dever::input('new');
  696. $old = Dever::input('old');
  697. if ($admin && $admin['id'] > 0 && $new && $old && $new != $old && hash('sha256', ($old)) == $admin['password']) {
  698. $param['set_password'] = $new;
  699. $param['where_id'] = $admin['id'];
  700. Dever::db('manage/admin')->password($param);
  701. $admin['password'] = hash('sha256', ($new));
  702. $this->save->add($this->name, $admin);
  703. return '修改成功';
  704. } else {
  705. return '修改失败';
  706. }
  707. }
  708. /**
  709. * 更新数据到数据库
  710. *
  711. * @return array
  712. */
  713. public function update_action($param = array())
  714. {
  715. if (isset($param['key'])) {
  716. $info = Dever::db('manage/auth')->key(array('where_key' => $param['key']));
  717. //print_r($info);die;
  718. if (!$info) {
  719. $update['add_project'] = $param['project'];
  720. $update['add_project_name'] = $param['project_name'];
  721. $update['add_key'] = $param['key'];
  722. $update['add_name'] = $param['name'];
  723. $update['add_auth_id'] = isset($param['auth']) ? $param['auth'] : -1;
  724. $update['add_value'] = $param['value'];
  725. $update['add_state'] = isset($param['state']) ? $param['state'] : 1;
  726. $info['id'] = Dever::db('manage/auth')->insert($update);
  727. } else {
  728. $update['set_project'] = $param['project'];
  729. $update['set_project_name'] = $param['project_name'];
  730. $update['set_name'] = $param['name'];
  731. $update['set_value'] = $param['value'];
  732. $update['set_state'] = isset($param['state']) ? $param['state'] : 1;
  733. $update['where_id'] = $info['id'];
  734. Dever::db('manage/auth')->update($update);
  735. }
  736. return $info['id'];
  737. }
  738. return false;
  739. }
  740. /**
  741. * 同步子权限更新到数据库
  742. *
  743. * @return array
  744. */
  745. public function sync($param = array())
  746. {
  747. if (isset($param[0]) && isset($param[1])) {
  748. $key = $param[1]['key'];
  749. $info = Dever::db('manage/auth')->key(array('where_key' => $key));
  750. if ($info) {
  751. $update['value'] = Dever::input('where_id', $param[0]);
  752. $update['name'] = Dever::input('name', '-u');
  753. $update['top'] = $info['id'];
  754. $update['key'] = $key . '_' . $update['value'];
  755. $update['state'] = Dever::input('state', '-u');
  756. $this->update_action($update);
  757. }
  758. }
  759. }
  760. /**
  761. * 获取所有的项目精细权限
  762. *
  763. * @return array
  764. */
  765. public function all()
  766. {
  767. $data = Dever::db('manage/auth')->main;
  768. if ($data) {
  769. $child = Dever::db('manage/auth')->child;
  770. foreach ($data as $t => $d) {
  771. foreach ($d as $k => $v) {
  772. if (isset($child[$v['id']])) {
  773. $c = 0;
  774. foreach ($child[$v['id']] as $i => $j) {
  775. $data[$t][$k]['child'][$i] = $j;
  776. $c++;
  777. }
  778. if (!isset($data[$t][$k]['child'])) {
  779. unset($data[$t][$k]);
  780. }
  781. }
  782. }
  783. $data[$t]['state'] = 1;
  784. }
  785. }
  786. return $data;
  787. }
  788. /**
  789. * opt push
  790. *
  791. * @return mixed
  792. */
  793. public function opt($param = false)
  794. {
  795. $opt = new Opt;
  796. $opt->push($param);
  797. }
  798. /**
  799. * opt push
  800. *
  801. * @return mixed
  802. */
  803. public function opt_api($param = false)
  804. {
  805. $opt = new Opt;
  806. $opt->push($param);
  807. }
  808. /**
  809. * opt push
  810. *
  811. * @return mixed
  812. */
  813. public function api($param = false)
  814. {
  815. $api = new Api;
  816. $api->push($param);
  817. }
  818. /**
  819. * opt push
  820. *
  821. * @return mixed
  822. */
  823. public function api_api($param = false)
  824. {
  825. $api = new Api;
  826. $api->push($param);
  827. }
  828. # 测试
  829. public function test_call()
  830. {
  831. $data = Dever::db('manage/admin')->select(array(), function($data) {
  832. $data['username'] .= 'test';
  833. return $data;
  834. });
  835. print_r($data);die;
  836. # 重新命名项目,方便复用
  837. //Dever::setAlias('area', 'sarea');
  838. //return Dever::load('area/api')->string('110000,110100');
  839. }
  840. }