2015-01-19 09:28:45 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
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-01-19 11:57:51 -06:00
|
|
|
bus.AddHandler("sql", UpdateUser)
|
2015-01-19 11:01:04 -06:00
|
|
|
bus.AddHandler("sql", GetUserByLogin)
|
|
|
|
bus.AddHandler("sql", SetUsingAccount)
|
|
|
|
bus.AddHandler("sql", GetUserInfo)
|
|
|
|
bus.AddHandler("sql", GetSignedInUser)
|
|
|
|
bus.AddHandler("sql", SearchUsers)
|
|
|
|
bus.AddHandler("sql", GetUserAccounts)
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
|
|
|
|
2015-02-04 10:15:05 -06:00
|
|
|
func getAccountIdForNewUser(userEmail string, sess *session) (int64, error) {
|
2015-01-27 08:14:53 -06:00
|
|
|
var account m.Account
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2015-01-27 08:14:53 -06:00
|
|
|
if setting.SingleAccountMode {
|
|
|
|
has, err := sess.Where("name=?", setting.DefaultAccountName).Get(&account)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2015-01-19 09:28:45 -06:00
|
|
|
}
|
2015-01-27 08:14:53 -06:00
|
|
|
if has {
|
|
|
|
return account.Id, nil
|
|
|
|
} else {
|
|
|
|
account.Name = setting.DefaultAccountName
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
account.Name = userEmail
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Created = time.Now()
|
|
|
|
account.Updated = time.Now()
|
2015-01-19 09:28:45 -06:00
|
|
|
|
2015-01-27 08:14:53 -06:00
|
|
|
if _, err := sess.Insert(&account); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.Id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateUser(cmd *m.CreateUserCommand) error {
|
2015-02-04 10:15:05 -06:00
|
|
|
return inTransaction2(func(sess *session) error {
|
2015-01-27 08:14:53 -06:00
|
|
|
accountId, err := getAccountIdForNewUser(cmd.Email, sess)
|
|
|
|
if err != nil {
|
2015-01-19 09:28:45 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create user
|
|
|
|
user := m.User{
|
|
|
|
Email: cmd.Email,
|
2015-01-19 11:01:04 -06:00
|
|
|
Name: cmd.Name,
|
|
|
|
Login: cmd.Login,
|
|
|
|
Company: cmd.Company,
|
2015-01-19 09:28:45 -06:00
|
|
|
IsAdmin: cmd.IsAdmin,
|
2015-01-27 08:14:53 -06:00
|
|
|
AccountId: accountId,
|
2015-01-19 09:28:45 -06:00
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// create account user link
|
2015-01-27 08:14:53 -06:00
|
|
|
accountUser := m.AccountUser{
|
|
|
|
AccountId: accountId,
|
2015-01-19 09:28:45 -06:00
|
|
|
UserId: user.Id,
|
|
|
|
Role: m.ROLE_ADMIN,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2015-01-27 08:14:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if setting.SingleAccountMode {
|
|
|
|
accountUser.Role = m.RoleType(setting.DefaultAccountRole)
|
|
|
|
}
|
|
|
|
|
2015-02-04 10:15:05 -06:00
|
|
|
if _, err = sess.Insert(&accountUser); err != nil {
|
|
|
|
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
|
|
|
|
|
|
|
func GetUserByLogin(query *m.GetUserByLoginQuery) error {
|
|
|
|
if query.LoginOrEmail == "" {
|
|
|
|
return m.ErrAccountNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
user := new(m.User)
|
|
|
|
if strings.Contains(query.LoginOrEmail, "@") {
|
|
|
|
user = &m.User{Email: query.LoginOrEmail}
|
|
|
|
} else {
|
|
|
|
user = &m.User{Login: strings.ToLower(query.LoginOrEmail)}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
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-01-19 11:01:04 -06:00
|
|
|
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
user := m.User{}
|
|
|
|
sess.Id(cmd.UserId).Get(&user)
|
|
|
|
|
|
|
|
user.AccountId = cmd.AccountId
|
|
|
|
_, err := sess.Id(user.Id).Update(&user)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserInfo(query *m.GetUserInfoQuery) error {
|
|
|
|
var user m.User
|
|
|
|
has, err := x.Id(query.UserId).Get(&user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = m.UserDTO{
|
|
|
|
Name: user.Name,
|
|
|
|
Email: user.Email,
|
|
|
|
Login: user.Login,
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserAccounts(query *m.GetUserAccountsQuery) error {
|
|
|
|
query.Result = make([]*m.UserAccountDTO, 0)
|
|
|
|
sess := x.Table("account_user")
|
|
|
|
sess.Join("INNER", "account", "account_user.account_id=account.id")
|
|
|
|
sess.Where("account_user.user_id=?", query.UserId)
|
|
|
|
sess.Cols("account.name", "account_user.role", "account_user.account_id")
|
|
|
|
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-01-19 11:01:04 -06:00
|
|
|
account.name as account_name,
|
|
|
|
account_user.role as account_role,
|
|
|
|
account.id as account_id
|
2015-01-20 07:15:48 -06:00
|
|
|
FROM ` + dialect.Quote("user") + ` as u
|
|
|
|
LEFT OUTER JOIN account_user on account_user.account_id = u.account_id and account_user.user_id = u.id
|
|
|
|
LEFT OUTER JOIN account on account.id = u.account_id
|
|
|
|
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
|
|
|
|
}
|