mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
ent/privacy: move some of the generated privacy helpers to ent/privacy (#3527)
This commit is contained in:
@@ -35,9 +35,9 @@ var (
|
||||
)
|
||||
|
||||
{{- range $decision := list "Allow" "Deny" "Skip" }}
|
||||
// {{ $decision }}f returns an formatted wrapped {{ $decision }} decision.
|
||||
// {{ $decision }}f returns a formatted wrapped {{ $decision }} decision.
|
||||
func {{ $decision }}f(format string, a ...any) error {
|
||||
return fmt.Errorf(format+": %w", append(a, {{ $decision }})...)
|
||||
return privacy.{{ $decision }}f(format, a...)
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
@@ -67,6 +67,12 @@ type (
|
||||
MutationRule = privacy.MutationRule
|
||||
// MutationPolicy combines multiple mutation rules into a single policy.
|
||||
MutationPolicy = privacy.MutationPolicy
|
||||
// MutationRuleFunc type is an adapter which allows the use of
|
||||
// ordinary functions as mutation rules.
|
||||
MutationRuleFunc = privacy.MutationRuleFunc
|
||||
|
||||
// QueryMutationRule is an interface which groups query and mutation rules.
|
||||
QueryMutationRule = privacy.QueryMutationRule
|
||||
)
|
||||
|
||||
// QueryRuleFunc type is an adapter to allow the use of
|
||||
@@ -78,68 +84,24 @@ func (f QueryRuleFunc) EvalQuery(ctx context.Context, q {{ $pkg }}.Query) error
|
||||
return f(ctx, q)
|
||||
}
|
||||
|
||||
// MutationRuleFunc type is an adapter which allows the use of
|
||||
// ordinary functions as mutation rules.
|
||||
type MutationRuleFunc func(context.Context, {{ $pkg }}.Mutation) error
|
||||
|
||||
// EvalMutation returns f(ctx, m).
|
||||
func (f MutationRuleFunc) EvalMutation(ctx context.Context, m {{ $pkg }}.Mutation) error {
|
||||
return f(ctx, m)
|
||||
}
|
||||
|
||||
// QueryMutationRule is an interface which groups query and mutation rules.
|
||||
type QueryMutationRule interface {
|
||||
QueryRule
|
||||
MutationRule
|
||||
}
|
||||
|
||||
// AlwaysAllowRule returns a rule that returns an allow decision.
|
||||
func AlwaysAllowRule() QueryMutationRule {
|
||||
return fixedDecision{Allow}
|
||||
return privacy.AlwaysAllowRule()
|
||||
}
|
||||
|
||||
// AlwaysDenyRule returns a rule that returns a deny decision.
|
||||
func AlwaysDenyRule() QueryMutationRule {
|
||||
return fixedDecision{Deny}
|
||||
}
|
||||
|
||||
type fixedDecision struct {
|
||||
decision error
|
||||
}
|
||||
|
||||
func (f fixedDecision) EvalQuery(context.Context, {{ $pkg }}.Query) error {
|
||||
return f.decision
|
||||
}
|
||||
|
||||
func (f fixedDecision) EvalMutation(context.Context, {{ $pkg }}.Mutation) error {
|
||||
return f.decision
|
||||
}
|
||||
|
||||
type contextDecision struct {
|
||||
eval func(context.Context) error
|
||||
return privacy.AlwaysDenyRule()
|
||||
}
|
||||
|
||||
// ContextQueryMutationRule creates a query/mutation rule from a context eval func.
|
||||
func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule {
|
||||
return contextDecision{eval}
|
||||
}
|
||||
|
||||
func (c contextDecision) EvalQuery(ctx context.Context, _ {{ $pkg }}.Query) error {
|
||||
return c.eval(ctx)
|
||||
}
|
||||
|
||||
func (c contextDecision) EvalMutation(ctx context.Context, _ {{ $pkg }}.Mutation) error {
|
||||
return c.eval(ctx)
|
||||
return privacy.ContextQueryMutationRule(eval)
|
||||
}
|
||||
|
||||
// OnMutationOperation evaluates the given rule only on a given mutation operation.
|
||||
func OnMutationOperation(rule MutationRule, op {{ $pkg }}.Op) MutationRule {
|
||||
return MutationRuleFunc(func(ctx context.Context, m {{ $pkg }}.Mutation) error {
|
||||
if m.Op().Is(op) {
|
||||
return rule.EvalMutation(ctx, m)
|
||||
}
|
||||
return Skip
|
||||
})
|
||||
return privacy.OnMutationOperation(rule, op)
|
||||
}
|
||||
|
||||
// DenyMutationOperationRule returns a rule denying specified mutation operation.
|
||||
|
||||
Reference in New Issue
Block a user