2015-02-23 13:07:49 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2017-08-09 03:36:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2015-02-23 13:07:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", AddOrgUser)
|
|
|
|
bus.AddHandler("sql", RemoveOrgUser)
|
|
|
|
bus.AddHandler("sql", GetOrgUsers)
|
2015-05-01 02:48:07 -05:00
|
|
|
bus.AddHandler("sql", UpdateOrgUser)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddOrgUser(cmd *m.AddOrgUserCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-07-21 05:18:11 -05:00
|
|
|
// check if user exists
|
|
|
|
if res, err := sess.Query("SELECT 1 from org_user WHERE org_id=? and user_id=?", cmd.OrgId, cmd.UserId); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(res) == 1 {
|
|
|
|
return m.ErrOrgUserAlreadyAdded
|
|
|
|
}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
2016-06-07 22:46:21 -05:00
|
|
|
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.OrgId); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(res) != 1 {
|
|
|
|
return m.ErrOrgNotFound
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
entity := m.OrgUser{
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
UserId: cmd.UserId,
|
|
|
|
Role: cmd.Role,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sess.Insert(&entity)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:48:07 -05:00
|
|
|
func UpdateOrgUser(cmd *m.UpdateOrgUserCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-05-01 02:48:07 -05:00
|
|
|
var orgUser m.OrgUser
|
|
|
|
exists, err := sess.Where("org_id=? AND user_id=?", cmd.OrgId, cmd.UserId).Get(&orgUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return m.ErrOrgUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
orgUser.Role = cmd.Role
|
|
|
|
orgUser.Updated = time.Now()
|
|
|
|
_, err = sess.Id(orgUser.Id).Update(&orgUser)
|
2015-05-19 04:47:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateOneAdminLeftInOrg(cmd.OrgId, sess)
|
2015-05-01 02:48:07 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
func GetOrgUsers(query *m.GetOrgUsersQuery) error {
|
|
|
|
query.Result = make([]*m.OrgUserDTO, 0)
|
|
|
|
sess := x.Table("org_user")
|
2015-02-24 11:32:29 -06:00
|
|
|
sess.Join("INNER", "user", fmt.Sprintf("org_user.user_id=%s.id", x.Dialect().Quote("user")))
|
2015-02-23 13:07:49 -06:00
|
|
|
sess.Where("org_user.org_id=?", query.OrgId)
|
2017-08-09 03:36:41 -05:00
|
|
|
sess.Cols("org_user.org_id", "org_user.user_id", "user.email", "user.login", "org_user.role", "user.last_seen_at")
|
2015-02-23 13:07:49 -06:00
|
|
|
sess.Asc("user.email", "user.login")
|
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
if err := sess.Find(&query.Result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range query.Result {
|
|
|
|
user.LastSeenAtAge = util.GetAgeString(user.LastSeenAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveOrgUser(cmd *m.RemoveOrgUserCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-02-23 13:07:49 -06:00
|
|
|
var rawSql = "DELETE FROM org_user WHERE org_id=? and user_id=?"
|
|
|
|
_, err := sess.Exec(rawSql, cmd.OrgId, cmd.UserId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-19 04:47:14 -05:00
|
|
|
return validateOneAdminLeftInOrg(cmd.OrgId, sess)
|
|
|
|
})
|
|
|
|
}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
2017-05-23 03:56:23 -05:00
|
|
|
func validateOneAdminLeftInOrg(orgId int64, sess *DBSession) error {
|
2015-05-19 04:47:14 -05:00
|
|
|
// validate that there is an admin user left
|
|
|
|
res, err := sess.Query("SELECT 1 from org_user WHERE org_id=? and role='Admin'", orgId)
|
|
|
|
if err != nil {
|
2015-02-23 13:07:49 -06:00
|
|
|
return err
|
2015-05-19 04:47:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(res) == 0 {
|
|
|
|
return m.ErrLastOrgAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|