/
middleware_store.go
127 lines (100 loc) · 3.2 KB
/
middleware_store.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
package marketplace
import (
"net/http"
"github.com/gocraft/web"
)
// PrivateStoreMiddleware controls access rights to store
func (c *Context) PrivateStoreMiddleware(w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
store, _ := FindStoreByStorename(r.PathParams["store"])
if store == nil {
http.NotFound(w, r.Request)
return
}
viewStore := store.ViewStore(c.ViewUser.Language)
c.ViewStore = &viewStore
c.CanEdit = store.UserIsAdministration(c.ViewUser.Uuid) || c.ViewUser.IsAdmin || c.ViewUser.IsStaff
if !c.CanEdit {
http.NotFound(w, r.Request)
return
}
next(w, r)
}
// PrivateStoreItemMiddleware controls access rights to item editing
func (c *Context) PrivateStoreItemMiddleware(w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
if r.PathParams["item"] != "" && r.PathParams["item"] != "new" {
item, _ := FindItemByUuid(r.PathParams["item"])
// if item not found
if item == nil {
http.NotFound(w, r.Request)
return
}
// if item does not belong to user's store
if !item.Store.UserIsAdministration(c.ViewUser.Uuid) && !c.CanEdit {
http.NotFound(w, r.Request)
return
}
viewItem := item.ViewItem(c.ViewUser.Language)
c.ViewItem = &viewItem
} else {
item := Item{}
viewItem := item.ViewItem(c.ViewUser.Language)
c.ViewItem = &viewItem
}
next(w, r)
}
// SellerItemPackageMiddleware controls access rights to item package editing
func (c *Context) PrivateStoreItemPackageMiddleware(w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
if r.PathParams["package"] != "" && r.PathParams["package"] != "new" {
itemPackage, _ := FindPackageByUuid(r.PathParams["package"])
if itemPackage == nil {
http.NotFound(w, r.Request)
return
}
if itemPackage.ItemUuid != c.ViewItem.Uuid {
http.NotFound(w, r.Request)
return
}
c.ViewPackage = itemPackage.ViewPackage()
}
next(w, r)
}
// PublicStoreMiddleware populates all views and relations to display a store
func (c *Context) PublicStoreMiddleware(w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
store, _ := FindStoreByStorename(r.PathParams["store"])
if store == nil {
http.NotFound(w, r.Request)
return
}
// Warnings
// TODO: Add to Preload
warnings := FindStoreWarningsForStore(store.Uuid)
for _, w := range warnings {
if !w.HasExpired() && !w.IsApproved {
store.Warnings = append(store.Warnings, w)
}
}
c.ViewStoreWarnings = warnings.ViewStoreWarnings(c.ViewUser.Language)
// Items
c.ViewItems = store.Items.ViewItems(c.ViewUser.Language)
// View Model
viewStore := store.ViewStore(c.ViewUser.Language)
c.ViewStore = &viewStore
// Editing rights
c.CanEdit = store.UserIsAdministration(c.ViewUser.Uuid) || c.ViewUser.IsAdmin || c.ViewUser.IsStaff
next(w, r)
}
// VendorItemMiddleware populates all views and relations to display an item
func (c *Context) PublicStoreItemMiddleware(w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
item, _ := FindItemByUuid(r.PathParams["item"])
if item == nil {
http.NotFound(w, r.Request)
return
}
if item.StoreUuid != c.ViewStore.Uuid {
http.NotFound(w, r.Request)
return
}
viewItem := item.ViewItem(c.ViewUser.Language)
c.ViewItem = &viewItem
next(w, r)
}