/
AdminController.go
233 lines (192 loc) · 6.22 KB
/
AdminController.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
// Copyright 2013-2018 Adam Presley. All rights reserved
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package controllers
import (
"fmt"
"net/http"
"sync"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/labstack/echo-contrib/session"
"github.com/mailslurper/mailslurper/pkg/auth/auth"
"github.com/mailslurper/mailslurper/pkg/auth/authfactory"
"github.com/mailslurper/mailslurper/pkg/cache"
"github.com/mailslurper/mailslurper/pkg/contexts"
"github.com/mailslurper/mailslurper/pkg/mailslurper"
"github.com/mailslurper/mailslurper/pkg/ui"
"github.com/sirupsen/logrus"
)
/*
AdminController provides methods for handling admin endpoints.
This is to primarily support the front-end
*/
type AdminController struct {
CacheService cache.ICacheService
Config *mailslurper.Configuration
ConfigFileName string
DebugMode bool
Renderer *ui.TemplateRenderer
Lock *sync.Mutex
Logger *logrus.Entry
ServerVersion string
}
/*
Admin is the page for performing administrative tasks in MailSlurper
*/
func (c *AdminController) Admin(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
data := mailslurper.Page{
PublicWWWURL: c.Config.GetPublicWWWURL(),
Theme: c.Config.GetTheme(),
Title: "Admin",
User: context.User,
}
return context.Render(http.StatusOK, "mainLayout:admin", data)
}
/*
ApplyTheme updates the theme in the config file, and refreshes the renderer
POST: /theme
*/
func (c *AdminController) ApplyTheme(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
c.Lock.Lock()
defer c.Lock.Unlock()
var err error
var applyThemeRequest *mailslurper.ApplyThemeRequest
if applyThemeRequest, err = mailslurper.NewApplyThemeRequest(context); err != nil {
return context.String(http.StatusBadRequest, "Invalid request")
}
c.Config.Theme = applyThemeRequest.Theme
if err = c.Config.SaveConfiguration(c.ConfigFileName); err != nil {
c.Logger.Errorf("Error saving configuration file in ApplyTheme: %s", err.Error())
return context.String(http.StatusOK, fmt.Sprintf("Error saving configuration file: %s", err.Error()))
}
c.Renderer.LoadTemplates(c.DebugMode)
return context.String(http.StatusOK, "OK")
}
/*
Index is the main view. This endpoint provides the email list and email detail
views.
*/
func (c *AdminController) Index(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
data := mailslurper.Page{
PublicWWWURL: c.Config.GetPublicWWWURL(),
Theme: c.Config.GetTheme(),
Title: "Mail",
User: context.User,
}
return context.Render(http.StatusOK, "mainLayout:index", data)
}
/*
Login renders the login page
*/
func (c *AdminController) Login(ctx echo.Context) error {
data := mailslurper.Page{
PublicWWWURL: c.Config.GetPublicWWWURL(),
Theme: c.Config.GetTheme(),
}
if ctx.QueryParam("message") != "" {
data.Message = ctx.QueryParam("message")
data.Error = true
}
return ctx.Render(http.StatusOK, "loginLayout:login", data)
}
/*
ManageSavedSearches is the page for managing saved searches
*/
func (c *AdminController) ManageSavedSearches(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
data := mailslurper.Page{
PublicWWWURL: c.Config.GetPublicWWWURL(),
Theme: c.Config.GetTheme(),
Title: "Manage Saved Searches",
User: context.User,
}
return context.Render(http.StatusOK, "mainLayout:manageSavedSearches", data)
}
/*
GetPruneOptions returns a set of valid pruning options.
GET: /v1/pruneoptions
*/
func (c *AdminController) GetPruneOptions(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
return context.JSON(http.StatusOK, mailslurper.PruneOptions)
}
/*
GetServiceSettings returns the settings necessary to talk to the MailSlurper
back-end service tier.
*/
func (c *AdminController) GetServiceSettings(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
settings := mailslurper.ServiceSettings{
AuthenticationScheme: c.Config.AuthenticationScheme,
URL: c.Config.GetPublicServiceURL(),
Version: c.ServerVersion,
}
return context.JSON(http.StatusOK, settings)
}
/*
GetVersion outputs the current running version of this MailSlurper server instance
*/
func (c *AdminController) GetVersion(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
result := mailslurper.Version{
Version: c.ServerVersion,
}
return context.JSON(http.StatusOK, result)
}
/*
GetVersionFromMaster returns the current MailSlurper version from GitHub
*/
func (c *AdminController) GetVersionFromMaster(ctx echo.Context) error {
context := contexts.GetAdminContext(ctx)
var err error
var result *mailslurper.Version
if result, err = mailslurper.GetServerVersionFromMaster(); err != nil {
c.Logger.Errorf("Error getting version file from Github: %s", err.Error())
return context.String(http.StatusInternalServerError, "There was an error reading the version file from GitHub")
}
return context.JSON(http.StatusOK, result)
}
/*
PerformLogin validates credentials, and if correct, sets the session
and redirects to the home page
*/
func (c *AdminController) PerformLogin(ctx echo.Context) error {
var err error
var authProvider auth.IAuthProvider
authFactory := &authfactory.AuthFactory{
Config: c.Config,
}
authProvider = authFactory.Get()
credentials := &auth.AuthCredentials{
UserName: ctx.FormValue("userName"),
Password: ctx.FormValue("password"),
}
if err = authProvider.Login(credentials); err != nil {
c.Logger.WithError(err).Errorf("Admin authentication error")
return ctx.Redirect(http.StatusFound, "/login?message=Invalid user name or password")
}
s, _ := session.Get("session", ctx)
s.Options = &sessions.Options{
Path: "/",
MaxAge: c.Config.AuthTimeoutInMinutes,
}
s.Values["user"] = credentials.UserName
s.Save(ctx.Request(), ctx.Response())
return ctx.Redirect(http.StatusFound, "/")
}
/*
Logout logs a user out, and deletes the cookie
*/
func (c *AdminController) Logout(ctx echo.Context) error {
s, _ := session.Get("session", ctx)
s.Options = &sessions.Options{
Path: "/",
MaxAge: -1,
}
s.Save(ctx.Request(), ctx.Response())
return ctx.Redirect(http.StatusFound, "/login")
}