privacy.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Code generated by entc, DO NOT EDIT.
  2. package privacy
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "code.osinet.fr/fgm/entdemo/ent"
  8. )
  9. var (
  10. // Allow may be returned by rules to indicate that the policy
  11. // evaluation should terminate with an allow decision.
  12. Allow = errors.New("ent/privacy: allow rule")
  13. // Deny may be returned by rules to indicate that the policy
  14. // evaluation should terminate with an deny decision.
  15. Deny = errors.New("ent/privacy: deny rule")
  16. // Skip may be returned by rules to indicate that the policy
  17. // evaluation should continue to the next rule.
  18. Skip = errors.New("ent/privacy: skip rule")
  19. )
  20. // Allowf returns an formatted wrapped Allow decision.
  21. func Allowf(format string, a ...interface{}) error {
  22. return fmt.Errorf(format+": %w", append(a, Allow)...)
  23. }
  24. // Denyf returns an formatted wrapped Deny decision.
  25. func Denyf(format string, a ...interface{}) error {
  26. return fmt.Errorf(format+": %w", append(a, Deny)...)
  27. }
  28. // Skipf returns an formatted wrapped Skip decision.
  29. func Skipf(format string, a ...interface{}) error {
  30. return fmt.Errorf(format+": %w", append(a, Skip)...)
  31. }
  32. type (
  33. // QueryPolicy combines multiple query rules into a single policy.
  34. QueryPolicy []QueryRule
  35. // QueryRule defines the interface deciding whether a
  36. // query is allowed and optionally modify it.
  37. QueryRule interface {
  38. EvalQuery(context.Context, ent.Query) error
  39. }
  40. )
  41. // EvalQuery evaluates a query against a query policy.
  42. func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error {
  43. for _, rule := range policy {
  44. switch err := rule.EvalQuery(ctx, q); {
  45. case err == nil || errors.Is(err, Skip):
  46. case errors.Is(err, Allow):
  47. return nil
  48. default:
  49. return err
  50. }
  51. }
  52. return nil
  53. }
  54. // QueryRuleFunc type is an adapter to allow the use of
  55. // ordinary functions as query rules.
  56. type QueryRuleFunc func(context.Context, ent.Query) error
  57. // Eval returns f(ctx, q).
  58. func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
  59. return f(ctx, q)
  60. }
  61. type (
  62. // MutationPolicy combines multiple mutation rules into a single policy.
  63. MutationPolicy []MutationRule
  64. // MutationRule defines the interface deciding whether a
  65. // mutation is allowed and optionally modify it.
  66. MutationRule interface {
  67. EvalMutation(context.Context, ent.Mutation) error
  68. }
  69. )
  70. // EvalMutation evaluates a mutation against a mutation policy.
  71. func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error {
  72. for _, rule := range policy {
  73. switch err := rule.EvalMutation(ctx, m); {
  74. case err == nil || errors.Is(err, Skip):
  75. case errors.Is(err, Allow):
  76. return nil
  77. default:
  78. return err
  79. }
  80. }
  81. return nil
  82. }
  83. // MutationRuleFunc type is an adapter to allow the use of
  84. // ordinary functions as mutation rules.
  85. type MutationRuleFunc func(context.Context, ent.Mutation) error
  86. // EvalMutation returns f(ctx, m).
  87. func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
  88. return f(ctx, m)
  89. }
  90. // Policy groups query and mutation policies.
  91. type Policy struct {
  92. Query QueryPolicy
  93. Mutation MutationPolicy
  94. }
  95. // EvalQuery forwards evaluation to query policy.
  96. func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error {
  97. return policy.Query.EvalQuery(ctx, q)
  98. }
  99. // EvalMutation forwards evaluation to mutation policy.
  100. func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error {
  101. return policy.Mutation.EvalMutation(ctx, m)
  102. }
  103. // QueryMutationRule is the interface that groups query and mutation rules.
  104. type QueryMutationRule interface {
  105. QueryRule
  106. MutationRule
  107. }
  108. // AlwaysAllowRule returns a rule that returns an allow decision.
  109. func AlwaysAllowRule() QueryMutationRule {
  110. return fixedDecisionRule{Allow}
  111. }
  112. // AlwaysDenyRule returns a rule that returns a deny decision.
  113. func AlwaysDenyRule() QueryMutationRule {
  114. return fixedDecisionRule{Deny}
  115. }
  116. type fixedDecisionRule struct{ err error }
  117. func (f fixedDecisionRule) EvalQuery(context.Context, ent.Query) error { return f.err }
  118. func (f fixedDecisionRule) EvalMutation(context.Context, ent.Mutation) error { return f.err }
  119. // The CarQueryRuleFunc type is an adapter to allow the use of ordinary
  120. // functions as a query rule.
  121. type CarQueryRuleFunc func(context.Context, *ent.CarQuery) error
  122. // EvalQuery return f(ctx, q).
  123. func (f CarQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
  124. if q, ok := q.(*ent.CarQuery); ok {
  125. return f(ctx, q)
  126. }
  127. return Denyf("ent/privacy: unexpected query type %T, expect *ent.CarQuery", q)
  128. }
  129. // The CarMutationRuleFunc type is an adapter to allow the use of ordinary
  130. // functions as a mutation rule.
  131. type CarMutationRuleFunc func(context.Context, *ent.CarMutation) error
  132. // EvalMutation calls f(ctx, m).
  133. func (f CarMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
  134. if m, ok := m.(*ent.CarMutation); ok {
  135. return f(ctx, m)
  136. }
  137. return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CarMutation", m)
  138. }
  139. // The GroupQueryRuleFunc type is an adapter to allow the use of ordinary
  140. // functions as a query rule.
  141. type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error
  142. // EvalQuery return f(ctx, q).
  143. func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
  144. if q, ok := q.(*ent.GroupQuery); ok {
  145. return f(ctx, q)
  146. }
  147. return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q)
  148. }
  149. // The GroupMutationRuleFunc type is an adapter to allow the use of ordinary
  150. // functions as a mutation rule.
  151. type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error
  152. // EvalMutation calls f(ctx, m).
  153. func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
  154. if m, ok := m.(*ent.GroupMutation); ok {
  155. return f(ctx, m)
  156. }
  157. return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m)
  158. }
  159. // The UserQueryRuleFunc type is an adapter to allow the use of ordinary
  160. // functions as a query rule.
  161. type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error
  162. // EvalQuery return f(ctx, q).
  163. func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
  164. if q, ok := q.(*ent.UserQuery); ok {
  165. return f(ctx, q)
  166. }
  167. return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q)
  168. }
  169. // The UserMutationRuleFunc type is an adapter to allow the use of ordinary
  170. // functions as a mutation rule.
  171. type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error
  172. // EvalMutation calls f(ctx, m).
  173. func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
  174. if m, ok := m.(*ent.UserMutation); ok {
  175. return f(ctx, m)
  176. }
  177. return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m)
  178. }