2022-09-14 22:14:13 +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"
|
|
|
|
|
2022-09-29 19:44:08 +08:00
|
|
|
"github.com/beego/beego/utils/pagination"
|
2022-09-14 22:14:13 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2023-02-12 09:33:24 +08:00
|
|
|
xormadapter "github.com/casdoor/xorm-adapter/v3"
|
2022-09-14 22:14:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *ApiController) GetCasbinAdapters() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
|
|
|
field := c.Input().Get("field")
|
|
|
|
value := c.Input().Get("value")
|
|
|
|
sortField := c.Input().Get("sortField")
|
|
|
|
sortOrder := c.Input().Get("sortOrder")
|
2023-05-09 00:06:52 +08:00
|
|
|
organization := c.Input().Get("organization")
|
2022-09-14 22:14:13 +08:00
|
|
|
if limit == "" || page == "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
adapters, err := object.GetCasbinAdapters(owner, organization)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-08 10:22:59 +08:00
|
|
|
c.ResponseOk(adapters)
|
2022-09-14 22:14:13 +08:00
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2023-05-30 15:49:39 +08:00
|
|
|
count, err := object.GetCasbinAdapterCount(owner, organization, field, value)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
|
|
|
adapters, err := object.GetPaginationCasbinAdapters(owner, organization, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:14:13 +08:00
|
|
|
c.ResponseOk(adapters, paginator.Nums())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) GetCasbinAdapter() {
|
|
|
|
id := c.Input().Get("id")
|
2023-05-30 15:49:39 +08:00
|
|
|
adapter, err := object.GetCasbinAdapter(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-08 10:22:59 +08:00
|
|
|
c.ResponseOk(adapter)
|
2022-09-14 22:14:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) UpdateCasbinAdapter() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
|
|
|
var casbinAdapter object.CasbinAdapter
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.UpdateCasbinAdapter(id, &casbinAdapter))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) AddCasbinAdapter() {
|
|
|
|
var casbinAdapter object.CasbinAdapter
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.AddCasbinAdapter(&casbinAdapter))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) DeleteCasbinAdapter() {
|
|
|
|
var casbinAdapter object.CasbinAdapter
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.DeleteCasbinAdapter(&casbinAdapter))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) SyncPolicies() {
|
|
|
|
id := c.Input().Get("id")
|
2023-05-30 15:49:39 +08:00
|
|
|
adapter, err := object.GetCasbinAdapter(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-09-14 22:14:13 +08:00
|
|
|
|
2022-11-25 16:02:20 +08:00
|
|
|
policies, err := object.SyncPolicies(adapter)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-08 10:22:59 +08:00
|
|
|
c.ResponseOk(policies)
|
2022-11-25 16:02:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) UpdatePolicy() {
|
|
|
|
id := c.Input().Get("id")
|
2023-05-30 15:49:39 +08:00
|
|
|
adapter, err := object.GetCasbinAdapter(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:02:20 +08:00
|
|
|
var policies []xormadapter.CasbinRule
|
2023-05-30 15:49:39 +08:00
|
|
|
err = json.Unmarshal(c.Ctx.Input.RequestBody, &policies)
|
2022-11-25 16:02:20 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := object.UpdatePolicy(util.CasbinToSlice(policies[0]), util.CasbinToSlice(policies[1]), adapter)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Data["json"] = wrapActionResponse(affected)
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) AddPolicy() {
|
|
|
|
id := c.Input().Get("id")
|
2023-05-30 15:49:39 +08:00
|
|
|
adapter, err := object.GetCasbinAdapter(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:02:20 +08:00
|
|
|
var policy xormadapter.CasbinRule
|
2023-05-30 15:49:39 +08:00
|
|
|
err = json.Unmarshal(c.Ctx.Input.RequestBody, &policy)
|
2022-11-25 16:02:20 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := object.AddPolicy(util.CasbinToSlice(policy), adapter)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Data["json"] = wrapActionResponse(affected)
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApiController) RemovePolicy() {
|
|
|
|
id := c.Input().Get("id")
|
2023-05-30 15:49:39 +08:00
|
|
|
adapter, err := object.GetCasbinAdapter(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:02:20 +08:00
|
|
|
var policy xormadapter.CasbinRule
|
2023-05-30 15:49:39 +08:00
|
|
|
err = json.Unmarshal(c.Ctx.Input.RequestBody, &policy)
|
2022-11-25 16:02:20 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := object.RemovePolicy(util.CasbinToSlice(policy), adapter)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Data["json"] = wrapActionResponse(affected)
|
2022-09-14 22:14:13 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|