diff --git a/changelog/unreleased/filter-ns-users-groups.md b/changelog/unreleased/filter-ns-users-groups.md new file mode 100644 index 0000000000..7d7f15c19a --- /dev/null +++ b/changelog/unreleased/filter-ns-users-groups.md @@ -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 diff --git a/pkg/cbox/group/rest/rest.go b/pkg/cbox/group/rest/rest.go index 63b9cab945..2e93809d60 100644 --- a/pkg/cbox/group/rest/rest.go +++ b/pkg/cbox/group/rest/rest.go @@ -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, "@") diff --git a/pkg/cbox/user/rest/rest.go b/pkg/cbox/user/rest/rest.go index ecd854b3b6..55e445967d 100644 --- a/pkg/cbox/user/rest/rest.go +++ b/pkg/cbox/user/rest/rest.go @@ -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): @@ -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)