2015-07-16 10:59:11 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-07-20 03:57:39 -05:00
|
|
|
"github.com/go-xorm/xorm"
|
2015-07-16 10:59:11 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", CreateTempUser)
|
|
|
|
bus.AddHandler("sql", GetTempUsersForOrg)
|
2015-07-20 03:57:39 -05:00
|
|
|
bus.AddHandler("sql", UpdateTempUserStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateTempUserStatus(cmd *m.UpdateTempUserStatusCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
var rawSql = "UPDATE temp_user SET status=? WHERE id=? and org_id=?"
|
|
|
|
_, err := sess.Exec(rawSql, string(cmd.Status), cmd.Id, cmd.OrgId)
|
|
|
|
return err
|
|
|
|
})
|
2015-07-16 10:59:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTempUser(cmd *m.CreateTempUserCommand) error {
|
|
|
|
return inTransaction2(func(sess *session) error {
|
|
|
|
|
|
|
|
// create user
|
|
|
|
user := &m.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,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2015-07-16 10:59:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Insert(user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Result = user
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTempUsersForOrg(query *m.GetTempUsersForOrgQuery) error {
|
2015-07-17 07:42:49 -05:00
|
|
|
var rawSql = `SELECT
|
|
|
|
tu.id as id,
|
|
|
|
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-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,
|
|
|
|
u.login as invited_by
|
|
|
|
FROM ` + dialect.Quote("temp_user") + ` as tu
|
|
|
|
LEFT OUTER JOIN ` + dialect.Quote("user") + ` as u on u.id = tu.invited_by_user_id
|
2015-07-20 03:57:39 -05:00
|
|
|
WHERE tu.org_id=? AND tu.status =? ORDER BY tu.created desc`
|
2015-07-16 10:59:11 -05:00
|
|
|
|
2015-07-17 07:42:49 -05:00
|
|
|
query.Result = make([]*m.TempUserDTO, 0)
|
2015-07-20 03:57:39 -05:00
|
|
|
sess := x.Sql(rawSql, query.OrgId, string(query.Status))
|
2015-07-16 10:59:11 -05:00
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
|
|
|
}
|