mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-25 02:40:16 +08:00
added 完成用户和用户组的增删改查全部功能
This commit is contained in:
parent
064fa2ff19
commit
64b0361846
382
application/admin/controller/Auth.php
Normal file
382
application/admin/controller/Auth.php
Normal file
@ -0,0 +1,382 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @since 2016-02-18
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@outlook.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -389,7 +389,7 @@ class Menu extends Base {
|
|||||||
'description' => '',
|
'description' => '',
|
||||||
'info' => '',
|
'info' => '',
|
||||||
'attr' => [
|
'attr' => [
|
||||||
'name' => 'id',
|
'name' => $this->primaryKey,
|
||||||
'value' => $detail['id'],
|
'value' => $detail['id'],
|
||||||
'placeholder' => ''
|
'placeholder' => ''
|
||||||
]
|
]
|
||||||
|
15
application/admin/model/AuthGroup.php
Normal file
15
application/admin/model/AuthGroup.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @since 2016-02-18
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@outlook.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace app\admin\model;
|
||||||
|
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
|
||||||
|
class AuthGroup extends Model {
|
||||||
|
|
||||||
|
}
|
15
application/admin/model/AuthGroupAccess.php
Normal file
15
application/admin/model/AuthGroupAccess.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @since 2016-02-18
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@outlook.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace app\admin\model;
|
||||||
|
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
|
||||||
|
class AuthGroupAccess extends Model {
|
||||||
|
|
||||||
|
}
|
15
application/admin/model/AuthRule.php
Normal file
15
application/admin/model/AuthRule.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @since 2016-02-18
|
||||||
|
* @author zhaoxiang <zhaoxiang051405@outlook.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace app\admin\model;
|
||||||
|
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
|
||||||
|
class AuthRule extends Model {
|
||||||
|
|
||||||
|
}
|
@ -51,6 +51,9 @@
|
|||||||
case 'password':
|
case 'password':
|
||||||
formHtml += buildPassword(value);
|
formHtml += buildPassword(value);
|
||||||
break;
|
break;
|
||||||
|
case 'textarea':
|
||||||
|
formHtml += buildTextarea(value);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
formHtml += '</div><div class="box-footer">';
|
formHtml += '</div><div class="box-footer">';
|
||||||
@ -164,8 +167,26 @@
|
|||||||
return '<input type="hidden" class="form-control" value="'+ hiddenObj.attr.value +'" name="'+ hiddenObj.attr.name +'">';
|
return '<input type="hidden" class="form-control" value="'+ hiddenObj.attr.value +'" name="'+ hiddenObj.attr.name +'">';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建文本域
|
||||||
|
* @param textareaObj
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
function buildTextarea( textareaObj ) {
|
function buildTextarea( textareaObj ) {
|
||||||
|
var formHtml = '<div><div class="col-xs-8 form-group"><label>'+ textareaObj.info +'</label>';
|
||||||
|
var placeholder = '', value = '';
|
||||||
|
if( textareaObj.attr.placeholder){
|
||||||
|
placeholder = 'placeholder="'+ textareaObj.attr.placeholder +'"';
|
||||||
|
}
|
||||||
|
if( textareaObj.attr.value){
|
||||||
|
value = textareaObj.attr.value;
|
||||||
|
}
|
||||||
|
formHtml += '<textarea rows="5" class="form-control" '+ placeholder +' name="'+ textareaObj.attr.name +'">'+ value +'</textarea></div>';
|
||||||
|
if( textareaObj.description && textareaObj.description.length ){
|
||||||
|
formHtml += ' <div class="col-xs-4 form-group" style="margin-top: 30px"><span class="label label-info">'+ textareaObj.description +'</span></div>';
|
||||||
|
}
|
||||||
|
formHtml += '</div>';
|
||||||
|
return formHtml;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user