122 lines
3.0 KiB
Go
122 lines
3.0 KiB
Go
package routing
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
lishwist "lishwist/core"
|
|
"lishwist/http/props"
|
|
"lishwist/http/response"
|
|
"lishwist/http/session"
|
|
"lishwist/http/templates"
|
|
|
|
"github.com/Teajey/hmc"
|
|
"github.com/Teajey/rsvp"
|
|
)
|
|
|
|
type AccountProps struct {
|
|
Navbar templates.NavCollapse
|
|
GeneralError string `json:",omitempty"`
|
|
PasswordFromAdmin bool `json:",omitempty"`
|
|
Password hmc.Input
|
|
ConfirmPassword hmc.Input
|
|
}
|
|
|
|
func (p *AccountProps) Validate() (valid bool) {
|
|
valid = true
|
|
|
|
if p.Password.Value != p.ConfirmPassword.Value {
|
|
p.ConfirmPassword.Error = "Passwords didn't match"
|
|
valid = false
|
|
}
|
|
|
|
_ = p.Password.Validate()
|
|
|
|
_ = p.ConfirmPassword.Validate()
|
|
|
|
if valid {
|
|
valid = p.Password.Error == "" && p.ConfirmPassword.Error == ""
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confirmPassVal string) *AccountProps {
|
|
return &AccountProps{
|
|
Navbar: templates.NavCollapse{
|
|
Links: []hmc.Link{{Href: "/", Label: "Home"}},
|
|
User: &templates.User{Name: username},
|
|
},
|
|
PasswordFromAdmin: passwordFromAdmin,
|
|
Password: hmc.Input{
|
|
Type: "password",
|
|
Name: "new_password",
|
|
Required: true,
|
|
MinLength: 5,
|
|
Value: passwordVal,
|
|
},
|
|
ConfirmPassword: hmc.Input{
|
|
Type: "password",
|
|
Name: "confirm_password",
|
|
Required: true,
|
|
Value: confirmPassVal,
|
|
},
|
|
}
|
|
}
|
|
|
|
func Account(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
|
user := app.User()
|
|
props := NewAccountProps(user.Name, user.PasswordFromAdmin, "", "")
|
|
|
|
flash := session.FlashGet()
|
|
|
|
flashProps, _ := flash.(*AccountProps)
|
|
if flashProps != nil {
|
|
props.GeneralError = flashProps.GeneralError
|
|
props.ConfirmPassword.Error = flashProps.ConfirmPassword.Error
|
|
}
|
|
|
|
return response.Data("account.gotmpl", props)
|
|
}
|
|
|
|
func AccountPost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
|
user := app.User()
|
|
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return response.Error("Failed to parse form").StatusBadRequest()
|
|
}
|
|
|
|
intent := r.Form.Get("intent")
|
|
if intent != "set_password" {
|
|
return response.Error("Invalid intent %q", intent).StatusBadRequest()
|
|
}
|
|
|
|
newPassword := r.Form.Get("new_password")
|
|
confirmPassword := r.Form.Get("confirm_password")
|
|
|
|
account := NewAccountProps(user.Name, user.PasswordFromAdmin, newPassword, confirmPassword)
|
|
|
|
valid := account.Validate()
|
|
account.Password.Value = ""
|
|
account.ConfirmPassword.Value = ""
|
|
if !valid {
|
|
log.Printf("Invalid account submission: %#v\n", account)
|
|
session.FlashSet(&account)
|
|
return rsvp.Data(account).StatusSeeOther("/account")
|
|
}
|
|
|
|
err = user.SetPassword(newPassword)
|
|
if err != nil {
|
|
account.GeneralError = "Something went wrong."
|
|
log.Printf("Set password failed: %s\n", err)
|
|
session.FlashSet(&account)
|
|
return rsvp.Data(account).StatusSeeOther("/account")
|
|
}
|
|
|
|
session.RemoveValue("sessionKey")
|
|
|
|
session.FlashSet(&props.Login{SuccessfulSetPassword: true})
|
|
return rsvp.Data("Set password successful!").StatusSeeOther("/")
|
|
}
|