chore: refactor enforce() handler and update Swagger docs (#1931)

* chore: add swaggerof enforce api

* Update enforcer.go

* Update string.go

---------

Co-authored-by: hsluoyz <hsluoyz@qq.com>
This commit is contained in:
Yaodong Yu 2023-06-04 17:19:58 +08:00 committed by GitHub
parent 2d43fe0b39
commit fdaad2b608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 369 additions and 40 deletions

View File

@ -21,6 +21,16 @@ import (
"github.com/casdoor/casdoor/util"
)
// Enforce
// @Title Enforce
// @Tag Enforce API
// @Description Call Casbin Enforce API
// @Param body body object.CasbinRequest true "Casbin request"
// @Param permissionId query string false "permission id"
// @Param modelId query string false "model id"
// @Param resourceId query string false "resource id"
// @Success 200 {object} controllers.Response The Response object
// @router /enforce [post]
func (c *ApiController) Enforce() {
permissionId := c.Input().Get("permissionId")
modelId := c.Input().Get("modelId")
@ -38,29 +48,41 @@ func (c *ApiController) Enforce() {
return
}
permissions := make([]*object.Permission, 0)
res := []bool{}
permissions := []*object.Permission{}
if modelId != "" {
owner, modelName := util.GetOwnerAndNameFromId(modelId)
permissions, err = object.GetPermissionsByModel(owner, modelName)
if err != nil {
panic(err)
c.ResponseError(err.Error())
return
}
} else {
} else if resourceId != "" {
permissions, err = object.GetPermissionsByResource(resourceId)
if err != nil {
panic(err)
c.ResponseError(err.Error())
return
}
} else {
c.ResponseError(c.T("general:Missing parameter"))
return
}
res := []bool{}
for _, permission := range permissions {
res = append(res, object.Enforce(permission.GetId(), &request))
}
c.Data["json"] = res
c.ServeJSON()
c.ResponseOk(res)
}
// BatchEnforce
// @Title BatchEnforce
// @Tag Enforce API
// @Description Call Casbin BatchEnforce API
// @Param body body object.CasbinRequest true "array of casbin requests"
// @Param permissionId query string false "permission id"
// @Param modelId query string false "model id"
// @Success 200 {object} controllers.Response The Response object
// @router /batch-enforce [post]
func (c *ApiController) BatchEnforce() {
permissionId := c.Input().Get("permissionId")
modelId := c.Input().Get("modelId")
@ -68,26 +90,33 @@ func (c *ApiController) BatchEnforce() {
var requests []object.CasbinRequest
err := json.Unmarshal(c.Ctx.Input.RequestBody, &requests)
if err != nil {
panic(err)
c.ResponseError(err.Error())
return
}
if permissionId != "" {
c.Data["json"] = object.BatchEnforce(permissionId, &requests)
c.ServeJSON()
} else {
owner, modelName := util.GetOwnerAndNameFromId(modelId)
permissions, err := object.GetPermissionsByModel(owner, modelName)
if err != nil {
panic(err)
}
res := [][]bool{}
for _, permission := range permissions {
res = append(res, object.BatchEnforce(permission.GetId(), &requests))
}
c.ResponseOk(res)
c.ResponseOk(object.BatchEnforce(permissionId, &requests))
return
}
permissions := []*object.Permission{}
if modelId != "" {
owner, modelName := util.GetOwnerAndNameFromId(modelId)
permissions, err = object.GetPermissionsByModel(owner, modelName)
if err != nil {
c.ResponseError(err.Error())
return
}
} else {
c.ResponseError(c.T("general:Missing parameter"))
return
}
res := [][]bool{}
for _, permission := range permissions {
res = append(res, object.BatchEnforce(permission.GetId(), &requests))
}
c.ResponseOk(res)
}
func (c *ApiController) GetAllObjects() {

View File

@ -777,6 +777,46 @@
"operationId": "ApiController.HandleOfficialAccountEvent"
}
},
"/api/batch-enforce": {
"post": {
"tags": [
"Enforce API"
],
"description": "perform enforce",
"operationId": "ApiController.BatchEnforce",
"parameters": [
{
"in": "body",
"name": "body",
"description": "casbin request array",
"required": true,
"schema": {
"$ref": "#/definitions/object.CasbinRequest"
}
},
{
"in": "query",
"name": "permissionId",
"description": "permission id",
"type": "string"
},
{
"in": "query",
"name": "modelId",
"description": "model id",
"type": "string"
}
],
"responses": {
"200": {
"description": "The Response object",
"schema": {
"$ref": "#/definitions/controllers.Response"
}
}
}
}
},
"/api/buy-product": {
"post": {
"tags": [
@ -1384,6 +1424,52 @@
}
}
},
"/api/enforce": {
"post": {
"tags": [
"Enforce API"
],
"description": "perform enforce",
"operationId": "ApiController.Enforce",
"parameters": [
{
"in": "body",
"name": "body",
"description": "casbin request",
"required": true,
"schema": {
"$ref": "#/definitions/object.CasbinRequest"
}
},
{
"in": "query",
"name": "permissionId",
"description": "permission id",
"type": "string"
},
{
"in": "query",
"name": "modelId",
"description": "model id",
"type": "string"
},
{
"in": "query",
"name": "resourceId",
"description": "resource id",
"type": "string"
}
],
"responses": {
"200": {
"description": "The Response object",
"schema": {
"$ref": "#/definitions/controllers.Response"
}
}
}
}
},
"/api/get-account": {
"get": {
"tags": [
@ -1954,6 +2040,35 @@
}
}
},
"/api/get-organization-names": {
"get": {
"tags": [
"Organization API"
],
"description": "get all organization names",
"operationId": "ApiController.GetOrganizationNames",
"parameters": [
{
"in": "query",
"name": "owner",
"description": "owner",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "The Response object",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/object.Organization"
}
}
}
}
}
},
"/api/get-organizations": {
"get": {
"tags": [
@ -2826,7 +2941,6 @@
"in": "query",
"name": "id",
"description": "The id ( owner/name ) of the user",
"required": true,
"type": "string"
},
{
@ -3062,6 +3176,23 @@
}
}
},
"/api/health": {
"get": {
"tags": [
"System API"
],
"description": "check if the system is live",
"operationId": "ApiController.Health",
"responses": {
"200": {
"description": "The Response object",
"schema": {
"$ref": "#/definitions/controllers.Response"
}
}
}
}
},
"/api/invoice-payment": {
"post": {
"tags": [
@ -4501,11 +4632,11 @@
}
},
"definitions": {
"1183.0x1400042eb70.false": {
"1225.0xc0002e2ae0.false": {
"title": "false",
"type": "object"
},
"1217.0x1400042eba0.false": {
"1260.0xc0002e2b10.false": {
"title": "false",
"type": "object"
},
@ -4554,10 +4685,10 @@
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/1183.0x1400042eb70.false"
"$ref": "#/definitions/1225.0xc0002e2ae0.false"
},
"data2": {
"$ref": "#/definitions/1217.0x1400042eba0.false"
"$ref": "#/definitions/1260.0xc0002e2b10.false"
},
"msg": {
"type": "string"
@ -4595,6 +4726,10 @@
"title": "JSONWebKey",
"type": "object"
},
"object.\u0026{179844 0xc000a02f90 false}": {
"title": "\u0026{179844 0xc000a02f90 false}",
"type": "object"
},
"object.AccountItem": {
"title": "AccountItem",
"type": "object",
@ -4693,6 +4828,9 @@
"formCss": {
"type": "string"
},
"formCssMobile": {
"type": "string"
},
"formOffset": {
"type": "integer",
"format": "int64"
@ -4715,6 +4853,9 @@
"name": {
"type": "string"
},
"orgChoiceMode": {
"type": "string"
},
"organization": {
"type": "string"
},
@ -4772,6 +4913,13 @@
}
}
},
"object.CasbinRequest": {
"title": "CasbinRequest",
"type": "array",
"items": {
"$ref": "#/definitions/object.\u0026{179844 0xc000a02f90 false}"
}
},
"object.Cert": {
"title": "Cert",
"type": "object",
@ -5008,6 +5156,18 @@
}
}
},
"object.MfaItem": {
"title": "MfaItem",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"rule": {
"type": "string"
}
}
},
"object.MfaProps": {
"title": "MfaProps",
"type": "object",
@ -5190,6 +5350,12 @@
"masterPassword": {
"type": "string"
},
"mfaItems": {
"type": "array",
"items": {
"$ref": "#/definitions/object.MfaItem"
}
},
"name": {
"type": "string"
},
@ -5395,9 +5561,18 @@
"displayName": {
"type": "string"
},
"isEnabled": {
"type": "boolean"
},
"name": {
"type": "string"
},
"options": {
"type": "array",
"items": {
"type": "string"
}
},
"owner": {
"type": "string"
},
@ -5411,9 +5586,6 @@
},
"role": {
"type": "string"
},
"options": {
"type": "array"
}
}
},
@ -5737,6 +5909,9 @@
"name": {
"type": "string"
},
"object": {
"type": "string"
},
"organization": {
"type": "string"
},
@ -6341,6 +6516,9 @@
"passwordSalt": {
"type": "string"
},
"passwordType": {
"type": "string"
},
"patreon": {
"type": "string"
},
@ -6505,6 +6683,9 @@
"name": {
"type": "string"
},
"organization": {
"type": "string"
},
"phone": {
"type": "string"
},
@ -6634,4 +6815,4 @@
"type": "object"
}
}
}
}

