2015-01-19 09:28:45 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2015-02-11 09:47:22 -06:00
|
|
|
"fmt"
|
2015-01-19 11:01:04 -06:00
|
|
|
"strings"
|
2015-01-19 09:28:45 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/events"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2015-01-19 09:28:45 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", CreateUser)
|
2015-02-10 08:36:51 -06:00
|
|
|
bus.AddHandler("sql", GetUserById)
|
2015-01-19 11:57:51 -06:00
|
|
|
bus.AddHandler("sql", UpdateUser)
|
2015-02-19 09:09:49 -06:00
|
|
|
bus.AddHandler("sql", ChangeUserPassword)
|
2015-01-19 11:01:04 -06:00
|
|
|
bus.AddHandler("sql", GetUserByLogin)
|
2015-02-23 13:07:49 -06:00
|
|
|
bus.AddHandler("sql", SetUsingOrg)
|
2015-02-28 07:30:08 -06:00
|
|
|
bus.AddHandler("sql", GetUserProfile)
|
2015-01-19 11:01:04 -06:00
|
|
|
bus.AddHandler("sql", GetSignedInUser)
|
|
|
|
bus.AddHandler("sql", SearchUsers)
|
2015-02-23 13:07:49 -06:00
|
|
|
bus.AddHandler("sql", GetUserOrgList)
|
2015-02-11 09:47:22 -06:00
|
|
|
bus.AddHandler("sql", DeleteUser)
|
2015-02-23 13:07:49 -06:00
|
|
|
bus.AddHandler("sql", SetUsingOrg)
|
2015-02-26 08:43:48 -06:00
|
|
|
bus.AddHandler("sql", UpdateUserPermissions)
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
func getOrgIdForNewUser(userEmail string, sess *session) (int64, error) {
|
|
|
|
var org m.Org
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2015-03-11 10:19:29 -05:00
|
|
|
if setting.AutoAssignOrg {
|
|
|
|
// right now auto assign to org with id 1
|
|
|
|
has, err := sess.Where("id=?", 1).Get(&org)
|
2015-01-27 08:14:53 -06:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
2015-01-27 08:14:53 -06:00
|
|
|
if has {
|
2015-02-23 13:07:49 -06:00
|
|
|
return org.Id, nil
|
2015-01-27 08:14:53 -06:00
|
|
|
} else {
|
2015-03-12 13:43:52 -05:00
|
|
|
org.Name = "Main Org."
|
2015-03-11 10:19:29 -05:00
|
|
|
org.Id = 1
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
} else {
|
2015-02-23 13:07:49 -06:00
|
|
|
org.Name = userEmail
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
org.Created = time.Now()
|
|
|
|
org.Updated = time.Now()
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
if _, err := sess.Insert(&org); err != nil {
|
2015-01-27 08:14:53 -06:00
|
|
|
return 0, err
|
|
|
|
}
|
2015-03-17 10:10:49 -05:00
|
|
|
|
2015-03-17 10:07:08 -05:00
|
|
|
sess.publishAfterCommit(&events.OrgCreated{
|
|
|
|
Timestamp: org.Created,
|
|
|
|
Id: org.Id,
|
|
|
|
Name: org.Name,
|
|
|
|
})
|
2015-03-17 10:10:49 -05:00
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
return org.Id, nil
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateUser(cmd *m.CreateUserCommand) error {
|
2015-02-04 10:15:05 -06:00
|
|
|
return inTransaction2(func(sess *session) error {
|
2015-02-23 13:07:49 -06:00
|
|
|
orgId, err := getOrgIdForNewUser(cmd.Email, sess)
|
2015-01-27 08:14:53 -06:00
|
|
|
if err != nil {
|
2015-01-19 09:28:45 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create user
|
|
|
|
user := m.User{
|
2015-02-23 13:07:49 -06:00
|
|
|
Email: cmd.Email,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Login: cmd.Login,
|
|
|
|
Company: cmd.Company,
|
|
|
|
IsAdmin: cmd.IsAdmin,
|
|
|
|
OrgId: orgId,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 04:35:59 -06:00
|
|
|
if len(cmd.Password) > 0 {
|
|
|
|
user.Salt = util.GetRandomString(10)
|
|
|
|
user.Rands = util.GetRandomString(10)
|
|
|
|
user.Password = util.EncodePassword(cmd.Password, user.Salt)
|
|
|
|
}
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2015-01-19 09:28:45 -06:00
|
|
|
sess.UseBool("is_admin")
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2015-01-19 09:28:45 -06:00
|
|
|
if _, err := sess.Insert(&user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
// create org user link
|
|
|
|
orgUser := m.OrgUser{
|
|
|
|
OrgId: orgId,
|
|
|
|
UserId: user.Id,
|
|
|
|
Role: m.ROLE_ADMIN,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
|
2015-03-11 10:19:29 -05:00
|
|
|
if setting.AutoAssignOrg && !user.IsAdmin {
|
|
|
|
orgUser.Role = m.RoleType(setting.AutoAssignOrgRole)
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
if _, err = sess.Insert(&orgUser); err != nil {
|
2015-02-04 10:15:05 -06:00
|
|
|
return err
|
|
|
|
}
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2015-02-04 10:15:05 -06:00
|
|
|
sess.publishAfterCommit(&events.UserCreated{
|
2015-02-03 09:57:42 -06:00
|
|
|
Timestamp: user.Created,
|
2015-02-04 10:15:05 -06:00
|
|
|
Id: user.Id,
|
|
|
|
Name: user.Name,
|
|
|
|
Login: user.Login,
|
|
|
|
Email: user.Email,
|
2015-02-03 09:57:42 -06:00
|
|
|
})
|
2015-01-19 09:28:45 -06:00
|
|
|
|
|
|
|
cmd.Result = user
|
2015-02-04 10:15:05 -06:00
|
|
|
return nil
|
2015-01-19 09:28:45 -06:00
|
|
|
})
|
|
|
|
}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
2015-02-10 08:36:51 -06:00
|
|
|
func GetUserById(query *m.GetUserByIdQuery) error {
|
|
|
|
user := new(m.User)
|
|
|
|
has, err := x.Id(query.Id).Get(user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = user
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
func GetUserByLogin(query *m.GetUserByLoginQuery) error {
|
|
|
|
if query.LoginOrEmail == "" {
|
2015-02-10 08:36:51 -06:00
|
|
|
return m.ErrUserNotFound
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
user := new(m.User)
|
|
|
|
if strings.Contains(query.LoginOrEmail, "@") {
|
|
|
|
user = &m.User{Email: query.LoginOrEmail}
|
|
|
|
} else {
|
2015-02-18 06:00:39 -06:00
|
|
|
user = &m.User{Login: query.LoginOrEmail}
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
has, err := x.Get(user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = user
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:57:51 -06:00
|
|
|
func UpdateUser(cmd *m.UpdateUserCommand) error {
|
2015-02-04 10:15:05 -06:00
|
|
|
return inTransaction2(func(sess *session) error {
|
2015-01-19 11:57:51 -06:00
|
|
|
|
|
|
|
user := m.User{
|
|
|
|
Name: cmd.Name,
|
|
|
|
Email: cmd.Email,
|
|
|
|
Login: cmd.Login,
|
2015-02-28 07:30:08 -06:00
|
|
|
Theme: cmd.Theme,
|
2015-01-19 11:57:51 -06:00
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
2015-02-04 10:15:05 -06:00
|
|
|
if _, err := sess.Id(cmd.UserId).Update(&user); err != nil {
|
|
|
|
return err
|
2015-02-03 09:57:42 -06:00
|
|
|
}
|
2015-02-04 10:15:05 -06:00
|
|
|
|
|
|
|
sess.publishAfterCommit(&events.UserUpdated{
|
|
|
|
Timestamp: user.Created,
|
|
|
|
Id: user.Id,
|
|
|
|
Name: user.Name,
|
|
|
|
Login: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2015-01-19 11:57:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-19 09:09:49 -06:00
|
|
|
func ChangeUserPassword(cmd *m.ChangeUserPasswordCommand) error {
|
|
|
|
return inTransaction2(func(sess *session) error {
|
|
|
|
|
|
|
|
user := m.User{
|
|
|
|
Password: cmd.NewPassword,
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Id(cmd.UserId).Update(&user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
func SetUsingOrg(cmd *m.SetUsingOrgCommand) error {
|
2015-01-19 11:01:04 -06:00
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
user := m.User{}
|
|
|
|
sess.Id(cmd.UserId).Get(&user)
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
user.OrgId = cmd.OrgId
|
2015-01-19 11:01:04 -06:00
|
|
|
_, err := sess.Id(user.Id).Update(&user)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-28 07:30:08 -06:00
|
|
|
func GetUserProfile(query *m.GetUserProfileQuery) error {
|
2015-01-19 11:01:04 -06:00
|
|
|
var user m.User
|
|
|
|
has, err := x.Id(query.UserId).Get(&user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
2015-02-28 07:30:08 -06:00
|
|
|
query.Result = m.UserProfileDTO{
|
2015-01-19 11:01:04 -06:00
|
|
|
Name: user.Name,
|
|
|
|
Email: user.Email,
|
|
|
|
Login: user.Login,
|
2015-02-28 07:30:08 -06:00
|
|
|
Theme: user.Theme,
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
func GetUserOrgList(query *m.GetUserOrgListQuery) error {
|
|
|
|
query.Result = make([]*m.UserOrgDTO, 0)
|
|
|
|
sess := x.Table("org_user")
|
|
|
|
sess.Join("INNER", "org", "org_user.org_id=org.id")
|
|
|
|
sess.Where("org_user.user_id=?", query.UserId)
|
2015-02-24 11:32:29 -06:00
|
|
|
sess.Cols("org.name", "org_user.role", "org_user.org_id")
|
2015-01-19 11:01:04 -06:00
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSignedInUser(query *m.GetSignedInUserQuery) error {
|
|
|
|
var rawSql = `SELECT
|
2015-01-20 07:15:48 -06:00
|
|
|
u.id as user_id,
|
|
|
|
u.is_admin as is_grafana_admin,
|
|
|
|
u.email as email,
|
|
|
|
u.login as login,
|
|
|
|
u.name as name,
|
2015-02-28 07:30:08 -06:00
|
|
|
u.theme as theme,
|
2015-02-23 13:07:49 -06:00
|
|
|
org.name as org_name,
|
|
|
|
org_user.role as org_role,
|
|
|
|
org.id as org_id
|
2015-01-20 07:15:48 -06:00
|
|
|
FROM ` + dialect.Quote("user") + ` as u
|
2015-02-23 13:07:49 -06:00
|
|
|
LEFT OUTER JOIN org_user on org_user.org_id = u.org_id and org_user.user_id = u.id
|
|
|
|
LEFT OUTER JOIN org on org.id = u.org_id
|
2015-01-20 07:15:48 -06:00
|
|
|
WHERE u.id=?`
|
2015-01-19 11:01:04 -06:00
|
|
|
|
|
|
|
var user m.SignedInUser
|
|
|
|
sess := x.Table("user")
|
|
|
|
has, err := sess.Sql(rawSql, query.UserId).Get(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return m.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &user
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchUsers(query *m.SearchUsersQuery) error {
|
|
|
|
query.Result = make([]*m.UserSearchHitDTO, 0)
|
|
|
|
sess := x.Table("user")
|
|
|
|
sess.Where("email LIKE ?", query.Query+"%")
|
|
|
|
sess.Limit(query.Limit, query.Limit*query.Page)
|
|
|
|
sess.Cols("id", "email", "name", "login", "is_admin")
|
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 09:47:22 -06:00
|
|
|
|
|
|
|
func DeleteUser(cmd *m.DeleteUserCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
var rawSql = fmt.Sprintf("DELETE FROM %s WHERE id=?", x.Dialect().Quote("user"))
|
|
|
|
_, err := sess.Exec(rawSql, cmd.UserId)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2015-02-26 08:43:48 -06:00
|
|
|
|
|
|
|
func UpdateUserPermissions(cmd *m.UpdateUserPermissionsCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
user := m.User{}
|
|
|
|
sess.Id(cmd.UserId).Get(&user)
|
|
|
|
|
|
|
|
user.IsAdmin = cmd.IsGrafanaAdmin
|
|
|
|
sess.UseBool("is_admin")
|
|
|
|
_, err := sess.Id(user.Id).Update(&user)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|