Commit 22bf20a1 by Torkel Ödegaard

Refactoring get account by id and by login to queries

parent 5dcf6ff2
Subproject commit ad91093902bdfc0d2a87bb362a76a9057aef4361
Subproject commit dede578c7d569f87c35724f74a72216743bf9508
......@@ -27,12 +27,15 @@ func AddCollaborator(c *middleware.Context) {
return
}
accountToAdd, err := m.GetAccountByLogin(cmd.Email)
userQuery := m.GetAccountByLoginQuery{Login: cmd.Email}
err := bus.Dispatch(&userQuery)
if err != nil {
c.JsonApiErr(404, "Collaborator not found", nil)
return
}
accountToAdd := userQuery.Result
if accountToAdd.Id == c.UserAccount.Id {
c.JsonApiErr(400, "Cannot add yourself as collaborator", nil)
return
......
......@@ -2,9 +2,10 @@ package api
import (
"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/middleware"
"github.com/torkelo/grafana-pro/pkg/models"
m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/utils"
)
......@@ -22,14 +23,18 @@ func LoginPost(c *middleware.Context) {
return
}
account, err := models.GetAccountByLogin(loginModel.Email)
userQuery := m.GetAccountByLoginQuery{Login: loginModel.Email}
err := bus.Dispatch(&userQuery)
if err != nil {
c.JSON(401, utils.DynMap{"status": "unauthorized"})
c.JsonApiErr(401, "Invalid username or password", err)
return
}
account := userQuery.Result
if loginModel.Password != account.Password {
c.JSON(401, utils.DynMap{"status": "unauthorized"})
c.JsonApiErr(401, "Invalid username or password", err)
return
}
......@@ -42,7 +47,7 @@ func LoginPost(c *middleware.Context) {
c.JSON(200, resp)
}
func loginUserWithAccount(account *models.Account, c *middleware.Context) {
func loginUserWithAccount(account *m.Account, c *middleware.Context) {
if account == nil {
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)
account, err := m.GetAccountByLogin(userInfo.Email)
userQuery := m.GetAccountByLoginQuery{Login: userInfo.Email}
err = bus.Dispatch(&userQuery)
// create account if missing
if err == m.ErrAccountNotFound {
......@@ -65,13 +66,13 @@ func OAuthLogin(ctx *middleware.Context) {
return
}
account = &cmd.Result
userQuery.Result = &cmd.Result
} else if err != nil {
ctx.Handle(500, "Unexpected error", err)
}
// login
loginUserWithAccount(account, ctx)
loginUserWithAccount(userQuery.Result, ctx)
ctx.Redirect("/")
}
......@@ -7,7 +7,8 @@ import (
"github.com/Unknwon/macaron"
"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) {
......@@ -40,19 +41,21 @@ func Auth() macaron.Handler {
return
}
account, err := models.GetAccount(accountId)
userQuery := m.GetAccountByIdQuery{Id: accountId}
err = bus.Dispatch(&userQuery)
if err != nil {
authDenied(c)
return
}
usingAccount, err := models.GetAccount(account.UsingAccountId)
usingQuery := m.GetAccountByIdQuery{Id: userQuery.Result.UsingAccountId}
err = bus.Dispatch(&usingQuery)
if err != nil {
authDenied(c)
return
}
c.UserAccount = account
c.Account = usingAccount
c.UserAccount = userQuery.Result
c.Account = usingQuery.Result
}
}
......@@ -5,11 +5,6 @@ import (
"time"
)
var (
GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int64) (*Account, error)
)
// Typed errors
var (
ErrAccountNotFound = errors.New("Account not found")
......@@ -80,3 +75,13 @@ type GetOtherAccountsQuery struct {
AccountId int64
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", CreateAccount)
bus.AddHandler("sql", SetUsingAccount)
bus.AddHandler("sql", GetAccountById)
bus.AddHandler("sql", GetAccountByLogin)
}
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 account m.Account
has, err := x.Id(id).Get(&account)
has, err := x.Id(query.Id).Get(&account)
if err != nil {
return nil, err
return err
} else if has == false {
return nil, m.ErrAccountNotFound
return m.ErrAccountNotFound
}
if account.UsingAccountId == 0 {
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
account := &m.Account{Login: emailOrLogin}
has, err := x.Get(account)
account := m.Account{Login: query.Login}
has, err := x.Get(&account)
if err != nil {
return nil, err
return err
} 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 {
......
......@@ -35,8 +35,6 @@ func init() {
}
func Init() {
m.GetAccount = GetAccount
m.GetAccountByLogin = GetAccountByLogin
m.GetDashboard = GetDashboard
m.SaveDashboard = SaveDashboard
m.SearchQuery = SearchQuery
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment