Backend.php 6.1 KB

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