From 7e664ba90d024809c036cb8e880456c12fd7abb6 Mon Sep 17 00:00:00 2001 From: zhuying Date: Fri, 15 Mar 2024 23:16:49 +0800 Subject: [PATCH] feat:add add-policy feature --- casdoorsdk/enforcer.go | 18 ++++++++++++++++++ casdoorsdk/enforcer_global.go | 4 ++++ casdoorsdk/enforcer_test.go | 29 +++++++++++++++++++++++++++++ casdoorsdk/util_modify.go | 23 +++++++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/casdoorsdk/enforcer.go b/casdoorsdk/enforcer.go index dd0e220..739929a 100644 --- a/casdoorsdk/enforcer.go +++ b/casdoorsdk/enforcer.go @@ -36,6 +36,19 @@ type Enforcer struct { //*casbin.Enforcer } +type CasbinRule struct { + Id int64 `xorm:"pk autoincr"` + Ptype string `xorm:"varchar(100) index not null default ''"` + V0 string `xorm:"varchar(100) index not null default ''"` + V1 string `xorm:"varchar(100) index not null default ''"` + V2 string `xorm:"varchar(100) index not null default ''"` + V3 string `xorm:"varchar(100) index not null default ''"` + V4 string `xorm:"varchar(100) index not null default ''"` + V5 string `xorm:"varchar(100) index not null default ''"` + + tableName string `xorm:"-"` +} + func (c *Client) GetEnforcers() ([]*Enforcer, error) { queryMap := map[string]string{ "owner": c.OrganizationName, @@ -110,3 +123,8 @@ func (c *Client) DeleteEnforcer(enforcer *Enforcer) (bool, error) { _, affected, err := c.modifyEnforcer("delete-enforcer", enforcer, nil) return affected, err } + +func (c *Client) AddPolicy(enforcer *Enforcer, policy *CasbinRule) (bool, error) { + _, affected, err := c.modifyPolicy("add-policy", enforcer, policy, nil) + return affected, err +} diff --git a/casdoorsdk/enforcer_global.go b/casdoorsdk/enforcer_global.go index 7d9b900..7840f97 100644 --- a/casdoorsdk/enforcer_global.go +++ b/casdoorsdk/enforcer_global.go @@ -37,3 +37,7 @@ func AddEnforcer(enforcer *Enforcer) (bool, error) { func DeleteEnforcer(enforcer *Enforcer) (bool, error) { return globalClient.DeleteEnforcer(enforcer) } + +func AddPolicy(enforcer *Enforcer, policy *CasbinRule) (bool, error) { + return globalClient.AddPolicy(enforcer, policy) +} diff --git a/casdoorsdk/enforcer_test.go b/casdoorsdk/enforcer_test.go index 82ee8fe..cb6e8e5 100644 --- a/casdoorsdk/enforcer_test.go +++ b/casdoorsdk/enforcer_test.go @@ -18,6 +18,35 @@ import ( "testing" ) +func TestPolicy(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + name := getRandomName("Enforcer") + + // Add a new object + enforcer := &Enforcer{ + Owner: "admin", + Name: name, + CreatedTime: GetCurrentTime(), + DisplayName: name, + Model: "built-in/user-model-built-in", + Adapter: "built-in/user-adapter-built-in", + Description: "Casdoor Website", + } + _, err := AddEnforcer(enforcer) + if err != nil { + t.Fatalf("Failed to add enforcer: %v", err) + } + //Add a new policy + policy := &CasbinRule{ + Ptype: "p", + } + _, err = AddPolicy(enforcer, policy) + if err != nil { + t.Fatalf("Failed to add policy: %v", err) + } + +} + func TestEnforcer(t *testing.T) { InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) diff --git a/casdoorsdk/util_modify.go b/casdoorsdk/util_modify.go index 8fe6ccf..0418575 100644 --- a/casdoorsdk/util_modify.go +++ b/casdoorsdk/util_modify.go @@ -250,6 +250,29 @@ func (c *Client) modifyEnforcer(action string, enforcer *Enforcer, columns []str return resp, resp.Data == "Affected", nil } +// modifyPolicy is an encapsulation of cert CUD(Create, Update, Delete) operations. +func (c *Client) modifyPolicy(action string, enforcer *Enforcer, policy *CasbinRule, columns []string) (*Response, bool, error) { + queryMap := map[string]string{ + "id": fmt.Sprintf("%s/%s", enforcer.Owner, enforcer.Name), + } + + if len(columns) != 0 { + queryMap["columns"] = strings.Join(columns, ",") + } + + postBytes, err := json.Marshal(policy) + if err != nil { + return nil, false, err + } + + resp, err := c.DoPost(action, queryMap, postBytes, false, false) + if err != nil { + return nil, false, err + } + + return resp, resp.Data == "Affected", nil +} + // modifyEnforcer is an encapsulation of cert CUD(Create, Update, Delete) operations. // possible actions are `add-group`, `update-group`, `delete-group`, func (c *Client) modifyGroup(action string, group *Group, columns []string) (*Response, bool, error) {