-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat:add add-policy feature * feat: add all functions about policy * feat: split files
- Loading branch information
1 parent
cfe0e63
commit 419a5ab
Showing
4 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 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 casdoorsdk | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
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) AddPolicy(enforcer *Enforcer, policy *CasbinRule) (bool, error) { | ||
var policies []*CasbinRule | ||
policies = make([]*CasbinRule, 1) | ||
policies[0] = policy | ||
_, affected, err := c.modifyPolicy("add-policy", enforcer, policies, nil) | ||
return affected, err | ||
} | ||
|
||
func (c *Client) UpdatePolicy(enforcer *Enforcer, oldpolicy *CasbinRule, newpolicy *CasbinRule) (bool, error) { | ||
var policies []*CasbinRule | ||
policies = make([]*CasbinRule, 2) | ||
policies[0] = oldpolicy | ||
policies[1] = newpolicy | ||
_, affected, err := c.modifyPolicy("update-policy", enforcer, policies, nil) | ||
return affected, err | ||
} | ||
|
||
func (c *Client) RemovePolicy(enforcer *Enforcer, policy *CasbinRule) (bool, error) { | ||
var policies []*CasbinRule | ||
policies = make([]*CasbinRule, 1) | ||
policies[0] = policy | ||
_, affected, err := c.modifyPolicy("remove-policy", enforcer, policies, nil) | ||
return affected, err | ||
} | ||
|
||
func (c *Client) GetPolicies(enforcerName string, adapterId string) ([]*CasbinRule, error) { | ||
queryMap := map[string]string{ | ||
"id": fmt.Sprintf("%s/%s", c.OrganizationName, enforcerName), | ||
"adapterId": adapterId, | ||
} | ||
|
||
url := c.GetUrl("get-policies", queryMap) | ||
|
||
bytes, err := c.DoGetBytes(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var policies []*CasbinRule | ||
err = json.Unmarshal(bytes, &policies) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return policies, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 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 casdoorsdk | ||
|
||
func AddPolicy(enforcer *Enforcer, policy *CasbinRule) (bool, error) { | ||
return globalClient.AddPolicy(enforcer, policy) | ||
} | ||
|
||
func UpdatePolicy(enforcer *Enforcer, oldpolicy *CasbinRule, newpolicy *CasbinRule) (bool, error) { | ||
return globalClient.UpdatePolicy(enforcer, oldpolicy, newpolicy) | ||
} | ||
|
||
func RemovePolicy(enforcer *Enforcer, policy *CasbinRule) (bool, error) { | ||
return globalClient.RemovePolicy(enforcer, policy) | ||
} | ||
|
||
func GetPolicies(enforcerName string, adapterId string) ([]*CasbinRule, error) { | ||
return globalClient.GetPolicies(enforcerName, adapterId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2024 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 casdoorsdk | ||
|
||
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 object: %v", err) | ||
} | ||
|
||
//Add a new policy | ||
policy := &CasbinRule{ | ||
Ptype: "p", | ||
V0: "1", | ||
V1: "2", | ||
V2: "4", | ||
} | ||
_, err = AddPolicy(enforcer, policy) | ||
if err != nil { | ||
t.Fatalf("Failed to add policy: %v", err) | ||
} | ||
// | ||
// Get all objects, check if our added object is inside the list | ||
Policies, err := GetPolicies(name, "") | ||
if err != nil { | ||
t.Fatalf("Failed to get objects: %v", err) | ||
} | ||
found := false | ||
for _, item := range Policies { | ||
if item.Ptype == "p" && item.V2 == "4" { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
t.Fatalf("Added object not found in list") | ||
} | ||
|
||
newPolicy := &CasbinRule{ | ||
Ptype: "p", | ||
V0: "1", | ||
V1: "2", | ||
V2: "5", | ||
} | ||
|
||
// Update the object | ||
_, err = UpdatePolicy(enforcer, policy, newPolicy) | ||
if err != nil { | ||
t.Fatalf("Failed to update object: %v", err) | ||
} | ||
|
||
// Validate the update | ||
Policies, err = GetPolicies(name, "") | ||
if err != nil { | ||
t.Fatalf("Failed to get updated object: %v", err) | ||
} | ||
found = false | ||
for _, item := range Policies { | ||
if item.Ptype == "p" && item.V2 == "5" { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
t.Fatalf("Update object not found in list") | ||
} | ||
|
||
// Delete the object | ||
_, err = RemovePolicy(enforcer, newPolicy) | ||
if err != nil { | ||
t.Fatalf("Failed to delete object: %v", err) | ||
} | ||
|
||
// Validate the deletion | ||
Policies, err = GetPolicies(name, "") | ||
if err != nil { | ||
t.Fatalf("Failed to get updated object: %v", err) | ||
} | ||
found = false | ||
for _, item := range Policies { | ||
if item.Ptype == "p" && item.V2 == "3" { | ||
found = true | ||
break | ||
} | ||
} | ||
if found { | ||
t.Fatalf("Delete object found in list") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters