Repository: casbin/casbin
Pull request #954 (2022-01-27 12:53:55)

feat: add batch methods for update grouping policies
Add Batch operations for `UpdateGroupingPolicy` and `UpdateNamedGroupingPolicy`.

CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/casbin/casbin?pullRequest=954) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/casbin/casbin?pullRequest=954) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/casbin/casbin?pullRequest=954) it.</sub>
casbin-bot:  @tangyang9464 @closetool @sagilio please review
hsluoyz: @zsh1995 plz fix:

![image](https://user-images.githubusercontent.com/3787410/151404157-476f67d8-06cc-40ba-b33a-9ca6a6551d2f.png)

zsh1995: @hsluoyz fix done.
hsluoyz: @abichinger @Abingcbc @seriouszyx @fabian4 plz review

Patch:
diff --git a/enforcer_synced.go b/enforcer_synced.go
index 677b577..2929b07 100644
--- a/enforcer_synced.go
+++ b/enforcer_synced.go
@@ -20,6 +20,7 @@ import (
 	"time"
 
 	"github.com/Knetic/govaluate"
+
 	"github.com/casbin/casbin/v2/persist"
 )
 
@@ -516,12 +517,24 @@ func (e *SyncedEnforcer) UpdateGroupingPolicy(oldRule []string, newRule []string
 	return e.Enforcer.UpdateGroupingPolicy(oldRule, newRule)
 }
 
+func (e *SyncedEnforcer) UpdateGroupingPolicies(oldRules [][]string, newRules [][]string) (bool, error) {
+	e.m.Lock()
+	defer e.m.Unlock()
+	return e.Enforcer.UpdateGroupingPolicies(oldRules, newRules)
+}
+
 func (e *SyncedEnforcer) UpdateNamedGroupingPolicy(ptype string, oldRule []string, newRule []string) (bool, error) {
 	e.m.Lock()
 	defer e.m.Unlock()
 	return e.Enforcer.UpdateNamedGroupingPolicy(ptype, oldRule, newRule)
 }
 
+func (e *SyncedEnforcer) UpdateNamedGroupingPolicies(ptype string, oldRules [][]string, newRules [][]string) (bool, error) {
+	e.m.Lock()
+	defer e.m.Unlock()
+	return e.Enforcer.UpdateNamedGroupingPolicies(ptype, oldRules, newRules)
+}
+
 // RemoveFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
 func (e *SyncedEnforcer) RemoveFilteredNamedGroupingPolicy(ptype string, fieldIndex int, fieldValues ...string) (bool, error) {
 	e.m.Lock()
diff --git a/management_api.go b/management_api.go
index ddef0ae..f3f62b9 100644
--- a/management_api.go
+++ b/management_api.go
@@ -318,10 +318,19 @@ func (e *Enforcer) UpdateGroupingPolicy(oldRule []string, newRule []string) (boo
 	return e.UpdateNamedGroupingPolicy("g", oldRule, newRule)
 }
 
+// UpdateGroupingPolicies updates authorization rules from the current policies.
+func (e *Enforcer) UpdateGroupingPolicies(oldRules [][]string, newRules [][]string) (bool, error) {
+	return e.UpdateNamedGroupingPolicies("g", oldRules, newRules)
+}
+
 func (e *Enforcer) UpdateNamedGroupingPolicy(ptype string, oldRule []string, newRule []string) (bool, error) {
 	return e.updatePolicy("g", ptype, oldRule, newRule)
 }
 
+func (e *Enforcer) UpdateNamedGroupingPolicies(ptype string, oldRules [][]string, newRules [][]string) (bool, error) {
+	return e.updatePolicies("g", ptype, oldRules, newRules)
+}
+
 // RemoveFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
 func (e *Enforcer) RemoveFilteredNamedGroupingPolicy(ptype string, fieldIndex int, fieldValues ...string) (bool, error) {
 	return e.removeFilteredPolicy("g", ptype, fieldIndex, fieldValues...)


Test patch:
diff --git a/management_api_test.go b/management_api_test.go
index bbd0e5b..ced884e 100644
--- a/management_api_test.go
+++ b/management_api_test.go
@@ -271,4 +271,12 @@ func TestModifyGroupingPolicyAPI(t *testing.T) {
 	testGetRoles(t, e, []string{"admin"}, "eve")
 	testGetRoles(t, e, []string{"data4_admin"}, "admin")
 
+	_, _ = e.UpdateGroupingPolicies([][]string{{"eve", "admin"}}, [][]string{{"eve", "admin_groups"}})
+	_, _ = e.UpdateGroupingPolicies([][]string{{"admin", "data4_admin"}}, [][]string{{"admin", "data5_admin"}})
+	testGetUsers(t, e, []string{"admin"}, "data5_admin")
+	testGetUsers(t, e, []string{"eve"}, "admin_groups")
+
+	testGetRoles(t, e, []string{"data5_admin"}, "admin")
+	testGetRoles(t, e, []string{"admin_groups"}, "eve")
+
 }