diff --git a/handler/routes.go b/handler/routes.go index 1019183..295eb9d 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -100,6 +100,63 @@ func Logout() echo.HandlerFunc { } } +// LoadProfile to load user information +func LoadProfile(db store.IStore) echo.HandlerFunc { + return func(c echo.Context) error { + + userInfo, err := db.GetUser() + if err != nil { + log.Error("Cannot get user information: ", err) + } + + return c.Render(http.StatusOK, "profile.html", map[string]interface{}{ + "baseData": model.BaseData{Active: "profile", CurrentUser: currentUser(c)}, + "userInfo": userInfo, + }) + } +} + +// UpdateProfile to update user information +func UpdateProfile(db store.IStore) echo.HandlerFunc { + return func(c echo.Context) error { + data := make(map[string]interface{}) + err := json.NewDecoder(c.Request().Body).Decode(&data) + + if err != nil { + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"}) + } + + username := data["username"].(string) + password := data["password"].(string) + + user, err := db.GetUser() + if err != nil { + return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, err.Error()}) + } + + if username == "" { + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Please provide a valid username"}) + } else { + user.Username = username + } + + if password != "" { + hash, err := util.HashPassword(password) + if err != nil { + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()}) + } + user.PasswordHash = hash + } + + if err := db.SaveUser(user); err != nil { + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()}) + } + log.Infof("Updated admin user information successfully") + + return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated admin user information successfully"}) + } +} + // WireGuardClients handler func WireGuardClients(db store.IStore) echo.HandlerFunc { return func(c echo.Context) error { diff --git a/main.go b/main.go index 2887fad..3f0cd13 100644 --- a/main.go +++ b/main.go @@ -135,6 +135,9 @@ func main() { if !util.DisableLogin { app.GET(util.BasePath+"/login", handler.LoginPage()) app.POST(util.BasePath+"/login", handler.Login(db)) + app.GET(util.BasePath+"/logout", handler.Logout(), handler.ValidSession) + app.GET(util.BasePath+"/profile", handler.LoadProfile(db), handler.ValidSession) + app.POST(util.BasePath+"/profile", handler.UpdateProfile(db), handler.ValidSession) } var sendmail emailer.Emailer @@ -145,7 +148,6 @@ func main() { } app.GET(util.BasePath+"/_health", handler.Health()) - app.GET(util.BasePath+"/logout", handler.Logout(), handler.ValidSession) app.POST(util.BasePath+"/new-client", handler.NewClient(db), handler.ValidSession, handler.ContentTypeJson) app.POST(util.BasePath+"/update-client", handler.UpdateClient(db), handler.ValidSession, handler.ContentTypeJson) app.POST(util.BasePath+"/email-client", handler.EmailClient(db, sendmail, defaultEmailSubject, defaultEmailContent), handler.ValidSession, handler.ContentTypeJson) diff --git a/router/router.go b/router/router.go index 45ee836..0f9facc 100644 --- a/router/router.go +++ b/router/router.go @@ -63,6 +63,11 @@ func New(tmplBox *rice.Box, extraData map[string]string, secret []byte) *echo.Ec log.Fatal(err) } + tmplProfileString, err := tmplBox.String("profile.html") + if err != nil { + log.Fatal(err) + } + tmplClientsString, err := tmplBox.String("clients.html") if err != nil { log.Fatal(err) @@ -94,6 +99,7 @@ func New(tmplBox *rice.Box, extraData map[string]string, secret []byte) *echo.Ec } templates := make(map[string]*template.Template) templates["login.html"] = template.Must(template.New("login").Funcs(funcs).Parse(tmplLoginString)) + templates["profile.html"] = template.Must(template.New("profile").Funcs(funcs).Parse(tmplBaseString + tmplProfileString)) templates["clients.html"] = template.Must(template.New("clients").Funcs(funcs).Parse(tmplBaseString + tmplClientsString)) templates["server.html"] = template.Must(template.New("server").Funcs(funcs).Parse(tmplBaseString + tmplServerString)) templates["global_settings.html"] = template.Must(template.New("global_settings").Funcs(funcs).Parse(tmplBaseString + tmplGlobalSettingsString)) diff --git a/store/jsondb/jsondb.go b/store/jsondb/jsondb.go index b0d39f5..bef533e 100644 --- a/store/jsondb/jsondb.go +++ b/store/jsondb/jsondb.go @@ -127,6 +127,11 @@ func (o *JsonDB) GetUser() (model.User, error) { return user, o.conn.Read("server", "users", &user) } +// SaveUser func to user info to the database +func (o *JsonDB) SaveUser(user model.User) error { + return o.conn.Write("server", "users", user) +} + // GetGlobalSettings func to query global settings from the database func (o *JsonDB) GetGlobalSettings() (model.GlobalSetting, error) { settings := model.GlobalSetting{} diff --git a/store/store.go b/store/store.go index 2dca8a7..86d6224 100644 --- a/store/store.go +++ b/store/store.go @@ -7,6 +7,7 @@ import ( type IStore interface { Init() error GetUser() (model.User, error) + SaveUser(user model.User) error GetGlobalSettings() (model.GlobalSetting, error) GetServer() (model.Server, error) GetClients(hasQRCode bool) ([]model.ClientData, error) diff --git a/templates/base.html b/templates/base.html index 03d91bf..fd337a7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -87,7 +87,11 @@
Are you sure to generate a new key pair for the Wireguard server?
- The existing Clients's peer public key need to be updated to keep the connection working.