Skip to content

Commit

Permalink
feat: make hard-coded authz adapter editable, rename adapter to ormer (
Browse files Browse the repository at this point in the history
…casdoor#2149)

* refactor: rename casbinAdapter to casdoorAdapter

* feat: add initEnforcer

* fix: router

* refactor: make hard-coded code configurable

* fix: data type

* feat: support sqlite3

* feat: disable delete and edit name for built in resources

* feat: optimize code

* fix: init

* fix: e2e

* fix: remove datasourcename

* fix: revert rename

* refactor: change all ORM's Adatper to Ormer

* refactor: name
  • Loading branch information
leo220yuyaodog authored Jul 29, 2023
1 parent 74b058a commit ea10f8e
Show file tree
Hide file tree
Showing 56 changed files with 1,238 additions and 955 deletions.
40 changes: 3 additions & 37 deletions authz/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,22 @@ import (
"strings"

"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/model"
"github.com/casdoor/casdoor/conf"
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
xormadapter "github.com/casdoor/xorm-adapter/v3"
stringadapter "github.com/qiangmzsx/string-adapter/v2"
)

var Enforcer *casbin.Enforcer

func InitAuthz() {
func InitApi() {
var err error

tableNamePrefix := conf.GetConfigString("tableNamePrefix")
driverName := conf.GetConfigString("driverName")
dataSourceName := conf.GetConfigRealDataSourceName(driverName)
a, err := xormadapter.NewAdapterWithTableName(driverName, dataSourceName, "casbin_rule", tableNamePrefix, true)
e, err := object.GetEnforcer(util.GetId("built-in", "api-enforcer-built-in"))
if err != nil {
panic(err)
}

modelText := `
[request_definition]
r = subOwner, subName, method, urlPath, objOwner, objName
[policy_definition]
p = subOwner, subName, method, urlPath, objOwner, objName
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = (r.subOwner == p.subOwner || p.subOwner == "*") && \
(r.subName == p.subName || p.subName == "*" || r.subName != "anonymous" && p.subName == "!anonymous") && \
(r.method == p.method || p.method == "*") && \
(r.urlPath == p.urlPath || p.urlPath == "*") && \
(r.objOwner == p.objOwner || p.objOwner == "*") && \
(r.objName == p.objName || p.objName == "*") || \
(r.subOwner == r.objOwner && r.subName == r.objName)
`

m, err := model.NewModelFromString(modelText)
if err != nil {
panic(err)
}

Enforcer, err = casbin.NewEnforcer(m, a)
Enforcer, err = e.InitEnforcer()
if err != nil {
panic(err)
}
Expand Down
64 changes: 32 additions & 32 deletions controllers/casbin_adapter.go → controllers/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
xormadapter "github.com/casdoor/xorm-adapter/v3"
)

// GetCasbinAdapters
// @Title GetCasbinAdapters
// GetAdapters
// @Title GetAdapters
// @Tag Adapter API
// @Description get adapters
// @Param owner query string true "The owner of adapters"
// @Success 200 {array} object.Adapter The Response object
// @router /get-adapters [get]
func (c *ApiController) GetCasbinAdapters() {
func (c *ApiController) GetAdapters() {
owner := c.Input().Get("owner")
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
Expand All @@ -40,7 +40,7 @@ func (c *ApiController) GetCasbinAdapters() {
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
adapters, err := object.GetCasbinAdapters(owner)
adapters, err := object.GetAdapters(owner)
if err != nil {
c.ResponseError(err.Error())
return
Expand All @@ -49,14 +49,14 @@ func (c *ApiController) GetCasbinAdapters() {
c.ResponseOk(adapters)
} else {
limit := util.ParseInt(limit)
count, err := object.GetCasbinAdapterCount(owner, field, value)
count, err := object.GetAdapterCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}

paginator := pagination.SetPaginator(c.Ctx, limit, count)
adapters, err := object.GetPaginationCasbinAdapters(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
adapters, err := object.GetPaginationAdapters(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
Expand All @@ -66,17 +66,17 @@ func (c *ApiController) GetCasbinAdapters() {
}
}

// GetCasbinAdapter
// @Title GetCasbinAdapter
// GetAdapter
// @Title GetAdapter
// @Tag Adapter API
// @Description get adapter
// @Param id query string true "The id ( owner/name ) of the adapter"
// @Success 200 {object} object.Adapter The Response object
// @router /get-adapter [get]
func (c *ApiController) GetCasbinAdapter() {
func (c *ApiController) GetAdapter() {
id := c.Input().Get("id")

adapter, err := object.GetCasbinAdapter(id)
adapter, err := object.GetAdapter(id)
if err != nil {
c.ResponseError(err.Error())
return
Expand All @@ -85,69 +85,69 @@ func (c *ApiController) GetCasbinAdapter() {
c.ResponseOk(adapter)
}

// UpdateCasbinAdapter
// @Title UpdateCasbinAdapter
// UpdateAdapter
// @Title UpdateAdapter
// @Tag Adapter API
// @Description update adapter
// @Param id query string true "The id ( owner/name ) of the adapter"
// @Param body body object.Adapter true "The details of the adapter"
// @Success 200 {object} controllers.Response The Response object
// @router /update-adapter [post]
func (c *ApiController) UpdateCasbinAdapter() {
func (c *ApiController) UpdateAdapter() {
id := c.Input().Get("id")

var casbinAdapter object.CasbinAdapter
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
var adapter object.Adapter
err := json.Unmarshal(c.Ctx.Input.RequestBody, &adapter)
if err != nil {
c.ResponseError(err.Error())
return
}

c.Data["json"] = wrapActionResponse(object.UpdateCasbinAdapter(id, &casbinAdapter))
c.Data["json"] = wrapActionResponse(object.UpdateAdapter(id, &adapter))
c.ServeJSON()
}

// AddCasbinAdapter
// @Title AddCasbinAdapter
// AddAdapter
// @Title AddAdapter
// @Tag Adapter API
// @Description add adapter
// @Param body body object.Adapter true "The details of the adapter"
// @Success 200 {object} controllers.Response The Response object
// @router /add-adapter [post]
func (c *ApiController) AddCasbinAdapter() {
var casbinAdapter object.CasbinAdapter
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
func (c *ApiController) AddAdapter() {
var adapter object.Adapter
err := json.Unmarshal(c.Ctx.Input.RequestBody, &adapter)
if err != nil {
c.ResponseError(err.Error())
return
}

c.Data["json"] = wrapActionResponse(object.AddCasbinAdapter(&casbinAdapter))
c.Data["json"] = wrapActionResponse(object.AddAdapter(&adapter))
c.ServeJSON()
}

// DeleteCasbinAdapter
// @Title DeleteCasbinAdapter
// DeleteAdapter
// @Title DeleteAdapter
// @Tag Adapter API
// @Description delete adapter
// @Param body body object.Adapter true "The details of the adapter"
// @Success 200 {object} controllers.Response The Response object
// @router /delete-adapter [post]
func (c *ApiController) DeleteCasbinAdapter() {
var casbinAdapter object.CasbinAdapter
err := json.Unmarshal(c.Ctx.Input.RequestBody, &casbinAdapter)
func (c *ApiController) DeleteAdapter() {
var adapter object.Adapter
err := json.Unmarshal(c.Ctx.Input.RequestBody, &adapter)
if err != nil {
c.ResponseError(err.Error())
return
}

c.Data["json"] = wrapActionResponse(object.DeleteCasbinAdapter(&casbinAdapter))
c.Data["json"] = wrapActionResponse(object.DeleteAdapter(&adapter))
c.ServeJSON()
}

func (c *ApiController) SyncPolicies() {
id := c.Input().Get("id")
adapter, err := object.GetCasbinAdapter(id)
adapter, err := object.GetAdapter(id)
if err != nil {
c.ResponseError(err.Error())
return
Expand All @@ -164,7 +164,7 @@ func (c *ApiController) SyncPolicies() {

func (c *ApiController) UpdatePolicy() {
id := c.Input().Get("id")
adapter, err := object.GetCasbinAdapter(id)
adapter, err := object.GetAdapter(id)
if err != nil {
c.ResponseError(err.Error())
return
Expand All @@ -188,7 +188,7 @@ func (c *ApiController) UpdatePolicy() {

func (c *ApiController) AddPolicy() {
id := c.Input().Get("id")
adapter, err := object.GetCasbinAdapter(id)
adapter, err := object.GetAdapter(id)
if err != nil {
c.ResponseError(err.Error())
return
Expand All @@ -212,7 +212,7 @@ func (c *ApiController) AddPolicy() {

func (c *ApiController) RemovePolicy() {
id := c.Input().Get("id")
adapter, err := object.GetCasbinAdapter(id)
adapter, err := object.GetAdapter(id)
if err != nil {
c.ResponseError(err.Error())
return
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
object.InitDefaultStorageProvider()
object.InitLdapAutoSynchronizer()
proxy.InitHttpClient()
authz.InitAuthz()
authz.InitApi()

util.SafeGoroutine(func() { object.RunSyncUsersJob() })

Expand All @@ -62,7 +62,7 @@ func main() {
beego.InsertFilter("*", beego.BeforeRouter, routers.StaticFilter)
beego.InsertFilter("*", beego.BeforeRouter, routers.AutoSigninFilter)
beego.InsertFilter("*", beego.BeforeRouter, routers.CorsFilter)
beego.InsertFilter("*", beego.BeforeRouter, routers.AuthzFilter)
beego.InsertFilter("*", beego.BeforeRouter, routers.ApiFilter)
beego.InsertFilter("*", beego.BeforeRouter, routers.PrometheusFilter)
beego.InsertFilter("*", beego.BeforeRouter, routers.RecordMessage)

Expand Down
Loading

0 comments on commit ea10f8e

Please sign in to comment.