Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
22bf20a1
Commit
22bf20a1
authored
Dec 19, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring get account by id and by login to queries
parent
5dcf6ff2
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
58 additions
and
33 deletions
+58
-33
grafana
+1
-1
pkg/api/api_account.go
+4
-1
pkg/api/api_login.go
+10
-5
pkg/api/api_login_oauth.go
+4
-3
pkg/middleware/auth.go
+8
-5
pkg/models/account.go
+10
-5
pkg/stores/sqlstore/accounts.go
+21
-11
pkg/stores/sqlstore/sqlstore.go
+0
-2
No files found.
grafana
@
dede578c
Subproject commit
ad91093902bdfc0d2a87bb362a76a9057aef4361
Subproject commit
dede578c7d569f87c35724f74a72216743bf9508
pkg/api/api_account.go
View file @
22bf20a1
...
@@ -27,12 +27,15 @@ func AddCollaborator(c *middleware.Context) {
...
@@ -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
...
...
pkg/api/api_login.go
View file @
22bf20a1
...
@@ -2,9 +2,10 @@ package api
...
@@ -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) {
...
@@ -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
.
J
SON
(
401
,
utils
.
DynMap
{
"status"
:
"unauthorized"
}
)
c
.
J
sonApiErr
(
401
,
"Invalid username or password"
,
err
)
return
return
}
}
account
:=
userQuery
.
Result
if
loginModel
.
Password
!=
account
.
Password
{
if
loginModel
.
Password
!=
account
.
Password
{
c
.
J
SON
(
401
,
utils
.
DynMap
{
"status"
:
"unauthorized"
}
)
c
.
J
sonApiErr
(
401
,
"Invalid username or password"
,
err
)
return
return
}
}
...
@@ -42,7 +47,7 @@ func LoginPost(c *middleware.Context) {
...
@@ -42,7 +47,7 @@ func LoginPost(c *middleware.Context) {
c
.
JSON
(
200
,
resp
)
c
.
JSON
(
200
,
resp
)
}
}
func
loginUserWithAccount
(
account
*
m
odels
.
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"
)
}
}
...
...
pkg/api/api_login_oauth.go
View file @
22bf20a1
...
@@ -49,7 +49,8 @@ func OAuthLogin(ctx *middleware.Context) {
...
@@ -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) {
...
@@ -65,13 +66,13 @@ func OAuthLogin(ctx *middleware.Context) {
return
return
}
}
accoun
t
=
&
cmd
.
Result
userQuery
.
Resul
t
=
&
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
(
accoun
t
,
ctx
)
loginUserWithAccount
(
userQuery
.
Resul
t
,
ctx
)
ctx
.
Redirect
(
"/"
)
ctx
.
Redirect
(
"/"
)
}
}
pkg/middleware/auth.go
View file @
22bf20a1
...
@@ -7,7 +7,8 @@ import (
...
@@ -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 {
...
@@ -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
=
accoun
t
c
.
UserAccount
=
userQuery
.
Resul
t
c
.
Account
=
using
Accoun
t
c
.
Account
=
using
Query
.
Resul
t
}
}
}
}
pkg/models/account.go
View file @
22bf20a1
...
@@ -5,11 +5,6 @@ import (
...
@@ -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 {
...
@@ -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
}
pkg/stores/sqlstore/accounts.go
View file @
22bf20a1
...
@@ -15,6 +15,8 @@ func init() {
...
@@ -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 {
...
@@ -85,38 +87,46 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
})
})
}
}
func
GetAccount
(
id
int64
)
(
*
m
.
Account
,
error
)
{
func
GetAccount
ById
(
query
*
m
.
GetAccountByIdQuery
)
error
{
var
err
error
var
err
error
var
account
m
.
Account
var
account
m
.
Account
has
,
err
:=
x
.
Id
(
i
d
)
.
Get
(
&
account
)
has
,
err
:=
x
.
Id
(
query
.
I
d
)
.
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
:
emailOr
Login
}
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
{
...
...
pkg/stores/sqlstore/sqlstore.go
View file @
22bf20a1
...
@@ -35,8 +35,6 @@ func init() {
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment