Skip to content

Commit 1aba8b9

Browse files
committed
feat: add GetAllUsers() API to separate users from roles (#1652)
1 parent b190fce commit 1aba8b9

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

enforcer_interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type IEnforcer interface {
101101
GetAllNamedActions(ptype string) ([]string, error)
102102
GetAllRoles() ([]string, error)
103103
GetAllNamedRoles(ptype string) ([]string, error)
104+
GetAllUsers() ([]string, error)
104105
GetPolicy() ([][]string, error)
105106
GetFilteredPolicy(fieldIndex int, fieldValues ...string) ([][]string, error)
106107
GetNamedPolicy(ptype string) ([][]string, error)

enforcer_synced.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ func (e *SyncedEnforcer) GetAllNamedRoles(ptype string) ([]string, error) {
285285
return e.Enforcer.GetAllNamedRoles(ptype)
286286
}
287287

288+
// GetAllUsers gets the list of users that show up in the current policy.
289+
func (e *SyncedEnforcer) GetAllUsers() ([]string, error) {
290+
e.m.RLock()
291+
defer e.m.RUnlock()
292+
return e.Enforcer.GetAllUsers()
293+
}
294+
288295
// GetPolicy gets all the authorization rules in the policy.
289296
func (e *SyncedEnforcer) GetPolicy() ([][]string, error) {
290297
e.m.RLock()

management_api.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ func (e *Enforcer) GetAllNamedRoles(ptype string) ([]string, error) {
7676
return e.model.GetValuesForFieldInPolicy("g", ptype, 1)
7777
}
7878

79+
// GetAllUsers gets the list of users that show up in the current policy.
80+
// Users are subjects that are not roles (i.e., subjects that do not appear as the second element in any grouping policy).
81+
func (e *Enforcer) GetAllUsers() ([]string, error) {
82+
subjects, err := e.GetAllSubjects()
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
roles, err := e.GetAllRoles()
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
users := util.SetSubtract(subjects, roles)
93+
return users, nil
94+
}
95+
7996
// GetPolicy gets all the authorization rules in the policy.
8097
func (e *Enforcer) GetPolicy() ([][]string, error) {
8198
return e.GetNamedPolicy("p")

management_api_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestGetList(t *testing.T) {
4141
testStringList(t, "Objects", e.GetAllObjects, []string{"data1", "data2"})
4242
testStringList(t, "Actions", e.GetAllActions, []string{"read", "write"})
4343
testStringList(t, "Roles", e.GetAllRoles, []string{"data2_admin"})
44+
testStringList(t, "Users", e.GetAllUsers, []string{"alice", "bob"})
4445
}
4546

4647
func TestGetListWithDomains(t *testing.T) {
@@ -50,6 +51,7 @@ func TestGetListWithDomains(t *testing.T) {
5051
testStringList(t, "Objects", e.GetAllObjects, []string{"data1", "data2"})
5152
testStringList(t, "Actions", e.GetAllActions, []string{"read", "write"})
5253
testStringList(t, "Roles", e.GetAllRoles, []string{"admin"})
54+
testStringList(t, "Users", e.GetAllUsers, []string{})
5355
}
5456

5557
func testGetPolicy(t *testing.T, e *Enforcer, res [][]string) {

0 commit comments

Comments
 (0)