Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
// Copyright © 2023 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package namespace
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/ory/herodot"
|
|
|
|
"github.com/ory/keto/internal/namespace/ast"
|
|
)
|
|
|
|
type (
|
|
Namespace struct {
|
|
// Deprecated: Only use the Name instead.
|
|
ID int32 `json:"id" db:"-" toml:"id"`
|
|
// The unique name of the namespace.
|
|
Name string `json:"name" db:"-" toml:"name"`
|
|
Config json.RawMessage `json:"config,omitempty" db:"-" toml:"config,omitempty"`
|
|
|
|
Relations []ast.Relation `json:"-" db:"-"`
|
|
}
|
|
Manager interface {
|
|
GetNamespaceByName(ctx context.Context, name string) (*Namespace, error)
|
|
// Deprecated: Use GetNamespaceByName instead.
|
|
GetNamespaceByConfigID(ctx context.Context, id int32) (*Namespace, error)
|
|
Namespaces(ctx context.Context) ([]*Namespace, error)
|
|
ShouldReload(newValue interface{}) bool
|
|
}
|
|
ManagerProvider interface {
|
|
NamespaceManager() (Manager, error)
|
|
}
|
|
)
|
|
|
|
func (ns *Namespace) FindRelation(name string) *ast.Relation {
|
|
if ns == nil || len(ns.Relations) == 0 {
|
|
return nil
|
|
}
|
|
for _, rel := range ns.Relations {
|
|
if rel.Name == name {
|
|
return &rel
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ASTRelationFor(ctx context.Context, m Manager, namespace, relation string) (*ast.Relation, error) {
|
|
// Special case: If the relationTuple's relation is empty, then it is not an
|
|
// error that the relation was not found.
|
|
if relation == "" {
|
|
return nil, nil
|
|
}
|
|
ns, err := m.GetNamespaceByName(ctx, namespace)
|
|
if err != nil {
|
|
// On an unknown namespace the answer should be "not allowed", not "not
|
|
// found". Therefore, we don't return the error here.
|
|
return nil, nil
|
|
}
|
|
|
|
// Special case: If Relations is empty, then there is no namespace
|
|
// configuration, and it is not an error that the relation was not found.
|
|
if len(ns.Relations) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
rel := ns.FindRelation(relation)
|
|
if rel == nil {
|
|
return nil, herodot.ErrBadRequest().WithReasonf("relation %q does not exist", relation)
|
|
}
|
|
return rel, nil
|
|
}
|