Skip to content

Commit

Permalink
Add "a" and "l" filters in grappa (#1887)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 authored Jul 13, 2021
1 parent 4cf9aac commit a87aa93
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions changelog/unreleased/filter-ns-users-groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Enhancement: Add "a" and "l" filter for grappa queries

This PR adds the namespace filters "a" and "l" for grappa queries.
With no filter will look into primary and e-groups, with "a" will look
into primary/secondary/service/e-groups and with "l" will look into
lightweight accounts.


https://github.com/cs3org/reva/pull/1887
https://github.com/cs3org/reva/issues/1773
15 changes: 15 additions & 0 deletions pkg/cbox/group/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,21 @@ func (m *manager) findGroupsByFilter(ctx context.Context, url string, groups map
}

func (m *manager) FindGroups(ctx context.Context, query string) ([]*grouppb.Group, error) {

// Look at namespaces filters. If the query starts with:
// "a" or none => get egroups
// other filters => get empty list

parts := strings.SplitN(query, ":", 2)

if len(parts) == 2 {
if parts[0] == "a" {
query = parts[1]
} else {
return []*grouppb.Group{}, nil
}
}

filters := []string{"groupIdentifier"}
if emailRegex.MatchString(query) {
parts := strings.Split(query, "@")
Expand Down
40 changes: 38 additions & 2 deletions pkg/cbox/user/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ func (m *manager) findUsersByFilter(ctx context.Context, url string, users map[s

func (m *manager) FindUsers(ctx context.Context, query string) ([]*userpb.User, error) {

// Look at namespaces filters. If the query starts with:
// "a" => look into primary/secondary/service accounts
// "l" => look into lightweight accounts
// none => look into primary

parts := strings.SplitN(query, ":", 2)

var namespace string
if len(parts) == 2 {
// the query contains a namespace filter
namespace, query = parts[0], parts[1]
}

var filters []string
switch {
case usernameRegex.MatchString(query):
Expand All @@ -326,13 +339,36 @@ func (m *manager) FindUsers(ctx context.Context, query string) ([]*userpb.User,
}

userSlice := []*userpb.User{}
for _, v := range users {
userSlice = append(userSlice, v)

var accountsFilters []userpb.UserType
switch namespace {
case "":
accountsFilters = []userpb.UserType{userpb.UserType_USER_TYPE_PRIMARY}
case "a":
accountsFilters = []userpb.UserType{userpb.UserType_USER_TYPE_PRIMARY, userpb.UserType_USER_TYPE_SECONDARY, userpb.UserType_USER_TYPE_SERVICE}
case "l":
accountsFilters = []userpb.UserType{userpb.UserType_USER_TYPE_LIGHTWEIGHT}
}

for _, u := range users {
if isUserAnyType(u, accountsFilters) {
userSlice = append(userSlice, u)
}
}

return userSlice, nil
}

// isUserAnyType returns true if the user's type is one of types list
func isUserAnyType(user *userpb.User, types []userpb.UserType) bool {
for _, t := range types {
if user.GetId().Type == t {
return true
}
}
return false
}

func (m *manager) GetUserGroups(ctx context.Context, uid *userpb.UserId) ([]string, error) {

groups, err := m.fetchCachedUserGroups(uid)
Expand Down

0 comments on commit a87aa93

Please sign in to comment.