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
f858f6b6
Commit
f858f6b6
authored
Jan 16, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add collaborator now handles role, added macaron-contrib/binding for binding and validation
parent
cd5843e9
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
81 additions
and
75 deletions
+81
-75
grafana
+1
-1
pkg/api/api.go
+40
-37
pkg/api/collaborators.go
+2
-9
pkg/api/login.go
+1
-1
pkg/api/login_oauth.go
+1
-1
pkg/api/token.go
+4
-4
pkg/models/account.go
+2
-2
pkg/models/collaborator.go
+21
-15
pkg/services/sqlstore/accounts.go
+7
-3
pkg/services/sqlstore/accounts_test.go
+1
-1
pkg/services/sqlstore/sqlstore.go
+1
-1
No files found.
grafana
@
500e0006
Subproject commit
9d1dacb8d417cac2cc66797e5dae6deba36c3080
Subproject commit
500e00066139b861a2898db6ef80ef87b8b8daa6
pkg/api/api.go
View file @
f858f6b6
...
@@ -2,76 +2,79 @@ package api
...
@@ -2,76 +2,79 @@ package api
import
(
import
(
"github.com/Unknwon/macaron"
"github.com/Unknwon/macaron"
"github.com/macaron-contrib/binding"
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/torkelo/grafana-pro/pkg/middleware"
m
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
"github.com/torkelo/grafana-pro/pkg/setting"
)
)
// Register adds http routes
// Register adds http routes
func
Register
(
m
*
macaron
.
Macaron
)
{
func
Register
(
r
*
macaron
.
Macaron
)
{
reqSignedIn
:=
middleware
.
Auth
(
&
middleware
.
AuthOptions
{
ReqSignedIn
:
true
})
reqSignedIn
:=
middleware
.
Auth
(
&
middleware
.
AuthOptions
{
ReqSignedIn
:
true
})
reqAdmin
:=
middleware
.
Auth
(
&
middleware
.
AuthOptions
{
ReqSignedIn
:
true
,
ReqAdmin
:
true
})
reqAdmin
:=
middleware
.
Auth
(
&
middleware
.
AuthOptions
{
ReqSignedIn
:
true
,
ReqAdmin
:
true
})
bind
:=
binding
.
Bind
// not logged in views
// not logged in views
m
.
Get
(
"/"
,
reqSignedIn
,
Index
)
r
.
Get
(
"/"
,
reqSignedIn
,
Index
)
m
.
Post
(
"/logout"
,
LogoutPost
)
r
.
Post
(
"/logout"
,
LogoutPost
)
m
.
Post
(
"/login"
,
LoginPost
)
r
.
Post
(
"/login"
,
LoginPost
)
m
.
Get
(
"/login/:name"
,
OAuthLogin
)
r
.
Get
(
"/login/:name"
,
OAuthLogin
)
m
.
Get
(
"/login"
,
Index
)
r
.
Get
(
"/login"
,
Index
)
// authed views
// authed views
m
.
Get
(
"/account/"
,
reqSignedIn
,
Index
)
r
.
Get
(
"/account/"
,
reqSignedIn
,
Index
)
m
.
Get
(
"/account/datasources/"
,
reqSignedIn
,
Index
)
r
.
Get
(
"/account/datasources/"
,
reqSignedIn
,
Index
)
m
.
Get
(
"/admin"
,
reqSignedIn
,
Index
)
r
.
Get
(
"/admin"
,
reqSignedIn
,
Index
)
m
.
Get
(
"/dashboard/*"
,
reqSignedIn
,
Index
)
r
.
Get
(
"/dashboard/*"
,
reqSignedIn
,
Index
)
// sign up
// sign up
m
.
Get
(
"/signup"
,
Index
)
r
.
Get
(
"/signup"
,
Index
)
m
.
Post
(
"/api/account/signup"
,
SignUp
)
r
.
Post
(
"/api/account/signup"
,
SignUp
)
// authed api
// authed api
m
.
Group
(
"/api"
,
func
()
{
r
.
Group
(
"/api"
,
func
()
{
// account
// account
m
.
Group
(
"/account"
,
func
()
{
r
.
Group
(
"/account"
,
func
()
{
m
.
Get
(
"/"
,
GetAccount
)
r
.
Get
(
"/"
,
GetAccount
)
m
.
Post
(
"/"
,
UpdateAccount
)
r
.
Post
(
"/"
,
UpdateAccount
)
m
.
Put
(
"/collaborators"
,
AddCollaborator
)
r
.
Put
(
"/collaborators"
,
bind
(
m
.
AddCollaboratorCommand
{})
,
AddCollaborator
)
m
.
Get
(
"/collaborators"
,
GetCollaborators
)
r
.
Get
(
"/collaborators"
,
GetCollaborators
)
m
.
Delete
(
"/collaborators/:id"
,
RemoveCollaborator
)
r
.
Delete
(
"/collaborators/:id"
,
RemoveCollaborator
)
m
.
Post
(
"/using/:id"
,
SetUsingAccount
)
r
.
Post
(
"/using/:id"
,
SetUsingAccount
)
m
.
Get
(
"/others"
,
GetOtherAccounts
)
r
.
Get
(
"/others"
,
GetOtherAccounts
)
})
})
// Token
// Token
m
.
Group
(
"/tokens"
,
func
()
{
r
.
Group
(
"/tokens"
,
func
()
{
m
.
Combo
(
"/"
)
.
Get
(
GetTokens
)
.
Put
(
AddToken
)
.
Post
(
UpdateToken
)
r
.
Combo
(
"/"
)
.
Get
(
GetTokens
)
.
Put
(
AddToken
)
.
Post
(
UpdateToken
)
m
.
Delete
(
"/:id"
,
DeleteToken
)
r
.
Delete
(
"/:id"
,
DeleteToken
)
})
})
// Data sources
// Data sources
m
.
Group
(
"/datasources"
,
func
()
{
r
.
Group
(
"/datasources"
,
func
()
{
m
.
Combo
(
"/"
)
.
Get
(
GetDataSources
)
.
Put
(
AddDataSource
)
.
Post
(
UpdateDataSource
)
r
.
Combo
(
"/"
)
.
Get
(
GetDataSources
)
.
Put
(
AddDataSource
)
.
Post
(
UpdateDataSource
)
m
.
Delete
(
"/:id"
,
DeleteDataSource
)
r
.
Delete
(
"/:id"
,
DeleteDataSource
)
m
.
Any
(
"/proxy/:id/*"
,
reqSignedIn
,
ProxyDataSourceRequest
)
r
.
Any
(
"/proxy/:id/*"
,
reqSignedIn
,
ProxyDataSourceRequest
)
})
})
// Dashboard
// Dashboard
m
.
Group
(
"/dashboard"
,
func
()
{
r
.
Group
(
"/dashboard"
,
func
()
{
m
.
Combo
(
"/:slug"
)
.
Get
(
GetDashboard
)
.
Delete
(
DeleteDashboard
)
r
.
Combo
(
"/:slug"
)
.
Get
(
GetDashboard
)
.
Delete
(
DeleteDashboard
)
m
.
Post
(
"/"
,
PostDashboard
)
r
.
Post
(
"/"
,
PostDashboard
)
})
})
// Search
// Search
m
.
Get
(
"/search/"
,
Search
)
r
.
Get
(
"/search/"
,
Search
)
// metrics
// metrics
m
.
Get
(
"/metrics/test"
,
GetTestMetrics
)
r
.
Get
(
"/metrics/test"
,
GetTestMetrics
)
},
reqSignedIn
)
},
reqSignedIn
)
// admin api
// admin api
m
.
Group
(
"/api/admin"
,
func
()
{
r
.
Group
(
"/api/admin"
,
func
()
{
m
.
Get
(
"/accounts"
,
AdminSearchAccounts
)
r
.
Get
(
"/accounts"
,
AdminSearchAccounts
)
},
reqAdmin
)
},
reqAdmin
)
// rendering
// rendering
m
.
Get
(
"/render/*"
,
reqSignedIn
,
RenderToPng
)
r
.
Get
(
"/render/*"
,
reqSignedIn
,
RenderToPng
)
m
.
NotFound
(
NotFound
)
r
.
NotFound
(
NotFound
)
}
}
func
setIndexViewData
(
c
*
middleware
.
Context
)
error
{
func
setIndexViewData
(
c
*
middleware
.
Context
)
error
{
...
...
pkg/api/collaborators.go
View file @
f858f6b6
...
@@ -6,15 +6,9 @@ import (
...
@@ -6,15 +6,9 @@ import (
m
"github.com/torkelo/grafana-pro/pkg/models"
m
"github.com/torkelo/grafana-pro/pkg/models"
)
)
func
AddCollaborator
(
c
*
middleware
.
Context
)
{
func
AddCollaborator
(
c
*
middleware
.
Context
,
cmd
m
.
AddCollaboratorCommand
)
{
var
cmd
m
.
AddCollaboratorCommand
if
!
c
.
JsonBody
(
&
cmd
)
{
userQuery
:=
m
.
GetAccountByLoginQuery
{
LoginOrEmail
:
cmd
.
LoginOrEmail
}
c
.
JsonApiErr
(
400
,
"Invalid request"
,
nil
)
return
}
userQuery
:=
m
.
GetAccountByLoginQuery
{
Login
:
cmd
.
Email
}
err
:=
bus
.
Dispatch
(
&
userQuery
)
err
:=
bus
.
Dispatch
(
&
userQuery
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Collaborator not found"
,
nil
)
c
.
JsonApiErr
(
404
,
"Collaborator not found"
,
nil
)
...
@@ -30,7 +24,6 @@ func AddCollaborator(c *middleware.Context) {
...
@@ -30,7 +24,6 @@ func AddCollaborator(c *middleware.Context) {
cmd
.
AccountId
=
c
.
UserAccount
.
Id
cmd
.
AccountId
=
c
.
UserAccount
.
Id
cmd
.
CollaboratorId
=
accountToAdd
.
Id
cmd
.
CollaboratorId
=
accountToAdd
.
Id
cmd
.
Role
=
m
.
ROLE_READ_WRITE
err
=
bus
.
Dispatch
(
&
cmd
)
err
=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/api/login.go
View file @
f858f6b6
...
@@ -23,7 +23,7 @@ func LoginPost(c *middleware.Context) {
...
@@ -23,7 +23,7 @@ func LoginPost(c *middleware.Context) {
return
return
}
}
userQuery
:=
m
.
GetAccountByLoginQuery
{
Login
:
loginModel
.
Email
}
userQuery
:=
m
.
GetAccountByLoginQuery
{
Login
OrEmail
:
loginModel
.
Email
}
err
:=
bus
.
Dispatch
(
&
userQuery
)
err
:=
bus
.
Dispatch
(
&
userQuery
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/api/login_oauth.go
View file @
f858f6b6
...
@@ -51,7 +51,7 @@ func OAuthLogin(ctx *middleware.Context) {
...
@@ -51,7 +51,7 @@ func OAuthLogin(ctx *middleware.Context) {
log
.
Info
(
"login.OAuthLogin(social login): %s"
,
userInfo
)
log
.
Info
(
"login.OAuthLogin(social login): %s"
,
userInfo
)
userQuery
:=
m
.
GetAccountByLoginQuery
{
Login
:
userInfo
.
Email
}
userQuery
:=
m
.
GetAccountByLoginQuery
{
Login
OrEmail
:
userInfo
.
Email
}
err
=
bus
.
Dispatch
(
&
userQuery
)
err
=
bus
.
Dispatch
(
&
userQuery
)
// create account if missing
// create account if missing
...
...
pkg/api/token.go
View file @
f858f6b6
...
@@ -49,10 +49,10 @@ func AddToken(c *middleware.Context) {
...
@@ -49,10 +49,10 @@ func AddToken(c *middleware.Context) {
return
return
}
}
if
cmd
.
Role
!=
m
.
ROLE_READ_WRITE
&&
cmd
.
Role
!=
m
.
ROLE_READ
{
//
if cmd.Role != m.ROLE_READ_WRITE && cmd.Role != m.ROLE_READ {
c
.
JsonApiErr
(
400
,
"Invalid role specified"
,
nil
)
//
c.JsonApiErr(400, "Invalid role specified", nil)
return
//
return
}
//
}
cmd
.
AccountId
=
c
.
Account
.
Id
cmd
.
AccountId
=
c
.
Account
.
Id
cmd
.
Token
=
util
.
GetRandomString
(
64
)
cmd
.
Token
=
util
.
GetRandomString
(
64
)
...
...
pkg/models/account.go
View file @
f858f6b6
...
@@ -74,8 +74,8 @@ type GetAccountByIdQuery struct {
...
@@ -74,8 +74,8 @@ type GetAccountByIdQuery struct {
}
}
type
GetAccountByLoginQuery
struct
{
type
GetAccountByLoginQuery
struct
{
Login
string
Login
OrEmail
string
Result
*
Account
Result
*
Account
}
}
type
SearchAccountsQuery
struct
{
type
SearchAccountsQuery
struct
{
...
...
pkg/models/collaborator.go
View file @
f858f6b6
package
models
package
models
import
(
import
(
"errors"
"time"
"time"
)
)
const
(
// Typed errors
ROLE_READ_WRITE
RoleType
=
"ReadWrite"
var
(
ROLE_READ
=
"Read"
ErrInvalidRoleType
=
errors
.
New
(
"Invalid role type"
)
)
)
type
RoleType
string
type
RoleType
string
const
(
ROLE_OWNER
RoleType
=
"Owner"
ROLE_VIEWER
RoleType
=
"Viewer"
ROLE_EDITOR
RoleType
=
"Editor"
ROLE_ADMIN
RoleType
=
"Admin"
)
func
(
r
RoleType
)
Validate
()
error
{
if
r
==
ROLE_OWNER
||
r
==
ROLE_VIEWER
||
r
==
ROLE_ADMIN
||
r
==
ROLE_EDITOR
{
return
nil
}
return
ErrInvalidRoleType
}
type
Collaborator
struct
{
type
Collaborator
struct
{
Id
int64
Id
int64
AccountId
int64
`xorm:"not null unique(uix_account_id_for_account_id)"`
AccountId
int64
`xorm:"not null unique(uix_account_id_for_account_id)"`
...
@@ -21,16 +37,6 @@ type Collaborator struct {
...
@@ -21,16 +37,6 @@ type Collaborator struct {
Updated
time
.
Time
Updated
time
.
Time
}
}
func
NewCollaborator
(
accountId
int64
,
collaboratorId
int64
,
role
RoleType
)
*
Collaborator
{
return
&
Collaborator
{
AccountId
:
accountId
,
CollaboratorId
:
collaboratorId
,
Role
:
role
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
}
// ---------------------
// ---------------------
// COMMANDS
// COMMANDS
...
@@ -40,10 +46,10 @@ type RemoveCollaboratorCommand struct {
...
@@ -40,10 +46,10 @@ type RemoveCollaboratorCommand struct {
}
}
type
AddCollaboratorCommand
struct
{
type
AddCollaboratorCommand
struct
{
Email
string
`json:"email" binding:"required"`
LoginOrEmail
string
`json:"loginOrEmail" binding:"Required"`
Role
RoleType
`json:"role" binding:"Required"`
AccountId
int64
`json:"-"`
AccountId
int64
`json:"-"`
CollaboratorId
int64
`json:"-"`
CollaboratorId
int64
`json:"-"`
Role
RoleType
`json:"-"`
}
}
// ----------------------
// ----------------------
...
...
pkg/services/sqlstore/accounts.go
View file @
f858f6b6
...
@@ -134,11 +134,15 @@ func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
...
@@ -134,11 +134,15 @@ func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
}
}
func
GetAccountByLogin
(
query
*
m
.
GetAccountByLoginQuery
)
error
{
func
GetAccountByLogin
(
query
*
m
.
GetAccountByLoginQuery
)
error
{
if
query
.
LoginOrEmail
==
""
{
return
m
.
ErrAccountNotFound
}
account
:=
new
(
m
.
Account
)
account
:=
new
(
m
.
Account
)
if
strings
.
Contains
(
query
.
Login
,
"@"
)
{
if
strings
.
Contains
(
query
.
Login
OrEmail
,
"@"
)
{
account
=
&
m
.
Account
{
Email
:
query
.
Login
}
account
=
&
m
.
Account
{
Email
:
query
.
Login
OrEmail
}
}
else
{
}
else
{
account
=
&
m
.
Account
{
Login
:
strings
.
ToLower
(
query
.
Login
)}
account
=
&
m
.
Account
{
Login
:
strings
.
ToLower
(
query
.
Login
OrEmail
)}
}
}
has
,
err
:=
x
.
Get
(
account
)
has
,
err
:=
x
.
Get
(
account
)
...
...
pkg/services/sqlstore/accounts_test.go
View file @
f858f6b6
...
@@ -46,7 +46,7 @@ func TestAccountDataAccess(t *testing.T) {
...
@@ -46,7 +46,7 @@ func TestAccountDataAccess(t *testing.T) {
cmd
:=
m
.
AddCollaboratorCommand
{
cmd
:=
m
.
AddCollaboratorCommand
{
AccountId
:
ac1
.
Id
,
AccountId
:
ac1
.
Id
,
CollaboratorId
:
ac2
.
Id
,
CollaboratorId
:
ac2
.
Id
,
Role
:
m
.
ROLE_
READ_WRITE
,
Role
:
m
.
ROLE_
VIEWER
,
}
}
err
:=
AddCollaborator
(
&
cmd
)
err
:=
AddCollaborator
(
&
cmd
)
...
...
pkg/services/sqlstore/sqlstore.go
View file @
f858f6b6
...
@@ -45,7 +45,7 @@ func init() {
...
@@ -45,7 +45,7 @@ func init() {
}
}
func
EnsureAdminUser
()
{
func
EnsureAdminUser
()
{
adminQuery
:=
m
.
GetAccountByLoginQuery
{
Login
:
setting
.
AdminUser
}
adminQuery
:=
m
.
GetAccountByLoginQuery
{
Login
OrEmail
:
setting
.
AdminUser
}
if
err
:=
bus
.
Dispatch
(
&
adminQuery
);
err
==
m
.
ErrAccountNotFound
{
if
err
:=
bus
.
Dispatch
(
&
adminQuery
);
err
==
m
.
ErrAccountNotFound
{
cmd
:=
m
.
CreateAccountCommand
{}
cmd
:=
m
.
CreateAccountCommand
{}
...
...
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