Added ability to update Passwords
This commit is contained in:
parent
75a21eeb9f
commit
96fe38726c
@ -41,7 +41,7 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
|
||||
}
|
||||
|
||||
if !permissionsOK(writer, editor, admin) {
|
||||
return fmt.Errorf("error with mutually exclusive user permissions: writer = %v, editor = %v, admin = %v",
|
||||
return fmt.Errorf("error with mutually exclusive permissions: writer = %v, editor = %v, admin = %v",
|
||||
writer, editor, admin)
|
||||
}
|
||||
|
||||
@ -58,3 +58,39 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
|
||||
var oldHashedPass string
|
||||
|
||||
selectQuery := `
|
||||
SELECT password FROM
|
||||
users
|
||||
WHERE
|
||||
id = ?
|
||||
`
|
||||
row := db.QueryRow(selectQuery, id)
|
||||
if err := row.Scan(&oldHashedPass); err != nil {
|
||||
return fmt.Errorf("error reading password from DB: %v", err)
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(oldHashedPass), []byte(oldPass)); err != nil {
|
||||
return fmt.Errorf("error checking password: %v", err)
|
||||
}
|
||||
|
||||
newHashedPass, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating password hash: %v", err)
|
||||
}
|
||||
|
||||
updateQuery := `
|
||||
UPDATE users
|
||||
SET password = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
_, err = db.Exec(updateQuery, newHashedPass, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error updating password in DB: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user