2022-08-07 23:42:45 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/casdoor/casdoor/object"
|
2023-05-12 21:39:57 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2022-08-07 23:42:45 +08:00
|
|
|
)
|
|
|
|
|
2023-06-04 17:19:58 +08:00
|
|
|
// 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]
|
2022-08-07 23:42:45 +08:00
|
|
|
func (c *ApiController) Enforce() {
|
2023-05-12 21:32:48 +08:00
|
|
|
permissionId := c.Input().Get("permissionId")
|
2023-05-12 21:39:57 +08:00
|
|
|
modelId := c.Input().Get("modelId")
|
2023-05-18 15:36:03 +07:00
|
|
|
resourceId := c.Input().Get("resourceId")
|
2023-05-12 21:32:48 +08:00
|
|
|
|
|
|
|
var request object.CasbinRequest
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &request)
|
2022-08-07 23:42:45 +08:00
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2022-08-07 23:42:45 +08:00
|
|
|
}
|
|
|
|
|
2023-05-12 21:39:57 +08:00
|
|
|
if permissionId != "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
c.ResponseOk(object.Enforce(permissionId, &request))
|
2023-05-18 15:36:03 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-04 17:19:58 +08:00
|
|
|
permissions := []*object.Permission{}
|
2023-05-18 15:36:03 +07:00
|
|
|
if modelId != "" {
|
2023-05-12 21:39:57 +08:00
|
|
|
owner, modelName := util.GetOwnerAndNameFromId(modelId)
|
2023-05-30 15:49:39 +08:00
|
|
|
permissions, err = object.GetPermissionsByModel(owner, modelName)
|
|
|
|
if err != nil {
|
2023-06-04 17:19:58 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
2023-06-04 17:19:58 +08:00
|
|
|
} else if resourceId != "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
permissions, err = object.GetPermissionsByResource(resourceId)
|
|
|
|
if err != nil {
|
2023-06-04 17:19:58 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
2023-06-04 17:19:58 +08:00
|
|
|
} else {
|
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
|
|
|
return
|
2023-05-18 15:36:03 +07:00
|
|
|
}
|
2023-05-12 21:39:57 +08:00
|
|
|
|
2023-06-04 17:19:58 +08:00
|
|
|
res := []bool{}
|
2023-05-18 15:36:03 +07:00
|
|
|
for _, permission := range permissions {
|
|
|
|
res = append(res, object.Enforce(permission.GetId(), &request))
|
2023-05-12 21:39:57 +08:00
|
|
|
}
|
2023-06-04 17:19:58 +08:00
|
|
|
c.ResponseOk(res)
|
2022-08-07 23:42:45 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 17:19:58 +08:00
|
|
|
// 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]
|
2022-08-07 23:42:45 +08:00
|
|
|
func (c *ApiController) BatchEnforce() {
|
2023-05-12 21:32:48 +08:00
|
|
|
permissionId := c.Input().Get("permissionId")
|
2023-05-12 21:39:57 +08:00
|
|
|
modelId := c.Input().Get("modelId")
|
2023-05-12 21:32:48 +08:00
|
|
|
|
|
|
|
var requests []object.CasbinRequest
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &requests)
|
2022-08-07 23:42:45 +08:00
|
|
|
if err != nil {
|
2023-06-04 17:19:58 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2022-08-07 23:42:45 +08:00
|
|
|
}
|
|
|
|
|
2023-05-12 21:39:57 +08:00
|
|
|
if permissionId != "" {
|
2023-06-04 17:19:58 +08:00
|
|
|
c.ResponseOk(object.BatchEnforce(permissionId, &requests))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
permissions := []*object.Permission{}
|
|
|
|
if modelId != "" {
|
2023-05-12 21:39:57 +08:00
|
|
|
owner, modelName := util.GetOwnerAndNameFromId(modelId)
|
2023-06-04 17:19:58 +08:00
|
|
|
permissions, err = object.GetPermissionsByModel(owner, modelName)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
2023-06-04 17:19:58 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-12 21:39:57 +08:00
|
|
|
}
|
2023-06-04 17:19:58 +08:00
|
|
|
} else {
|
|
|
|
c.ResponseError(c.T("general:Missing parameter"))
|
|
|
|
return
|
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2023-06-04 17:19:58 +08:00
|
|
|
res := [][]bool{}
|
|
|
|
for _, permission := range permissions {
|
|
|
|
res = append(res, object.BatchEnforce(permission.GetId(), &requests))
|
2023-05-12 21:39:57 +08:00
|
|
|
}
|
2023-06-04 17:19:58 +08:00
|
|
|
c.ResponseOk(res)
|
2022-08-07 23:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) GetAllObjects() {
|
|
|
|
userId := c.GetSessionUsername()
|
|
|
|
if userId == "" {
|
2023-01-06 20:12:32 +08:00
|
|
|
c.ResponseError(c.T("general:Please login first"))
|
2022-08-07 23:42:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
c.ResponseOk(object.GetAllObjects(userId))
|
2022-08-07 23:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) GetAllActions() {
|
|
|
|
userId := c.GetSessionUsername()
|
|
|
|
if userId == "" {
|
2023-01-06 20:12:32 +08:00
|
|
|
c.ResponseError(c.T("general:Please login first"))
|
2022-08-07 23:42:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
c.ResponseOk(object.GetAllActions(userId))
|
2022-08-07 23:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) GetAllRoles() {
|
|
|
|
userId := c.GetSessionUsername()
|
|
|
|
if userId == "" {
|
2023-01-06 20:12:32 +08:00
|
|
|
c.ResponseError(c.T("general:Please login first"))
|
2022-08-07 23:42:45 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
c.ResponseOk(object.GetAllRoles(userId))
|
2022-08-07 23:42:45 +08:00
|
|
|
}
|