2016-11-16 13:47:49 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-09-14 14:44:02 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2017-01-13 18:35:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-16 13:47:49 +00:00
|
|
|
)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) NewPasswordResetRequest(ctx context.Context, 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-14 14:44:02 +00:00
|
|
|
response, err := d.writer.ExecContext(ctx, 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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) DeletePasswordResetRequestsForUser(ctx context.Context, userID uint) error {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
DELETE FROM password_reset_requests WHERE user_id = ?
|
|
|
|
`
|
2021-09-14 14:44:02 +00:00
|
|
|
_, err := d.writer.ExecContext(ctx, 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-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) FindPassswordResetByToken(ctx context.Context, 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-14 14:44:02 +00:00
|
|
|
err := sqlx.GetContext(ctx, d.reader, 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
|
|
|
|
|
|
|
|
}
|