-
Notifications
You must be signed in to change notification settings - Fork 208
/
provider.go
269 lines (231 loc) · 8.52 KB
/
provider.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
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package provider
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sort"
"strings"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/log"
)
// SecretReplacement is a constant used to replace secrets in data-structures when required.
// 8 stars.
const SecretReplacement = "********"
// sectionsMap is where individual sections register themselves.
var sectionsMap = make(map[string]Provider)
// TypeMeta details a "smart section" that represents a "page" in a document.
type TypeMeta struct {
ID string `json:"id"`
Order int `json:"order"`
ContentType string `json:"contentType"`
PageType string `json:"pageType"`
Title string `json:"title"`
Description string `json:"description"`
Preview bool `json:"preview"` // coming soon!
Callback func(http.ResponseWriter, *http.Request) error `json:"-"`
}
// ConfigHandle returns the key name for database config table
func (t *TypeMeta) ConfigHandle() string {
return fmt.Sprintf("SECTION-%s", strings.ToUpper(t.ContentType))
}
// Provider represents a 'page' in a document.
type Provider interface {
Meta() TypeMeta // Meta returns section details
Command(ctx *Context, w http.ResponseWriter, r *http.Request) // Command is general-purpose method that can return data to UI
Render(ctx *Context, config, data string) string // Render converts section data into presentable HTML
Refresh(ctx *Context, config, data string) string // Refresh returns latest data
}
// Context describes the environment the section code runs in
type Context struct {
OrgID string
UserID string
prov Provider
inCommand bool
}
// NewContext is a convenience function.
func NewContext(orgid, userid string) *Context {
if orgid == "" || userid == "" {
log.Error("NewContext incorrect orgid:"+orgid+" userid:"+userid, errors.New("bad section context"))
}
return &Context{OrgID: orgid, UserID: userid}
}
// Register makes document section type available
func Register(name string, p Provider) {
sectionsMap[name] = p
}
// List returns available types
func List() map[string]Provider {
return sectionsMap
}
// GetSectionMeta returns a list of smart sections.
func GetSectionMeta() []TypeMeta {
sections := []TypeMeta{}
for _, section := range sectionsMap {
sections = append(sections, section.Meta())
}
return sortSections(sections)
}
// Command passes parameters to the given section id, the returned bool indicates success.
func Command(section string, ctx *Context, w http.ResponseWriter, r *http.Request) bool {
s, ok := sectionsMap[section]
if ok {
ctx.prov = s
ctx.inCommand = true
s.Command(ctx, w, r)
}
return ok
}
// Callback passes parameters to the given section callback, the returned error indicates success.
func Callback(section string, w http.ResponseWriter, r *http.Request) error {
s, ok := sectionsMap[section]
if ok {
if cb := s.Meta().Callback; cb != nil {
return cb(w, r)
}
}
return errors.New("section not found")
}
// Render runs that operation for the given section id, the returned bool indicates success.
func Render(section string, ctx *Context, config, data string) (string, bool) {
s, ok := sectionsMap[section]
if ok {
ctx.prov = s
return s.Render(ctx, config, data), true
}
return "", false
}
// Refresh returns the latest data for a section.
func Refresh(section string, ctx *Context, config, data string) (string, bool) {
s, ok := sectionsMap[section]
if ok {
ctx.prov = s
return s.Refresh(ctx, config, data), true
}
return "", false
}
// WriteJSON writes data as JSON to HTTP response.
func WriteJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
j, err := json.Marshal(v)
if err != nil {
WriteMarshalError(w, err)
return
}
_, err = w.Write(j)
log.IfErr(err)
}
// WriteString writes string tp HTTP response.
func WriteString(w http.ResponseWriter, data string) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(data))
log.IfErr(err)
}
// WriteEmpty returns just OK to HTTP response.
func WriteEmpty(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("{}"))
log.IfErr(err)
}
// WriteMarshalError write JSON marshalling error to HTTP response.
func WriteMarshalError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}"))
log.IfErr(err2)
log.Error("JSON marshall failed", err)
}
// WriteMessage write string to HTTP response.
func WriteMessage(w http.ResponseWriter, section, msg string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("{Message: " + msg + "}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Error for section %s: %s", section, msg))
}
// WriteError write given error to HTTP response.
func WriteError(w http.ResponseWriter, section string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'Internal server error'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Error for section %s", section), err)
}
// WriteForbidden write 403 to HTTP response.
func WriteForbidden(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte("{Error: 'Unauthorized'}"))
log.IfErr(err)
}
// Secrets handling
// SaveSecrets for the current user/org combination.
// The secrets must be in the form of a JSON format string, for example `{"mysecret":"lover"}`.
// An empty string signifies no valid secrets for this user/org combination.
// Note that this function can only be called within the Command method of a section.
func (c *Context) SaveSecrets(JSONobj string) error {
if !c.inCommand {
return errors.New("SaveSecrets() may only be called from within Command()")
}
m := c.prov.Meta()
return request.UserConfigSetJSON(c.OrgID, c.UserID, m.ContentType, JSONobj)
}
// MarshalSecrets to the database.
// Parameter the same as for json.Marshal().
func (c *Context) MarshalSecrets(sec interface{}) error {
if !c.inCommand {
return errors.New("MarshalSecrets() may only be called from within Command()")
}
byts, err := json.Marshal(sec)
if err != nil {
return err
}
return c.SaveSecrets(string(byts))
}
// GetSecrets for the current context user/org.
// For example (see SaveSecrets example): thisContext.GetSecrets("mysecret")
// JSONpath format is defined at https://dev.mysql.com/doc/refman/5.7/en/json-path-syntax.html .
// An empty JSONpath returns the whole JSON object, as JSON.
// Errors return the empty string.
func (c *Context) GetSecrets(JSONpath string) string {
m := c.prov.Meta()
return request.UserConfigGetJSON(c.OrgID, c.UserID, m.ContentType, JSONpath)
}
// ErrNoSecrets is returned if no secret is found in the database.
var ErrNoSecrets = errors.New("no secrets in database")
// UnmarshalSecrets from the database.
// Parameter the same as for "v" in json.Unmarshal().
func (c *Context) UnmarshalSecrets(v interface{}) error {
secTxt := c.GetSecrets("") // get all the json of the secrets
if len(secTxt) > 0 {
return json.Unmarshal([]byte(secTxt), v)
}
return ErrNoSecrets
}
// sort sections in order that that should be presented.
type sectionsToSort []TypeMeta
func (s sectionsToSort) Len() int { return len(s) }
func (s sectionsToSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sectionsToSort) Less(i, j int) bool {
if s[i].Order == s[j].Order {
return s[i].Title < s[j].Title
}
return s[i].Order > s[j].Order
}
func sortSections(in []TypeMeta) []TypeMeta {
sts := sectionsToSort(in)
sort.Sort(sts)
return []TypeMeta(sts)
}