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" "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() { func (c *ApiController) Enforce() {
permissionId := c.Input().Get("permissionId") permissionId := c.Input().Get("permissionId")
modelId := c.Input().Get("modelId") modelId := c.Input().Get("modelId")
@ -38,29 +48,41 @@ func (c *ApiController) Enforce() {
return return
} }
permissions := make([]*object.Permission, 0) permissions := []*object.Permission{}
res := []bool{}
if modelId != "" { if modelId != "" {
owner, modelName := util.GetOwnerAndNameFromId(modelId) owner, modelName := util.GetOwnerAndNameFromId(modelId)
permissions, err = object.GetPermissionsByModel(owner, modelName) permissions, err = object.GetPermissionsByModel(owner, modelName)
if err != nil { if err != nil {
panic(err) c.ResponseError(err.Error())
return
} }
} else { } else if resourceId != "" {
permissions, err = object.GetPermissionsByResource(resourceId) permissions, err = object.GetPermissionsByResource(resourceId)
if err != nil { 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 { for _, permission := range permissions {
res = append(res, object.Enforce(permission.GetId(), &request)) res = append(res, object.Enforce(permission.GetId(), &request))
} }
c.Data["json"] = res c.ResponseOk(res)
c.ServeJSON()
} }
// 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() { func (c *ApiController) BatchEnforce() {
permissionId := c.Input().Get("permissionId") permissionId := c.Input().Get("permissionId")
modelId := c.Input().Get("modelId") modelId := c.Input().Get("modelId")
@ -68,26 +90,33 @@ func (c *ApiController) BatchEnforce() {
var requests []object.CasbinRequest var requests []object.CasbinRequest
err := json.Unmarshal(c.Ctx.Input.RequestBody, &requests) err := json.Unmarshal(c.Ctx.Input.RequestBody, &requests)
if err != nil { if err != nil {
panic(err) c.ResponseError(err.Error())
return
} }
if permissionId != "" { if permissionId != "" {
c.Data["json"] = object.BatchEnforce(permissionId, &requests) c.ResponseOk(object.BatchEnforce(permissionId, &requests))
c.ServeJSON() return
} 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)
} }
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() { func (c *ApiController) GetAllObjects() {

View File

@ -777,6 +777,46 @@
"operationId": "ApiController.HandleOfficialAccountEvent" "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": { "/api/buy-product": {
"post": { "post": {
"tags": [ "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": { "/api/get-account": {
"get": { "get": {
"tags": [ "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": { "/api/get-organizations": {
"get": { "get": {
"tags": [ "tags": [
@ -2826,7 +2941,6 @@
"in": "query", "in": "query",
"name": "id", "name": "id",
"description": "The id ( owner/name ) of the user", "description": "The id ( owner/name ) of the user",
"required": true,
"type": "string" "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": { "/api/invoice-payment": {
"post": { "post": {
"tags": [ "tags": [
@ -4501,11 +4632,11 @@
} }
}, },
"definitions": { "definitions": {
"1183.0x1400042eb70.false": { "1225.0xc0002e2ae0.false": {
"title": "false", "title": "false",
"type": "object" "type": "object"
}, },
"1217.0x1400042eba0.false": { "1260.0xc0002e2b10.false": {
"title": "false", "title": "false",
"type": "object" "type": "object"
}, },
@ -4554,10 +4685,10 @@
"type": "object", "type": "object",
"properties": { "properties": {
"data": { "data": {
"$ref": "#/definitions/1183.0x1400042eb70.false" "$ref": "#/definitions/1225.0xc0002e2ae0.false"
}, },
"data2": { "data2": {
"$ref": "#/definitions/1217.0x1400042eba0.false" "$ref": "#/definitions/1260.0xc0002e2b10.false"
}, },
"msg": { "msg": {
"type": "string" "type": "string"
@ -4595,6 +4726,10 @@
"title": "JSONWebKey", "title": "JSONWebKey",
"type": "object" "type": "object"
}, },
"object.\u0026{179844 0xc000a02f90 false}": {
"title": "\u0026{179844 0xc000a02f90 false}",
"type": "object"
},
"object.AccountItem": { "object.AccountItem": {
"title": "AccountItem", "title": "AccountItem",
"type": "object", "type": "object",
@ -4693,6 +4828,9 @@
"formCss": { "formCss": {
"type": "string" "type": "string"
}, },
"formCssMobile": {
"type": "string"
},
"formOffset": { "formOffset": {
"type": "integer", "type": "integer",
"format": "int64" "format": "int64"
@ -4715,6 +4853,9 @@
"name": { "name": {
"type": "string" "type": "string"
}, },
"orgChoiceMode": {
"type": "string"
},
"organization": { "organization": {
"type": "string" "type": "string"
}, },
@ -4772,6 +4913,13 @@
} }
} }
}, },
"object.CasbinRequest": {
"title": "CasbinRequest",
"type": "array",
"items": {
"$ref": "#/definitions/object.\u0026{179844 0xc000a02f90 false}"
}
},
"object.Cert": { "object.Cert": {
"title": "Cert", "title": "Cert",
"type": "object", "type": "object",
@ -5008,6 +5156,18 @@
} }
} }
}, },
"object.MfaItem": {
"title": "MfaItem",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"rule": {
"type": "string"
}
}
},
"object.MfaProps": { "object.MfaProps": {
"title": "MfaProps", "title": "MfaProps",
"type": "object", "type": "object",
@ -5190,6 +5350,12 @@
"masterPassword": { "masterPassword": {
"type": "string" "type": "string"
}, },
"mfaItems": {
"type": "array",
"items": {
"$ref": "#/definitions/object.MfaItem"
}
},
"name": { "name": {
"type": "string" "type": "string"
}, },
@ -5395,9 +5561,18 @@
"displayName": { "displayName": {
"type": "string" "type": "string"
}, },
"isEnabled": {
"type": "boolean"
},
"name": { "name": {
"type": "string" "type": "string"
}, },
"options": {
"type": "array",
"items": {
"type": "string"
}
},
"owner": { "owner": {
"type": "string" "type": "string"
}, },
@ -5411,9 +5586,6 @@
}, },
"role": { "role": {
"type": "string" "type": "string"
},
"options": {
"type": "array"
} }
} }
}, },
@ -5737,6 +5909,9 @@
"name": { "name": {
"type": "string" "type": "string"
}, },
"object": {
"type": "string"
},
"organization": { "organization": {
"type": "string" "type": "string"
}, },
@ -6341,6 +6516,9 @@
"passwordSalt": { "passwordSalt": {
"type": "string" "type": "string"
}, },
"passwordType": {
"type": "string"
},
"patreon": { "patreon": {
"type": "string" "type": "string"
}, },
@ -6505,6 +6683,9 @@
"name": { "name": {
"type": "string" "type": "string"
}, },
"organization": {
"type": "string"
},
"phone": { "phone": {
"type": "string" "type": "string"
}, },
@ -6634,4 +6815,4 @@
"type": "object" "type": "object"
} }
} }
} }

View File

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