Repository: casbin/casbin (go)

Issue:
[Feature] I want to get the current domains
t seems that there is no API for getdomains. Would you like to add it? Because the adapter is implemented with MySQL

Pull request:
feat: Add RBAC API for GetAllDomains
Fixes #971 
Adds an API to get all the domains.

Kindly review @hsluoyz and please tell if you have any suggestions.

Patch:
diff --git a/rbac/default-role-manager/role_manager.go b/rbac/default-role-manager/role_manager.go
index 27c0b10..94eb752 100644
--- a/rbac/default-role-manager/role_manager.go
+++ b/rbac/default-role-manager/role_manager.go
@@ -357,6 +357,16 @@ func (rm *RoleManager) GetDomains(name string) ([]string, error) {
 	return domains, nil
 }
 
+// GetAllDomains gets all domains
+func (rm *RoleManager) GetAllDomains() ([]string, error) {
+	var domains []string
+	rm.allDomains.Range(func(key, value interface{}) bool {
+		domains = append(domains, key.(string))
+		return true
+	})
+	return domains, nil
+}
+
 func (rm *RoleManager) hasAnyRole(name string, domain string) bool {
 	patternDomain := rm.getPatternDomain(domain)
 	for _, domain := range patternDomain {
diff --git a/rbac/role_manager.go b/rbac/role_manager.go
index 362f71c..9c5183c 100644
--- a/rbac/role_manager.go
+++ b/rbac/role_manager.go
@@ -38,6 +38,8 @@ type RoleManager interface {
 	GetUsers(name string, domain ...string) ([]string, error)
 	// GetDomains gets domains that a user has
 	GetDomains(name string) ([]string, error)
+	// GetAllDomains gets all domains
+	GetAllDomains() ([]string, error)
 	// PrintRoles prints all the roles to log.
 	PrintRoles() error
 	// SetLogger sets role manager's logger.
diff --git a/rbac_api_with_domains.go b/rbac_api_with_domains.go
index 356888e..874d07f 100644
--- a/rbac_api_with_domains.go
+++ b/rbac_api_with_domains.go
@@ -137,3 +137,8 @@ func (e *Enforcer) DeleteDomains(domains ...string) (bool, error) {
 	}
 	return true, nil
 }
+
+// GetAllDomains would get all domains.
+func (e *Enforcer) GetAllDomains() ([]string, error) {
+	return e.model["g"]["g"].RM.GetAllDomains()
+}


Test patch:
diff --git a/model_test.go b/model_test.go
index c0810f7..b8848f7 100644
--- a/model_test.go
+++ b/model_test.go
@@ -354,6 +354,9 @@ func (rm *testCustomRoleManager) GetUsers(name string, domain ...string) ([]stri
 func (rm *testCustomRoleManager) GetDomains(name string) ([]string, error) {
 	return []string{}, nil
 }
+func (rm *testCustomRoleManager) GetAllDomains() ([]string, error) {
+	return []string{}, nil
+}
 func (rm *testCustomRoleManager) PrintRoles() error           { return nil }
 func (rm *testCustomRoleManager) SetLogger(logger log.Logger) {}
 
diff --git a/rbac_api_with_domains_test.go b/rbac_api_with_domains_test.go
index fbbdb89..79cb9b0 100644
--- a/rbac_api_with_domains_test.go
+++ b/rbac_api_with_domains_test.go
@@ -250,3 +250,20 @@ func TestDeleteAllUsersByDomain(t *testing.T) {
 		{"alice", "admin", "domain1"},
 	})
 }
+
+// testGetAllDomains tests GetAllDomains()
+func testGetAllDomains(t *testing.T, e *Enforcer, res []string) {
+	t.Helper()
+	myRes, _ := e.GetAllDomains()
+	sort.Strings(myRes)
+	sort.Strings(res)
+	if !util.ArrayEquals(res, myRes) {
+		t.Error("domains: ", myRes, ", supposed to be ", res)
+	}
+}
+
+func TestGetAllDomains(t *testing.T) {
+	e, _ := NewEnforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv")
+
+	testGetAllDomains(t, e, []string{"domain1", "domain2"})
+}