2015-07-16 10:59:11 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2021-11-04 05:17:07 -05:00
|
|
|
"context"
|
2015-07-16 10:59:11 -05:00
|
|
|
"time"
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-07-16 10:59:11 -05:00
|
|
|
)
|
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
func (ss *SQLStore) UpdateTempUserStatus(ctx context.Context, cmd *models.UpdateTempUserStatusCommand) error {
|
|
|
|
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawSQL = "UPDATE temp_user SET status=? WHERE code=?"
|
|
|
|
_, err := sess.Exec(rawSQL, string(cmd.Status), cmd.Code)
|
2015-07-20 03:57:39 -05:00
|
|
|
return err
|
|
|
|
})
|
2015-07-16 10:59:11 -05:00
|
|
|
}
|
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
func (ss *SQLStore) CreateTempUser(ctx context.Context, cmd *models.CreateTempUserCommand) error {
|
|
|
|
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
2015-07-16 10:59:11 -05:00
|
|
|
// create user
|
2020-02-29 06:35:15 -06:00
|
|
|
user := &models.TempUser{
|
2015-07-17 02:51:34 -05:00
|
|
|
Email: cmd.Email,
|
|
|
|
Name: cmd.Name,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Code: cmd.Code,
|
2015-07-17 07:42:49 -05:00
|
|
|
Role: cmd.Role,
|
2015-07-20 03:57:39 -05:00
|
|
|
Status: cmd.Status,
|
|
|
|
RemoteAddr: cmd.RemoteAddr,
|
2015-07-17 02:51:34 -05:00
|
|
|
InvitedByUserId: cmd.InvitedByUserId,
|
2017-06-30 13:21:05 -05:00
|
|
|
EmailSentOn: time.Now(),
|
2020-10-13 05:30:09 -05:00
|
|
|
Created: time.Now().Unix(),
|
|
|
|
Updated: time.Now().Unix(),
|
2015-07-16 10:59:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Insert(user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Result = user
|
2021-11-04 05:17:07 -05:00
|
|
|
|
2015-07-16 10:59:11 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
func (ss *SQLStore) UpdateTempUserWithEmailSent(ctx context.Context, cmd *models.UpdateTempUserWithEmailSentCommand) error {
|
|
|
|
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
2020-02-29 06:35:15 -06:00
|
|
|
user := &models.TempUser{
|
2017-06-30 13:21:05 -05:00
|
|
|
EmailSent: true,
|
|
|
|
EmailSentOn: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sess.Where("code = ?", cmd.Code).Cols("email_sent", "email_sent_on").Update(user)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
func (ss *SQLStore) GetTempUsersQuery(ctx context.Context, query *models.GetTempUsersQuery) error {
|
|
|
|
return ss.WithDbSession(ctx, func(dbSess *DBSession) error {
|
|
|
|
rawSQL := `SELECT
|
2015-07-17 07:42:49 -05:00
|
|
|
tu.id as id,
|
2015-08-11 03:45:03 -05:00
|
|
|
tu.org_id as org_id,
|
2015-07-17 07:42:49 -05:00
|
|
|
tu.email as email,
|
|
|
|
tu.name as name,
|
|
|
|
tu.role as role,
|
2015-07-20 07:26:49 -05:00
|
|
|
tu.code as code,
|
2015-08-10 06:46:59 -05:00
|
|
|
tu.status as status,
|
2015-07-17 07:42:49 -05:00
|
|
|
tu.email_sent as email_sent,
|
|
|
|
tu.email_sent_on as email_sent_on,
|
|
|
|
tu.created as created,
|
2015-08-10 06:46:59 -05:00
|
|
|
u.login as invited_by_login,
|
|
|
|
u.name as invited_by_name,
|
|
|
|
u.email as invited_by_email
|
2015-07-17 07:42:49 -05:00
|
|
|
FROM ` + dialect.Quote("temp_user") + ` as tu
|
|
|
|
LEFT OUTER JOIN ` + dialect.Quote("user") + ` as u on u.id = tu.invited_by_user_id
|
2015-08-28 08:14:24 -05:00
|
|
|
WHERE tu.status=?`
|
2021-11-04 05:17:07 -05:00
|
|
|
params := []interface{}{string(query.Status)}
|
2015-08-28 08:14:24 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
if query.OrgId > 0 {
|
|
|
|
rawSQL += ` AND tu.org_id=?`
|
|
|
|
params = append(params, query.OrgId)
|
|
|
|
}
|
2015-08-28 08:14:24 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
if query.Email != "" {
|
|
|
|
rawSQL += ` AND tu.email=?`
|
|
|
|
params = append(params, query.Email)
|
|
|
|
}
|
2015-08-28 08:14:24 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
rawSQL += " ORDER BY tu.created desc"
|
2015-07-16 10:59:11 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
query.Result = make([]*models.TempUserDTO, 0)
|
|
|
|
sess := dbSess.SQL(rawSQL, params...)
|
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
|
|
|
})
|
2015-07-16 10:59:11 -05:00
|
|
|
}
|
2015-07-20 08:52:49 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
func (ss *SQLStore) GetTempUserByCode(ctx context.Context, query *models.GetTempUserByCodeQuery) error {
|
|
|
|
return ss.WithDbSession(ctx, func(dbSess *DBSession) error {
|
|
|
|
var rawSQL = `SELECT
|
2015-08-10 06:46:59 -05:00
|
|
|
tu.id as id,
|
2015-08-11 03:45:03 -05:00
|
|
|
tu.org_id as org_id,
|
2015-08-10 06:46:59 -05:00
|
|
|
tu.email as email,
|
|
|
|
tu.name as name,
|
|
|
|
tu.role as role,
|
|
|
|
tu.code as code,
|
|
|
|
tu.status as status,
|
|
|
|
tu.email_sent as email_sent,
|
|
|
|
tu.email_sent_on as email_sent_on,
|
|
|
|
tu.created as created,
|
|
|
|
u.login as invited_by_login,
|
|
|
|
u.name as invited_by_name,
|
|
|
|
u.email as invited_by_email
|
|
|
|
FROM ` + dialect.Quote("temp_user") + ` as tu
|
|
|
|
LEFT OUTER JOIN ` + dialect.Quote("user") + ` as u on u.id = tu.invited_by_user_id
|
|
|
|
WHERE tu.code=?`
|
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
var tempUser models.TempUserDTO
|
|
|
|
sess := dbSess.SQL(rawSQL, query.Code)
|
|
|
|
has, err := sess.Get(&tempUser)
|
2015-07-20 08:52:49 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return models.ErrTempUserNotFound
|
|
|
|
}
|
2015-07-20 08:52:49 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
query.Result = &tempUser
|
|
|
|
return err
|
|
|
|
})
|
2015-07-20 08:52:49 -05:00
|
|
|
}
|
2020-10-13 05:30:09 -05:00
|
|
|
|
2021-11-04 05:17:07 -05:00
|
|
|
func (ss *SQLStore) ExpireOldUserInvites(ctx context.Context, cmd *models.ExpireTempUsersCommand) error {
|
|
|
|
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawSQL = "UPDATE temp_user SET status = ?, updated = ? WHERE created <= ? AND status in (?, ?)"
|
|
|
|
if result, err := sess.Exec(rawSQL, string(models.TmpUserExpired), time.Now().Unix(), cmd.OlderThan.Unix(), string(models.TmpUserSignUpStarted), string(models.TmpUserInvitePending)); err != nil {
|
2020-10-13 05:30:09 -05:00
|
|
|
return err
|
|
|
|
} else if cmd.NumExpired, err = result.RowsAffected(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|