From 217120d27471dfb00bb31ad76604b1b5b6b2bab6 Mon Sep 17 00:00:00 2001 From: augushong Date: Thu, 14 May 2020 21:36:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=89=8B=E5=8A=A8=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E7=9A=84=E6=9D=83=E9=99=90=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/AdminGroup.php | 5 +- app/admin/controller/AdminPermission.php | 10 +- app/admin/controller/Common.php | 20 -- app/common.php | 47 ++++ app/index/controller/BaseController.php | 4 +- app/middleware/PermissionRecord.php | 24 +- app/model/Admin.php | 2 +- app/model/AdminLog.php | 17 -- app/model/AdminPermission.php | 9 - ...10125828_create_table_admin_permission.php | 8 +- database/seeds/InitAdminPermission.php | 229 +----------------- view/admin/admin/admin_log.html | 9 +- view/admin/admin_group/create.html | 2 +- view/admin/admin_group/edit.html | 4 +- view/admin/admin_permission/index.html | 6 +- view/admin/common/_header.html | 2 + 16 files changed, 71 insertions(+), 327 deletions(-) diff --git a/app/admin/controller/AdminGroup.php b/app/admin/controller/AdminGroup.php index 92b27ee..4c47e7b 100644 --- a/app/admin/controller/AdminGroup.php +++ b/app/admin/controller/AdminGroup.php @@ -31,7 +31,7 @@ class AdminGroup extends Common public function create() { // - $premission_list = AdminPermission::order('app,controller,action')->select(); + $premission_list = AdminPermission::order('key')->select(); View::assign('permission_list',$premission_list); return View::fetch(); @@ -87,7 +87,8 @@ class AdminGroup extends Common $model_admin_group = AppAdminGroup::find($id); - $premission_list = AdminPermission::order('app,controller,action')->select(); + $premission_list = AdminPermission::order('key')->select(); + View::assign('permission_list',$premission_list); View::assign('admin_group',$model_admin_group); diff --git a/app/admin/controller/AdminPermission.php b/app/admin/controller/AdminPermission.php index 09c0e0e..44a3334 100644 --- a/app/admin/controller/AdminPermission.php +++ b/app/admin/controller/AdminPermission.php @@ -18,7 +18,7 @@ class AdminPermission extends Common { // - $list = AppAdminPermission::order('app,controller,action')->paginate(); + $list = AppAdminPermission::order('key')->paginate(); View::assign('list',$list); @@ -40,14 +40,6 @@ class AdminPermission extends Common $model_permission = AppAdminPermission::find($id); - if(isset($post_data['url'])){ - $url_info = \explode('/',$post_data['url']); - - $post_data['app'] = $url_info[0]; - $post_data['controller'] = $url_info[1]; - $post_data['action'] = $url_info[2]; - } - $model_permission->data($post_data); $model_permission->save(); diff --git a/app/admin/controller/Common.php b/app/admin/controller/Common.php index bac9c5c..262ec79 100644 --- a/app/admin/controller/Common.php +++ b/app/admin/controller/Common.php @@ -31,26 +31,6 @@ class Common extends BaseController{ throw new HttpResponseException(redirect('admin/Login/index')); } } - - if(!empty($this->adminInfo['group'])){ - - $current_access_info = [ - 'app'=>app('http')->getName(), - 'controller'=>request()->controller(), - 'action'=>request()->action() - ]; - - $model_permission = AdminPermission::where($current_access_info)->find(); - - if(!empty($model_permission)){ - if(!in_array($model_permission->id,$this->adminInfo->group->permissions)){ - return $this->error('您没有访问权限'); - } - } - } - - - } View::assign('admin',$this->adminInfo); diff --git a/app/common.php b/app/common.php index eff1af6..cb743be 100644 --- a/app/common.php +++ b/app/common.php @@ -10,12 +10,16 @@ // +---------------------------------------------------------------------- // 应用公共文件 + +use app\model\Admin; +use app\model\AdminPermission; use app\model\SystemConfig; use think\facade\Cache; use League\Flysystem\Util\MimeType; use think\File; use think\facade\Filesystem; use app\model\UploadFiles; +use think\facade\Session; function json_message($data = [], $code = 0, $msg = '') { @@ -193,3 +197,46 @@ function array2level($array, $pid = 0, $level = 1) return $list; } + + +function check_permission($key,$admin_id = null) +{ + if(is_null($admin_id)){ + $admin_id = Session::get('admin_id'); + } + + if(empty($admin_id)){ + return true; + } + + if($admin_id == 1){ + return true; + } + + $model_admin = Admin::cache(60)->find($admin_id); + + if(empty($model_admin->getData('group_id'))){ + return true; + } + + + $cache_key = 'permission_'.$key; + + $model_permission = Cache::get($cache_key); + if (empty($model_permission)) { + $model_permission = AdminPermission::where('key',$key)->find(); + } + + if (empty($model_permission)) { + $model_permission = AdminPermission::create([ + 'key'=>$key + ]); + Cache::set($cache_key,$model_permission,60); + } + + if(in_array($model_permission->id,$model_admin->group->permissions)){ + return true; + } + + return false; +} \ No newline at end of file diff --git a/app/index/controller/BaseController.php b/app/index/controller/BaseController.php index de9feca..f67d013 100644 --- a/app/index/controller/BaseController.php +++ b/app/index/controller/BaseController.php @@ -35,13 +35,13 @@ class BaseController extends AppBaseController $this->indexTplMethodCurrentAction = $this->indexTplMethod.Str::studly($this->request->action()); - + } public function assign($template, $value) { return View::assign($template, $value); - } + } public function fetch($template = '', $vars = []) { diff --git a/app/middleware/PermissionRecord.php b/app/middleware/PermissionRecord.php index de7cc06..72a579f 100644 --- a/app/middleware/PermissionRecord.php +++ b/app/middleware/PermissionRecord.php @@ -7,25 +7,9 @@ use app\Request; class PermissionRecord { - public function handle(Request $request, \Closure $next) - { + public function handle(Request $request, \Closure $next) + { - $current_access_info = [ - 'app'=>app('http')->getName(), - 'controller'=>$request->controller(), - 'action'=>$request->action() - ]; - - if(in_array('',$current_access_info)){ - return $next($request); - } - - $model_permission = AdminPermission::where($current_access_info)->find(); - - if(empty($model_permission)){ - AdminPermission::create($current_access_info); - } - - return $next($request); - } + return $next($request); + } } diff --git a/app/model/Admin.php b/app/model/Admin.php index c78f5cb..6c97d7f 100644 --- a/app/model/Admin.php +++ b/app/model/Admin.php @@ -27,7 +27,7 @@ class Admin extends Model return []; } - return AdminGroup::where('id',$this->getData('group_id'))->cache(1)->find(); + return AdminGroup::where('id',$this->getData('group_id'))->cache(60)->find(); } } diff --git a/app/model/AdminLog.php b/app/model/AdminLog.php index 0352016..77f97ad 100644 --- a/app/model/AdminLog.php +++ b/app/model/AdminLog.php @@ -19,22 +19,5 @@ class AdminLog extends Model return $this->belongsTo('Admin','admin_id'); } - public function getUrlAttr() - { - return AdminPermission::where([ - 'app'=>$this->getData('app'), - 'controller'=>$this->getData('controller'), - 'action'=>$this->getData('action'), - ])->find(); - } - public function setParamAttr($value) - { - return json_encode($value,JSON_UNESCAPED_UNICODE); - } - - public function getParamAttr($value) - { - return \mb_substr($value,0,30); - } } diff --git a/app/model/AdminPermission.php b/app/model/AdminPermission.php index cf9ad53..c79b081 100644 --- a/app/model/AdminPermission.php +++ b/app/model/AdminPermission.php @@ -22,14 +22,5 @@ class AdminPermission extends Model return $status[intval($value)]; } - public function getNameAttr($value) - { - if(empty($value)){ - $value = $this->getData('app').'/'.$this->getData('controller').'/'.$this->getData('action'); - } - - return $value; - } - } diff --git a/database/migrations/20191010125828_create_table_admin_permission.php b/database/migrations/20191010125828_create_table_admin_permission.php index b3b1157..7fd23fc 100644 --- a/database/migrations/20191010125828_create_table_admin_permission.php +++ b/database/migrations/20191010125828_create_table_admin_permission.php @@ -34,13 +34,9 @@ class CreateTableAdminPermission extends Migrator ]); $table->addColumn('name','string',['limit'=>20,'default'=>'0','comment'=>'权限名称']); - $table->addColumn('app','string',['limit'=>50,'comment'=>'应用名']); - $table->addColumn('controller','string',['limit'=>50,'comment'=>'控制器名']); - $table->addColumn('action','string',['limit'=>50,'comment'=>'方法名']); + $table->addColumn('key','string',['limit'=>100,'comment'=>'权限标识']); $table->addColumn('is_log','integer',['limit'=>1,'default'=>0,'comment'=>'是否把这个访问记录下来']); - $table->addIndex('app'); - $table->addIndex('controller'); - $table->addIndex('action'); + $table->addIndex('key'); $table->addIndex('is_log'); $table->create(); } diff --git a/database/seeds/InitAdminPermission.php b/database/seeds/InitAdminPermission.php index 8b40578..b081f55 100644 --- a/database/seeds/InitAdminPermission.php +++ b/database/seeds/InitAdminPermission.php @@ -15,234 +15,7 @@ class InitAdminPermission extends Seeder */ public function run() { - $permission_content = ' - [ - { - "id" : 3, - "name" : "系统设置", - "app" : "admin", - "controller" : "System", - "action" : "index", - "is_log" : 1 - }, - { - "id" : 9, - "name" : "系统第三方设置", - "app" : "admin", - "controller" : "System", - "action" : "others", - "is_log" : 1 - }, - { - "id" : 12, - "name" : "登录页面", - "app" : "admin", - "controller" : "Login", - "action" : "index", - "is_log" : 1 - }, - { - "id" : 13, - "name" : "登录验证", - "app" : "admin", - "controller" : "Login", - "action" : "auth", - "is_log" : 1 - }, - { - "id" : 18, - "name" : "退出", - "app" : "admin", - "controller" : "Login", - "action" : "logout", - "is_log" : 1 - }, - { - "id" : 21, - "name" : "系统设置更新", - "app" : "admin", - "controller" : "System", - "action" : "update", - "is_log" : 1 - }, - { - "id" : 24, - "name" : "管理员权限-删除", - "app" : "admin", - "controller" : "AdminPermission", - "action" : "delete", - "is_log" : 0 - }, - { - "id" : 25, - "name" : "管理员权限-列表", - "app" : "admin", - "controller" : "AdminPermission", - "action" : "index", - "is_log" : 0 - }, - { - "id" : 26, - "name" : "后台首页", - "app" : "admin", - "controller" : "Index", - "action" : "index", - "is_log" : 0 - }, - { - "id" : 27, - "name" : "管理员分组-列表", - "app" : "admin", - "controller" : "AdminGroup", - "action" : "index", - "is_log" : 0 - }, - { - "id" : 29, - "name" : "文件-列表", - "app" : "admin", - "controller" : "File", - "action" : "index", - "is_log" : 0 - }, - { - "id" : 30, - "name" : "管理员帐号-列表", - "app" : "admin", - "controller" : "Admin", - "action" : "index", - "is_log" : 1 - }, - { - "id" : 31, - "name" : "管理员权限-保存编辑", - "app" : "admin", - "controller" : "AdminPermission", - "action" : "update", - "is_log" : 0 - }, - { - "id" : 32, - "name" : "管理员-编辑(登陆的人自己改自己)", - "app" : "admin", - "controller" : "Admin", - "action" : "edit", - "is_log" : 0 - }, - { - "id" : 33, - "name" : "管理员日志-列表", - "app" : "admin", - "controller" : "Admin", - "action" : "adminLog", - "is_log" : 0 - }, - { - "id" : 34, - "name" : "管理员-改密码(自己改自己)", - "app" : "admin", - "controller" : "Admin", - "action" : "password", - "is_log" : 0 - }, - { - "id" : 35, - "name" : "管理员分组-添加", - "app" : "admin", - "controller" : "AdminGroup", - "action" : "create", - "is_log" : 0 - }, - { - "id" : 36, - "name" : "管理员分组-保存添加", - "app" : "admin", - "controller" : "AdminGroup", - "action" : "save", - "is_log" : 0 - }, - { - "id" : 37, - "name" : "管理员分组-删除", - "app" : "admin", - "controller" : "AdminGroup", - "action" : "delete", - "is_log" : 0 - }, - { - "id" : 38, - "name" : "管理员分组-编辑", - "app" : "admin", - "controller" : "AdminGroup", - "action" : "edit", - "is_log" : 0 - }, - { - "id" : 39, - "name" : "管理员分组-保存编辑", - "app" : "admin", - "controller" : "AdminGroup", - "action" : "update", - "is_log" : 0 - }, - { - "id" : 40, - "name" : "管理员-保存更新", - "app" : "admin", - "controller" : "Admin", - "action" : "update", - "is_log" : 0 - }, - { - "id" : 41, - "name" : "文件-磁盘清空", - "app" : "admin", - "controller" : "File", - "action" : "clear", - "is_log" : 0 - }, - { - "id" : 42, - "name" : "管理员帐号-添加", - "app" : "admin", - "controller" : "Admin", - "action" : "create", - "is_log" : 0 - }, - { - "id" : 43, - "name" : "管理员帐号-保存添加", - "app" : "admin", - "controller" : "Admin", - "action" : "save", - "is_log" : 0 - }, - { - "id" : 45, - "name" : "管理员帐号-编辑", - "app" : "admin", - "controller" : "Admin", - "action" : "editAccount", - "is_log" : 0 - }, - { - "id" : 46, - "name" : "管理员帐号-删除", - "app" : "admin", - "controller" : "Admin", - "action" : "delete", - "is_log" : 0 - }, - { - "id" : 47, - "name" : "管理员帐号-保存编辑", - "app" : "admin", - "controller" : "Admin", - "action" : "updateAccount", - "is_log" : 0 - } - ] - '; + $permission_content = ''; $permissions = json_decode($permission_content,true); diff --git a/view/admin/admin/admin_log.html b/view/admin/admin/admin_log.html index ed51992..624fb28 100644 --- a/view/admin/admin/admin_log.html +++ b/view/admin/admin/admin_log.html @@ -46,7 +46,6 @@ ID 访问地址 访问信息 - 携带参数 操作管理员 @@ -58,17 +57,13 @@ {$vo.id}

{$vo.url.name}

-

{$vo.app}/{$vo.controller}/{$vo.action}

+

{$vo.key}

访问时间:{$vo.create_time}

访问IP:{$vo.ip}

- -
查看详情
-
- {$vo.param} - + {$vo.admin_id}/{$vo.admin.nickname} diff --git a/view/admin/admin_group/create.html b/view/admin/admin_group/create.html index f28f5c9..db12588 100644 --- a/view/admin/admin_group/create.html +++ b/view/admin/admin_group/create.html @@ -45,7 +45,7 @@
分组权限
{volist name='permission_list' id='vo'} - + {/volist}
diff --git a/view/admin/admin_group/edit.html b/view/admin/admin_group/edit.html index dd338e9..7934ce5 100644 --- a/view/admin/admin_group/edit.html +++ b/view/admin/admin_group/edit.html @@ -50,13 +50,13 @@ {else /} - + {/if} {/volist}
- +
diff --git a/view/admin/admin_permission/index.html b/view/admin/admin_permission/index.html index 0af88d8..8618aae 100644 --- a/view/admin/admin_permission/index.html +++ b/view/admin/admin_permission/index.html @@ -39,7 +39,7 @@ ID 权限名称 - 权限URL + 权限标识 操作 @@ -53,9 +53,9 @@
设置
- {$vo.app}/{$vo.controller}/{$vo.action} + {$vo.key}
设置
+ data-url="{$vo.key}">设置
diff --git a/view/admin/common/_header.html b/view/admin/common/_header.html index ae4dbfe..6b61e43 100644 --- a/view/admin/common/_header.html +++ b/view/admin/common/_header.html @@ -4,7 +4,9 @@