/
admin.go
207 lines (182 loc) · 5.41 KB
/
admin.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
package handlers
import (
"net/http"
"github.com/getfider/fider/app/actions"
"github.com/getfider/fider/app/models/cmd"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/web"
)
// GeneralSettingsPage is the general settings page
func GeneralSettingsPage() web.HandlerFunc {
return func(c *web.Context) error {
return c.Page(http.StatusOK, web.Props{
Page: "Administration/pages/GeneralSettings.page",
Title: "General · Site Settings",
})
}
}
// AdvancedSettingsPage is the advanced settings page
func AdvancedSettingsPage() web.HandlerFunc {
return func(c *web.Context) error {
return c.Page(http.StatusOK, web.Props{
Page: "Administration/pages/AdvancedSettings.page",
Title: "Advanced · Site Settings",
Data: web.Map{
"customCSS": c.Tenant().CustomCSS,
},
})
}
}
// UpdateSettings update current tenant' settings
func UpdateSettings() web.HandlerFunc {
return func(c *web.Context) error {
action := actions.NewUpdateTenantSettings()
if result := c.BindTo(action); !result.Ok {
return c.HandleValidation(result)
}
if err := bus.Dispatch(c,
&cmd.UploadImage{
Image: action.Logo,
Folder: "logos",
},
&cmd.UpdateTenantSettings{
Logo: action.Logo,
Title: action.Title,
Invitation: action.Invitation,
WelcomeMessage: action.WelcomeMessage,
CNAME: action.CNAME,
Locale: action.Locale,
},
); err != nil {
return c.Failure(err)
}
return c.Ok(web.Map{})
}
}
// UpdateAdvancedSettings update current tenant' advanced settings
func UpdateAdvancedSettings() web.HandlerFunc {
return func(c *web.Context) error {
action := new(actions.UpdateTenantAdvancedSettings)
if result := c.BindTo(action); !result.Ok {
return c.HandleValidation(result)
}
if err := bus.Dispatch(c, &cmd.UpdateTenantAdvancedSettings{
CustomCSS: action.CustomCSS,
}); err != nil {
return c.Failure(err)
}
return c.Ok(web.Map{})
}
}
// UpdatePrivacy update current tenant's privacy settings
func UpdatePrivacy() web.HandlerFunc {
return func(c *web.Context) error {
action := new(actions.UpdateTenantPrivacy)
if result := c.BindTo(action); !result.Ok {
return c.HandleValidation(result)
}
updateSettings := &cmd.UpdateTenantPrivacySettings{
IsPrivate: action.IsPrivate,
}
if err := bus.Dispatch(c, updateSettings); err != nil {
return c.Failure(err)
}
return c.Ok(web.Map{})
}
}
// UpdateEmailAuthAllowed update current tenant's allow email auth settings
func UpdateEmailAuthAllowed() web.HandlerFunc {
return func(c *web.Context) error {
action := new(actions.UpdateTenantEmailAuthAllowed)
if result := c.BindTo(action); !result.Ok {
return c.HandleValidation(result)
}
updateSettings := &cmd.UpdateTenantEmailAuthAllowedSettings{
IsEmailAuthAllowed: action.IsEmailAuthAllowed,
}
if err := bus.Dispatch(c, updateSettings); err != nil {
return c.Failure(err)
}
return c.Ok(web.Map{})
}
}
// ManageMembers is the page used by administrators to change member's role
func ManageMembers() web.HandlerFunc {
return func(c *web.Context) error {
allUsers := &query.GetAllUsers{}
if err := bus.Dispatch(c, allUsers); err != nil {
return c.Failure(err)
}
return c.Page(http.StatusOK, web.Props{
Page: "Administration/pages/ManageMembers.page",
Title: "Manage Members · Site Settings",
Data: web.Map{
"users": allUsers.Result,
},
})
}
}
// ManageAuthentication is the page used by administrators to change site authentication settings
func ManageAuthentication() web.HandlerFunc {
return func(c *web.Context) error {
listProviders := &query.ListAllOAuthProviders{}
if err := bus.Dispatch(c, listProviders); err != nil {
return c.Failure(err)
}
return c.Page(http.StatusOK, web.Props{
Page: "Administration/pages/ManageAuthentication.page",
Title: "Authentication · Site Settings",
Data: web.Map{
"providers": listProviders.Result,
},
})
}
}
// GetOAuthConfig returns OAuth config based on given provider
func GetOAuthConfig() web.HandlerFunc {
return func(c *web.Context) error {
getConfig := &query.GetCustomOAuthConfigByProvider{
Provider: c.Param("provider"),
}
if err := bus.Dispatch(c, getConfig); err != nil {
return c.Failure(err)
}
return c.Ok(getConfig.Result)
}
}
// SaveOAuthConfig is used to create/edit OAuth configurations
func SaveOAuthConfig() web.HandlerFunc {
return func(c *web.Context) error {
action := actions.NewCreateEditOAuthConfig()
if result := c.BindTo(action); !result.Ok {
return c.HandleValidation(result)
}
if err := bus.Dispatch(c,
&cmd.UploadImage{
Image: action.Logo,
Folder: "logos",
},
&cmd.SaveCustomOAuthConfig{
ID: action.ID,
Logo: action.Logo,
Provider: action.Provider,
Status: action.Status,
DisplayName: action.DisplayName,
ClientID: action.ClientID,
ClientSecret: action.ClientSecret,
AuthorizeURL: action.AuthorizeURL,
TokenURL: action.TokenURL,
Scope: action.Scope,
ProfileURL: action.ProfileURL,
IsTrusted: action.IsTrusted,
JSONUserIDPath: action.JSONUserIDPath,
JSONUserNamePath: action.JSONUserNamePath,
JSONUserEmailPath: action.JSONUserEmailPath,
},
); err != nil {
return c.Failure(err)
}
return c.Ok(web.Map{})
}
}