-
Notifications
You must be signed in to change notification settings - Fork 0
/
privacy.go
510 lines (419 loc) · 19 KB
/
privacy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Code generated by ent, DO NOT EDIT.
package privacy
import (
"context"
"fmt"
"github.com/fogo-sh/grackdb/ent"
"entgo.io/ent/privacy"
)
var (
// Allow may be returned by rules to indicate that the policy
// evaluation should terminate with allow decision.
Allow = privacy.Allow
// Deny may be returned by rules to indicate that the policy
// evaluation should terminate with deny decision.
Deny = privacy.Deny
// Skip may be returned by rules to indicate that the policy
// evaluation should continue to the next rule.
Skip = privacy.Skip
)
// 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)...)
}
// DecisionContext creates a new context from the given parent context with
// a policy decision attach to it.
func DecisionContext(parent context.Context, decision error) context.Context {
return privacy.DecisionContext(parent, decision)
}
// DecisionFromContext retrieves the policy decision from the context.
func DecisionFromContext(ctx context.Context) (error, bool) {
return privacy.DecisionFromContext(ctx)
}
type (
// Policy groups query and mutation policies.
Policy = privacy.Policy
// QueryRule defines the interface deciding whether a
// query is allowed and optionally modify it.
QueryRule = privacy.QueryRule
// QueryPolicy combines multiple query rules into a single policy.
QueryPolicy = privacy.QueryPolicy
// MutationRule defines the interface which decides whether a
// mutation is allowed and optionally modifies it.
MutationRule = privacy.MutationRule
// MutationPolicy combines multiple mutation rules into a single policy.
MutationPolicy = privacy.MutationPolicy
)
// 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)
}
// MutationRuleFunc type is an adapter which allows 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)
}
// 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}
}
// 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, ent.Query) error {
return f.decision
}
func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error {
return f.decision
}
type contextDecision struct {
eval func(context.Context) error
}
// 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, _ ent.Query) error {
return c.eval(ctx)
}
func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error {
return c.eval(ctx)
}
// OnMutationOperation evaluates the given rule only on a given mutation operation.
func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule {
return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error {
if m.Op().Is(op) {
return rule.EvalMutation(ctx, m)
}
return Skip
})
}
// DenyMutationOperationRule returns a rule denying specified mutation operation.
func DenyMutationOperationRule(op ent.Op) MutationRule {
rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error {
return Denyf("ent/privacy: operation %s is not allowed", m.Op())
})
return OnMutationOperation(rule, op)
}
// The DiscordAccountQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type DiscordAccountQueryRuleFunc func(context.Context, *ent.DiscordAccountQuery) error
// EvalQuery return f(ctx, q).
func (f DiscordAccountQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.DiscordAccountQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.DiscordAccountQuery", q)
}
// The DiscordAccountMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type DiscordAccountMutationRuleFunc func(context.Context, *ent.DiscordAccountMutation) error
// EvalMutation calls f(ctx, m).
func (f DiscordAccountMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.DiscordAccountMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.DiscordAccountMutation", m)
}
// The DiscordBotQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type DiscordBotQueryRuleFunc func(context.Context, *ent.DiscordBotQuery) error
// EvalQuery return f(ctx, q).
func (f DiscordBotQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.DiscordBotQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.DiscordBotQuery", q)
}
// The DiscordBotMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type DiscordBotMutationRuleFunc func(context.Context, *ent.DiscordBotMutation) error
// EvalMutation calls f(ctx, m).
func (f DiscordBotMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.DiscordBotMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.DiscordBotMutation", m)
}
// The GithubAccountQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type GithubAccountQueryRuleFunc func(context.Context, *ent.GithubAccountQuery) error
// EvalQuery return f(ctx, q).
func (f GithubAccountQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.GithubAccountQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.GithubAccountQuery", q)
}
// The GithubAccountMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type GithubAccountMutationRuleFunc func(context.Context, *ent.GithubAccountMutation) error
// EvalMutation calls f(ctx, m).
func (f GithubAccountMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.GithubAccountMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GithubAccountMutation", m)
}
// The GithubOrganizationQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type GithubOrganizationQueryRuleFunc func(context.Context, *ent.GithubOrganizationQuery) error
// EvalQuery return f(ctx, q).
func (f GithubOrganizationQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.GithubOrganizationQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.GithubOrganizationQuery", q)
}
// The GithubOrganizationMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type GithubOrganizationMutationRuleFunc func(context.Context, *ent.GithubOrganizationMutation) error
// EvalMutation calls f(ctx, m).
func (f GithubOrganizationMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.GithubOrganizationMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GithubOrganizationMutation", m)
}
// The GithubOrganizationMemberQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type GithubOrganizationMemberQueryRuleFunc func(context.Context, *ent.GithubOrganizationMemberQuery) error
// EvalQuery return f(ctx, q).
func (f GithubOrganizationMemberQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.GithubOrganizationMemberQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.GithubOrganizationMemberQuery", q)
}
// The GithubOrganizationMemberMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type GithubOrganizationMemberMutationRuleFunc func(context.Context, *ent.GithubOrganizationMemberMutation) error
// EvalMutation calls f(ctx, m).
func (f GithubOrganizationMemberMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.GithubOrganizationMemberMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GithubOrganizationMemberMutation", m)
}
// The ProjectQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type ProjectQueryRuleFunc func(context.Context, *ent.ProjectQuery) error
// EvalQuery return f(ctx, q).
func (f ProjectQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.ProjectQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.ProjectQuery", q)
}
// The ProjectMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type ProjectMutationRuleFunc func(context.Context, *ent.ProjectMutation) error
// EvalMutation calls f(ctx, m).
func (f ProjectMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.ProjectMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.ProjectMutation", m)
}
// The ProjectAssociationQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type ProjectAssociationQueryRuleFunc func(context.Context, *ent.ProjectAssociationQuery) error
// EvalQuery return f(ctx, q).
func (f ProjectAssociationQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.ProjectAssociationQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.ProjectAssociationQuery", q)
}
// The ProjectAssociationMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type ProjectAssociationMutationRuleFunc func(context.Context, *ent.ProjectAssociationMutation) error
// EvalMutation calls f(ctx, m).
func (f ProjectAssociationMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.ProjectAssociationMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.ProjectAssociationMutation", m)
}
// The ProjectContributorQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type ProjectContributorQueryRuleFunc func(context.Context, *ent.ProjectContributorQuery) error
// EvalQuery return f(ctx, q).
func (f ProjectContributorQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.ProjectContributorQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.ProjectContributorQuery", q)
}
// The ProjectContributorMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type ProjectContributorMutationRuleFunc func(context.Context, *ent.ProjectContributorMutation) error
// EvalMutation calls f(ctx, m).
func (f ProjectContributorMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.ProjectContributorMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.ProjectContributorMutation", m)
}
// The ProjectTechnologyQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type ProjectTechnologyQueryRuleFunc func(context.Context, *ent.ProjectTechnologyQuery) error
// EvalQuery return f(ctx, q).
func (f ProjectTechnologyQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.ProjectTechnologyQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.ProjectTechnologyQuery", q)
}
// The ProjectTechnologyMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type ProjectTechnologyMutationRuleFunc func(context.Context, *ent.ProjectTechnologyMutation) error
// EvalMutation calls f(ctx, m).
func (f ProjectTechnologyMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.ProjectTechnologyMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.ProjectTechnologyMutation", m)
}
// The RepositoryQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type RepositoryQueryRuleFunc func(context.Context, *ent.RepositoryQuery) error
// EvalQuery return f(ctx, q).
func (f RepositoryQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.RepositoryQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.RepositoryQuery", q)
}
// The RepositoryMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type RepositoryMutationRuleFunc func(context.Context, *ent.RepositoryMutation) error
// EvalMutation calls f(ctx, m).
func (f RepositoryMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.RepositoryMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.RepositoryMutation", m)
}
// The RepositoryTechnologyQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type RepositoryTechnologyQueryRuleFunc func(context.Context, *ent.RepositoryTechnologyQuery) error
// EvalQuery return f(ctx, q).
func (f RepositoryTechnologyQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.RepositoryTechnologyQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.RepositoryTechnologyQuery", q)
}
// The RepositoryTechnologyMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type RepositoryTechnologyMutationRuleFunc func(context.Context, *ent.RepositoryTechnologyMutation) error
// EvalMutation calls f(ctx, m).
func (f RepositoryTechnologyMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.RepositoryTechnologyMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.RepositoryTechnologyMutation", m)
}
// The SiteQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type SiteQueryRuleFunc func(context.Context, *ent.SiteQuery) error
// EvalQuery return f(ctx, q).
func (f SiteQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.SiteQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.SiteQuery", q)
}
// The SiteMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type SiteMutationRuleFunc func(context.Context, *ent.SiteMutation) error
// EvalMutation calls f(ctx, m).
func (f SiteMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.SiteMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.SiteMutation", m)
}
// The TechnologyQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type TechnologyQueryRuleFunc func(context.Context, *ent.TechnologyQuery) error
// EvalQuery return f(ctx, q).
func (f TechnologyQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.TechnologyQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.TechnologyQuery", q)
}
// The TechnologyMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type TechnologyMutationRuleFunc func(context.Context, *ent.TechnologyMutation) error
// EvalMutation calls f(ctx, m).
func (f TechnologyMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.TechnologyMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.TechnologyMutation", m)
}
// The TechnologyAssociationQueryRuleFunc type is an adapter to allow the use of ordinary
// functions as a query rule.
type TechnologyAssociationQueryRuleFunc func(context.Context, *ent.TechnologyAssociationQuery) error
// EvalQuery return f(ctx, q).
func (f TechnologyAssociationQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error {
if q, ok := q.(*ent.TechnologyAssociationQuery); ok {
return f(ctx, q)
}
return Denyf("ent/privacy: unexpected query type %T, expect *ent.TechnologyAssociationQuery", q)
}
// The TechnologyAssociationMutationRuleFunc type is an adapter to allow the use of ordinary
// functions as a mutation rule.
type TechnologyAssociationMutationRuleFunc func(context.Context, *ent.TechnologyAssociationMutation) error
// EvalMutation calls f(ctx, m).
func (f TechnologyAssociationMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.TechnologyAssociationMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.TechnologyAssociationMutation", 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)
}