From 64b0361846f73c72dd2604860307401654ca9f9e Mon Sep 17 00:00:00 2001 From: zhaoxiang <756958008@qq.com> Date: Fri, 11 Nov 2016 18:14:23 +0800 Subject: [PATCH] =?UTF-8?q?added=20=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=92=8C=E7=94=A8=E6=88=B7=E7=BB=84=E7=9A=84=E5=A2=9E=E5=88=A0?= =?UTF-8?q?=E6=94=B9=E6=9F=A5=E5=85=A8=E9=83=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Auth.php | 382 ++++++++++++++++++++ application/admin/controller/Menu.php | 2 +- application/admin/model/AuthGroup.php | 15 + application/admin/model/AuthGroupAccess.php | 15 + application/admin/model/AuthRule.php | 15 + public/admin/static/js/template/form.js | 25 +- 6 files changed, 451 insertions(+), 3 deletions(-) create mode 100644 application/admin/controller/Auth.php create mode 100644 application/admin/model/AuthGroup.php create mode 100644 application/admin/model/AuthGroupAccess.php create mode 100644 application/admin/model/AuthRule.php diff --git a/application/admin/controller/Auth.php b/application/admin/controller/Auth.php new file mode 100644 index 0000000..814d6eb --- /dev/null +++ b/application/admin/controller/Auth.php @@ -0,0 +1,382 @@ + + */ + +namespace app\admin\controller; + + +use app\admin\model\AuthGroup; +use app\admin\model\AuthGroupAccess; +use app\admin\model\AuthRule; +use think\Validate; + +class Auth extends Base { + /** + * 用户组列表获取 + */ + public function index(){ + $data = []; + $dataObj = AuthGroup::all(); + if( !is_null($dataObj) ){ + foreach ($dataObj as $value){ + $data[] = $value->toArray(); + } + } + $table = [ + 'tempType' => 'table', + 'header' => [ + [ + 'field' => 'name', + 'info' => '用户组' + ], + [ + 'field' => 'description', + 'info' => '描述' + ], + [ + 'field' => 'access', + 'info' => '访问授权' + ], + [ + 'field' => 'userAuth', + 'info' => '成员授权' + ], + [ + 'field' => 'status', + 'info' => '状态' + ] + ], + 'topButton' => [ + [ + 'href' => url('Auth/add'), + 'class'=> 'btn-success', + 'info'=> '新增', + 'icon' => 'fa fa-plus', + 'confirm' => 0, + ] + ], + 'rightButton' => [ + [ + 'info' => '编辑', + 'href' => url('Auth/edit'), + 'class'=> 'btn-info', + 'param'=> [$this->primaryKey], + 'icon' => 'fa fa-pencil', + 'confirm' => 0, + 'show' => '' + ], + [ + 'info' => '启用', + 'href' => url('Auth/open'), + 'class'=> 'btn-success ajax-put-url', + 'param'=> [$this->primaryKey], + 'icon' => 'fa fa-check', + 'confirm' => 1, + 'show' => ['status', 0] + ], + [ + 'info' => '禁用', + 'href' => url('Auth/close'), + 'class'=> 'btn-warning ajax-put-url', + 'param'=> [$this->primaryKey], + 'icon' => 'fa fa-close', + 'confirm' => 1, + 'show' => ['status', 1] + ], + [ + 'info' => '删除', + 'href' => url('Auth/del'), + 'class'=> 'btn-danger ajax-delete', + 'param'=> [$this->primaryKey], + 'icon' => 'fa fa-trash', + 'confirm' => 1, + ] + ], + 'typeRule' => [ + 'access' => [ + 'module' => 'a', + 'rule' => [ + 'info' => '访问授权', + 'href' => url('Auth/access'), + 'param'=> [$this->primaryKey], + 'class' => 'refresh' + ] + ], + 'userAuth' => [ + 'module' => 'a', + 'rule' => [ + 'info' => '成员授权', + 'href' => url('Auth/userAuth'), + 'param'=> [$this->primaryKey], + 'class' => 'refresh' + ] + ], + 'hide' => [ + 'module' => 'label', + 'rule' => [ + [ + 'info' => '显示', + 'class' => 'label label-success' + ], + [ + 'info' => '隐藏', + 'class' => 'label label-warning' + ] + ] + ], + 'status' => [ + 'module' => 'label', + 'rule' => [ + [ + 'info' => '禁用', + 'class' => 'label label-danger' + ], + [ + 'info' => '启用', + 'class' => 'label label-success' + ] + ] + ] + ], + 'data' => $data + ]; + $this->result($table, ReturnCode::GET_TEMPLATE_SUCCESS); + } + + /** + * 新增权限组 + */ + public function add(){ + if( $this->request->isPost() ){ + $authGroupModel = new AuthGroup(); + $result = $authGroupModel->allowField(true)->validate( + [ + 'name' => 'require', + ],[ + 'name.require' => '用户组名不能为空', + ] + )->save($this->request->post()); + if(false === $result){ + $this->error($authGroupModel->getError()); + }else{ + $this->success('操作成功!', url('Auth/index')); + } + }else { + $form = [ + 'formTitle' => $this->menuInfo['name'], + 'tempType' => 'add', + 'formAttr' => [ + 'target' => url('Auth/add'), + 'formId' => 'add-authGroup-form', + 'backUrl' => url('Auth/index'), + ], + 'formList' => [ + [ + 'module' => 'text', + 'description' => '', + 'info' => '用户组名称:', + 'attr' => [ + 'name' => 'name', + 'value' => '', + 'placeholder' => '' + ] + ], + [ + 'module' => 'textarea', + 'description' => '', + 'info' => '用户组描述:', + 'attr' => [ + 'name' => 'description', + 'value' => '', + 'placeholder' => '' + ] + ] + ] + ]; + $this->result($form, ReturnCode::GET_TEMPLATE_SUCCESS); + } + } + + /** + * 编辑用户组 + */ + public function edit(){ + if( $this->request->isPut() ){ + $data = $this->request->put(); + $validate = new Validate([ + 'name' => 'require', + ],[ + 'name.require' => '用户组名不能为空', + ]); + if(!$validate->check($data)){ + $this->error($validate->getError()); + }else{ + $menuModel = new AuthGroup(); + $menuModel->allowField(true)->update($data); + $this->success('操作成功!', url('Auth/index')); + } + }else{ + $detail = AuthGroup::get($this->request->get($this->primaryKey))->toArray(); + $form = [ + 'formTitle' => $this->menuInfo['name'], + 'tempType' => 'edit', + 'formAttr' => [ + 'target' => url('Auth/edit'), + 'formId' => 'edit-authGroup-form', + 'backUrl' => url('Auth/index'), + ], + 'formList' => [ + [ + 'module' => 'hidden', + 'description' => '', + 'info' => '', + 'attr' => [ + 'name' => $this->primaryKey, + 'value' => $detail['id'], + 'placeholder' => '' + ] + ], + [ + 'module' => 'text', + 'description' => '', + 'info' => '用户组名称:', + 'attr' => [ + 'name' => 'name', + 'value' => $detail['name'], + 'placeholder' => '' + ] + ], + [ + 'module' => 'textarea', + 'description' => '', + 'info' => '用户组描述:', + 'attr' => [ + 'name' => 'description', + 'value' => $detail['description'], + 'placeholder' => '' + ] + ] + ] + ]; + $this->result($form, ReturnCode::GET_TEMPLATE_SUCCESS); + } + } + + /** + * 启用用户组 + */ + public function open(){ + if( $this->request->isPut() ){ + $id = $this->request->put($this->primaryKey); + $authGroupObj = AuthGroup::get([$this->primaryKey => $id]); + if( is_null($authGroupObj) ){ + $this->error('用户组不存在',''); + }else{ + $authGroupObj->status = 1; + $authGroupObj->save(); + $this->success('操作成功', url('Auth/index')); + } + } + } + + /** + * 禁用用户组 + */ + public function close(){ + if( $this->request->isPut() ){ + $id = $this->request->put($this->primaryKey); + $authGroupObj = AuthGroup::get([$this->primaryKey => $id]); + if( is_null($authGroupObj) ){ + $this->error('用户组不存在',''); + }else{ + $authGroupObj->status = 0; + $authGroupObj->save(); + $this->success('操作成功', url('Auth/index')); + } + } + } + + /** + * 删除用户组 + */ + public function del(){ + if( $this->request->isDelete() ){ + $key = $this->request->delete($this->primaryKey); + $authAccessNum = AuthGroupAccess::where(['group_id' => $key])->count(); + if( $authAccessNum ){ + $this->error('当前用户组存在用户不能删除!'); + } + AuthGroup::destroy([$this->primaryKey => $key]); + AuthRule::destroy(['group_id' => $key]); + $this->success('操作成功', url('Auth/index')); + } + } + + /** + * 用户授权(加用户入组) + */ + public function group(){ + if( $this->request->isPut() ){ + $authAccessObj = AuthGroupAccess::get(['uid' => $this->request->put('uid')]); + if( is_null($authAccessObj) ){ + $authAccessObj = new AuthGroupAccess(); + } + $authAccessObj->group_id = $this->request->put('group_id'); + $authAccessObj->uid = $this->request->put('uid'); + $authAccessObj->save(); + $this->success('操作成功', url('User/index')); + }else{ + $authAccess = ''; + $authGroupArr = []; + $authAccessObj = AuthGroupAccess::get(['uid' => $this->request->get($this->primaryKey)]); + if( !is_null($authAccessObj) ){ + $authAccess = $authAccessObj->group_id; + } + $authGroupObj = AuthGroup::all(['status' => 1]); + if( !empty($authGroupObj) ){ + foreach ( $authGroupObj as $value ){ + $authGroupArr[$value[$this->primaryKey]] = $value->name; + } + }else{ + $this->result('', ReturnCode::GET_TEMPLATE_ERROR, '没有可用用户组'); + } + $form = [ + 'formTitle' => $this->menuInfo['name'], + 'tempType' => 'edit', + 'formAttr' => [ + 'target' => url('Auth/group'), + 'formId' => 'add-authGroup-form', + 'backUrl' => url('User/index'), + ], + 'formList' => [ + [ + 'module' => 'hidden', + 'description' => '', + 'info' => '', + 'attr' => [ + 'name' => 'uid', + 'value' => $this->request->get($this->primaryKey), + 'placeholder' => '' + ] + ], + [ + 'module' => 'radio', + 'description' => '', + 'info' => '请选择用户组:', + 'attr' => [ + 'name' => 'group_id', + 'value' => $authAccess, + 'options' => $authGroupArr + ] + ], + ] + ]; + $this->result($form, ReturnCode::GET_TEMPLATE_SUCCESS); + } + } + + +} \ No newline at end of file diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php index ac8bfd1..467fb57 100644 --- a/application/admin/controller/Menu.php +++ b/application/admin/controller/Menu.php @@ -389,7 +389,7 @@ class Menu extends Base { 'description' => '', 'info' => '', 'attr' => [ - 'name' => 'id', + 'name' => $this->primaryKey, 'value' => $detail['id'], 'placeholder' => '' ] diff --git a/application/admin/model/AuthGroup.php b/application/admin/model/AuthGroup.php new file mode 100644 index 0000000..8f8e30e --- /dev/null +++ b/application/admin/model/AuthGroup.php @@ -0,0 +1,15 @@ + + */ + +namespace app\admin\model; + + +use think\Model; + +class AuthGroup extends Model { + +} \ No newline at end of file diff --git a/application/admin/model/AuthGroupAccess.php b/application/admin/model/AuthGroupAccess.php new file mode 100644 index 0000000..7a526ed --- /dev/null +++ b/application/admin/model/AuthGroupAccess.php @@ -0,0 +1,15 @@ + + */ + +namespace app\admin\model; + + +use think\Model; + +class AuthGroupAccess extends Model { + +} \ No newline at end of file diff --git a/application/admin/model/AuthRule.php b/application/admin/model/AuthRule.php new file mode 100644 index 0000000..ae63876 --- /dev/null +++ b/application/admin/model/AuthRule.php @@ -0,0 +1,15 @@ + + */ + +namespace app\admin\model; + + +use think\Model; + +class AuthRule extends Model { + +} \ No newline at end of file diff --git a/public/admin/static/js/template/form.js b/public/admin/static/js/template/form.js index d92e6da..308d720 100644 --- a/public/admin/static/js/template/form.js +++ b/public/admin/static/js/template/form.js @@ -51,6 +51,9 @@ case 'password': formHtml += buildPassword(value); break; + case 'textarea': + formHtml += buildTextarea(value); + break; } }); formHtml += '