/
app.go
676 lines (550 loc) · 16.7 KB
/
app.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
// Copyright 2014-2015 Simon HEGE. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//Package okinotes contains all the model and business layers for the okinotes application.
package okinotes
import (
"errors"
"fmt"
"html/template"
"net/http"
"regexp"
"time"
)
var (
//ErrFirstUserConnection is the errors returned when connecting
//for the first time with an identity.
ErrFirstUserConnection = errors.New("User not created")
)
//AppFactory represents the objects able to create an App
type AppFactory interface {
CreateApp(r *http.Request) (App, error)
}
//App is the main application.
type App struct {
repository Repository
userInteractor UserInteractor
logInteractor LogInteractor
uploadInteractor UploadInteractor
}
//NewApp creates a new App using the given services
func NewApp(r Repository, u UserInteractor, l LogInteractor, up UploadInteractor) App {
return App{
repository: r,
userInteractor: u,
logInteractor: l,
uploadInteractor: up,
}
}
//CurrentUserName returns the name of the current user.
//Returns an empty string if not logged in.
func (app App) CurrentUserName() string {
identity, err := app.CurrentIdentity()
if err != nil {
return ""
}
return identity.UserName
}
//CurrentUserIsAdmin returns true if the current user is an administrator
func (app App) CurrentUserIsAdmin() bool {
return app.userInteractor.CurrentUserIsAdmin()
}
//CurrentUser returns the current user
func (app App) CurrentUser() (User, error) {
ident, err := app.userInteractor.CurrentIdentity()
if err != nil {
app.logInteractor.Infof("CurrentUser error: %v", err)
return User{}, err
}
u, err := app.repository.GetUser(ident)
if err != nil {
if _, notFound := err.(NotInDatastoreError); notFound {
err = ErrFirstUserConnection
}
return User{}, err
}
return u, nil
}
//CurrentIdentity returns the Identity used to logged in.
func (app App) CurrentIdentity() (Identity, error) {
ident, err := app.userInteractor.CurrentIdentity()
if err != nil {
app.logInteractor.Infof("CurrentIdentity error: %v", err)
return Identity{}, err
}
app.logInteractor.Infof("ident.Identity: %s", ident.Identity)
if len(ident.Identity) == 0 {
return Identity{}, nil //Not logged in
}
id, err := app.repository.GetIdentity(ident)
if err != nil {
if _, notFound := err.(NotInDatastoreError); notFound {
return Identity{Ident: ident}, ErrFirstUserConnection
}
app.logInteractor.Infof("CurrentIdentity->GetIdentity error: %v", err)
return Identity{}, err
}
return id, nil
}
//LoginURL returns the URL to the log-in ressource
func (app App) LoginURL(destURL string) (string, error) {
return app.userInteractor.LoginURL(destURL)
}
//LogoutURL returns the URL to the log-out ressource
func (app App) LogoutURL(destURL string) (string, error) {
return app.userInteractor.LogoutURL(destURL)
}
//CreateUser creates a user
func (app App) CreateUser(ident Ident, userName string) error {
//Check user name pattern
ok, err := regexp.MatchString("[a-z0-9_\\-]{3,}", userName)
if err != nil {
return err
}
if !ok {
return errors.New("The selected user name does not follow the required pattern.")
}
user := User{
Name: userName,
Kind: UserKindUSER,
FullName: userName,
}
identity := Identity{ident, userName}
return app.repository.RunInTransaction(func(repo Repository) error {
exists, err := repo.FindUser(userName)
if err != nil {
return err
}
if exists {
return errors.New("The selected user name already exists")
}
err = repo.StoreUser(user)
if err != nil {
return err
}
err = repo.StoreIdentity(identity)
if err != nil {
return err
}
return nil
})
}
//GetPage retrieve an existing single page
func (app App) GetPage(userName string, pageName string) (Page, error) {
//Read the page in the datastore
page, err := app.repository.GetPage(userName, pageName)
if err != nil {
app.logInteractor.Infof("GetPage error: %v", err)
return Page{}, err
}
if page.Policy == PolicyPRIVATE && app.CurrentUserName() != userName {
app.logInteractor.Infof("Not authorized to read the page")
return Page{}, NotAuthorizedError{"Read page"}
}
return page, nil
}
//ListPublicPages returns the list of public pages for a given user
func (app App) ListPublicPages(limit int) ([]Page, bool, error) {
//Get the list of public pages
pages, bMore, err := app.repository.NewPageQuery().Filter("Policy =", PolicyPUBLIC).Order("-LastModificationDate").Limit(limit).GetAll()
if err != nil {
app.logInteractor.Errorf("ListPublicPages failed in GetAll(PUBLIC): %v", err)
return nil, false, err
}
return pages, bMore, nil
}
//ListOwnedPages returns the list of pages owned by a given user
func (app App) ListOwnedPages(limit int) ([]Page, bool, error) {
user, err := app.CurrentUser()
if err != nil {
app.logInteractor.Errorf("ListOwnedPages failed in CurrentUser: %v", err)
return nil, false, err
}
//Get the list of public pages
pages, bMore, err := app.repository.NewPageQuery().Filter("UserName=", user.Name).Order("-LastModificationDate").Limit(limit).GetAll()
if err != nil {
app.logInteractor.Errorf("ListOwnedPages failed in GetAll: %v", err)
return nil, false, err
}
return pages, bMore, nil
}
//CreatePage create a new page for the given user
//Returns the id of the stored page.
func (app App) CreatePage(page Page) error {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 {
return NotAuthorizedError{"Update page"}
}
page.UserName = currentUserName
page.LastModificationDate = time.Now()
page.CreationDate = page.LastModificationDate
if len(page.Policy) == 0 {
page.Policy = PolicyPRIVATE
}
err := app.repository.RunInTransaction(func(repo Repository) error {
//Check for existence of user/page
_, err := repo.GetPage(page.UserName, page.Name)
if err == nil {
return errors.New("Page already exists")
}
if _, notFound := err.(NotInDatastoreError); !notFound {
return err
}
//Store
return repo.StorePage(page)
})
return err
}
//UpdatePage updates a given page.
//The description of tags in the current template must be provided.
func (app App) UpdatePage(page Page, pageTags TagDescriptionList) error {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != page.UserName {
return NotAuthorizedError{"Update page"}
}
page.LastModificationDate = time.Now()
err := app.repository.RunInTransaction(func(repo Repository) error {
//Check for existence of user/page
oldPage, err := repo.GetPage(page.UserName, page.Name)
if err != nil {
return err
}
page.CreationDate = oldPage.CreationDate
//Remove previous images usages
err = repo.DeleteUsages(page.UserName, page.Name)
if err != nil {
return err
}
for _, t := range pageTags {
if t.Kind == "imageId" {
imgID := page.Tags.Tag(t.Key)
err = repo.StoreUsage(page.UserName, page.Name, imgID)
if err != nil {
return err
}
}
}
//Store
return repo.StorePage(page)
})
return err
}
//UpdateTemplate changes the template of page.
func (app App) UpdateTemplate(userName string, pageName string, newTemplateID string) error {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != userName {
return NotAuthorizedError{"Update page"}
}
tNow := time.Now()
err := app.repository.RunInTransaction(func(repo Repository) error {
//Check for existence of user/page
page, err := repo.GetPage(userName, pageName)
if err != nil {
return err
}
if page.TemplateID == newTemplateID {
return nil
}
page.LastModificationDate = tNow
page.TemplateID = newTemplateID
//Store
return repo.StorePage(page)
})
return err
}
//DeletePage removes permanently a page and all the associated content (items and permissions)
func (app App) DeletePage(userName, pageName string) error {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != userName {
return NotAuthorizedError{"Delete page"}
}
//TODO: Use a transactional mechanism inn order to have them all succeed or all failed?
//Delete items
err := app.repository.DeleteItemsFromPage(userName, pageName)
if err != nil {
return err
}
//Delete page
err = app.repository.DeletePage(userName, pageName)
if err != nil {
return err
}
return nil
}
//CreateItem stores an item
//Returns the stored item.
func (app App) CreateItem(userName, pageName string, i Item) (Item, error) {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != userName {
return Item{}, NotAuthorizedError{"Store item"}
}
if len(i.Content) == 0 && len(i.URL) == 0 {
return Item{}, fmt.Errorf("Empty item not allowed. Either content or URL must be provided: %#v", i)
}
//Compute HTML from markdown
i.HTMLContent = template.HTML(markdownToHTML(i.Content))
i.CreationDate = time.Now()
i.LastModificationDate = i.CreationDate
i.ID = generateID()
//Stores the item
err := app.repository.RunInTransaction(func(repo Repository) error {
//Generate unique item ID
for {
found, err := repo.FindItem(userName, pageName, i.ID)
if err != nil {
return err
}
if !found {
break
}
i.ID = generateID()
}
//Store
return repo.StoreItem(userName, pageName, i)
})
if err != nil {
return Item{}, err
}
{
//Update LastModificationDate on Page
err := app.repository.RunInTransaction(func(repo Repository) error {
//Check for existence of user/page
page, err := repo.GetPage(userName, pageName)
if err != nil {
return err
}
page.LastModificationDate = i.LastModificationDate
//Store
return repo.StorePage(page)
})
if err != nil {
return Item{}, err
}
}
return i, nil
}
//PutItem stores a fully defined item. Replace the item if it already exists
//Returns the stored item.
func (app App) PutItem(userName, pageName string, i Item) (Item, error) {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != userName {
return Item{}, NotAuthorizedError{"Store item"}
}
if len(i.Content) == 0 && len(i.URL) == 0 {
return Item{}, errors.New("Empty item not allowed. Either content or URL must be provided.")
}
//Compute HTML from markdown
i.HTMLContent = template.HTML(markdownToHTML(i.Content))
//Stores the item
err := app.repository.RunInTransaction(func(repo Repository) error {
//Store
return repo.StoreItem(userName, pageName, i)
})
if err != nil {
return Item{}, err
}
{
//Update LastModificationDate on Page
err := app.repository.RunInTransaction(func(repo Repository) error {
//Check for existence of user/page
page, err := repo.GetPage(userName, pageName)
if err != nil {
return err
}
page.LastModificationDate = i.LastModificationDate
//Store
return repo.StorePage(page)
})
if err != nil {
return Item{}, err
}
}
return i, nil
}
//UpdateItem stores an updated item
func (app App) UpdateItem(userName, pageName string, i Item, updateTags bool) (Item, error) {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != userName {
return Item{}, NotAuthorizedError{"Store item"}
}
if len(i.Content) == 0 && len(i.URL) == 0 {
return Item{}, errors.New("Empty item not allowed. Either content or URL must be provided.")
}
//Compute HTML from markdown
i.HTMLContent = template.HTML(markdownToHTML(i.Content))
i.LastModificationDate = time.Now()
//Stores the item
err := app.repository.RunInTransaction(func(repo Repository) error {
oldItem, err := repo.GetItem(userName, pageName, i.ID)
if err != nil {
return err
}
i.CreationDate = oldItem.CreationDate
if !updateTags {
i.Tags = oldItem.Tags
}
//Store
return repo.StoreItem(userName, pageName, i)
})
if err != nil {
return Item{}, err
}
{
//Update LastModificationDate on Page
err := app.repository.RunInTransaction(func(repo Repository) error {
//Check for existence of user/page
page, err := repo.GetPage(userName, pageName)
if err != nil {
return err
}
page.LastModificationDate = i.LastModificationDate
//Store
return repo.StorePage(page)
})
if err != nil {
return Item{}, err
}
}
return i, nil
}
//SetItemTag stores a new value for an item tag
func (app App) SetItemTag(userName, pageName string, itemID string, tagKey string, tagValue string) error {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != userName {
return NotAuthorizedError{"Store item"}
}
if len(tagKey) == 0 {
return errors.New("Empty tag key not allowed.")
}
tNow := time.Now()
//Stores the item
err := app.repository.RunInTransaction(func(repo Repository) error {
oldItem, err := repo.GetItem(userName, pageName, itemID)
if err != nil {
return err
}
oldItem.LastModificationDate = tNow
oldItem.Tags.SetTag(tagKey, tagValue)
//Store
return repo.StoreItem(userName, pageName, oldItem)
})
if err != nil {
return err
}
{
//Update LastModificationDate on Page
err := app.repository.RunInTransaction(func(repo Repository) error {
//Check for existence of user/page
page, err := repo.GetPage(userName, pageName)
if err != nil {
return err
}
page.LastModificationDate = tNow
//Store
return repo.StorePage(page)
})
if err != nil {
return err
}
}
return nil
}
//DeleteItem removes permanently an item
func (app App) DeleteItem(userName, pageName string, itemID string) error {
currentUserName := app.CurrentUserName()
//We can only update owned pages
if len(currentUserName) == 0 || currentUserName != userName {
return NotAuthorizedError{"Delete page"}
}
//Delete item
err := app.repository.DeleteItem(userName, pageName, itemID)
if err != nil {
return err
}
return nil
}
//listItems get the list of items for a given page. No authorisation check (should be done before by the caller)
func (app App) listItems(userName, pageName string, limit int) ([]Item, error) {
//Get the items
items, err := app.repository.GetItemsFromPage(userName, pageName, limit)
if err != nil {
return nil, err
}
return items, nil
}
//getItem retrieve a specific item. No authorisation check (should be done before by the caller)
func (app App) getItem(userName, pageName string, itemID string) (Item, error) {
return app.repository.GetItem(userName, pageName, itemID)
}
//StoreTemplate insert or update a template in database
func (app App) StoreTemplate(tpl Template) error {
if !app.userInteractor.CurrentUserIsAdmin() {
return NotAuthorizedError{"Store template"}
}
_, err := app.repository.StoreTemplate(tpl, generateID)
return err
}
//GetTemplate retrieve a template from the datastore
func (app App) GetTemplate(templateID string) (Template, error) {
return app.repository.GetTemplate(templateID)
}
//GetAllTemplates retrieve all templates from the datastore
func (app App) GetAllTemplates() ([]Template, error) {
return app.repository.GetAllTemplates()
}
//UploadURL returns the URL to the upload ressource
func (app App) UploadURL(destURL string) (string, error) {
return app.uploadInteractor.UploadURL(destURL, 10000000) //TODO quotas per user
}
//StoreImage stores an uplaoded image in the datastore and associate it with the current user
func (app App) StoreImage(r *http.Request, name string) error {
identity, err := app.CurrentIdentity()
if err != nil {
return err
}
img, err := app.uploadInteractor.UploadInfo(r, name)
if err != nil {
return err
}
return app.repository.StoreImage(img, identity.UserName)
}
//RenameImage changes the name of an uploaded image
func (app App) RenameImage(imgID string, newName string) error {
identity, err := app.CurrentIdentity()
if err != nil {
return err
}
return app.repository.RenameImage(newName, imgID, identity.UserName)
}
//DeleteImage delete an uploaded image
func (app App) DeleteImage(imgID string) error {
identity, err := app.CurrentIdentity()
if err != nil {
return err
}
return app.repository.DeleteImage(imgID, identity.UserName)
}
//Images retrieves the images associated with the current user
func (app App) Images(limit int) ([]UploadInfo, error) {
identity, err := app.CurrentIdentity()
if err != nil {
return nil, err
}
return app.repository.GetImages(identity.UserName, limit)
}
//ImageURL retrieves the image URL associated with the image
func (app App) ImageURL(img UploadInfo, secure bool, size int) (string, error) {
return app.uploadInteractor.ImageURL(img.Key, secure, size)
}
//Version returns a user readable text describing the current version of the application
func (app App) Version() string {
return "1.0-alpha7"
}