2014-11-14 10:13:33 -06:00
|
|
|
package sqlstore
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
import (
|
2015-01-15 07:44:15 -06:00
|
|
|
"strings"
|
2014-12-19 03:45:22 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
|
|
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
|
|
|
)
|
2014-11-14 10:13:33 -06:00
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", GetAccountInfo)
|
2014-12-19 04:08:49 -06:00
|
|
|
bus.AddHandler("sql", GetOtherAccounts)
|
2014-12-19 04:53:27 -06:00
|
|
|
bus.AddHandler("sql", CreateAccount)
|
2014-12-19 06:12:47 -06:00
|
|
|
bus.AddHandler("sql", SetUsingAccount)
|
2014-12-19 06:40:02 -06:00
|
|
|
bus.AddHandler("sql", GetAccountById)
|
|
|
|
bus.AddHandler("sql", GetAccountByLogin)
|
2015-01-15 05:16:54 -06:00
|
|
|
bus.AddHandler("sql", SearchAccounts)
|
2015-01-15 12:14:47 -06:00
|
|
|
bus.AddHandler("sql", UpdateAccount)
|
2015-01-16 07:32:18 -06:00
|
|
|
bus.AddHandler("sql", GetSignedInUser)
|
2014-12-19 04:53:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
|
|
|
|
account := m.Account{
|
|
|
|
Email: cmd.Email,
|
2015-01-16 07:32:18 -06:00
|
|
|
Name: cmd.Name,
|
2014-12-19 04:53:27 -06:00
|
|
|
Login: cmd.Login,
|
|
|
|
Password: cmd.Password,
|
2015-01-08 02:00:00 -06:00
|
|
|
Salt: cmd.Salt,
|
2015-01-15 07:44:15 -06:00
|
|
|
IsAdmin: cmd.IsAdmin,
|
2014-12-19 04:53:27 -06:00
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
sess.UseBool("is_admin")
|
|
|
|
|
2014-12-19 04:53:27 -06:00
|
|
|
_, err := sess.Insert(&account)
|
|
|
|
cmd.Result = account
|
|
|
|
return err
|
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
}
|
|
|
|
|
2015-01-15 12:14:47 -06:00
|
|
|
func UpdateAccount(cmd *m.UpdateAccountCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
|
|
|
|
account := m.Account{
|
|
|
|
Email: cmd.Email,
|
|
|
|
Login: cmd.Login,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sess.Id(cmd.AccountId).Update(&account)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:12:47 -06:00
|
|
|
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
account := m.Account{}
|
|
|
|
sess.Id(cmd.AccountId).Get(&account)
|
|
|
|
|
|
|
|
account.UsingAccountId = cmd.UsingAccountId
|
|
|
|
_, err := sess.Id(account.Id).Update(&account)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
|
|
|
var account m.Account
|
|
|
|
has, err := x.Id(query.Id).Get(&account)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrAccountNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = m.AccountDTO{
|
2015-01-15 12:14:47 -06:00
|
|
|
Name: account.Name,
|
|
|
|
Email: account.Email,
|
|
|
|
Login: account.Login,
|
2014-12-19 02:43:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
func GetAccountById(query *m.GetAccountByIdQuery) error {
|
2014-11-20 05:11:07 -06:00
|
|
|
var err error
|
|
|
|
|
2014-12-19 02:43:16 -06:00
|
|
|
var account m.Account
|
2014-12-19 06:40:02 -06:00
|
|
|
has, err := x.Id(query.Id).Get(&account)
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2014-11-14 10:13:33 -06:00
|
|
|
if err != nil {
|
2014-12-19 06:40:02 -06:00
|
|
|
return err
|
2014-11-20 05:11:07 -06:00
|
|
|
} else if has == false {
|
2014-12-19 06:40:02 -06:00
|
|
|
return m.ErrAccountNotFound
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
|
|
|
|
2014-11-20 08:19:44 -06:00
|
|
|
if account.UsingAccountId == 0 {
|
|
|
|
account.UsingAccountId = account.Id
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
query.Result = &account
|
|
|
|
|
|
|
|
return nil
|
2014-11-20 05:11:07 -06:00
|
|
|
}
|
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
|
2015-01-16 04:54:19 -06:00
|
|
|
if query.LoginOrEmail == "" {
|
|
|
|
return m.ErrAccountNotFound
|
|
|
|
}
|
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
account := new(m.Account)
|
2015-01-16 04:54:19 -06:00
|
|
|
if strings.Contains(query.LoginOrEmail, "@") {
|
|
|
|
account = &m.Account{Email: query.LoginOrEmail}
|
2015-01-15 07:44:15 -06:00
|
|
|
} else {
|
2015-01-16 04:54:19 -06:00
|
|
|
account = &m.Account{Login: strings.ToLower(query.LoginOrEmail)}
|
2015-01-15 07:44:15 -06:00
|
|
|
}
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
has, err := x.Get(account)
|
2014-11-20 05:11:07 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2014-12-19 06:40:02 -06:00
|
|
|
return err
|
2014-11-20 05:11:07 -06:00
|
|
|
} else if has == false {
|
2014-12-19 06:40:02 -06:00
|
|
|
return m.ErrAccountNotFound
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2014-12-19 06:40:02 -06:00
|
|
|
if account.UsingAccountId == 0 {
|
|
|
|
account.UsingAccountId = account.Id
|
|
|
|
}
|
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
query.Result = account
|
2014-12-19 06:40:02 -06:00
|
|
|
|
|
|
|
return nil
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
2014-11-21 09:43:04 -06:00
|
|
|
|
2014-12-19 04:08:49 -06:00
|
|
|
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
|
|
|
|
query.Result = make([]*m.OtherAccountDTO, 0)
|
2014-11-24 03:17:13 -06:00
|
|
|
sess := x.Table("collaborator")
|
2015-01-14 09:12:37 -06:00
|
|
|
sess.Join("INNER", "account", "collaborator.account_id=account.id")
|
|
|
|
sess.Where("collaborator_id=?", query.AccountId)
|
|
|
|
sess.Cols("collaborator.account_id", "collaborator.role", "account.email")
|
2014-12-19 04:08:49 -06:00
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
2014-11-24 01:04:41 -06:00
|
|
|
}
|
2015-01-15 05:16:54 -06:00
|
|
|
|
|
|
|
func SearchAccounts(query *m.SearchAccountsQuery) error {
|
|
|
|
query.Result = make([]*m.AccountSearchHitDTO, 0)
|
|
|
|
sess := x.Table("account")
|
|
|
|
sess.Where("email LIKE ?", query.Query+"%")
|
|
|
|
sess.Limit(query.Limit, query.Limit*query.Page)
|
2015-01-15 08:54:22 -06:00
|
|
|
sess.Cols("id", "email", "name", "login", "is_admin")
|
2015-01-15 05:16:54 -06:00
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
2015-01-16 07:32:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetSignedInUser(query *m.GetSignedInUserQuery) error {
|
|
|
|
var rawSql = `SELECT
|
|
|
|
userAccount.id as account_id,
|
|
|
|
userAccount.is_admin as is_grafana_admin,
|
|
|
|
userAccount.email as user_email,
|
|
|
|
userAccount.name as user_name,
|
|
|
|
userAccount.login as user_login,
|
|
|
|
usingAccount.id as using_account_id,
|
|
|
|
usingAccount.name as using_account_name,
|
|
|
|
collaborator.role as user_role
|
|
|
|
FROM account as userAccount
|
|
|
|
LEFT OUTER JOIN account as usingAccount on usingAccount.id = userAccount.using_account_id
|
|
|
|
LEFT OUTER JOIN collaborator on collaborator.account_id = usingAccount.id AND collaborator.collaborator_id = userAccount.id
|
|
|
|
WHERE userAccount.id=?`
|
|
|
|
|
2015-01-16 09:17:35 -06:00
|
|
|
var user m.SignedInUser
|
2015-01-16 07:32:18 -06:00
|
|
|
sess := x.Table("account")
|
|
|
|
has, err := sess.Sql(rawSql, query.AccountId).Get(&user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return m.ErrAccountNotFound
|
|
|
|
}
|
2015-01-15 05:16:54 -06:00
|
|
|
|
2015-01-16 07:32:18 -06:00
|
|
|
if user.UsingAccountId == 0 || user.UsingAccountId == user.AccountId {
|
|
|
|
user.UsingAccountId = query.AccountId
|
|
|
|
user.UsingAccountName = user.UserName
|
|
|
|
user.UserRole = m.ROLE_OWNER
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &user
|
|
|
|
return err
|
2015-01-15 05:16:54 -06:00
|
|
|
}
|