123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- // Code generated by entc, DO NOT EDIT.
- package privacy
- import (
- "context"
- "errors"
- "fmt"
- "code.osinet.fr/fgm/entdemo/ent"
- )
- var (
- // Allow may be returned by rules to indicate that the policy
- // evaluation should terminate with an allow decision.
- Allow = errors.New("ent/privacy: allow rule")
- // Deny may be returned by rules to indicate that the policy
- // evaluation should terminate with an deny decision.
- Deny = errors.New("ent/privacy: deny rule")
- // Skip may be returned by rules to indicate that the policy
- // evaluation should continue to the next rule.
- Skip = errors.New("ent/privacy: skip rule")
- )
- // Allowf returns an formatted wrapped Allow decision.
- func Allowf(format string, a ...interface{}) error {
- return fmt.Errorf(format+": %w", append(a, Allow)...)
- }
- // Denyf returns an formatted wrapped Deny decision.
- func Denyf(format string, a ...interface{}) error {
- return fmt.Errorf(format+": %w", append(a, Deny)...)
- }
- // Skipf returns an formatted wrapped Skip decision.
- func Skipf(format string, a ...interface{}) error {
- return fmt.Errorf(format+": %w", append(a, Skip)...)
- }
- type (
- // QueryPolicy combines multiple query rules into a single policy.
- QueryPolicy []QueryRule
- // QueryRule defines the interface deciding whether a
- // query is allowed and optionally modify it.
- QueryRule interface {
- EvalQuery(context.Context, ent.Query) error
- }
- )
- // EvalQuery evaluates a query against a query policy.
- func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error {
- for _, rule := range policy {
- switch err := rule.EvalQuery(ctx, q); {
- case err == nil || errors.Is(err, Skip):
- case errors.Is(err, Allow):
- return nil
- default:
- return err
- }
- }
- return nil
- }
- // QueryRuleFunc type is an adapter to allow the use of
- // ordinary functions as query rules.
- type QueryRuleFunc func(context.Context, ent.Query) error
- // Eval returns f(ctx, q).
- func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
- return f(ctx, q)
- }
- type (
- // MutationPolicy combines multiple mutation rules into a single policy.
- MutationPolicy []MutationRule
- // MutationRule defines the interface deciding whether a
- // mutation is allowed and optionally modify it.
- MutationRule interface {
- EvalMutation(context.Context, ent.Mutation) error
- }
- )
- // EvalMutation evaluates a mutation against a mutation policy.
- func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error {
- for _, rule := range policy {
- switch err := rule.EvalMutation(ctx, m); {
- case err == nil || errors.Is(err, Skip):
- case errors.Is(err, Allow):
- return nil
- default:
- return err
- }
- }
- return nil
- }
- // MutationRuleFunc type is an adapter to allow the use of
- // ordinary functions as mutation rules.
- type MutationRuleFunc func(context.Context, ent.Mutation) error
- // EvalMutation returns f(ctx, m).
- func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
- return f(ctx, m)
- }
- // Policy groups query and mutation policies.
- type Policy struct {
- Query QueryPolicy
- Mutation MutationPolicy
- }
- // EvalQuery forwards evaluation to query policy.
- func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error {
- return policy.Query.EvalQuery(ctx, q)
- }
- // EvalMutation forwards evaluation to mutation policy.
- func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error {
- return policy.Mutation.EvalMutation(ctx, m)
- }
- // QueryMutationRule is the interface that groups query and mutation rules.
- type QueryMutationRule interface {
- QueryRule
- MutationRule
- }
- // AlwaysAllowRule returns a rule that returns an allow decision.
- func AlwaysAllowRule() QueryMutationRule {
- return fixedDecisionRule{Allow}
- }
- // AlwaysDenyRule returns a rule that returns a deny decision.
- func AlwaysDenyRule() QueryMutationRule {
- return fixedDecisionRule{Deny}
- }
- type fixedDecisionRule struct{ err error }
- func (f fixedDecisionRule) EvalQuery(context.Context, ent.Query) error { return f.err }
- func (f fixedDecisionRule) EvalMutation(context.Context, ent.Mutation) error { return f.err }
- // The CarQueryRuleFunc type is an adapter to allow the use of ordinary
- // functions as a query rule.
- type CarQueryRuleFunc func(context.Context, *ent.CarQuery) error
- // EvalQuery return f(ctx, q).
- func (f CarQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
- if q, ok := q.(*ent.CarQuery); ok {
- return f(ctx, q)
- }
- return Denyf("ent/privacy: unexpected query type %T, expect *ent.CarQuery", q)
- }
- // The CarMutationRuleFunc type is an adapter to allow the use of ordinary
- // functions as a mutation rule.
- type CarMutationRuleFunc func(context.Context, *ent.CarMutation) error
- // EvalMutation calls f(ctx, m).
- func (f CarMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
- if m, ok := m.(*ent.CarMutation); ok {
- return f(ctx, m)
- }
- return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CarMutation", m)
- }
- // The GroupQueryRuleFunc type is an adapter to allow the use of ordinary
- // functions as a query rule.
- type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error
- // EvalQuery return f(ctx, q).
- func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
- if q, ok := q.(*ent.GroupQuery); ok {
- return f(ctx, q)
- }
- return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q)
- }
- // The GroupMutationRuleFunc type is an adapter to allow the use of ordinary
- // functions as a mutation rule.
- type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error
- // EvalMutation calls f(ctx, m).
- func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
- if m, ok := m.(*ent.GroupMutation); ok {
- return f(ctx, m)
- }
- return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m)
- }
- // The UserQueryRuleFunc type is an adapter to allow the use of ordinary
- // functions as a query rule.
- type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error
- // EvalQuery return f(ctx, q).
- func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
- if q, ok := q.(*ent.UserQuery); ok {
- return f(ctx, q)
- }
- return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q)
- }
- // The UserMutationRuleFunc type is an adapter to allow the use of ordinary
- // functions as a mutation rule.
- type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error
- // EvalMutation calls f(ctx, m).
- func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
- if m, ok := m.(*ent.UserMutation); ok {
- return f(ctx, m)
- }
- return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m)
- }
|