forked from owncloud/ocis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graph: Initial support for $filter in /users
This adds some initial support for using $filter (as defined in the odata spec) on the /users endpoint. Currently the only supported filter is a single filter on `id` property of the `memberOf` relation of users. To list all users that are members of a specific group: ``` curl 'https://localhost:9200/graph/v1.0/users?$filter=memberOf/any(m:m/id eq '262982c1-2362-4afa-bfdf-8cbfef64a06e') ``` Closes: owncloud#5487
- Loading branch information
Showing
6 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package svc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/CiscoM31/godata" | ||
libregraph "github.com/owncloud/libre-graph-api-go" | ||
) | ||
|
||
func (g Graph) applyUserFilter(ctx context.Context, req *godata.GoDataRequest) (users []*libregraph.User, err error) { | ||
logger := g.logger.SubloggerWithRequestID(ctx) | ||
logger.Debug().Str("filter", req.Query.Filter.RawValue).Msg("applying filter") | ||
|
||
switch req.Query.Filter.Tree.Token.Type { | ||
case godata.ExpressionTokenLambdaNav: | ||
return g.applyLambdaFilter(ctx, req, req.Query.Filter.Tree.Children) | ||
} | ||
return users, godata.NotImplementedError(fmt.Sprintf("Filter '%s' is not supported", req.Query.Filter.Tree.Token.Value)) | ||
} | ||
|
||
func (g Graph) applyLambdaFilter(ctx context.Context, req *godata.GoDataRequest, nodes []*godata.ParseNode) (users []*libregraph.User, err error) { | ||
logger := g.logger.SubloggerWithRequestID(ctx) | ||
// logger.Debug().Str("filter", nodes. ).Msg("applying filter") | ||
if len(nodes) != 2 { | ||
logger.Debug().Msg("unexpected filter length ") | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
// We only support memberOf/any queries for now | ||
if nodes[0].Token.Type != godata.ExpressionTokenLiteral || nodes[0].Token.Value != "memberOf" { | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
if nodes[1].Token.Type != godata.ExpressionTokenLambda || nodes[1].Token.Value != "any" { | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
return g.applyLambdaMemberOfAny(ctx, req, nodes[1].Children) | ||
} | ||
|
||
func (g Graph) applyLambdaMemberOfAny(ctx context.Context, req *godata.GoDataRequest, nodes []*godata.ParseNode) (users []*libregraph.User, err error) { | ||
logger := g.logger.SubloggerWithRequestID(ctx) | ||
if len(nodes) != 2 { | ||
logger.Debug().Msg("unexpected filter length ") | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
|
||
// First element is the "name" of the lambda function's parameter | ||
if nodes[0].Token.Type != godata.ExpressionTokenLiteral { | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
|
||
// We only support the 'eq' expression for now | ||
if nodes[1].Token.Type != godata.ExpressionTokenLogical && nodes[1].Token.Value != "eq" { | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
return g.applyMemberOfEq(ctx, req, nodes[1].Children) | ||
} | ||
|
||
func (g Graph) applyMemberOfEq(ctx context.Context, req *godata.GoDataRequest, nodes []*godata.ParseNode) (users []*libregraph.User, err error) { | ||
logger := g.logger.SubloggerWithRequestID(ctx) | ||
if len(nodes) != 2 { | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
|
||
if nodes[0].Token.Type != godata.ExpressionTokenNav { | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
|
||
if len(nodes[0].Children) != 2 { | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
|
||
filterProperty := nodes[0].Children[1].Token.Value | ||
var filterValue string | ||
switch nodes[1].Token.Type { | ||
case godata.ExpressionTokenGuid: | ||
filterValue = nodes[1].Token.Value | ||
case godata.ExpressionTokenString: | ||
// unquote | ||
filterValue = strings.Trim(nodes[1].Token.Value, "'") | ||
default: | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
logger.Debug().Str("property", filterProperty).Str("value", filterValue).Msg("Filtering memberOf") | ||
|
||
switch filterProperty { | ||
case "id": | ||
logger.Debug().Str("property", filterProperty).Str("value", filterValue).Msg("Filtering memberOf by group id") | ||
return g.identityBackend.GetGroupMembers(ctx, filterValue, req) | ||
default: | ||
return users, godata.BadRequestError("bad filter") | ||
} | ||
|
||
} |