View File

@ -502,6 +502,32 @@ paths:
tags:
- HandleOfficialAccountEvent API
operationId: ApiController.HandleOfficialAccountEvent
/api/batch-enforce:
post:
tags:
- Enforce API
description: perform enforce
operationId: ApiController.BatchEnforce
parameters:
- in: body
name: body
description: casbin request array
required: true
schema:
$ref: '#/definitions/object.CasbinRequest'
- in: query
name: permissionId
description: permission id
type: string
- in: query
name: modelId
description: model id
type: string
responses:
"200":
description: The Response object
schema:
$ref: '#/definitions/controllers.Response'
/api/buy-product:
post:
tags:
@ -893,6 +919,36 @@ paths:
description: The Response object
schema:
$ref: '#/definitions/controllers.Response'
/api/enforce:
post:
tags:
- Enforce API
description: perform enforce
operationId: ApiController.Enforce
parameters:
- in: body
name: body
description: casbin request
required: true
schema:
$ref: '#/definitions/object.CasbinRequest'
- in: query
name: permissionId
description: permission id
type: string
- in: query
name: modelId
description: model id
type: string
- in: query
name: resourceId
description: resource id
type: string
responses:
"200":
description: The Response object
schema:
$ref: '#/definitions/controllers.Response'
/api/get-account:
get:
tags:
@ -1267,6 +1323,25 @@ paths:
type: array
items:
$ref: '#/definitions/object.Application'
/api/get-organization-names:
get:
tags:
- Organization API
description: get all organization names
operationId: ApiController.GetOrganizationNames
parameters:
- in: query
name: owner
description: owner
required: true
type: string
responses:
"200":
description: The Response object
schema:
type: array
items:
$ref: '#/definitions/object.Organization'
/api/get-organizations:
get:
tags:
@ -1841,7 +1916,6 @@ paths:
- in: query
name: id
description: The id ( owner/name ) of the user
required: true
type: string
- in: query
name: owner
@ -1994,6 +2068,17 @@ paths:
type: array
items:
$ref: '#/definitions/object.Webhook'
/api/health:
get:
tags:
- System API
description: check if the system is live
operationId: ApiController.Health
responses:
"200":
description: The Response object
schema:
$ref: '#/definitions/controllers.Response'
/api/invoice-payment:
post:
tags:
@ -2940,10 +3025,10 @@ paths:
schema:
$ref: '#/definitions/Response'
definitions:
1183.0x1400042eb70.false:
1225.0xc0002e2ae0.false:
title: "false"
type: object
1217.0x1400042eba0.false:
1260.0xc0002e2b10.false:
title: "false"
type: object
LaravelResponse:
@ -2979,9 +3064,9 @@ definitions:
type: object
properties:
data:
$ref: '#/definitions/1183.0x1400042eb70.false'
$ref: '#/definitions/1225.0xc0002e2ae0.false'
data2:
$ref: '#/definitions/1217.0x1400042eba0.false'
$ref: '#/definitions/1260.0xc0002e2b10.false'
msg:
type: string
name:
@ -3005,6 +3090,9 @@ definitions:
jose.JSONWebKey:
title: JSONWebKey
type: object
object.&{179844 0xc000a02f90 false}:
title: '&{179844 0xc000a02f90 false}'
type: object
object.AccountItem:
title: AccountItem
type: object
@ -3072,6 +3160,8 @@ definitions:
type: string
formCss:
type: string
formCssMobile:
type: string
formOffset:
type: integer
format: int64
@ -3087,6 +3177,8 @@ definitions:
type: string
name:
type: string
orgChoiceMode:
type: string
organization:
type: string
organizationObj:
@ -3124,6 +3216,11 @@ definitions:
$ref: '#/definitions/object.ThemeData'
tokenFormat:
type: string
object.CasbinRequest:
title: CasbinRequest
type: array
items:
$ref: '#/definitions/object.&{179844 0xc000a02f90 false}'
object.Cert:
title: Cert
type: object
@ -3284,6 +3381,14 @@ definitions:
type: string
text:
type: string
object.MfaItem:
title: MfaItem
type: object
properties:
name:
type: string
rule:
type: string
object.MfaProps:
title: MfaProps
type: object
@ -3407,6 +3512,10 @@ definitions:
type: string
masterPassword:
type: string
mfaItems:
type: array
items:
$ref: '#/definitions/object.MfaItem'
name:
type: string
owner:
@ -3544,8 +3653,14 @@ definitions:
type: string
displayName:
type: string
isEnabled:
type: boolean
name:
type: string
options:
type: array
items:
type: string
owner:
type: string
pricePerMonth:
@ -3556,8 +3671,6 @@ definitions:
format: double
role:
type: string
options:
type: array
object.Pricing:
title: Pricing
type: object
@ -3775,6 +3888,8 @@ definitions:
type: string
name:
type: string
object:
type: string
organization:
type: string
owner:
@ -4181,6 +4296,8 @@ definitions:
type: string
passwordSalt:
type: string
passwordType:
type: string
patreon:
type: string
paypal:
@ -4291,6 +4408,8 @@ definitions:
type: string
name:
type: string
organization:
type: string
phone:
type: string
picture: