Browse Source

增加角色复制后台功能 修复swagger不能带token的问题

pixel 4 years ago
parent
commit
e794ef05c2

+ 19 - 0
server/api/v1/sys_authority.go

@@ -29,6 +29,25 @@ func CreateAuthority(c *gin.Context) {
 	}
 }
 
+// @Tags authority
+// @Summary 拷贝角色
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body response.SysAuthorityCopyResponse true "拷贝角色"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"拷贝成功"}"
+// @Router /authority/copyAuthority [post]
+func CopyAuthority(c *gin.Context) {
+	var copyInfo resp.SysAuthorityCopyResponse
+	_ = c.ShouldBindJSON(&copyInfo)
+	err, authBack := service.CopyAuthority(copyInfo)
+	if err != nil {
+		response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
+	} else {
+		response.OkWithData(resp.SysAuthorityResponse{Authority: authBack}, c)
+	}
+}
+
 // @Tags authority
 // @Summary 删除角色
 // @Security ApiKeyAuth

+ 147 - 5
server/docs/docs.go

@@ -1,6 +1,6 @@
 // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
 // This file was generated by swaggo/swag at
-// 2020-04-15 09:37:13.5084762 +0800 CST m=+0.126566601
+// 2020-04-20 14:50:58.4141704 +0800 CST m=+0.881639701
 
 package docs
 
@@ -248,6 +248,45 @@ var doc = `{
                 }
             }
         },
+        "/authority/copyAuthority": {
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "authority"
+                ],
+                "summary": "拷贝角色",
+                "parameters": [
+                    {
+                        "description": "拷贝角色",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "object",
+                            "$ref": "#/definitions/response.SysAuthorityCopyResponse"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{\"success\":true,\"data\":{},\"msg\":\"拷贝成功\"}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/authority/createAuthority": {
             "post": {
                 "security": [
@@ -404,6 +443,45 @@ var doc = `{
                 }
             }
         },
