github.com/openfga/openfga@v1.5.4-rc1/pkg/server/commands/read_authzmodels.go (about) 1 package commands 2 3 import ( 4 "context" 5 6 openfgav1 "github.com/openfga/api/proto/openfga/v1" 7 8 "github.com/openfga/openfga/pkg/encoder" 9 "github.com/openfga/openfga/pkg/logger" 10 serverErrors "github.com/openfga/openfga/pkg/server/errors" 11 "github.com/openfga/openfga/pkg/storage" 12 ) 13 14 type ReadAuthorizationModelsQuery struct { 15 backend storage.AuthorizationModelReadBackend 16 logger logger.Logger 17 encoder encoder.Encoder 18 } 19 20 type ReadAuthModelsQueryOption func(*ReadAuthorizationModelsQuery) 21 22 func WithReadAuthModelsQueryLogger(l logger.Logger) ReadAuthModelsQueryOption { 23 return func(rm *ReadAuthorizationModelsQuery) { 24 rm.logger = l 25 } 26 } 27 28 func WithReadAuthModelsQueryEncoder(e encoder.Encoder) ReadAuthModelsQueryOption { 29 return func(rm *ReadAuthorizationModelsQuery) { 30 rm.encoder = e 31 } 32 } 33 34 func NewReadAuthorizationModelsQuery(backend storage.AuthorizationModelReadBackend, opts ...ReadAuthModelsQueryOption) *ReadAuthorizationModelsQuery { 35 rm := &ReadAuthorizationModelsQuery{ 36 backend: backend, 37 logger: logger.NewNoopLogger(), 38 encoder: encoder.NewBase64Encoder(), 39 } 40 41 for _, opt := range opts { 42 opt(rm) 43 } 44 return rm 45 } 46 47 func (q *ReadAuthorizationModelsQuery) Execute(ctx context.Context, req *openfgav1.ReadAuthorizationModelsRequest) (*openfgav1.ReadAuthorizationModelsResponse, error) { 48 decodedContToken, err := q.encoder.Decode(req.GetContinuationToken()) 49 if err != nil { 50 return nil, serverErrors.InvalidContinuationToken 51 } 52 53 paginationOptions := storage.NewPaginationOptions(req.GetPageSize().GetValue(), string(decodedContToken)) 54 55 models, contToken, err := q.backend.ReadAuthorizationModels(ctx, req.GetStoreId(), paginationOptions) 56 if err != nil { 57 return nil, serverErrors.HandleError("", err) 58 } 59 60 encodedContToken, err := q.encoder.Encode(contToken) 61 if err != nil { 62 return nil, serverErrors.HandleError("", err) 63 } 64 65 resp := &openfgav1.ReadAuthorizationModelsResponse{ 66 AuthorizationModels: models, 67 ContinuationToken: encodedContToken, 68 } 69 return resp, nil 70 }