1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00

implement changing user password function

This commit is contained in:
Jackson 2023-11-27 14:55:43 +01:00 committed by Timur Ismagilov
parent 6c2a3c9745
commit b41acf1f57

View File

@ -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 {