-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
291 lines (233 loc) · 6.87 KB
/
endpoint.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
// 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 setting manages both global and user level settings
package setting
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/core/request"
"github.com/documize/community/core/response"
"github.com/documize/community/core/streamutil"
"github.com/documize/community/domain"
"github.com/documize/community/domain/smtp"
"github.com/documize/community/domain/store"
"github.com/documize/community/model/audit"
)
// Handler contains the runtime information such as logging and database.
type Handler struct {
Runtime *env.Runtime
Store *store.Store
}
// SMTP returns installation-wide SMTP settings
func (h *Handler) SMTP(w http.ResponseWriter, r *http.Request) {
method := "setting.SMTP"
ctx := domain.GetRequestContext(r)
if !ctx.GlobalAdmin {
response.WriteForbiddenError(w)
return
}
config, _ := h.Store.Setting.Get("SMTP", "")
var y map[string]interface{}
json.Unmarshal([]byte(config), &y)
j, err := json.Marshal(y)
if err != nil {
response.WriteBadRequestError(w, method, err.Error())
h.Runtime.Log.Error(method, err)
return
}
response.WriteBytes(w, j)
}
// SetSMTP persists global SMTP configuration.
func (h *Handler) SetSMTP(w http.ResponseWriter, r *http.Request) {
method := "setting.SetSMTP"
ctx := domain.GetRequestContext(r)
if !ctx.GlobalAdmin {
response.WriteForbiddenError(w)
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
response.WriteBadRequestError(w, method, err.Error())
h.Runtime.Log.Error(method, err)
return
}
var config string
config = string(body)
// ctx.Transaction, err = h.Runtime.Db.Beginx()
// if err != nil {
// response.WriteServerError(w, method, err)
// h.Runtime.Log.Error(method, err)
// return
// }
h.Store.Setting.Set("SMTP", config)
// ctx.Transaction.Commit()
h.Store.Audit.Record(ctx, audit.EventTypeSystemSMTP)
// test connection
var result struct {
Success bool `json:"success"`
Message string `json:"message"`
}
result.Message = "Email sent successfully!"
u, err := h.Store.User.Get(ctx, ctx.UserID)
if err != nil {
result.Success = false
result.Message = err.Error()
h.Runtime.Log.Error(method, err)
response.WriteJSON(w, result)
return
}
cfg := GetSMTPConfig(h.Store)
h.Runtime.Log.Infof("%v", cfg)
dialer, err := smtp.Connect(cfg)
em := smtp.EmailMessage{}
em.Subject = "Documize SMTP Test"
em.BodyHTML = "<p>This is a test email from Documize using current SMTP settings.</p>"
em.ToEmail = u.Email
em.ToName = u.Fullname()
result.Success, err = smtp.SendMessage(dialer, cfg, em)
if !result.Success {
result.Message = fmt.Sprintf("Unable to send test email: %s", err.Error())
}
response.WriteJSON(w, result)
}
// AuthConfig returns installation-wide auth configuration
func (h *Handler) AuthConfig(w http.ResponseWriter, r *http.Request) {
method := "global.auth"
ctx := domain.GetRequestContext(r)
if !ctx.GlobalAdmin {
response.WriteForbiddenError(w)
return
}
org, err := h.Store.Organization.GetOrganization(ctx, ctx.OrgID)
if err != nil {
response.WriteForbiddenError(w)
h.Runtime.Log.Error(method, err)
return
}
response.WriteJSON(w, org.AuthConfig)
}
// SetAuthConfig persists installation-wide authentication configuration
func (h *Handler) SetAuthConfig(w http.ResponseWriter, r *http.Request) {
method := "global.auth.save"
ctx := domain.GetRequestContext(r)
if !ctx.GlobalAdmin {
response.WriteForbiddenError(w)
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
response.WriteBadRequestError(w, method, err.Error())
return
}
var data authData
err = json.Unmarshal(body, &data)
if err != nil {
response.WriteBadRequestError(w, method, err.Error())
h.Runtime.Log.Error(method, err)
return
}
org, err := h.Store.Organization.GetOrganization(ctx, ctx.OrgID)
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
org.AuthProvider = data.AuthProvider
org.AuthConfig = data.AuthConfig
ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
err = h.Store.Organization.UpdateAuthConfig(ctx, org)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
ctx.Transaction.Commit()
h.Store.Audit.Record(ctx, audit.EventTypeSystemAuth)
response.WriteEmpty(w)
}
// GetInstanceSetting returns the requested organization level setting.
func (h *Handler) GetInstanceSetting(w http.ResponseWriter, r *http.Request) {
ctx := domain.GetRequestContext(r)
orgID := request.Param(r, "orgID")
if orgID != ctx.OrgID || !ctx.Administrator {
response.WriteForbiddenError(w)
return
}
key := request.Query(r, "key")
setting, _ := h.Store.Setting.GetUser(orgID, "", key, "")
if len(setting) == 0 {
setting = "{}"
}
response.WriteJSON(w, setting)
}
// SaveInstanceSetting saves org level setting.
func (h *Handler) SaveInstanceSetting(w http.ResponseWriter, r *http.Request) {
method := "org.SaveInstanceSetting"
ctx := domain.GetRequestContext(r)
orgID := request.Param(r, "orgID")
if orgID != ctx.OrgID || !ctx.Administrator {
response.WriteForbiddenError(w)
return
}
key := request.Query(r, "key")
defer streamutil.Close(r.Body)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
config := string(body)
h.Store.Setting.SetUser(orgID, "", key, config)
response.WriteEmpty(w)
}
// GetGlobalSetting returns the requested organization level setting.
func (h *Handler) GetGlobalSetting(w http.ResponseWriter, r *http.Request) {
ctx := domain.GetRequestContext(r)
if !ctx.GlobalAdmin {
response.WriteForbiddenError(w)
return
}
key := request.Query(r, "key")
setting, _ := h.Store.Setting.Get(key, "")
response.WriteJSON(w, setting)
}
// SaveGlobalSetting saves org level setting.
func (h *Handler) SaveGlobalSetting(w http.ResponseWriter, r *http.Request) {
method := "org.SaveGlobalSetting"
ctx := domain.GetRequestContext(r)
if !ctx.GlobalAdmin {
response.WriteForbiddenError(w)
return
}
key := request.Query(r, "key")
defer streamutil.Close(r.Body)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
config := string(body)
h.Store.Setting.Set(key, config)
response.WriteEmpty(w)
}