diff --git a/server/api/v1/sys_authority.go b/server/api/v1/sys_authority.go index 7ce4a2fa2f7f97b81a3ea85f622eb618312dc9ba..8a58da8bb02bdb5722775f662242d98f99bc530f 100644 --- a/server/api/v1/sys_authority.go +++ b/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(©Info) + 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 diff --git a/server/docs/docs.go b/server/docs/docs.go index 766d7c9ce64666b2000630be7c8fe0fa786970d7..72d08422450efa3208ba2657d6386d9fe23ffb1c 100644 --- a/server/docs/docs.go +++ b/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{} diff --git a/server/docs/swagger.json b/server/docs/swagger.json index b17f8cc279bd947e3fd8ec335ecddb3c082d28d2..55b5be90f1983ad6bf45303d49d3036204d65dcc 100644 --- a/server/docs/swagger.json +++ b/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" } } } \ No newline at end of file diff --git a/server/docs/swagger.yaml b/server/docs/swagger.yaml index 6231d64c917121de43e17c927f5e4e9ee884fef6..af4fb132ac3b9426c40db2ad2de1fc5f8823ce04 100644 --- a/server/docs/swagger.yaml +++ b/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" diff --git a/server/initialize/router.go b/server/initialize/router.go index a51ae754426aa0ea9a007ee559b4d3c76cc61599..433325e3b0516fce9f62cfb2bcd76159d154a70e 100644 --- a/server/initialize/router.go +++ b/server/initialize/router.go @@ -11,6 +11,7 @@ import ( ) //初始化总路由 + func Routers() *gin.Engine { var Router = gin.Default() diff --git a/server/main.go b/server/main.go index a761e4d37b36340a8d56d47760696175bce2c36a..5b4785b50417701784cbbfc29af77add3f117951 100644 --- a/server/main.go +++ b/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": diff --git a/server/model/response/sys_authority.go b/server/model/response/sys_authority.go index a60842602f64688a393fbcadfa46d752e3e79caf..8f0e0407a3ee4a6ee152ff4abbb16eff808c226c 100644 --- a/server/model/response/sys_authority.go +++ b/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"` +} diff --git a/server/model/response/sys_casbin.go b/server/model/response/sys_casbin.go index a1be69ab299a9a74f140977a83a0e9fa75953047..5bf962c220d4ca201f39d43e306ad48dbc418516 100644 --- a/server/model/response/sys_casbin.go +++ b/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"` } diff --git a/server/router/sys_authority.go b/server/router/sys_authority.go index 933f10e9da32a9419a3e4f7fc90e30a787f08eeb..ecc0b9a3cdf3a2dcbe41e5aab57a7b7157292866 100644 --- a/server/router/sys_authority.go +++ b/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) //设置角色资源权限 } diff --git a/server/service/sys_authority.go b/server/service/sys_authority.go index fc4de8a8b5cdbef7eb7fe363fd22708c258b1ac0..36ac12664323722da411d0273d4f11d41966bbf0 100644 --- a/server/service/sys_authority.go +++ b/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(©Info.Authority).Error + + paths := GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId) + err = UpdateCasbin(copyInfo.Authority.AuthorityId, paths) + if err != nil { + _ = DeleteAuthority(©Info.Authority) + } + return err, copyInfo.Authority +} + // @title UpdateAuthority // @description 更改一个角色 // @auth (2020/04/05 20:22) diff --git a/server/service/sys_casbin.go b/server/service/sys_casbin.go index 976dfd60ef41229d46c901926d378b383da2bb67..6c918c59dd33b41d9eb45478f3c2f107d1ffdbfc 100644 --- a/server/service/sys_casbin.go +++ b/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