+        "/authority/updateAuthority": {
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "authority"
+                ],
+                "summary": "设置角色资源权限",
+                "parameters": [
+                    {
+                        "description": "设置角色资源权限",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "object",
+                            "$ref": "#/definitions/model.SysAuthority"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{\"success\":true,\"data\":{},\"msg\":\"设置成功\"}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/autoCode/createTemp": {
             "post": {
                 "security": [
@@ -1588,6 +1666,45 @@ var doc = `{
                 }
             }
         },
+        "/user/deleteUser": {
+            "delete": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "SysUser"
+                ],
+                "summary": "删除用户",
+                "parameters": [
+                    {
+                        "description": "删除用户",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "object",
+                            "$ref": "#/definitions/request.SetUserAuth"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{\"success\":true,\"data\":{},\"msg\":\"修改成功\"}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/user/getUserList": {
             "post": {
                 "security": [
@@ -2072,12 +2189,18 @@ var doc = `{
                 "component": {
                     "type": "string"
                 },
+                "defaultMenu": {
+                    "type": "boolean"
+                },
                 "hidden": {
                     "type": "boolean"
                 },
                 "icon": {
                     "type": "string"
                 },
+                "keepAlive": {
+                    "type": "boolean"
+                },
                 "name": {
                     "type": "string"
                 },
@@ -2317,6 +2440,25 @@ var doc = `{
                     "type": "string"
                 }
             }
+        },
+        "response.SysAuthorityCopyResponse": {
+            "type": "object",
+            "properties": {
+                "authority": {
+                    "type": "object",
+                    "$ref": "#/definitions/model.SysAuthority"
+                },
+                "oldAuthorityId": {
+                    "type": "integer"
+                }
+            }
+        }
+    },
+    "securityDefinitions": {
+        "ApiKeyAuth": {
+            "type": "apiKey",
+            "name": "x-token",
+            "in": "header"
         }
     }
 }`
@@ -2332,12 +2474,12 @@ type swaggerInfo struct {
 
 // SwaggerInfo holds exported Swagger Info so clients can modify it
 var SwaggerInfo = swaggerInfo{
-	Version:     "",
+	Version:     "0.0.1",
 	Host:        "",
-	BasePath:    "",
+	BasePath:    "/",
 	Schemes:     []string{},
-	Title:       "",
-	Description: "",
+	Title:       "Swagger Example API",
+	Description: "This is a sample Server pets",
 }
 
 type s struct{}

+ 147 - 1
server/docs/swagger.json

@@ -1,9 +1,13 @@
 {
     "swagger": "2.0",
     "info": {
+        "description": "This is a sample Server pets",
+        "title": "Swagger Example API",
         "contact": {},
-        "license": {}
+        "license": {},
+        "version": "0.0.1"
     },
+    "basePath": "/",
     "paths": {
         "/api/createApi": {
             "post": {
@@ -227,6 +231,45 @@
                 }
             }
         },
+        "/authority/copyAuthority": {
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "authority"
+                ],
+                "summary": "拷贝角色",
+                "parameters": [
+                    {
+                        "description": "拷贝角色",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "object",
+                            "$ref": "#/definitions/response.SysAuthorityCopyResponse"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{\"success\":true,\"data\":{},\"msg\":\"拷贝成功\"}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/authority/createAuthority": {
             "post": {
                 "security": [
@@ -383,6 +426,45 @@
                 }
             }
         },
+        "/authority/updateAuthority": {
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "authority"
+                ],
+                "summary": "设置角色资源权限",
+                "parameters": [
+                    {
+                        "description": "设置角色资源权限",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "object",
+                            "$ref": "#/definitions/model.SysAuthority"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{\"success\":true,\"data\":{},\"msg\":\"设置成功\"}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/autoCode/createTemp": {
             "post": {
                 "security": [
@@ -1567,6 +1649,45 @@
                 }
             }
         },
+        "/user/deleteUser": {
+            "delete": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "SysUser"
+                ],
+                "summary": "删除用户",
+                "parameters": [
+                    {
+                        "description": "删除用户",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "object",
+                            "$ref": "#/definitions/request.SetUserAuth"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{\"success\":true,\"data\":{},\"msg\":\"修改成功\"}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/user/getUserList": {
             "post": {
                 "security": [
@@ -2051,12 +2172,18 @@
                 "component": {
                     "type": "string"
                 },
+                "defaultMenu": {
+                    "type": "boolean"
+                },
                 "hidden": {
                     "type": "boolean"
                 },
                 "icon": {
                     "type": "string"
                 },
+                "keepAlive": {
+                    "type": "boolean"
+                },
                 "name": {
                     "type": "string"
                 },
@@ -2296,6 +2423,25 @@
                     "type": "string"
                 }
             }
+        },
+        "response.SysAuthorityCopyResponse": {
+            "type": "object",
+            "properties": {
+                "authority": {
+                    "type": "object",
+                    "$ref": "#/definitions/model.SysAuthority"
+                },
+                "oldAuthorityId": {
+                    "type": "integer"
+                }
+            }
+        }
+    },
+    "securityDefinitions": {
+        "ApiKeyAuth": {
+            "type": "apiKey",
+            "name": "x-token",
+            "in": "header"
         }
     }
 }

+ 93 - 0
server/docs/swagger.yaml

@@ -1,3 +1,4 @@
+basePath: /
 definitions:
   config.Captcha:
     properties:
@@ -217,10 +218,14 @@ definitions:
         type: array
       component:
         type: string
+      defaultMenu:
+        type: boolean
       hidden:
         type: boolean
       icon:
         type: string
+      keepAlive:
+        type: boolean
       name:
         type: string
       parentId:
@@ -380,9 +385,20 @@ definitions:
       uuid:
         type: string
     type: object
+  response.SysAuthorityCopyResponse:
+    properties:
+      authority:
+        $ref: '#/definitions/model.SysAuthority'
+        type: object
+      oldAuthorityId:
+        type: integer
+    type: object
 info:
   contact: {}
+  description: This is a sample Server pets
   license: {}
+  title: Swagger Example API
+  version: 0.0.1
 paths:
   /api/createApi:
     post:
@@ -520,6 +536,30 @@ paths:
       summary: 创建基础api
       tags:
       - SysApi
+  /authority/copyAuthority:
+    post:
+      consumes:
+      - application/json
+      parameters:
+      - description: 拷贝角色
+        in: body
+        name: data
+        required: true
+        schema:
+          $ref: '#/definitions/response.SysAuthorityCopyResponse'
+          type: object
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: '{"success":true,"data":{},"msg":"拷贝成功"}'
+          schema:
+            type: string
+      security:
+      - ApiKeyAuth: []
+      summary: 拷贝角色
+      tags:
+      - authority
   /authority/createAuthority:
     post:
       consumes:
@@ -616,6 +656,30 @@ paths:
       summary: 设置角色资源权限
       tags:
       - authority
+  /authority/updateAuthority:
+    post:
+      consumes:
+      - application/json
+      parameters:
+      - description: 设置角色资源权限
+        in: body
+        name: data
+        required: true
+        schema:
+          $ref: '#/definitions/model.SysAuthority'
+          type: object
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: '{"success":true,"data":{},"msg":"设置成功"}'
+          schema:
+            type: string
+      security:
+      - ApiKeyAuth: []
+      summary: 设置角色资源权限
+      tags:
+      - authority
   /autoCode/createTemp:
     post:
       consumes:
@@ -1343,6 +1407,30 @@ paths:
       summary: 用户修改密码
       tags:
       - SysUser
+  /user/deleteUser:
+    delete:
+      consumes:
+      - application/json
+      parameters:
+      - description: 删除用户
+        in: body
+        name: data
+        required: true
+        schema:
+          $ref: '#/definitions/request.SetUserAuth'
+          type: object
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: '{"success":true,"data":{},"msg":"修改成功"}'
+          schema:
+            type: string
+      security:
+      - ApiKeyAuth: []
+      summary: 删除用户
+      tags:
+      - SysUser
   /user/getUserList:
     post:
       consumes:
@@ -1438,4 +1526,9 @@ paths:
       summary: 注册工作流
       tags:
       - workflow
+securityDefinitions:
+  ApiKeyAuth:
+    in: header
+    name: x-token
+    type: apiKey
 swagger: "2.0"

+ 1 - 0
server/initialize/router.go

@@ -11,6 +11,7 @@ import (
 )
 
 //初始化总路由
+
 func Routers() *gin.Engine {
 	var Router = gin.Default()
 

+ 7 - 0
server/main.go

@@ -7,6 +7,13 @@ import (
 	//"runtime"
 )
 
+// @title Swagger Example API
+// @version 0.0.1
+// @description This is a sample Server pets
+// @securityDefinitions.apikey ApiKeyAuth
+// @in header
+// @name x-token
+// @BasePath /
 func main() {
 	switch global.GVA_CONFIG.System.DbType {
 	case "mysql":

+ 5 - 0
server/model/response/sys_authority.go

@@ -5,3 +5,8 @@ import "gin-vue-admin/model"
 type SysAuthorityResponse struct {
 	Authority model.SysAuthority `json:"authority"`
 }
+
+type SysAuthorityCopyResponse struct {
+	Authority      model.SysAuthority `json:"authority"`
+	OldAuthorityId string             `json:"oldAuthorityId"`
+}

+ 3 - 1
server/model/response/sys_casbin.go

@@ -1,5 +1,7 @@
 package response
 
+import "gin-vue-admin/model/request"
+
 type PolicyPathResponse struct {
-	Paths []map[string]string `json:"paths"`
+	Paths []request.CasbinInfo `json:"paths"`
 }

+ 1 - 0
server/router/sys_authority.go

@@ -12,6 +12,7 @@ func InitAuthorityRouter(Router *gin.RouterGroup) {
 		AuthorityRouter.POST("createAuthority", v1.CreateAuthority)   //创建角色
 		AuthorityRouter.POST("deleteAuthority", v1.DeleteAuthority)   //删除角色
 		AuthorityRouter.PUT("updateAuthority", v1.UpdateAuthority)    //更新角色
+		AuthorityRouter.POST("copyAuthority", v1.CopyAuthority)       //更新角色
 		AuthorityRouter.POST("getAuthorityList", v1.GetAuthorityList) //获取角色列表
 		AuthorityRouter.POST("setDataAuthority", v1.SetDataAuthority) //设置角色资源权限
 	}

+ 29 - 0
server/service/sys_authority.go

@@ -5,6 +5,8 @@ import (
 	"gin-vue-admin/global"
 	"gin-vue-admin/model"
 	"gin-vue-admin/model/request"
+	"gin-vue-admin/model/response"
+	"strconv"
 )
 
 // @title    CreateAuthority
@@ -19,6 +21,33 @@ func CreateAuthority(auth model.SysAuthority) (err error, authority model.SysAut
 	return err, auth
 }
 
+// @title    CopyAuthority
+// @description   复制一个角色
+// @auth                     (2020/04/05  20:22)
+// @param     copyInfo        response.SysAuthorityCopyResponse
+// @return                    error
+// @return    authority       model.SysAuthority
+
+func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, authority model.SysAuthority) {
+	copyInfo.Authority.Children = []model.SysAuthority{}
+	err, menus := GetMenuAuthority(copyInfo.OldAuthorityId)
+	var baseMenu []model.SysBaseMenu
+	for _, v := range menus {
+		intNum, _ := strconv.Atoi(v.MenuId)
+		v.SysBaseMenu.ID = uint(intNum)
+		baseMenu = append(baseMenu, v.SysBaseMenu)
+	}
+	copyInfo.Authority.SysBaseMenus = baseMenu
+	err = global.GVA_DB.Create(&copyInfo.Authority).Error
+
+	paths := GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
+	err = UpdateCasbin(copyInfo.Authority.AuthorityId, paths)
+	if err != nil {
+		_ = DeleteAuthority(&copyInfo.Authority)
+	}
+	return err, copyInfo.Authority
+}
+
 // @title    UpdateAuthority
 // @description   更改一个角色
 // @auth                     (2020/04/05  20:22)

+ 4 - 4
server/service/sys_casbin.go

@@ -71,13 +71,13 @@ func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod
 // @param     authorityId     string
 // @return                    []string
 
-func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []map[string]string) {
+func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
 	e := Casbin()
 	list := e.GetFilteredPolicy(0, authorityId)
 	for _, v := range list {
-		pathMaps = append(pathMaps, map[string]string{
-			"path":   v[1],
-			"method": v[2],
+		pathMaps = append(pathMaps, request.CasbinInfo{
+			Path:   v[1],
+			Method: v[2],
 		})
 	}
 	return pathMaps