Auth.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/2
  12. */
  13. namespace app\admin\controller;
  14. use app\admin\model\AuthGroup;
  15. use app\admin\model\AuthRule;
  16. use app\admin\model\Admin;
  17. use app\common\controller\Backend;
  18. use lemo\helper\SignHelper;
  19. use lemo\helper\StringHelper;
  20. use lemo\helper\TreeHelper;
  21. use think\facade\Cache;
  22. use think\facade\Config;
  23. use think\facade\Db;
  24. use think\facade\Request;
  25. use think\facade\Session;
  26. use think\facade\View;
  27. class Auth extends Backend
  28. {
  29. public $uid = '';
  30. public function initialize()
  31. {
  32. $this->uid = Session::get('admin.id');
  33. parent::initialize(); // TODO: Change the autogenerated stub
  34. }
  35. /*-----------------------管理员管理----------------------*/
  36. // 管理员列表
  37. public function adminList()
  38. {
  39. if(Request::isPost()){
  40. $where=Request::post();
  41. $map=[];
  42. $map1=[];
  43. $map2=[];
  44. if(isset($where['keys'])) {
  45. $map = [
  46. ['a.username', 'like', "%" . $where['keys'] . "%"],
  47. ];
  48. $map1 = [
  49. ['a.email', 'like', "%" . $where['keys'] . "%"],
  50. ];
  51. $map2= [
  52. ['a.mobile', 'like', "%" . $where['keys'] . "%"],
  53. ];
  54. }
  55. $page=input("page")?:1;
  56. $limit=input("limit")?:10;
  57. $list=Db::name('admin')->order("id desc")->alias('a')
  58. ->join('auth_group ag','a.group_id = ag.id','left')
  59. ->field('a.*,ag.title')
  60. ->whereOr($map,$map1,$map2)
  61. ->paginate(['list_rows' =>$limit, 'page' => $page])->toArray();
  62. // ->select();
  63. // var_dump($list);
  64. return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
  65. // return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$list];
  66. }
  67. return View::fetch();
  68. }
  69. // 管理员添加
  70. public function adminAdd()
  71. {
  72. if (Request::isPost()) {
  73. $data = Request::post();
  74. try{
  75. $this->validate($data, 'Admin');
  76. }catch (\Exception $e){
  77. $this->error($e->getMessage());
  78. }
  79. $data['password'] = StringHelper::filterWords($data['password']);
  80. if(!$data['password']){
  81. $data['password']='123456';
  82. }
  83. $data['password'] = password_hash($data['password'],PASSWORD_BCRYPT, SignHelper::passwordSalt());
  84. //添加
  85. $result = Admin::create($data);
  86. if ($result) {
  87. $this->success(lang('add success'), url('adminList'));
  88. } else {
  89. $this->error(lang('add fail'));
  90. }
  91. } else {
  92. $info = '';
  93. $auth_group = AuthGroup::where('status', 1)->select();
  94. $view = [
  95. 'info' =>$info,
  96. 'authGroup' => $auth_group,
  97. 'title' => lang('add'),
  98. ];
  99. View::assign($view);
  100. return View::fetch();
  101. }
  102. }
  103. // 管理员删除
  104. public function adminDel()
  105. {
  106. $id = Request::post('id');
  107. if ($id > 1) {
  108. Admin::destroy($id);
  109. $this->success(lang('delete success'));
  110. } else {
  111. $this->error(lang('supper man cannot delete'));
  112. }
  113. }
  114. // 管理员状态修改
  115. public function adminState()
  116. {
  117. if (Request::isPost()) {
  118. $id = Request::post('id');
  119. if (empty($id)) {
  120. $this->error('id'.lang('not exist'));
  121. }
  122. if ($id == 1) {
  123. $this->error(lang('supper man cannot edit state'));
  124. }
  125. // if($this->uid==3){
  126. // $this->error(lang('test data cannot edit'));
  127. //
  128. // }
  129. $admin = Admin::find($id);
  130. $status = $admin['status'] == 1 ? 0 : 1;
  131. $admin->status = $status;
  132. $admin->save();
  133. $this->success(lang('edit success'));
  134. }
  135. }
  136. /**
  137. * 管理员修改
  138. */
  139. public function adminEdit()
  140. {
  141. if (Request::isPost()) {
  142. $data = Request::post();
  143. if(!$data['username']) $this->error(lang('username').lang('cannot null'));
  144. // if(!$data['password'])
  145. if(!$data['group_id']) $this->error(lang('adminGroup').lang('cannot null'));
  146. $admin = Admin::find($data['id']);
  147. // if(password_verify($data['password'],$admin['password'])){
  148. // unset($data['password']);
  149. //
  150. // }else{
  151. // $data['password'] = Request::post('password', '123456', 'lemo\helper\StringHelper::filterWords');
  152. // $data['password'] = password_hash($data['password'],PASSWORD_BCRYPT, SignHelper::passwordSalt());
  153. // }
  154. if ($data["password"]){
  155. $data['password'] = password_hash($data['password'],PASSWORD_BCRYPT, SignHelper::passwordSalt());
  156. }else{
  157. unset($data['password']);
  158. }
  159. Admin::update($data);
  160. // if($this->uid==$data['id']){
  161. // Session::set('admin',null);
  162. // }
  163. $this->success(lang('edit success'), url('Auth/adminList'));
  164. } else {
  165. $id = Request::param('id')?Request::param('id'):$this->uid;
  166. if ($id) {
  167. $auth_group = AuthGroup::select();
  168. $admin = Admin::field("id,group_id,username,mobile")->find($id);
  169. $view = [
  170. 'info' => $admin,
  171. 'authGroup' => $auth_group,
  172. 'title' => lang('edit'),
  173. ];
  174. View::assign($view);
  175. return View::fetch('admin_add');
  176. }
  177. }
  178. }
  179. /********************************权限管理*******************************/
  180. // 权限列表
  181. public function adminRule()
  182. {
  183. if(Request::isPost()){
  184. $uid = $this->uid;
  185. $arr = Db::name('auth_rule')
  186. ->where("status","<>",0)
  187. ->order('pid asc,sort asc')
  188. ->select()->toArray();
  189. foreach($arr as $k=>$v){
  190. $arr[$k]['lay_is_open']=false;
  191. }
  192. cache('authRuleList_'.$uid, $arr, 3600);
  193. return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$arr,'is'=>true,'tip'=>'操作成功'];
  194. }
  195. return View::fetch('admin_rule');
  196. }
  197. // 权限菜单显示或者隐藏
  198. public function ruleState()
  199. {
  200. if (Request::isPost()) {
  201. $id = Request::param('id');
  202. $info = AuthRule::find($id);
  203. $info->menu_status = $info['menu_status'] == 1 ? 0 : 1;
  204. $info->save();
  205. $this->success(lang('edit success'));
  206. }
  207. }
  208. // 设置权限是否验证
  209. public function ruleOpen()
  210. {
  211. if (Request::isPost()) {
  212. $id = Request::param('id');
  213. $info = AuthRule::find($id);
  214. $info->auth_open = $info['auth_open'] == 1 ? 0 : 1;
  215. $info->save();
  216. $this->success(lang('edit success'));
  217. }
  218. }
  219. // 设置权限排序
  220. public function ruleSort()
  221. {
  222. if (Request::isPost()) {
  223. $id = Request::param('id');
  224. $sort = Request::param('sort');
  225. $info = AuthRule::find($id);
  226. $info->sort = $sort;
  227. $info->save();
  228. $this->success(lang('edit success'));
  229. }
  230. }
  231. // 权限删除
  232. public function ruleDel()
  233. {
  234. $id = Request::param('id');
  235. $child = AuthRule::where('pid',$id)->find();
  236. if ($id && !$child) {
  237. AuthRule::destroy($id);
  238. $this->success(lang('delete success'));
  239. }elseif($child){
  240. $this->error(lang('delete child first'));
  241. }else{
  242. $this->error('id'.lang('not exist'));
  243. }
  244. }
  245. // 权限批量删除
  246. public function ruleSelectDel()
  247. {
  248. $ids = Request::param('ids');
  249. if ($ids) {
  250. AuthRule::destroy($ids);
  251. $this->success(lang('delete success'));
  252. }
  253. }
  254. // 权限增加
  255. public function ruleAdd()
  256. {
  257. if (Request::isPost()) {
  258. $data = Request::post();
  259. if (empty($data['title'])) {
  260. $this->error(lang('rule name cannot null'));
  261. }
  262. if (empty($data['sort'])) {
  263. $this->error(lang('sort').lang(' cannot null'));
  264. }
  265. $data['icon'] = $data['icon']?$data['icon']:'fa fa-adjust';
  266. if (AuthRule::create($data)) {
  267. $this->success(lang('add success'), url('adminRule'));
  268. } else {
  269. $this->error(lang('add fail'));
  270. }
  271. } else {
  272. $list = Db::name('auth_rule')
  273. ->order('sort ASC')
  274. ->select();
  275. $list = TreeHelper::cateTree($list);
  276. $pid = Request::param('id') ? Request::param('id') : 0;
  277. $rule = '';
  278. if(Request::get('rule_id')){
  279. $rule = Db::name('auth_rule')
  280. ->find(Request::get('rule_id'));
  281. }
  282. $view = [
  283. 'info' => null,
  284. 'pid' => $pid,
  285. 'ruleList' => $list,
  286. 'rule' =>$rule,
  287. ];
  288. View::assign($view);
  289. return View::fetch('rule_add');
  290. }
  291. }
  292. //权限修改
  293. public function ruleEdit()
  294. {
  295. if (request()->isPost()) {
  296. $data = Request::param();
  297. $data['icon'] = $data['icon']?$data['icon']:'fa fa-adjust';
  298. $where['id'] = $data['id'];
  299. AuthRule::update($data);
  300. $this->success(lang('edit success'), url('Auth/adminRule'));
  301. } else {
  302. $list = Db::name('auth_rule')
  303. ->order('sort asc')
  304. ->select();
  305. $list = TreeHelper::cateTree($list);
  306. $id = Request::param('id');
  307. $info = AuthRule::find($id)->toArray();
  308. $rule = '';
  309. if(Request::get('rule_id')){
  310. $rule = Db::name('auth_rule')
  311. ->find(Request::get('rule_id'));
  312. }
  313. $view = [
  314. 'info' => $info,
  315. 'ruleList' => $list,
  316. 'rule' => $rule,
  317. ];
  318. View::assign($view);
  319. return View::fetch('rule_add');
  320. }
  321. }
  322. /*-----------------------用户组管理----------------------*/
  323. // 用户组管理
  324. public function group()
  325. {
  326. if(Request::isPost()){
  327. //条件筛选
  328. $title = Request::param('title');
  329. //全局查询条件
  330. $where = [];
  331. if ($title) {
  332. $where[] = ['title', 'like', '%' . $title . '%'];
  333. }
  334. //显示数量
  335. $pageSize = Request::param('page_size', Config::get('app.page_size'));
  336. //查出所有数据
  337. $list = AuthGroup::where($where)
  338. ->paginate(
  339. $this->pageSize, false,
  340. ['query' => Request::param()]
  341. )->toArray();
  342. return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$list['data']];
  343. }
  344. return View::fetch();
  345. }
  346. // 用户组删除
  347. public function groupDel()
  348. {
  349. $id = Request::post('id');
  350. if ($id > 1) {
  351. AuthGroup::destroy($id);
  352. $this->success(lang('delete success'));
  353. } else {
  354. $this->error(lang('supper man cannot delete'));
  355. }
  356. }
  357. // 用户组添加
  358. public function groupAdd()
  359. {
  360. if (Request::isPost()) {
  361. $data = Request::post();
  362. try {
  363. $this->validate($data, 'AuthGroup');
  364. } catch (\Exception $e) {
  365. $this->error($e->getMessage());
  366. }
  367. $result = AuthGroup::create($data);
  368. if ($result) {
  369. $this->success(lang('add success'), 'Auth/adminGroup');
  370. } else {
  371. $this->error(lang('add fail'));
  372. }
  373. } else {
  374. $view = [
  375. 'info' => null
  376. ];
  377. View::assign($view);
  378. return View::fetch('group_add');
  379. }
  380. }
  381. // 用户组修改
  382. public function groupEdit()
  383. {
  384. if (Request::isPost()) {
  385. $data = Request::post();
  386. if($data['id']==1){
  387. $this->error(lang('supper man cannot edit'));
  388. }
  389. try{
  390. $this->validate($data, 'AuthGroup');
  391. }catch (\Exception $e){
  392. $this->error($e->getMessage());
  393. }
  394. $where['id'] = $data['id'];
  395. $res = AuthGroup::update($data, $where);
  396. if($res){
  397. $this->success(lang('edit success'), url('group'));
  398. }else{
  399. $this->error(lang('edit fail'));
  400. }
  401. } else {
  402. $id = Request::param('id');
  403. $info = AuthGroup::find(['id' => $id]);
  404. $view = [
  405. 'info' => $info,
  406. 'title' => lang('edit')
  407. ];
  408. View::assign($view);
  409. return View::fetch();
  410. }
  411. }
  412. // 用户组状态修改
  413. public function groupState()
  414. {
  415. if (Request::isPost()) {
  416. $id = Request::param('id');
  417. $info = AuthGroup::find($id);
  418. $info->status = $info['status'] == 1 ? 0 : 1;
  419. // if($this->uid==3){
  420. // $this->error(lang('test data cannot edit'));
  421. // }
  422. $info->save();
  423. $this->success(lang('edit success'));
  424. }
  425. }
  426. // 用户组批量删除
  427. public function groupSelectDel()
  428. {
  429. $id = Request::post('id');
  430. if ($id > 1) {
  431. AuthGroup::destroy($id);
  432. $this->success(lang('delete success'));
  433. } else {
  434. $this->error(lang('delete fail'));
  435. }
  436. }
  437. // 用户组显示权限
  438. public function groupAccess()
  439. {
  440. // Cache::set('AuthChecked',"");
  441. $id= Request::param('id');
  442. // $list = Cache::get('AuthChecked'.$id);
  443. // if(!$list){
  444. $admin_rule = AuthRule::field('id, pid, title')
  445. ->where('status',1)
  446. // ->order('sort asc')->cache(3600)
  447. ->order('sort asc')
  448. // ->order('sort asc')
  449. ->select()->toArray();
  450. $rules = AuthGroup::where('id', Request::param('id'))
  451. // ->where('status',1)->cache(3600)
  452. ->where('status',1)
  453. // ->where('status',1)
  454. ->value('rules');
  455. $list = TreeHelper::authChecked($admin_rule, $pid = 0, $rules);
  456. // Cache::set('AuthChecked'.$id,$list,3600);
  457. // Cache::set('AuthChecked',$list);
  458. // }
  459. $group_id = Request::param('id');
  460. $idList = AuthRule::column('id');
  461. sort($idList);
  462. $view = [
  463. 'list' => $list,
  464. 'idList' => $idList,
  465. 'group_id' => $group_id,
  466. ];
  467. View::assign($view);
  468. return View::fetch('group_access');
  469. }
  470. // 用户组保存权限
  471. public function groupSetaccess()
  472. {
  473. $rules = Request::post('rules');
  474. if (empty($rules)) {
  475. $this->error(lang('please choose rule'));
  476. }
  477. $data = Request::post();
  478. $rules = TreeHelper::authNormal($rules);
  479. $rls = '';
  480. foreach ($rules as $k=>$v){
  481. $rls.=$v['id'].',';
  482. }
  483. $where['id'] = $data['group_id'];
  484. $where['rules'] = $rls;
  485. if (AuthGroup::update($where)) {
  486. $admin = Session::get('admin');
  487. $admin['rules'] = $rls;
  488. Session::set('admin', $admin);
  489. $this->success(lang('rule assign success'),url('group'));
  490. } else {
  491. $this->error(lang('rule assign fail'));
  492. }
  493. }
  494. }