-
Notifications
You must be signed in to change notification settings - Fork 3
/
callbacks.go
557 lines (472 loc) · 14.4 KB
/
callbacks.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package fire
import (
"fmt"
"net/http"
"reflect"
"time"
"github.com/256dpi/fire/coal"
"github.com/256dpi/jsonapi"
"github.com/globalsign/mgo/bson"
)
// C is a short-hand function to construct a callback. It will also add tracing
// code around the execution of the callback.
func C(name string, m Matcher, h Handler) *Callback {
// panic if matcher or handler is not set
if m == nil || h == nil {
panic("fire: missing matcher or handler")
}
return &Callback{
Matcher: m,
Handler: func(ctx *Context) error {
// begin trace
ctx.Tracer.Push(name)
// call handler
err := h(ctx)
if err != nil {
return err
}
// finish trace
ctx.Tracer.Pop()
return nil
},
}
}
// Handler is function that takes a context, mutates is to modify the behaviour
// and response or return an error.
type Handler func(*Context) error
// Matcher is a function that makes an assessment of a context and decides whether
// a modification should be applied in the future.
type Matcher func(*Context) bool
// All will match all contexts.
func All() Matcher {
return func(ctx *Context) bool {
return true
}
}
// Only will match if the operation is present in the provided list.
func Only(ops ...Operation) Matcher {
return func(ctx *Context) bool {
// allow if operation is listed
for _, op := range ops {
if op == ctx.Operation {
return true
}
}
return false
}
}
// Except will match if the operation is not present in the provided list.
func Except(ops ...Operation) Matcher {
return func(ctx *Context) bool {
// disallow if operation is listed
for _, op := range ops {
if op == ctx.Operation {
return false
}
}
return true
}
}
// A Callback is called during the request processing flow of a controller.
//
// Note: If the callback returns an error wrapped using Fatal() the API returns
// an InternalServerError status and the error will be logged. All other errors
// are serialized to an error object and returned.
type Callback struct {
// The matcher that decides whether the callback should be run.
Matcher Matcher
// The handler handler that gets executed with the context.
//
// If returned errors are marked with Safe() they will be included in the
// returned JSON-API error.
Handler Handler
}
// ErrAccessDenied can be returned by any callback to deny access.
var ErrAccessDenied = jsonapi.ErrorFromStatus(http.StatusUnauthorized, "access denied")
// BasicAuthorizer authorizes requests based on a simple credentials list.
func BasicAuthorizer(credentials map[string]string) *Callback {
return C("fire/BasicAuthorizer", All(), func(ctx *Context) error {
// check for credentials
user, password, ok := ctx.HTTPRequest.BasicAuth()
if !ok {
return ErrAccessDenied
}
// check if credentials match
if val, ok := credentials[user]; !ok || val != password {
return ErrAccessDenied
}
return nil
})
}
// The ValidatableModel interface can be additionally implemented to provide
// a custom validation method that is used by the Validate function.
type ValidatableModel interface {
coal.Model
// The Validate method that should return normal errors about invalid fields.
Validate() error
}
// ModelValidator performs a validation of the model using the Validate method.
func ModelValidator() *Callback {
return C("fire/ModelValidator", Only(Create, Update), func(ctx *Context) error {
// check model
m, ok := ctx.Model.(ValidatableModel)
if !ok {
return fmt.Errorf("model is not validatable")
}
// validate model
err := m.Validate()
if err != nil {
return err
}
return nil
})
}
// TimestampValidator will set the specified timestamp fields on creation
// and update operations.
func TimestampValidator(createdAtField, updatedAtField string) *Callback {
return C("fire/TimestampValidator", Only(Create, Update), func(ctx *Context) error {
// set the created-at field if present and not already set
if createdAtField != "" && ctx.Model.MustGet(createdAtField).(time.Time).IsZero() {
ctx.Model.MustSet(createdAtField, time.Now())
}
// always set the updated-at field if present
if updatedAtField != "" {
ctx.Model.MustSet(updatedAtField, time.Now())
}
return nil
})
}
// NoDefault marks the specified field to have no default that needs to be
// enforced while executing the ProtectedFieldsValidator.
const NoDefault noDefault = iota
type noDefault int
// ProtectedFieldsValidator compares protected fields against their default
// during Create (if provided) or stored value during Update and returns an error
// if they have been changed.
//
// Protected fields are defined by passing pairs of fields and default values:
//
// fire.ProtectedFieldsValidator(map[string]interface{}{
// "Title": NoDefault, // can only be set during Create
// "Link": "", // default is fixed and cannot be changed
// })
//
// The special NoDefault value can be provided to skip the default enforcement
// on Create.
func ProtectedFieldsValidator(pairs map[string]interface{}) *Callback {
return C("fire/ProtectedFieldsValidator", Only(Create, Update), func(ctx *Context) error {
// handle resource creation
if ctx.Operation == Create {
// check all fields
for field, def := range pairs {
// skip fields that have no default
if def == NoDefault {
continue
}
// check equality
if !reflect.DeepEqual(ctx.Model.MustGet(field), def) {
return E("field " + field + " is protected")
}
}
}
// handle resource updates
if ctx.Operation == Update {
// read the original
original, err := ctx.Original()
if err != nil {
return err
}
// check all fields
for field := range pairs {
// check equality
if !reflect.DeepEqual(ctx.Model.MustGet(field), original.MustGet(field)) {
return E("field " + field + " is protected")
}
}
}
return nil
})
}
// DependentResourcesValidator counts related documents and returns an error if
// some get found. This callback is meant to protect resources from breaking
// relations when requested to be deleted.
//
// Dependent resources are defined by passing pairs of models and fields that
// reference the current model.
//
// fire.DependentResourcesValidator(map[string]string{
// &Post{}: "Author",
// &Comment{}: "Author",
// })
//
func DependentResourcesValidator(pairs map[coal.Model]string) *Callback {
return C("DependentResourcesValidator", Only(Delete), func(ctx *Context) error {
// check all relations
for model, field := range pairs {
// prepare query
query := bson.M{coal.F(model, field): ctx.Model.ID()}
// count referencing documents
ctx.Tracer.Push("mgo/Query.Count")
ctx.Tracer.Tag("query", query)
n, err := ctx.Store.DB().C(coal.C(model)).Find(query).Limit(1).Count()
if err != nil {
return err
}
ctx.Tracer.Pop()
// return err of documents are found
if n != 0 {
return E("resource has dependent resources")
}
}
// pass validation
return nil
})
}
// VerifyReferencesValidator makes sure all references in the document are
// existing by counting the references on the related collections.
//
// References are defined by passing pairs of fields and models who might be
// referenced by the current model:
//
// fire.VerifyReferencesValidator(map[string]string{
// "Post": &Post{},
// "Author": &User{},
// })
//
// The callbacks supports to-one, optional to-one and to-many relationships.
func VerifyReferencesValidator(pairs map[string]coal.Model) *Callback {
return C("fire/VerifyReferencesValidator", Only(Create, Update), func(ctx *Context) error {
// check all references
for field, collection := range pairs {
// read referenced id
ref := ctx.Model.MustGet(field)
// continue if reference is not set
if oid, ok := ref.(*bson.ObjectId); ok && oid == nil {
continue
}
// continue if slice is empty
if ids, ok := ref.([]bson.ObjectId); ok && ids == nil {
continue
}
// handle to-many relationships
if ids, ok := ref.([]bson.ObjectId); ok {
// prepare query
query := bson.M{"_id": bson.M{"$in": ids}}
// count entities in database
ctx.Tracer.Push("mgo/Query.Count")
ctx.Tracer.Tag("query", query)
n, err := ctx.Store.DB().C(coal.C(collection)).Find(query).Count()
if err != nil {
return err
}
ctx.Tracer.Pop()
// check for existence
if n != len(ids) {
return E("missing references for field " + field)
}
continue
}
// handle to-one relationships
// count entities in database
ctx.Tracer.Push("mgo/Query.Count")
ctx.Tracer.Tag("id", ref)
n, err := ctx.Store.DB().C(coal.C(collection)).FindId(ref).Limit(1).Count()
if err != nil {
return err
}
ctx.Tracer.Pop()
// check for existence
if n != 1 {
return E("missing reference for field " + field)
}
}
// pass validation
return nil
})
}
// RelationshipValidator makes sure all relationships of a model are correct and
// in place. It does so by combining a DependentResourcesValidator and a
// VerifyReferencesValidator based on the specified model and catalog.
func RelationshipValidator(model coal.Model, catalog *coal.Catalog, excludedFields ...string) *Callback {
// prepare lists
dependentResources := make(map[coal.Model]string)
references := make(map[string]coal.Model)
// iterate through all fields
for _, field := range coal.Init(model).Meta().Fields {
// exclude field if requested
if Contains(excludedFields, field.Name) {
continue
}
// handle has-one and has-many relationships
if field.HasOne || field.HasMany {
// get related model
relatedModel := catalog.Find(field.RelType)
if relatedModel == nil {
panic(fmt.Sprintf(`fire: missing model in catalog: "%s"`, field.RelType))
}
// get related bson field
bsonField := ""
for _, relatedField := range relatedModel.Meta().Fields {
if relatedField.RelName == field.RelInverse {
bsonField = relatedField.Name
}
}
if bsonField == "" {
panic(fmt.Sprintf(`fire: missing field for inverse relationship: "%s"`, field.RelInverse))
}
// add relationship
dependentResources[relatedModel] = bsonField
}
// handle to-one and to-many relationships
if field.ToOne || field.ToMany {
// get related model
relatedModel := catalog.Find(field.RelType)
if relatedModel == nil {
panic(fmt.Sprintf(`fire: missing model in catalog: "%s"`, field.RelType))
}
// add relationship
references[field.Name] = relatedModel
}
}
// create callbacks
cb1 := DependentResourcesValidator(dependentResources)
cb2 := VerifyReferencesValidator(references)
return C("RelationshipValidator", func(ctx *Context) bool {
return cb1.Matcher(ctx) || cb2.Matcher(ctx)
}, func(ctx *Context) error {
// run dependent resources validator
if cb1.Matcher(ctx) {
err := cb1.Handler(ctx)
if err != nil {
return err
}
}
// run dependent resources validator
if cb2.Matcher(ctx) {
err := cb2.Handler(ctx)
if err != nil {
return err
}
}
return nil
})
}
// MatchingReferencesValidator compares the model with one related model or all
// related models and checks if the specified references are shared exactly.
//
// The target is defined by passing the reference on the current model and the
// target model. The matcher is defined by passing pairs of fields on the current
// and target model:
//
// fire.MatchingReferencesValidator("Blog", &Blog{}, map[string]string{
// "Owner": "Owner",
// })
//
// To-many, optional to-many and has-many relationships are supported both for
// the initial reference and in the matchers.
func MatchingReferencesValidator(reference string, target coal.Model, matcher map[string]string) *Callback {
return C("fire/MatchingReferencesValidator", Only(Create, Update), func(ctx *Context) error {
// prepare ids
var ids []bson.ObjectId
// get reference
ref := ctx.Model.MustGet(reference)
// handle to-one reference
if id, ok := ref.(bson.ObjectId); ok {
ids = []bson.ObjectId{id}
}
// handle optional to-one reference
if oid, ok := ref.(*bson.ObjectId); ok {
// return immediately if not set
if oid == nil {
return nil
}
// set id
ids = []bson.ObjectId{*oid}
}
// handle to-many reference
if list, ok := ref.([]bson.ObjectId); ok {
// return immediately if empty
if len(list) == 0 {
return nil
}
// set list
ids = list
}
// ensure list is unique
ids = coal.Unique(ids)
// prepare query
query := bson.M{
"_id": bson.M{
"$in": ids,
},
}
// add matchers
for sourceField, targetField := range matcher {
query[coal.F(target, targetField)] = ctx.Model.MustGet(sourceField)
}
// find matching documents
ctx.Tracer.Push("mgo/Query.Count")
ctx.Tracer.Tag("query", query)
n, err := ctx.Store.DB().C(coal.C(target)).Find(query).Count()
if err != nil {
return err
}
ctx.Tracer.Pop()
// return error if a document is missing (does not match)
if n != len(ids) {
return E("references do not match")
}
return nil
})
}
// NoZero indicates that the zero value check should be skipped.
const NoZero noZero = iota
type noZero int
// UniqueFieldValidator ensures that the specified field of the current model will
// remain unique among the specified filters. If the value matches the provided
// zero value the check is skipped.
//
// fire.UniqueFieldValidator("Name", "", "Creator")
//
// The special NoZero value can be provided to skip the zero check.
func UniqueFieldValidator(field string, zero interface{}, filters ...string) *Callback {
return C("fire/UniqueFieldValidator", Only(Create, Update), func(ctx *Context) error {
// check if field has changed
if ctx.Operation == Update {
// get original model
original, err := ctx.Original()
if err != nil {
return err
}
// return if field has not been changed
if reflect.DeepEqual(ctx.Model.MustGet(field), original.MustGet(field)) {
return nil
}
}
// get value
value := ctx.Model.MustGet(field)
// return if value is the zero value
if value != NoZero && reflect.DeepEqual(value, zero) {
return nil
}
// prepare query
query := bson.M{
coal.F(ctx.Model, field): value,
}
// add filters
for _, field := range filters {
query[coal.F(ctx.Model, field)] = ctx.Model.MustGet(field)
}
// count
ctx.Tracer.Push("mgo/Query.Count")
ctx.Tracer.Tag("query", query)
n, err := ctx.Store.C(ctx.Model).Find(query).Limit(1).Count()
if err != nil {
return err
} else if n != 0 {
return E("attribute %s is not unique", field)
}
ctx.Tracer.Pop()
return nil
})
}