forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_user.go
147 lines (127 loc) · 4.33 KB
/
temp_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package sqlstore
import (
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", CreateTempUser)
bus.AddHandler("sql", GetTempUsersQuery)
bus.AddHandler("sql", UpdateTempUserStatus)
bus.AddHandler("sql", GetTempUserByCode)
bus.AddHandler("sql", UpdateTempUserWithEmailSent)
bus.AddHandler("sql", ExpireOldUserInvites)
}
func UpdateTempUserStatus(cmd *models.UpdateTempUserStatusCommand) error {
return inTransaction(func(sess *DBSession) error {
var rawSQL = "UPDATE temp_user SET status=? WHERE code=?"
_, err := sess.Exec(rawSQL, string(cmd.Status), cmd.Code)
return err
})
}
func CreateTempUser(cmd *models.CreateTempUserCommand) error {
return inTransaction(func(sess *DBSession) error {
// create user
user := &models.TempUser{
Email: cmd.Email,
Name: cmd.Name,
OrgId: cmd.OrgId,
Code: cmd.Code,
Role: cmd.Role,
Status: cmd.Status,
RemoteAddr: cmd.RemoteAddr,
InvitedByUserId: cmd.InvitedByUserId,
EmailSentOn: time.Now(),
Created: time.Now().Unix(),
Updated: time.Now().Unix(),
}
if _, err := sess.Insert(user); err != nil {
return err
}
cmd.Result = user
return nil
})
}
func UpdateTempUserWithEmailSent(cmd *models.UpdateTempUserWithEmailSentCommand) error {
return inTransaction(func(sess *DBSession) error {
user := &models.TempUser{
EmailSent: true,
EmailSentOn: time.Now(),
}
_, err := sess.Where("code = ?", cmd.Code).Cols("email_sent", "email_sent_on").Update(user)
return err
})
}
func GetTempUsersQuery(query *models.GetTempUsersQuery) error {
rawSQL := `SELECT
tu.id as id,
tu.org_id as org_id,
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.status=?`
params := []interface{}{string(query.Status)}
if query.OrgId > 0 {
rawSQL += ` AND tu.org_id=?`
params = append(params, query.OrgId)
}
if query.Email != "" {
rawSQL += ` AND tu.email=?`
params = append(params, query.Email)
}
rawSQL += " ORDER BY tu.created desc"
query.Result = make([]*models.TempUserDTO, 0)
sess := x.SQL(rawSQL, params...)
err := sess.Find(&query.Result)
return err
}
func GetTempUserByCode(query *models.GetTempUserByCodeQuery) error {
var rawSQL = `SELECT
tu.id as id,
tu.org_id as org_id,
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=?`
var tempUser models.TempUserDTO
sess := x.SQL(rawSQL, query.Code)
has, err := sess.Get(&tempUser)
if err != nil {
return err
} else if !has {
return models.ErrTempUserNotFound
}
query.Result = &tempUser
return err
}
func ExpireOldUserInvites(cmd *models.ExpireTempUsersCommand) error {
return inTransaction(func(sess *DBSession) error {
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 {
return err
} else if cmd.NumExpired, err = result.RowsAffected(); err != nil {
return err
}
return nil
})
}