mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Refactoring get account by id and by login to queries
This commit is contained in:
parent
5dcf6ff2d3
commit
22bf20a135
2
grafana
2
grafana
@ -1 +1 @@
|
|||||||
Subproject commit ad91093902bdfc0d2a87bb362a76a9057aef4361
|
Subproject commit dede578c7d569f87c35724f74a72216743bf9508
|
@ -27,12 +27,15 @@ func AddCollaborator(c *middleware.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
accountToAdd, err := m.GetAccountByLogin(cmd.Email)
|
userQuery := m.GetAccountByLoginQuery{Login: cmd.Email}
|
||||||
|
err := bus.Dispatch(&userQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(404, "Collaborator not found", nil)
|
c.JsonApiErr(404, "Collaborator not found", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accountToAdd := userQuery.Result
|
||||||
|
|
||||||
if accountToAdd.Id == c.UserAccount.Id {
|
if accountToAdd.Id == c.UserAccount.Id {
|
||||||
c.JsonApiErr(400, "Cannot add yourself as collaborator", nil)
|
c.JsonApiErr(400, "Cannot add yourself as collaborator", nil)
|
||||||
return
|
return
|
||||||
|
@ -2,9 +2,10 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/torkelo/grafana-pro/pkg/api/dtos"
|
"github.com/torkelo/grafana-pro/pkg/api/dtos"
|
||||||
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||||
"github.com/torkelo/grafana-pro/pkg/log"
|
"github.com/torkelo/grafana-pro/pkg/log"
|
||||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||||
"github.com/torkelo/grafana-pro/pkg/models"
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||||
"github.com/torkelo/grafana-pro/pkg/utils"
|
"github.com/torkelo/grafana-pro/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,14 +23,18 @@ func LoginPost(c *middleware.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
account, err := models.GetAccountByLogin(loginModel.Email)
|
userQuery := m.GetAccountByLoginQuery{Login: loginModel.Email}
|
||||||
|
err := bus.Dispatch(&userQuery)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(401, utils.DynMap{"status": "unauthorized"})
|
c.JsonApiErr(401, "Invalid username or password", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
account := userQuery.Result
|
||||||
|
|
||||||
if loginModel.Password != account.Password {
|
if loginModel.Password != account.Password {
|
||||||
c.JSON(401, utils.DynMap{"status": "unauthorized"})
|
c.JsonApiErr(401, "Invalid username or password", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +47,7 @@ func LoginPost(c *middleware.Context) {
|
|||||||
c.JSON(200, resp)
|
c.JSON(200, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginUserWithAccount(account *models.Account, c *middleware.Context) {
|
func loginUserWithAccount(account *m.Account, c *middleware.Context) {
|
||||||
if account == nil {
|
if account == nil {
|
||||||
log.Error(3, "Account login with nil account")
|
log.Error(3, "Account login with nil account")
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,8 @@ func OAuthLogin(ctx *middleware.Context) {
|
|||||||
|
|
||||||
log.Info("login.OAuthLogin(social login): %s", userInfo)
|
log.Info("login.OAuthLogin(social login): %s", userInfo)
|
||||||
|
|
||||||
account, err := m.GetAccountByLogin(userInfo.Email)
|
userQuery := m.GetAccountByLoginQuery{Login: userInfo.Email}
|
||||||
|
err = bus.Dispatch(&userQuery)
|
||||||
|
|
||||||
// create account if missing
|
// create account if missing
|
||||||
if err == m.ErrAccountNotFound {
|
if err == m.ErrAccountNotFound {
|
||||||
@ -65,13 +66,13 @@ func OAuthLogin(ctx *middleware.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
account = &cmd.Result
|
userQuery.Result = &cmd.Result
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
ctx.Handle(500, "Unexpected error", err)
|
ctx.Handle(500, "Unexpected error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// login
|
// login
|
||||||
loginUserWithAccount(account, ctx)
|
loginUserWithAccount(userQuery.Result, ctx)
|
||||||
|
|
||||||
ctx.Redirect("/")
|
ctx.Redirect("/")
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,8 @@ import (
|
|||||||
"github.com/Unknwon/macaron"
|
"github.com/Unknwon/macaron"
|
||||||
"github.com/macaron-contrib/session"
|
"github.com/macaron-contrib/session"
|
||||||
|
|
||||||
"github.com/torkelo/grafana-pro/pkg/models"
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||||
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func authGetRequestAccountId(c *Context, sess session.Store) (int64, error) {
|
func authGetRequestAccountId(c *Context, sess session.Store) (int64, error) {
|
||||||
@ -40,19 +41,21 @@ func Auth() macaron.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
account, err := models.GetAccount(accountId)
|
userQuery := m.GetAccountByIdQuery{Id: accountId}
|
||||||
|
err = bus.Dispatch(&userQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
authDenied(c)
|
authDenied(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
usingAccount, err := models.GetAccount(account.UsingAccountId)
|
usingQuery := m.GetAccountByIdQuery{Id: userQuery.Result.UsingAccountId}
|
||||||
|
err = bus.Dispatch(&usingQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
authDenied(c)
|
authDenied(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.UserAccount = account
|
c.UserAccount = userQuery.Result
|
||||||
c.Account = usingAccount
|
c.Account = usingQuery.Result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
GetAccountByLogin func(emailOrName string) (*Account, error)
|
|
||||||
GetAccount func(accountId int64) (*Account, error)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Typed errors
|
// Typed errors
|
||||||
var (
|
var (
|
||||||
ErrAccountNotFound = errors.New("Account not found")
|
ErrAccountNotFound = errors.New("Account not found")
|
||||||
@ -80,3 +75,13 @@ type GetOtherAccountsQuery struct {
|
|||||||
AccountId int64
|
AccountId int64
|
||||||
Result []*OtherAccountDTO
|
Result []*OtherAccountDTO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetAccountByIdQuery struct {
|
||||||
|
Id int64
|
||||||
|
Result *Account
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAccountByLoginQuery struct {
|
||||||
|
Login string
|
||||||
|
Result *Account
|
||||||
|
}
|
||||||
|
@ -15,6 +15,8 @@ func init() {
|
|||||||
bus.AddHandler("sql", GetOtherAccounts)
|
bus.AddHandler("sql", GetOtherAccounts)
|
||||||
bus.AddHandler("sql", CreateAccount)
|
bus.AddHandler("sql", CreateAccount)
|
||||||
bus.AddHandler("sql", SetUsingAccount)
|
bus.AddHandler("sql", SetUsingAccount)
|
||||||
|
bus.AddHandler("sql", GetAccountById)
|
||||||
|
bus.AddHandler("sql", GetAccountByLogin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||||
@ -85,38 +87,46 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccount(id int64) (*m.Account, error) {
|
func GetAccountById(query *m.GetAccountByIdQuery) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
var account m.Account
|
var account m.Account
|
||||||
has, err := x.Id(id).Get(&account)
|
has, err := x.Id(query.Id).Get(&account)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
} else if has == false {
|
} else if has == false {
|
||||||
return nil, m.ErrAccountNotFound
|
return m.ErrAccountNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
if account.UsingAccountId == 0 {
|
if account.UsingAccountId == 0 {
|
||||||
account.UsingAccountId = account.Id
|
account.UsingAccountId = account.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
return &account, nil
|
query.Result = &account
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
|
func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
account := &m.Account{Login: emailOrLogin}
|
account := m.Account{Login: query.Login}
|
||||||
has, err := x.Get(account)
|
has, err := x.Get(&account)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
} else if has == false {
|
} else if has == false {
|
||||||
return nil, m.ErrAccountNotFound
|
return m.ErrAccountNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return account, nil
|
if account.UsingAccountId == 0 {
|
||||||
|
account.UsingAccountId = account.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
query.Result = &account
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
|
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
|
||||||
|
@ -35,8 +35,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
m.GetAccount = GetAccount
|
|
||||||
m.GetAccountByLogin = GetAccountByLogin
|
|
||||||
m.GetDashboard = GetDashboard
|
m.GetDashboard = GetDashboard
|
||||||
m.SaveDashboard = SaveDashboard
|
m.SaveDashboard = SaveDashboard
|
||||||
m.SearchQuery = SearchQuery
|
m.SearchQuery = SearchQuery
|
||||||
|
Loading…
Reference in New Issue
Block a user