Auth.php 26 KB

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