Repository: casbin/casbin
Pull request #523 (2020-07-15 04:49:53)

fix: Add DeleteRolesForUserInDomain
fix: https://github.com/casbin/casbin/issues/522
Signed-off-by: dovics <wrs369@163.com>

coveralls: 
[![Coverage Status](https://coveralls.io/builds/32075841/badge)](https://coveralls.io/builds/32075841)

Coverage decreased (-0.1%) to 74.426% when pulling **95e38c0d7d8257bf10d985a808ccc4f15e6da34c on dovics:delete-roles-for-user-in-domain** into **eb3aa1c4a6ee9de8a658073c92c991c951773a83 on casbin:master**.

hsluoyz: @95Key can you review this PR?

Patch:
diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml
index f0a3464..784ea40 100644
--- a/.github/workflows/default.yml
+++ b/.github/workflows/default.yml
@@ -1,12 +1,6 @@
 name: Default
 
-on:
-  push:
-    branches:
-      - master
-  pull_request:
-    branches:
-      - master
+on: [push, pull_request]
 
 jobs:
   test:
@@ -44,6 +38,3 @@ jobs:
           semantic-release
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-
-
-
diff --git a/rbac_api_with_domains.go b/rbac_api_with_domains.go
index f1273cb..023388d 100644
--- a/rbac_api_with_domains.go
+++ b/rbac_api_with_domains.go
@@ -42,3 +42,19 @@ func (e *Enforcer) AddRoleForUserInDomain(user string, role string, domain strin
 func (e *Enforcer) DeleteRoleForUserInDomain(user string, role string, domain string) (bool, error) {
 	return e.RemoveGroupingPolicy(user, role, domain)
 }
+
+// DeleteRolesForUserInDomain deletes all roles for a user inside a domain.
+// Returns false if the user does not have any roles (aka not affected).
+func (e *Enforcer) DeleteRolesForUserInDomain(user string, domain string) (bool, error) {
+	roles, err := e.model["g"]["g"].RM.GetRoles(user, domain)
+	if err != nil {
+		return false, err
+	}
+
+	var rules [][]string
+	for _, role := range roles {
+		rules = append(rules, []string{user, role, domain})
+	}
+
+	return e.RemoveGroupingPolicies(rules)
+}
diff --git a/rbac_api_with_domains_synced.go b/rbac_api_with_domains_synced.go
index 90c67e9..bf194e0 100644
--- a/rbac_api_with_domains_synced.go
+++ b/rbac_api_with_domains_synced.go
@@ -50,3 +50,11 @@ func (e *SyncedEnforcer) DeleteRoleForUserInDomain(user string, role string, dom
 	defer e.m.Unlock()
 	return e.Enforcer.DeleteRoleForUserInDomain(user, role, domain)
 }
+
+// DeleteRolesForUserInDomain deletes all roles for a user inside a domain.
+// Returns false if the user does not have any roles (aka not affected).
+func (e *SyncedEnforcer) DeleteRolesForUserInDomain(user string, domain string) (bool, error) {
+	e.m.Lock()
+	defer e.m.Unlock()
+	return e.Enforcer.DeleteRolesForUserInDomain(user, domain)
+}


Test patch:
diff --git a/rbac_api_with_domains_test.go b/rbac_api_with_domains_test.go
index e4c10a1..6179ce0 100644
--- a/rbac_api_with_domains_test.go
+++ b/rbac_api_with_domains_test.go
@@ -136,6 +136,34 @@ func TestRoleAPIWithDomains(t *testing.T) {
 
 	testGetRoles(t, e, []string{}, "non_exist", "domain2")
 	testGetRolesInDomain(t, e, "non_exist", "domain2", []string{})
+
+	e.AddRoleForUserInDomain("alice", "admin", "domain1")
+	e.DeleteRolesForUserInDomain("bob", "domain1")
+
+	testGetRoles(t, e, []string{"admin"}, "alice", "domain1")
+	testGetRolesInDomain(t, e, "alice", "domain1", []string{"admin"})
+
+	testGetRoles(t, e, []string{}, "bob", "domain1")
+	testGetRolesInDomain(t, e, "bob", "domain1", []string{})
+
+	testGetRoles(t, e, []string{}, "admin", "domain1")
+	testGetRolesInDomain(t, e, "admin", "domain1", []string{})
+
+	testGetRoles(t, e, []string{}, "non_exist", "domain1")
+	testGetRolesInDomain(t, e, "non_exist", "domain1", []string{})
+
+	testGetRoles(t, e, []string{}, "alice", "domain2")
+	testGetRolesInDomain(t, e, "alice", "domain2", []string{})
+
+	testGetRoles(t, e, []string{"admin"}, "bob", "domain2")
+	testGetRolesInDomain(t, e, "bob", "domain2", []string{"admin"})
+
+	testGetRoles(t, e, []string{}, "admin", "domain2")
+	testGetRolesInDomain(t, e, "admin", "domain2", []string{})
+
+	testGetRoles(t, e, []string{}, "non_exist", "domain2")
+	testGetRolesInDomain(t, e, "non_exist", "domain2", []string{})
+
 }
 
 func testGetPermissionsInDomain(t *testing.T, e *Enforcer, name string, domain string, res [][]string) {