/
action.go
275 lines (237 loc) · 6.16 KB
/
action.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
package prago
import (
"fmt"
"sort"
"strings"
)
type buttonData struct {
Icon string
Name string
URL string
Params map[string]string
Priority int64
}
// Action represents action
type Action struct {
name func(string) string
icon string
permission Permission
method string
url string
handler func(*Request)
constraints []routerConstraint
parentBoard *Board
isPartOfBoard *Board
app *App
resource *Resource
isItemAction bool
isUserMenu bool
priority int64
childAction *Action
}
func bindAction(action *Action) error {
url := action.getURL()
controller := action.getController()
if action.permission == "" {
panic(fmt.Sprintf("Permission for action '%s %s' should not be empty", action.method, url))
}
controller.routeHandler(action.method, url, action.handler, action.constraints...)
return nil
}
func (app *App) bindAllActions() {
for _, v := range app.rootActions {
err := bindAction(v)
if err != nil {
panic(fmt.Sprintf("error while binding root action %s %s: %s", v.method, v.name("en"), err))
}
}
for _, resource := range app.resources {
resource.bindActions()
}
}
func (resource *Resource) bindActions() {
for _, v := range resource.actions {
err := bindAction(v)
if err != nil {
panic(fmt.Sprintf("error while binding resource %s action %s %s: %s", resource.id, v.method, v.name("en"), err))
}
}
for _, v := range resource.itemActions {
err := bindAction(v)
if err != nil {
panic(fmt.Sprintf("error while binding item resource %s action %s %s: %s", resource.id, v.method, v.name("en"), err))
}
}
}
func newAction(app *App, url string) *Action {
return &Action{
name: unlocalized(url),
permission: "",
method: "GET",
url: url,
app: app,
parentBoard: app.MainBoard,
icon: iconAction,
}
}
// AddAction adds action to root
func (app *App) Action(url string) *Action {
action := newAction(app, url)
app.rootActions = append(app.rootActions, action)
return action
}
func ResourceAction[T any](app *App, url string) *Action {
resource := GetResource[T](app)
return resource.action(url)
}
// AddAction adds action to resource
func (resource *Resource) action(url string) *Action {
action := newAction(resource.app, url)
action.resource = resource
action.permission = resource.canView
resource.actions = append(resource.actions, action)
return action
}
// Name sets action name
func (action *Action) Name(name func(string) string) *Action {
action.name = name
return action
}
// Permission sets action permission
func (action *Action) Permission(permission Permission) *Action {
must(action.app.validatePermission(permission))
action.permission = permission
if action.childAction != nil {
action.childAction.permission = permission
}
return action
}
// Method sets action method (GET, POST, PUT or DELETE)
func (action *Action) Method(method string) *Action {
if !isHTTPMethodValid(method) {
panic("unsupported method for action: " + method)
}
action.method = method
return action
}
func (action *Action) setPriority(priority int64) *Action {
action.priority = priority
return action
}
func (action *Action) Icon(icon string) *Action {
action.icon = icon
return action
}
func (action *Action) Board(board *Board) *Action {
action.parentBoard = board
return action
}
func (action *Action) userMenu() *Action {
action.isUserMenu = true
return action
}
func (action *Action) addConstraint(constraint routerConstraint) {
action.constraints = append(action.constraints, constraint)
}
func (resource *Resource) getItemButtonData(userData UserData, item interface{}) (ret []*buttonData) {
for _, v := range resource.itemActions {
if v.method != "GET" {
continue
}
if !userData.Authorize(v.permission) {
continue
}
if v.url == "" {
continue
}
name := v.name(userData.Locale())
if v.url == "" {
name = resource.previewer(userData, item).Name()
}
ret = append(ret, &buttonData{
Icon: v.icon,
Name: name,
URL: resource.getItemURL(item, v.url, userData),
Priority: v.priority,
},
)
}
sort.Slice(ret, func(i, j int) bool {
if ret[i].Priority > ret[j].Priority {
return true
} else {
return false
}
})
return ret
}
func (resource *Resource) getListItemActions(userData UserData, item any, id int64) listItemActions {
ret := listItemActions{
MenuButtons: resource.getItemButtonData(userData, item),
}
ret.VisibleButtons = append(ret.VisibleButtons, buttonData{
Icon: iconView,
URL: resource.getURL(fmt.Sprintf("%d", id)),
})
if userData.Authorize(resource.canUpdate) && resource.orderField != nil {
ret.ShowOrderButton = true
}
return ret
}
func (action *Action) getURL() string {
if strings.HasPrefix(action.url, "/") {
panic("url can't start with / character")
}
var url string
if action.resource == nil {
url = action.app.getAdminURL(action.url)
} else {
resource := action.resource
if action.isItemAction {
if action.url != "" {
url = resource.getURL(":id/" + action.url)
} else {
url = resource.getURL(":id")
}
} else {
url = resource.getURL(action.url)
}
}
return url
}
func (action *Action) getController() *controller {
if action.resource != nil {
return action.resource.getResourceControl()
} else {
return action.app.adminController
}
}
func (action *Action) View(template string, dataSource func(*Request) any) *Action {
return action.ui(func(request *Request, pd *pageData) {
pd.PageTemplate = template
if dataSource != nil {
pd.PageData = dataSource(request)
}
})
}
func (action *Action) ui(uiHandler func(*Request, *pageData)) *Action {
return action.Handler(func(request *Request) {
pageData := createPageData(request)
if action.isItemAction {
item := action.resource.query(request.r.Context()).ID(request.Param("id"))
pageData.Menu = action.app.getMenu(request, item)
}
uiHandler(request, pageData)
pageData.renderPage(request)
})
}
func (action *Action) Handler(handler func(*Request)) *Action {
action.handler = func(request *Request) {
if !request.Authorize(action.permission) {
renderErrorPage(request, 403)
return
}
handler(request)
}
return action
}