diff --git a/user/user.go b/user/user.go index 2854e6d..72a2c25 100644 --- a/user/user.go +++ b/user/user.go @@ -1,6 +1,7 @@ package user import ( + "fmt" "net/http" "strings" "sync" @@ -135,6 +136,20 @@ func (user *User) ShowLockMaybe(w http.ResponseWriter, rq *http.Request) bool { return false } +// Sets a new password for the user. +func (user *User) ChangePassword(password string) error { + if user.Source != "local" { + return fmt.Errorf("Only local users can change their passwords.") + } + + hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + return err + } + user.Password = string(hash) + return SaveUserDatabase() +} + // IsValidUsername checks if the given username is valid. func IsValidUsername(username string) bool { for _, r := range username {