添加 角色查询

This commit is contained in:
Shikong 2022-10-05 19:46:48 +08:00
parent e1858b1435
commit 28f4198697
6 changed files with 479 additions and 298 deletions

View File

@ -1,4 +1,4 @@
package test
package casbin
import (
"github.com/gofiber/fiber/v2"
@ -7,20 +7,27 @@ import (
"gofiber.study.skcks.cn/global"
)
func RegisterController(app *fiber.App) {
group := app.Group("/casbin")
group.Add(fiber.MethodGet, "/test", testCasbin)
group.Add(fiber.MethodPost, "/reload", reloadCasbin)
group.Add(fiber.MethodGet, "/getUserRoles", getUserRoles)
}
// testCasbin casbin 鉴权测试
//
// @Summary casbin 鉴权测试
// @Description casbin 鉴权测试
// @Tags Test
// @Tags CasBin
// @Accept json
// @Produce json
// @Param identity query string true "身份"
// @Param system query string true "系统"
// @Param identity query string true "身份"
// @Param system query string true "系统"
// @Param api query string true "api"
// @Param act query string true "动作"
// @Success 200 {object} response.Response{data=string}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /test/casbin [get]
// @Router /casbin/test [get]
func testCasbin(ctx *fiber.Ctx) error {
var identity, system, api, act string
@ -45,12 +52,12 @@ func testCasbin(ctx *fiber.Ctx) error {
//
// @Summary 重新加载 casbin 策略
// @Description 重新加载 casbin 策略
// @Tags Test
// @Tags CasBin
// @Accept json
// @Produce json
// @Success 200 {object} response.Response{data=string}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /test/casbin [post]
// @Router /casbin/reload [post]
func reloadCasbin(ctx *fiber.Ctx) error {
err := global.Enforcer.LoadPolicy()
if err != nil {
@ -59,3 +66,31 @@ func reloadCasbin(ctx *fiber.Ctx) error {
return ctx.JSON(response.NewResponse("重载成功"))
}
type GetUserRolesQuery struct {
Account string `json:"account" example:"root"`
}
// getUserRoles 用户所有角色
//
// @Summary 用户所有角色
// @Description 用户所有角色
// @Tags CasBin
// @Accept json
// @Produce json
// @Param account query string true "用户账号" default(root)
// @Success 200 {object} response.Response{data=[]string}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /casbin/getUserRoles [get]
func getUserRoles(ctx *fiber.Ctx) error {
query := &GetUserRolesQuery{}
err := ctx.QueryParser(query)
roles, err := global.Enforcer.GetRolesForUser("user::" + query.Account)
if err != nil {
if err != nil {
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, err.Error()))
}
}
return ctx.JSON(response.NewResponse(roles))
}

View File

@ -6,12 +6,14 @@ import (
"gofiber.study.skcks.cn/common/errorx"
"gofiber.study.skcks.cn/common/logger"
"gofiber.study.skcks.cn/common/response"
"gofiber.study.skcks.cn/controller/casbin"
"gofiber.study.skcks.cn/controller/test"
"gofiber.study.skcks.cn/controller/user"
)
func RegisterController(app *fiber.App) {
user.RegisterController(app)
casbin.RegisterController(app)
test.RegisterController(app)
}

View File

@ -3,7 +3,5 @@ package test
import "github.com/gofiber/fiber/v2"
func RegisterController(app *fiber.App) {
group := app.Group("/test")
group.Add(fiber.MethodGet, "/casbin", testCasbin)
group.Add(fiber.MethodPost, "/casbin", reloadCasbin)
app.Group("/test")
}

View File

@ -65,6 +65,172 @@ const docTemplate = `{
}
}
},
"/casbin/getUserRoles": {
"get": {
"description": "用户所有角色",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CasBin"
],
"summary": "用户所有角色",
"parameters": [
{
"type": "string",
"default": "root",
"description": "用户账号",
"name": "account",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/casbin/reload": {
"post": {
"description": "重新加载 casbin 策略",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CasBin"
],
"summary": "重新加载 casbin 策略",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/casbin/test": {
"get": {
"description": "casbin 鉴权测试",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CasBin"
],
"summary": "casbin 鉴权测试",
"parameters": [
{
"type": "string",
"description": "身份",
"name": "identity",
"in": "query",
"required": true
},
{
"type": "string",
"description": "系统",
"name": "system",
"in": "query",
"required": true
},
{
"type": "string",
"description": "api",
"name": "api",
"in": "query",
"required": true
},
{
"type": "string",
"description": "动作",
"name": "act",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/error": {
"get": {
"description": "错误信息示例",
@ -213,116 +379,6 @@ const docTemplate = `{
}
}
},
"/test/casbin": {
"get": {
"description": "casbin 鉴权测试",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "casbin 鉴权测试",
"parameters": [
{
"type": "string",
"description": "身份",
"name": "identity",
"in": "query",
"required": true
},
{
"type": "string",
"description": "系统",
"name": "system",
"in": "query",
"required": true
},
{
"type": "string",
"description": "api",
"name": "api",
"in": "query",
"required": true
},
{
"type": "string",
"description": "动作",
"name": "act",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"post": {
"description": "重新加载 casbin 策略",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "重新加载 casbin 策略",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/user/account": {
"get": {
"security": [

View File

@ -57,6 +57,172 @@
}
}
},
"/casbin/getUserRoles": {
"get": {
"description": "用户所有角色",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CasBin"
],
"summary": "用户所有角色",
"parameters": [
{
"type": "string",
"default": "root",
"description": "用户账号",
"name": "account",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/casbin/reload": {
"post": {
"description": "重新加载 casbin 策略",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CasBin"
],
"summary": "重新加载 casbin 策略",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/casbin/test": {
"get": {
"description": "casbin 鉴权测试",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CasBin"
],
"summary": "casbin 鉴权测试",
"parameters": [
{
"type": "string",
"description": "身份",
"name": "identity",
"in": "query",
"required": true
},
{
"type": "string",
"description": "系统",
"name": "system",
"in": "query",
"required": true
},
{
"type": "string",
"description": "api",
"name": "api",
"in": "query",
"required": true
},
{
"type": "string",
"description": "动作",
"name": "act",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/error": {
"get": {
"description": "错误信息示例",
@ -205,116 +371,6 @@
}
}
},
"/test/casbin": {
"get": {
"description": "casbin 鉴权测试",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "casbin 鉴权测试",
"parameters": [
{
"type": "string",
"description": "身份",
"name": "identity",
"in": "query",
"required": true
},
{
"type": "string",
"description": "系统",
"name": "system",
"in": "query",
"required": true
},
{
"type": "string",
"description": "api",
"name": "api",
"in": "query",
"required": true
},
{
"type": "string",
"description": "动作",
"name": "act",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"post": {
"description": "重新加载 casbin 策略",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "重新加载 casbin 策略",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/user/account": {
"get": {
"security": [

View File

@ -98,6 +98,108 @@ paths:
summary: HelloWorld
tags:
- HelloWorld
/casbin/getUserRoles:
get:
consumes:
- application/json
description: 用户所有角色
parameters:
- default: root
description: 用户账号
in: query
name: account
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
items:
type: string
type: array
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 用户所有角色
tags:
- CasBin
/casbin/reload:
post:
consumes:
- application/json
description: 重新加载 casbin 策略
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
type: string
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 重新加载 casbin 策略
tags:
- CasBin
/casbin/test:
get:
consumes:
- application/json
description: casbin 鉴权测试
parameters:
- description: 身份
in: query
name: identity
required: true
type: string
- description: 系统
in: query
name: system
required: true
type: string
- description: api
in: query
name: api
required: true
type: string
- description: 动作
in: query
name: act
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
type: string
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: casbin 鉴权测试
tags:
- CasBin
/error:
delete:
consumes:
@ -193,74 +295,6 @@ paths:
summary: 获取所有路由
tags:
- Routes
/test/casbin:
get:
consumes:
- application/json
description: casbin 鉴权测试
parameters:
- description: 身份
in: query
name: identity
required: true
type: string
- description: 系统
in: query
name: system
required: true
type: string
- description: api
in: query
name: api
required: true
type: string
- description: 动作
in: query
name: act
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
type: string
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: casbin 鉴权测试
tags:
- Test
post:
consumes:
- application/json
description: 重新加载 casbin 策略
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
type: string
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 重新加载 casbin 策略
tags:
- Test
/user/account:
get:
consumes: