mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-04 18:19:54 +00:00
implement changing user password function
This commit is contained in:
parent
6c2a3c9745
commit
b41acf1f57
15
user/user.go
15
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user