Skip to content

Commit

Permalink
feat: context on list requests (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: Sarah Funkhouser <[email protected]>
  • Loading branch information
golanglemonade authored Feb 18, 2025
1 parent fc18212 commit 682ffb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
2 changes: 2 additions & 0 deletions auth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type AuthenticatedUser struct {
SubjectID string
// SubjectName is the name of the authenticated user
SubjectName string
// SubjectEmail is the email of the authenticated user
SubjectEmail string
// OrganizationID is the organization ID of the authenticated user
OrganizationID string
// OrganizationName is the name of the organization the user is authenticated to
Expand Down
39 changes: 18 additions & 21 deletions fgax/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fgax
import (
"context"
"fmt"
"strings"

fgasdk "github.com/openfga/go-sdk"
ofgaclient "github.com/openfga/go-sdk/client"
Expand All @@ -21,6 +22,8 @@ type ListRequest struct {
SubjectType string
// Relation is the relationship between the subject and object
Relation string
// ConditionContext for the relationship
ConditionContext *map[string]any
}

// ListObjectsRequest creates the ClientListObjectsRequest and queries the FGA store for all objects with the user+relation
Expand All @@ -38,13 +41,14 @@ func (c *Client) ListObjectsRequest(ctx context.Context, req ListRequest) (*ofga
listReq := ofgaclient.ClientListObjectsRequest{
User: sub.String(),
Relation: req.Relation,
Type: req.ObjectType,
Type: strings.ToLower(req.ObjectType),
}

log.Debug().Str("relation", req.Relation).
Str("subject", sub.String()).
Str("type", req.ObjectType).
Msg("listing objects")
if req.ConditionContext != nil {
listReq.Context = req.ConditionContext
}

log.Debug().Interface("request", listReq).Msg("listing objects")

return c.listObjects(ctx, listReq)
}
Expand All @@ -57,21 +61,22 @@ func (c *Client) ListUserRequest(ctx context.Context, req ListRequest) (*ofgacli

// create the fga object
obj := fgasdk.FgaObject{
Type: req.ObjectType,
Type: strings.ToLower(req.ObjectType),
Id: req.ObjectID,
}

// compose the list request
listReq := ofgaclient.ClientListUsersRequest{
Object: obj,
Relation: req.Relation,
UserFilters: []fgasdk.UserTypeFilter{{Type: req.SubjectType}},
UserFilters: []fgasdk.UserTypeFilter{{Type: strings.ToLower(req.SubjectType)}},
}

if req.ConditionContext != nil {
listReq.Context = req.ConditionContext
}

log.Debug().Str("relation", req.Relation).
Str("object", obj.Id).
Str("type", obj.Type).
Msg("listing users")
log.Debug().Interface("request", listReq).Msg("listing users")

return c.listUsers(ctx, listReq)
}
Expand All @@ -80,11 +85,7 @@ func (c *Client) ListUserRequest(ctx context.Context, req ListRequest) (*ofgacli
func (c *Client) listObjects(ctx context.Context, req ofgaclient.ClientListObjectsRequest) (*ofgaclient.ClientListObjectsResponse, error) {
list, err := c.Ofga.ListObjects(ctx).Body(req).Execute()
if err != nil {
log.Error().Err(err).
Str("user", req.User).
Str("relation", req.Relation).
Str("type", req.Type).
Msg("error listing objects")
log.Error().Err(err).Interface("request", req).Msg("error listing objects")

return nil, err
}
Expand All @@ -96,11 +97,7 @@ func (c *Client) listObjects(ctx context.Context, req ofgaclient.ClientListObjec
func (c *Client) listUsers(ctx context.Context, req ofgaclient.ClientListUsersRequest) (*ofgaclient.ClientListUsersResponse, error) {
list, err := c.Ofga.ListUsers(ctx).Body(req).Execute()
if err != nil {
log.Error().Err(err).
Str("object", req.Object.Id).
Str("type", req.Object.Type).
Str("relation", req.Relation).
Msg("error listing users")
log.Error().Err(err).Interface("request", req).Msg("error listing users")

return nil, err
}
Expand Down

0 comments on commit 682ffb3

Please sign in to comment.