2016-11-16 13:47:49 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2017-01-13 18:35:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-16 13:47:49 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) NewPasswordResetRequest(req *fleet.PasswordResetRequest) (*fleet.PasswordResetRequest, error) {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
INSERT INTO password_reset_requests
|
2018-05-04 17:10:17 +00:00
|
|
|
( user_id, token, expires_at)
|
|
|
|
VALUES (?,?, NOW())
|
2016-11-16 13:47:49 +00:00
|
|
|
`
|
2021-09-01 19:50:52 +00:00
|
|
|
response, err := d.writer.Exec(sqlStatement, req.UserID, req.Token)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2017-01-13 18:35:25 +00:00
|
|
|
return nil, errors.Wrap(err, "inserting password reset requests")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := response.LastInsertId()
|
|
|
|
req.ID = uint(id)
|
|
|
|
return req, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Datastore) DeletePasswordResetRequestsForUser(userID uint) error {
|
|
|
|
sqlStatement := `
|
|
|
|
DELETE FROM password_reset_requests WHERE user_id = ?
|
|
|
|
`
|
2021-09-01 19:50:52 +00:00
|
|
|
_, err := d.writer.Exec(sqlStatement, userID)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2017-01-13 18:35:25 +00:00
|
|
|
return errors.Wrap(err, "deleting password reset request by user")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) FindPassswordResetByToken(token string) (*fleet.PasswordResetRequest, error) {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
2021-09-07 16:48:04 +00:00
|
|
|
SELECT * FROM password_reset_requests
|
|
|
|
WHERE token = ? LIMIT 1
|
|
|
|
`
|
2021-06-06 22:07:29 +00:00
|
|
|
passwordResetRequest := &fleet.PasswordResetRequest{}
|
2021-09-01 19:50:52 +00:00
|
|
|
err := d.reader.Get(passwordResetRequest, sqlStatement, token)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2017-01-13 18:35:25 +00:00
|
|
|
return nil, errors.Wrap(err, "selecting password reset requests")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return passwordResetRequest, nil
|
|
|
|
|
|
|
|
}
|