Files
schemas/graph/resolver.go
T

58 lines
1.6 KiB
Go
Raw Normal View History

2022-10-09 15:23:52 +02:00
package graph
import (
2022-12-17 14:36:42 +01:00
"context"
2023-04-27 07:09:10 +02:00
"fmt"
2022-12-17 14:36:42 +01:00
2022-10-09 15:23:52 +02:00
"github.com/apex/log"
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
"gitlab.com/unboundsoftware/schemas/cache"
"gitlab.com/unboundsoftware/schemas/middleware"
2022-10-09 15:23:52 +02:00
)
//go:generate go run github.com/99designs/gqlgen
// This file will not be regenerated automatically.
//
// It serves as dependency injection for your app, add any dependencies you require here.
type Publisher interface {
2022-12-17 14:36:42 +01:00
Publish(ctx context.Context, event eventsourced.Event) error
2022-10-09 15:23:52 +02:00
}
type Resolver struct {
EventStore eventsourced.EventStore
Publisher Publisher
Logger log.Interface
Cache *cache.Cache
}
func (r *Resolver) apiKeyCanAccessRef(ctx context.Context, ref string, publish bool) (string, error) {
key, err := middleware.ApiKeyFromContext(ctx)
if err != nil {
return "", err
}
apiKey := r.Cache.ApiKeyByKey(key)
if publish && !apiKey.Publish {
return "", fmt.Errorf("provided API-key doesn't have publish privilege")
}
if !publish && !apiKey.Read {
return "", fmt.Errorf("provided API-key doesn't have read privilege")
}
for _, rr := range apiKey.Refs {
if rr == ref {
return apiKey.Name, nil
}
}
return "", fmt.Errorf("provided API-key doesn't have the required privilege on the requested Schema Ref")
}
2022-12-17 14:36:42 +01:00
func (r *Resolver) handler(ctx context.Context, aggregate eventsourced.Aggregate) (eventsourced.CommandHandler, error) {
return eventsourced.NewHandler(ctx, aggregate, r.EventStore, eventsourced.WithEventPublisher(r.Publisher))
2022-10-09 15:23:52 +02:00
}
2023-04-27 07:09:10 +02:00
func apiKeyId(orgId, name string) string {
return fmt.Sprintf("%s-%s", orgId, name)
}