Backend.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/9/21
  12. */
  13. namespace app\common\controller;
  14. use app\admin\model\Admin;
  15. use app\admin\model\AuthRule;
  16. use app\common\controller\base;
  17. use lemo\helper\FileHelper;
  18. use lemo\helper\SignHelper;
  19. use think\facade\Db;
  20. use think\facade\Request;
  21. use think\facade\Session;
  22. use think\facade\View;
  23. class Backend extends \app\common\controller\Base
  24. {
  25. public $pageSize=15;
  26. public $menu = '';
  27. public $adminRules='';
  28. public $hrefId='';
  29. public $quanxian;
  30. public $adminid;
  31. public $project;
  32. public function initialize()
  33. {
  34. parent::initialize(); // TODO: Change the autogenerated stub
  35. //判断管理员是否登录
  36. if (!session('admin.id') && !session('admin')) {
  37. $this->redirect(url('/admin/login/index'));
  38. }
  39. $this->authCheck();
  40. $this->quanxian=\session("quanxian");
  41. $this->adminid=\session("adminid");
  42. $this->project=\session("project");
  43. }
  44. /**
  45. * 验证权限
  46. */
  47. public function authCheck(){
  48. $allow = [
  49. 'index/index',
  50. 'index/main',
  51. 'index/cleardata',
  52. 'index/logout',
  53. 'login/password',
  54. ];
  55. $route = strtolower(Request::controller()).'/'.strtolower(Request::action());
  56. if(session('admin.id')!==1){
  57. $this->hrefId = Db::name('auth_rule')->where('href',$route)->value('id');
  58. //当前管理员权限
  59. $map['a.id'] = Session::get('admin.id');
  60. $rules=Db::name('admin')->alias('a')
  61. ->join('auth_group ag','a.group_id = ag.id','left')
  62. ->where($map)
  63. ->value('ag.rules');
  64. //用户权限规则id
  65. $adminRules = explode(',',$rules);
  66. // 不需要权限的规则id;
  67. $noruls = AuthRule::where('auth_open',1)->column('id');
  68. $this->adminRules = array_merge($adminRules,$noruls);
  69. if($this->hrefId){
  70. // 不在权限里面,并且请求为post
  71. if(!in_array($this->hrefId,$this->adminRules)){
  72. $this->error(lang('permission denied'));exit();
  73. }
  74. }else{
  75. if(!in_array($route,$allow)) {
  76. $this->error(lang('permission denied'));
  77. exit();
  78. }
  79. }
  80. }
  81. return $this->adminRules;
  82. }
  83. /**
  84. * 退出登录
  85. */
  86. public function logout()
  87. {
  88. Session::clear();
  89. $this->success(lang('logout success'), '@admin/login');
  90. }
  91. /*
  92. * 修改密码
  93. */
  94. public function password(){
  95. if (!Request::isPost()){
  96. return View::fetch('login/password');
  97. }else{
  98. if( Request::isPost() and Session::get('admin.id')===3){
  99. $this->error(lang('test data cannot edit'));
  100. }
  101. $data = Request::post();
  102. $oldpassword = Request::post('oldpassword', '123456', 'lemo\helper\StringHelper::filterWords');
  103. $admin = Admin::find($data['id']);
  104. if(!password_verify($oldpassword, $admin['password'])){
  105. $this->error(lang('origin password error'));
  106. }
  107. $password = Request::post('password', '123456','lemo\helper\StringHelper::filterWords');
  108. try {
  109. $data['password'] = password_hash($password,PASSWORD_BCRYPT, SignHelper::passwordSalt());
  110. if(Session::get('admin.id')==1){
  111. Admin::update($data);
  112. }elseif(Session::get('admin.id')==$data['id']){
  113. Admin::update($data);
  114. }else{
  115. $this->error(lang('permission denied'));
  116. }
  117. } catch (\Exception $e) {
  118. $this->error($e->getMessage());
  119. }
  120. $this->success(lang('edit success'));
  121. }
  122. }
  123. public function base(){
  124. if (!Request::isPost()){
  125. return View::fetch('admin/password');
  126. }else{
  127. $data = Request::post();
  128. $admin = Admin::find($data['id']);
  129. $oldpassword = Request::post('oldpassword', '123456', 'lemo\helper\StringHelper::filterWords');
  130. if(!password_verify($oldpassword, $admin['password'])){
  131. $this->error(lang('origin password error'));
  132. }
  133. $password = Request::post('password', '123456','lemo\helper\StringHelper::filterWords');
  134. try {
  135. $data['password'] = password_hash($password,PASSWORD_BCRYPT, SignHelper::passwordSalt());
  136. if(Session::get('admin.id')==1){
  137. Admin::update($data);
  138. }elseif(Session::get('admin.id')==$data['id']){
  139. Admin::update($data);
  140. }else{
  141. $this->error(lang('permission denied'));
  142. }
  143. } catch (\Exception $e) {
  144. $this->error($e->getMessage());
  145. }
  146. $this->success(lang('edit success'));
  147. }
  148. }
  149. /*
  150. * 清除缓存 出去session缓存
  151. */
  152. public function clearData(){
  153. $dir = config('admin.clear_cache_dir') ? app()->getRootPath().'runtime/admin' : app()->getRootPath().'runtime';
  154. $cache = app()->getRootPath().'runtime/cache';
  155. if(is_dir($cache)){
  156. FileHelper::delDir($cache);
  157. }
  158. if(FileHelper::delDir($dir) ){
  159. $this->success('清除成功');
  160. }
  161. }
  162. }