2020-06-07 20:28:32 +03:00
|
|
|
|
package routes
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/gorilla/schema"
|
2020-10-09 22:37:16 +03:00
|
|
|
|
conf "github.com/muety/wakapi/config"
|
2020-06-07 20:28:32 +03:00
|
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-06 23:19:54 +03:00
|
|
|
|
"github.com/muety/wakapi/models/view"
|
2020-06-07 20:28:32 +03:00
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
|
"github.com/muety/wakapi/utils"
|
2020-11-06 19:09:41 +03:00
|
|
|
|
"log"
|
2020-06-07 20:28:32 +03:00
|
|
|
|
"net/http"
|
2020-10-27 00:34:50 +03:00
|
|
|
|
"strconv"
|
2020-06-07 20:28:32 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SettingsHandler struct {
|
2020-11-01 22:14:10 +03:00
|
|
|
|
config *conf.Config
|
2020-11-08 12:12:49 +03:00
|
|
|
|
userSrvc services.IUserService
|
|
|
|
|
summarySrvc services.ISummaryService
|
|
|
|
|
aggregationSrvc services.IAggregationService
|
|
|
|
|
languageMappingSrvc services.ILanguageMappingService
|
2020-06-07 20:28:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var credentialsDecoder = schema.NewDecoder()
|
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
|
func NewSettingsHandler(userService services.IUserService, summaryService services.ISummaryService, aggregationService services.IAggregationService, languageMappingService services.ILanguageMappingService) *SettingsHandler {
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return &SettingsHandler{
|
2020-11-01 22:14:10 +03:00
|
|
|
|
config: conf.Get(),
|
2020-11-06 19:09:41 +03:00
|
|
|
|
summarySrvc: summaryService,
|
|
|
|
|
aggregationSrvc: aggregationService,
|
2020-11-01 22:14:10 +03:00
|
|
|
|
languageMappingSrvc: languageMappingService,
|
|
|
|
|
userSrvc: userService,
|
2020-06-07 20:28:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *SettingsHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if h.config.IsDev() {
|
|
|
|
|
loadTemplates()
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *SettingsHandler) PostCredentials(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if h.config.IsDev() {
|
|
|
|
|
loadTemplates()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
|
|
|
|
|
var credentials models.CredentialsReset
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("missing parameters"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := credentialsDecoder.Decode(&credentials, r.PostForm); err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("missing parameters"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 17:11:14 +03:00
|
|
|
|
if !utils.CompareBcrypt(user.Password, credentials.PasswordOld, h.config.Security.PasswordSalt) {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("invalid credentials"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !credentials.IsValid() {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("invalid parameters"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Password = credentials.PasswordNew
|
2020-10-16 17:11:14 +03:00
|
|
|
|
if hash, err := utils.HashBcrypt(user.Password, h.config.Security.PasswordSalt); err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("internal server error"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return
|
2020-10-16 17:11:14 +03:00
|
|
|
|
} else {
|
|
|
|
|
user.Password = hash
|
2020-06-07 20:28:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := h.userSrvc.Update(user); err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("internal server error"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
login := &models.Login{
|
|
|
|
|
Username: user.ID,
|
|
|
|
|
Password: user.Password,
|
|
|
|
|
}
|
2020-10-04 11:37:38 +03:00
|
|
|
|
encoded, err := h.config.Security.SecureCookie.Encode(models.AuthCookieKey, login)
|
2020-06-07 20:28:32 +03:00
|
|
|
|
if err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("internal server error"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 00:30:56 +03:00
|
|
|
|
http.SetCookie(w, h.config.CreateCookie(models.AuthCookieKey, encoded, "/"))
|
2020-11-06 23:19:54 +03:00
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithSuccess("password was updated successfully"))
|
2020-10-25 09:22:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
|
func (h *SettingsHandler) DeleteLanguageMapping(w http.ResponseWriter, r *http.Request) {
|
2020-10-25 09:22:10 +03:00
|
|
|
|
if h.config.IsDev() {
|
|
|
|
|
loadTemplates()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
2020-11-01 22:14:10 +03:00
|
|
|
|
id, err := strconv.Atoi(r.PostFormValue("mapping_id"))
|
2020-10-25 09:22:10 +03:00
|
|
|
|
if err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("could not delete mapping"))
|
2020-10-25 09:22:10 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
|
mapping := &models.LanguageMapping{
|
|
|
|
|
ID: uint(id),
|
2020-10-25 09:22:10 +03:00
|
|
|
|
UserID: user.ID,
|
2020-10-27 00:34:50 +03:00
|
|
|
|
}
|
2020-11-01 18:56:36 +03:00
|
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
|
err = h.languageMappingSrvc.Delete(mapping)
|
2020-11-01 18:56:36 +03:00
|
|
|
|
if err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("could not delete mapping"))
|
2020-11-01 18:56:36 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithSuccess("mapping deleted successfully"))
|
2020-10-25 09:22:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 19:09:41 +03:00
|
|
|
|
func (h *SettingsHandler) PostLanguageMapping(w http.ResponseWriter, r *http.Request) {
|
2020-10-25 09:22:10 +03:00
|
|
|
|
if h.config.IsDev() {
|
|
|
|
|
loadTemplates()
|
|
|
|
|
}
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
extension := r.PostFormValue("extension")
|
|
|
|
|
language := r.PostFormValue("language")
|
|
|
|
|
|
|
|
|
|
if extension[0] == '.' {
|
|
|
|
|
extension = extension[1:]
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
|
mapping := &models.LanguageMapping{
|
2020-10-27 00:34:50 +03:00
|
|
|
|
UserID: user.ID,
|
2020-10-25 09:22:10 +03:00
|
|
|
|
Extension: extension,
|
2020-10-27 00:34:50 +03:00
|
|
|
|
Language: language,
|
|
|
|
|
}
|
2020-10-25 09:22:10 +03:00
|
|
|
|
|
2020-11-01 22:14:10 +03:00
|
|
|
|
if _, err := h.languageMappingSrvc.Create(mapping); err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("mapping already exists"))
|
2020-10-25 09:22:10 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithSuccess("mapping added successfully"))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *SettingsHandler) PostResetApiKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if h.config.IsDev() {
|
|
|
|
|
loadTemplates()
|
|
|
|
|
}
|
2020-06-07 20:58:06 +03:00
|
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
if _, err := h.userSrvc.ResetApiKey(user); err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("internal server error"))
|
2020-06-07 20:58:06 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 12:12:49 +03:00
|
|
|
|
msg := fmt.Sprintf("your new api key is: %s", user.ApiKey)
|
2020-11-06 23:19:54 +03:00
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithSuccess(msg))
|
2020-06-07 20:28:32 +03:00
|
|
|
|
}
|
2020-09-12 17:09:23 +03:00
|
|
|
|
|
|
|
|
|
func (h *SettingsHandler) PostToggleBadges(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if h.config.IsDev() {
|
|
|
|
|
loadTemplates()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
if _, err := h.userSrvc.ToggleBadges(user); err != nil {
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("internal server error"))
|
2020-09-12 17:09:23 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r))
|
2020-09-12 17:09:23 +03:00
|
|
|
|
}
|
2020-11-06 19:09:41 +03:00
|
|
|
|
|
|
|
|
|
func (h *SettingsHandler) PostRegenerateSummaries(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if h.config.IsDev() {
|
|
|
|
|
loadTemplates()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
|
|
|
|
|
log.Printf("clearing summaries for user '%s'\n", user.ID)
|
|
|
|
|
if err := h.summarySrvc.DeleteByUser(user.ID); err != nil {
|
|
|
|
|
log.Printf("failed to clear summaries: %v\n", err)
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("failed to delete old summaries"))
|
2020-11-06 19:09:41 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := h.aggregationSrvc.Run(map[string]bool{user.ID: true}); err != nil {
|
|
|
|
|
log.Printf("failed to regenerate summaries: %v\n", err)
|
2020-11-06 23:19:54 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithError("failed to generate aggregations"))
|
2020-11-06 19:09:41 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
|
templates[conf.SettingsTemplate].Execute(w, h.buildViewModel(r).WithSuccess("summaries are being regenerated – this may take a few seconds"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *SettingsHandler) buildViewModel(r *http.Request) *view.SettingsViewModel {
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
mappings, _ := h.languageMappingSrvc.GetByUser(user.ID)
|
|
|
|
|
return &view.SettingsViewModel{
|
|
|
|
|
User: user,
|
|
|
|
|
LanguageMappings: mappings,
|
|
|
|
|
Success: r.URL.Query().Get("success"),
|
|
|
|
|
Error: r.URL.Query().Get("error"),
|
|
|
|
|
}
|
2020-11-06 19:09:41 +03:00
|
|
|
|
}
|