Skip to content

Commit

Permalink
feat:add add-policy feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuying1999 committed Mar 15, 2024
1 parent cfe0e63 commit 7e664ba
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
18 changes: 18 additions & 0 deletions casdoorsdk/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions casdoorsdk/enforcer_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
29 changes: 29 additions & 0 deletions casdoorsdk/enforcer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions casdoorsdk/util_modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7e664ba

Please sign in to comment.