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
26e4809e
Commit
26e4809e
authored
Feb 23, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Big Backend Refatoring: Renamed Account -> Org
parent
e9e2fa29
Hide whitespace changes
Inline
Side-by-side
Showing
37 changed files
with
450 additions
and
449 deletions
+450
-449
main.go
+3
-3
pkg/api/api.go
+5
-5
pkg/api/apikey.go
+4
-4
pkg/api/dashboard.go
+4
-4
pkg/api/dataproxy.go
+2
-2
pkg/api/datasources.go
+8
-8
pkg/api/dtos/models.go
+3
-3
pkg/api/frontendsettings.go
+4
-4
pkg/api/index.go
+2
-2
pkg/api/org.go
+13
-13
pkg/api/org_users.go
+10
-10
pkg/api/search.go
+2
-2
pkg/api/user.go
+20
-20
pkg/cmd/dashboard.go
+8
-8
pkg/cmd/datasource.go
+23
-23
pkg/cmd/orgs.go
+35
-35
pkg/events/events.go
+2
-2
pkg/middleware/auth.go
+1
-1
pkg/middleware/middleware.go
+9
-8
pkg/models/apikey.go
+16
-16
pkg/models/dashboards.go
+10
-10
pkg/models/datasource.go
+15
-15
pkg/models/org.go
+21
-26
pkg/models/org_user.go
+23
-23
pkg/models/search.go
+3
-3
pkg/models/user.go
+13
-8
pkg/services/sqlstore/apikey.go
+15
-15
pkg/services/sqlstore/apikey_test.go
+1
-1
pkg/services/sqlstore/dashboard.go
+8
-8
pkg/services/sqlstore/dashboard_test.go
+11
-11
pkg/services/sqlstore/datasource.go
+10
-10
pkg/services/sqlstore/datasource_test.go
+15
-15
pkg/services/sqlstore/org.go
+46
-47
pkg/services/sqlstore/org_test.go
+7
-7
pkg/services/sqlstore/org_users.go
+20
-20
pkg/services/sqlstore/user.go
+46
-45
pkg/setting/setting.go
+12
-12
No files found.
main.go
View file @
26e4809e
...
...
@@ -32,9 +32,9 @@ func main() {
app
.
Usage
=
"grafana web"
app
.
Version
=
version
app
.
Commands
=
[]
cli
.
Command
{
cmd
.
List
Account
s
,
cmd
.
Create
Account
,
cmd
.
Delete
Account
,
cmd
.
List
Org
s
,
cmd
.
Create
Org
,
cmd
.
Delete
Org
,
cmd
.
ImportDashboard
,
cmd
.
ListDataSources
,
cmd
.
CreateDataSource
,
...
...
pkg/api/api.go
View file @
26e4809e
...
...
@@ -46,8 +46,8 @@ func Register(r *macaron.Macaron) {
r
.
Group
(
"/user"
,
func
()
{
r
.
Get
(
"/"
,
GetUser
)
r
.
Put
(
"/"
,
bind
(
m
.
UpdateUserCommand
{}),
UpdateUser
)
r
.
Post
(
"/using/:id"
,
SetUsingAccount
)
r
.
Get
(
"/
accounts"
,
GetUserAccounts
)
r
.
Post
(
"/using/:id"
,
UserSetUsingOrg
)
r
.
Get
(
"/
orgs"
,
GetUserOrgList
)
r
.
Post
(
"/stars/dashboard/:id"
,
StarDashboard
)
r
.
Delete
(
"/stars/dashboard/:id"
,
UnstarDashboard
)
r
.
Put
(
"/password"
,
bind
(
m
.
ChangeUserPasswordCommand
{}),
ChangeUserPassword
)
...
...
@@ -56,9 +56,9 @@ func Register(r *macaron.Macaron) {
// account
r
.
Group
(
"/org"
,
func
()
{
r
.
Get
(
"/"
,
GetOrg
)
r
.
Post
(
"/"
,
bind
(
m
.
Create
Account
Command
{}),
CreateOrg
)
r
.
Put
(
"/"
,
bind
(
m
.
Update
Account
Command
{}),
UpdateOrg
)
r
.
Post
(
"/users"
,
bind
(
m
.
Add
Account
UserCommand
{}),
AddOrgUser
)
r
.
Post
(
"/"
,
bind
(
m
.
Create
Org
Command
{}),
CreateOrg
)
r
.
Put
(
"/"
,
bind
(
m
.
Update
Org
Command
{}),
UpdateOrg
)
r
.
Post
(
"/users"
,
bind
(
m
.
Add
Org
UserCommand
{}),
AddOrgUser
)
r
.
Get
(
"/users"
,
GetOrgUsers
)
r
.
Delete
(
"/users/:id"
,
RemoveOrgUser
)
},
reqAccountAdmin
)
...
...
pkg/api/apikey.go
View file @
26e4809e
...
...
@@ -8,7 +8,7 @@ import (
)
func
GetApiKeys
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetApiKeysQuery
{
AccountId
:
c
.
Account
Id
}
query
:=
m
.
GetApiKeysQuery
{
OrgId
:
c
.
Org
Id
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to list api keys"
,
err
)
...
...
@@ -30,7 +30,7 @@ func GetApiKeys(c *middleware.Context) {
func
DeleteApiKey
(
c
*
middleware
.
Context
)
{
id
:=
c
.
ParamsInt64
(
":id"
)
cmd
:=
&
m
.
DeleteApiKeyCommand
{
Id
:
id
,
AccountId
:
c
.
Account
Id
}
cmd
:=
&
m
.
DeleteApiKeyCommand
{
Id
:
id
,
OrgId
:
c
.
Org
Id
}
err
:=
bus
.
Dispatch
(
cmd
)
if
err
!=
nil
{
...
...
@@ -47,7 +47,7 @@ func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) {
return
}
cmd
.
AccountId
=
c
.
Account
Id
cmd
.
OrgId
=
c
.
Org
Id
cmd
.
Key
=
util
.
GetRandomString
(
64
)
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
...
...
@@ -71,7 +71,7 @@ func UpdateApiKey(c *middleware.Context, cmd m.UpdateApiKeyCommand) {
return
}
cmd
.
AccountId
=
c
.
Account
Id
cmd
.
OrgId
=
c
.
Org
Id
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
...
...
pkg/api/dashboard.go
View file @
26e4809e
...
...
@@ -29,7 +29,7 @@ func isDasboardStarredByUser(c *middleware.Context, dashId int64) (bool, error)
func
GetDashboard
(
c
*
middleware
.
Context
)
{
slug
:=
c
.
Params
(
":slug"
)
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
AccountId
:
c
.
Account
Id
}
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
OrgId
:
c
.
Org
Id
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Dashboard not found"
,
nil
)
...
...
@@ -54,13 +54,13 @@ func GetDashboard(c *middleware.Context) {
func
DeleteDashboard
(
c
*
middleware
.
Context
)
{
slug
:=
c
.
Params
(
":slug"
)
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
AccountId
:
c
.
Account
Id
}
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
OrgId
:
c
.
Org
Id
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Dashboard not found"
,
nil
)
return
}
cmd
:=
m
.
DeleteDashboardCommand
{
Slug
:
slug
,
AccountId
:
c
.
Account
Id
}
cmd
:=
m
.
DeleteDashboardCommand
{
Slug
:
slug
,
OrgId
:
c
.
Org
Id
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to delete dashboard"
,
err
)
return
...
...
@@ -72,7 +72,7 @@ func DeleteDashboard(c *middleware.Context) {
}
func
PostDashboard
(
c
*
middleware
.
Context
,
cmd
m
.
SaveDashboardCommand
)
{
cmd
.
AccountId
=
c
.
Account
Id
cmd
.
OrgId
=
c
.
Org
Id
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
...
...
pkg/api/dataproxy.go
View file @
26e4809e
...
...
@@ -39,8 +39,8 @@ func ProxyDataSourceRequest(c *middleware.Context) {
id
:=
c
.
ParamsInt64
(
":id"
)
query
:=
m
.
GetDataSourceByIdQuery
{
Id
:
id
,
AccountId
:
c
.
Account
Id
,
Id
:
id
,
OrgId
:
c
.
Org
Id
,
}
err
:=
bus
.
Dispatch
(
&
query
)
...
...
pkg/api/datasources.go
View file @
26e4809e
...
...
@@ -8,7 +8,7 @@ import (
)
func
GetDataSources
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetDataSourcesQuery
{
AccountId
:
c
.
Account
Id
}
query
:=
m
.
GetDataSourcesQuery
{
OrgId
:
c
.
Org
Id
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to query datasources"
,
err
)
...
...
@@ -19,7 +19,7 @@ func GetDataSources(c *middleware.Context) {
for
i
,
ds
:=
range
query
.
Result
{
result
[
i
]
=
&
dtos
.
DataSource
{
Id
:
ds
.
Id
,
AccountId
:
ds
.
Account
Id
,
OrgId
:
ds
.
Org
Id
,
Name
:
ds
.
Name
,
Url
:
ds
.
Url
,
Type
:
ds
.
Type
,
...
...
@@ -37,8 +37,8 @@ func GetDataSources(c *middleware.Context) {
func
GetDataSourceById
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetDataSourceByIdQuery
{
Id
:
c
.
ParamsInt64
(
":id"
),
AccountId
:
c
.
Account
Id
,
Id
:
c
.
ParamsInt64
(
":id"
),
OrgId
:
c
.
Org
Id
,
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
...
...
@@ -50,7 +50,7 @@ func GetDataSourceById(c *middleware.Context) {
c
.
JSON
(
200
,
&
dtos
.
DataSource
{
Id
:
ds
.
Id
,
AccountId
:
ds
.
Account
Id
,
OrgId
:
ds
.
Org
Id
,
Name
:
ds
.
Name
,
Url
:
ds
.
Url
,
Type
:
ds
.
Type
,
...
...
@@ -71,7 +71,7 @@ func DeleteDataSource(c *middleware.Context) {
return
}
cmd
:=
&
m
.
DeleteDataSourceCommand
{
Id
:
id
,
AccountId
:
c
.
Account
Id
}
cmd
:=
&
m
.
DeleteDataSourceCommand
{
Id
:
id
,
OrgId
:
c
.
Org
Id
}
err
:=
bus
.
Dispatch
(
cmd
)
if
err
!=
nil
{
...
...
@@ -90,7 +90,7 @@ func AddDataSource(c *middleware.Context) {
return
}
cmd
.
AccountId
=
c
.
Account
Id
cmd
.
OrgId
=
c
.
Org
Id
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to add datasource"
,
err
)
...
...
@@ -108,7 +108,7 @@ func UpdateDataSource(c *middleware.Context) {
return
}
cmd
.
AccountId
=
c
.
Account
Id
cmd
.
OrgId
=
c
.
Org
Id
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
...
...
pkg/api/dtos/models.go
View file @
26e4809e
...
...
@@ -19,8 +19,8 @@ type CurrentUser struct {
Login
string
`json:"login"`
Email
string
`json:"email"`
Name
string
`json:"name"`
AccountRole
m
.
RoleType
`json:"account
Role"`
AccountName
string
`json:"account
Name"`
OrgRole
m
.
RoleType
`json:"org
Role"`
OrgName
string
`json:"org
Name"`
IsGrafanaAdmin
bool
`json:"isGrafanaAdmin"`
GravatarUrl
string
`json:"gravatarUrl"`
}
...
...
@@ -38,7 +38,7 @@ type Dashboard struct {
type
DataSource
struct
{
Id
int64
`json:"id"`
AccountId
int64
`json:"account
Id"`
OrgId
int64
`json:"org
Id"`
Name
string
`json:"name"`
Type
m
.
DsType
`json:"type"`
Access
m
.
DsAccess
`json:"access"`
...
...
pkg/api/frontendsettings.go
View file @
26e4809e
...
...
@@ -10,22 +10,22 @@ import (
)
func
getFrontendSettingsMap
(
c
*
middleware
.
Context
)
(
map
[
string
]
interface
{},
error
)
{
account
DataSources
:=
make
([]
*
m
.
DataSource
,
0
)
org
DataSources
:=
make
([]
*
m
.
DataSource
,
0
)
if
c
.
IsSignedIn
{
query
:=
m
.
GetDataSourcesQuery
{
AccountId
:
c
.
Account
Id
}
query
:=
m
.
GetDataSourcesQuery
{
OrgId
:
c
.
Org
Id
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
return
nil
,
err
}
account
DataSources
=
query
.
Result
org
DataSources
=
query
.
Result
}
datasources
:=
make
(
map
[
string
]
interface
{})
for
_
,
ds
:=
range
account
DataSources
{
for
_
,
ds
:=
range
org
DataSources
{
url
:=
ds
.
Url
if
ds
.
Access
==
m
.
DS_ACCESS_PROXY
{
...
...
pkg/api/index.go
View file @
26e4809e
...
...
@@ -17,8 +17,8 @@ func setIndexViewData(c *middleware.Context) error {
Login
:
c
.
Login
,
Email
:
c
.
Email
,
Name
:
c
.
Name
,
AccountName
:
c
.
Account
Name
,
AccountRole
:
c
.
Account
Role
,
OrgName
:
c
.
Org
Name
,
OrgRole
:
c
.
Org
Role
,
GravatarUrl
:
dtos
.
GetGravatarUrl
(
c
.
Email
),
IsGrafanaAdmin
:
c
.
IsGrafanaAdmin
,
}
...
...
pkg/api/
account
.go
→
pkg/api/
org
.go
View file @
26e4809e
...
...
@@ -7,44 +7,44 @@ import (
)
func
GetOrg
(
c
*
middleware
.
Context
)
{
query
:=
m
.
Get
AccountByIdQuery
{
Id
:
c
.
Account
Id
}
query
:=
m
.
Get
OrgByIdQuery
{
Id
:
c
.
Org
Id
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
if
err
==
m
.
Err
Account
NotFound
{
c
.
JsonApiErr
(
404
,
"
Account
not found"
,
err
)
if
err
==
m
.
Err
Org
NotFound
{
c
.
JsonApiErr
(
404
,
"
Organization
not found"
,
err
)
return
}
c
.
JsonApiErr
(
500
,
"Failed to get
account
"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to get
organization
"
,
err
)
return
}
account
:=
m
.
Account
DTO
{
org
:=
m
.
Org
DTO
{
Id
:
query
.
Result
.
Id
,
Name
:
query
.
Result
.
Name
,
}
c
.
JSON
(
200
,
&
account
)
c
.
JSON
(
200
,
&
org
)
}
func
CreateOrg
(
c
*
middleware
.
Context
,
cmd
m
.
Create
Account
Command
)
{
func
CreateOrg
(
c
*
middleware
.
Context
,
cmd
m
.
Create
Org
Command
)
{
cmd
.
UserId
=
c
.
UserId
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to create
account
"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to create
organization
"
,
err
)
return
}
c
.
JsonOK
(
"
Account
created"
)
c
.
JsonOK
(
"
Organization
created"
)
}
func
UpdateOrg
(
c
*
middleware
.
Context
,
cmd
m
.
Update
Account
Command
)
{
cmd
.
AccountId
=
c
.
Account
Id
func
UpdateOrg
(
c
*
middleware
.
Context
,
cmd
m
.
Update
Org
Command
)
{
cmd
.
OrgId
=
c
.
Org
Id
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to update
account
"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to update
organization
"
,
err
)
return
}
c
.
JsonOK
(
"
Account
updated"
)
c
.
JsonOK
(
"
Organization
updated"
)
}
pkg/api/
account
_users.go
→
pkg/api/
org
_users.go
View file @
26e4809e
...
...
@@ -6,7 +6,7 @@ import (
m
"github.com/grafana/grafana/pkg/models"
)
func
AddOrgUser
(
c
*
middleware
.
Context
,
cmd
m
.
Add
Account
UserCommand
)
{
func
AddOrgUser
(
c
*
middleware
.
Context
,
cmd
m
.
Add
Org
UserCommand
)
{
if
!
cmd
.
Role
.
IsValid
()
{
c
.
JsonApiErr
(
400
,
"Invalid role specified"
,
nil
)
return
...
...
@@ -26,19 +26,19 @@ func AddOrgUser(c *middleware.Context, cmd m.AddAccountUserCommand) {
return
}
cmd
.
AccountId
=
c
.
Account
Id
cmd
.
OrgId
=
c
.
Org
Id
cmd
.
UserId
=
userToAdd
.
Id
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Could not add user to
account
"
,
err
)
c
.
JsonApiErr
(
500
,
"Could not add user to
organization
"
,
err
)
return
}
c
.
JsonOK
(
"User added to
account
"
)
c
.
JsonOK
(
"User added to
organization
"
)
}
func
GetOrgUsers
(
c
*
middleware
.
Context
)
{
query
:=
m
.
Get
AccountUsersQuery
{
AccountId
:
c
.
Account
Id
}
query
:=
m
.
Get
OrgUsersQuery
{
OrgId
:
c
.
Org
Id
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to get account user"
,
err
)
...
...
@@ -51,15 +51,15 @@ func GetOrgUsers(c *middleware.Context) {
func
RemoveOrgUser
(
c
*
middleware
.
Context
)
{
userId
:=
c
.
ParamsInt64
(
":id"
)
cmd
:=
m
.
Remove
AccountUserCommand
{
AccountId
:
c
.
Account
Id
,
UserId
:
userId
}
cmd
:=
m
.
Remove
OrgUserCommand
{
OrgId
:
c
.
Org
Id
,
UserId
:
userId
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
if
err
==
m
.
ErrLast
Account
Admin
{
c
.
JsonApiErr
(
400
,
"Cannot remove last
account
admin"
,
nil
)
if
err
==
m
.
ErrLast
Org
Admin
{
c
.
JsonApiErr
(
400
,
"Cannot remove last
organization
admin"
,
nil
)
return
}
c
.
JsonApiErr
(
500
,
"Failed to remove user from
account
"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to remove user from
organization
"
,
err
)
}
c
.
JsonOK
(
"User removed from
account
"
)
c
.
JsonOK
(
"User removed from
organization
"
)
}
pkg/api/search.go
View file @
26e4809e
...
...
@@ -44,7 +44,7 @@ func Search(c *middleware.Context) {
if
tagcloud
==
"true"
{
query
:=
m
.
GetDashboardTagsQuery
{
AccountId
:
c
.
Account
Id
}
query
:=
m
.
GetDashboardTagsQuery
{
OrgId
:
c
.
Org
Id
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to get tags from database"
,
err
)
...
...
@@ -60,7 +60,7 @@ func Search(c *middleware.Context) {
UserId
:
c
.
UserId
,
Limit
:
limit
,
IsStarred
:
starred
==
"true"
,
AccountId
:
c
.
Account
Id
,
OrgId
:
c
.
Org
Id
,
}
err
:=
bus
.
Dispatch
(
&
query
)
...
...
pkg/api/user.go
View file @
26e4809e
...
...
@@ -11,7 +11,7 @@ func GetUser(c *middleware.Context) {
query
:=
m
.
GetUserInfoQuery
{
UserId
:
c
.
UserId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to get
account
"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to get
user
"
,
err
)
return
}
...
...
@@ -22,23 +22,23 @@ func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) {
cmd
.
UserId
=
c
.
UserId
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
400
,
"Failed to update
account
"
,
err
)
c
.
JsonApiErr
(
400
,
"Failed to update
user
"
,
err
)
return
}
c
.
JsonOK
(
"
Account
updated"
)
c
.
JsonOK
(
"
User
updated"
)
}
func
GetUser
Accounts
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetUser
Accounts
Query
{
UserId
:
c
.
UserId
}
func
GetUser
OrgList
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetUser
OrgList
Query
{
UserId
:
c
.
UserId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to get user
account
s"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed to get user
organization
s"
,
err
)
return
}
for
_
,
ac
:=
range
query
.
Result
{
if
ac
.
AccountId
==
c
.
Account
Id
{
if
ac
.
OrgId
==
c
.
Org
Id
{
ac
.
IsUsing
=
true
break
}
...
...
@@ -47,17 +47,17 @@ func GetUserAccounts(c *middleware.Context) {
c
.
JSON
(
200
,
query
.
Result
)
}
func
validateUsing
Account
(
userId
int64
,
account
Id
int64
)
bool
{
query
:=
m
.
GetUser
Accounts
Query
{
UserId
:
userId
}
func
validateUsing
Org
(
userId
int64
,
org
Id
int64
)
bool
{
query
:=
m
.
GetUser
OrgList
Query
{
UserId
:
userId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
return
false
}
// validate that the
account
id in the list
// validate that the
org
id in the list
valid
:=
false
for
_
,
other
:=
range
query
.
Result
{
if
other
.
AccountId
==
account
Id
{
if
other
.
OrgId
==
org
Id
{
valid
=
true
}
}
...
...
@@ -65,25 +65,25 @@ func validateUsingAccount(userId int64, accountId int64) bool {
return
valid
}
func
SetUsingAccount
(
c
*
middleware
.
Context
)
{
usingAccount
Id
:=
c
.
ParamsInt64
(
":id"
)
func
UserSetUsingOrg
(
c
*
middleware
.
Context
)
{
org
Id
:=
c
.
ParamsInt64
(
":id"
)
if
!
validateUsing
Account
(
c
.
UserId
,
usingAccount
Id
)
{
c
.
JsonApiErr
(
401
,
"Not a valid
account
"
,
nil
)
if
!
validateUsing
Org
(
c
.
UserId
,
org
Id
)
{
c
.
JsonApiErr
(
401
,
"Not a valid
organization
"
,
nil
)
return
}
cmd
:=
m
.
SetUsing
Account
Command
{
UserId
:
c
.
UserId
,
AccountId
:
usingAccount
Id
,
cmd
:=
m
.
SetUsing
Org
Command
{
UserId
:
c
.
UserId
,
OrgId
:
org
Id
,
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed change active
account
"
,
err
)
c
.
JsonApiErr
(
500
,
"Failed change active
organization
"
,
err
)
return
}
c
.
JsonOK
(
"Active
account
changed"
)
c
.
JsonOK
(
"Active
organization
changed"
)
}
func
ChangeUserPassword
(
c
*
middleware
.
Context
,
cmd
m
.
ChangeUserPasswordCommand
)
{
...
...
pkg/cmd/dashboard.go
View file @
26e4809e
...
...
@@ -41,19 +41,19 @@ func runImport(c *cli.Context) {
}
if
!
c
.
Args
()
.
Present
()
{
log
.
ConsoleFatal
(
"
Account
name arg is required"
)
log
.
ConsoleFatal
(
"
Organization
name arg is required"
)
}
account
Name
:=
c
.
Args
()
.
First
()
org
Name
:=
c
.
Args
()
.
First
()
initRuntime
(
c
)
accountQuery
:=
m
.
GetAccountByNameQuery
{
Name
:
account
Name
}
if
err
:=
bus
.
Dispatch
(
&
account
Query
);
err
!=
nil
{
orgQuery
:=
m
.
GetOrgByNameQuery
{
Name
:
org
Name
}
if
err
:=
bus
.
Dispatch
(
&
org
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find account"
,
err
)
}
accountId
:=
account
Query
.
Result
.
Id
orgId
:=
org
Query
.
Result
.
Id
visitor
:=
func
(
path
string
,
f
os
.
FileInfo
,
err
error
)
error
{
if
err
!=
nil
{
...
...
@@ -63,7 +63,7 @@ func runImport(c *cli.Context) {
return
nil
}
if
strings
.
HasSuffix
(
f
.
Name
(),
".json"
)
{
if
err
:=
importDashboard
(
path
,
account
Id
);
err
!=
nil
{
if
err
:=
importDashboard
(
path
,
org
Id
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to import dashboard file: %v, err: %v"
,
path
,
err
)
}
}
...
...
@@ -75,7 +75,7 @@ func runImport(c *cli.Context) {
}
}
func
importDashboard
(
path
string
,
account
Id
int64
)
error
{
func
importDashboard
(
path
string
,
org
Id
int64
)
error
{
log
.
ConsoleInfof
(
"Importing %v"
,
path
)
reader
,
err
:=
os
.
Open
(
path
)
...
...
@@ -92,7 +92,7 @@ func importDashboard(path string, accountId int64) error {
dash
.
Data
[
"id"
]
=
nil
cmd
:=
m
.
SaveDashboardCommand
{
AccountId
:
account
Id
,
OrgId
:
org
Id
,
Dashboard
:
dash
.
Data
,
}
...
...
pkg/cmd/datasource.go
View file @
26e4809e
...
...
@@ -81,14 +81,14 @@ func createDataSource(c *cli.Context) {
dsAccess
:=
c
.
String
(
"access"
)
dsDefault
:=
c
.
Bool
(
"default"
)
accountQuery
:=
m
.
GetAccount
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
account
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
account
: %s"
,
err
)
orgQuery
:=
m
.
GetOrg
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
org
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
organization
: %s"
,
err
)
}
accountId
:=
account
Query
.
Result
.
Id
orgId
:=
org
Query
.
Result
.
Id
query
:=
m
.
GetDataSourceByNameQuery
{
AccountId
:
account
Id
,
Name
:
ds
}
query
:=
m
.
GetDataSourceByNameQuery
{
OrgId
:
org
Id
,
Name
:
ds
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
if
err
!=
m
.
ErrDataSourceNotFound
{
log
.
ConsoleFatalf
(
"Failed to query for existing datasource: %s"
,
err
)
...
...
@@ -100,7 +100,7 @@ func createDataSource(c *cli.Context) {
}
cmd
:=
m
.
AddDataSourceCommand
{
AccountId
:
account
Id
,
OrgId
:
org
Id
,
Name
:
ds
,
Url
:
url
,
Type
:
m
.
DsType
(
dsType
),
...
...
@@ -135,14 +135,14 @@ func listDatasources(c *cli.Context) {
}
name
:=
c
.
Args
()
.
First
()
accountQuery
:=
m
.
GetAccount
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
account
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
account
: %s"
,
err
)
orgQuery
:=
m
.
GetOrg
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
org
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
organization
: %s"
,
err
)
}
accountId
:=
account
Query
.
Result
.
Id
orgId
:=
org
Query
.
Result
.
Id
query
:=
m
.
GetDataSourcesQuery
{
AccountId
:
account
Id
}
query
:=
m
.
GetDataSourcesQuery
{
OrgId
:
org
Id
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find datasources: %s"
,
err
)
}
...
...
@@ -161,20 +161,20 @@ func describeDataSource(c *cli.Context) {
initRuntime
(
c
)
if
len
(
c
.
Args
())
!=
2
{
log
.
ConsoleFatal
(
"
Account
and datasource name args are required"
)
log
.
ConsoleFatal
(
"
Organization
and datasource name args are required"
)
}
name
:=
c
.
Args
()
.
First
()
ds
:=
c
.
Args
()[
1
]
accountQuery
:=
m
.
GetAccount
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
account
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
account
: %s"
,
err
)
orgQuery
:=
m
.
GetOrg
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
org
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
organization
: %s"
,
err
)
}
accountId
:=
account
Query
.
Result
.
Id
orgId
:=
org
Query
.
Result
.
Id
query
:=
m
.
GetDataSourceByNameQuery
{
AccountId
:
account
Id
,
Name
:
ds
}
query
:=
m
.
GetDataSourceByNameQuery
{
OrgId
:
org
Id
,
Name
:
ds
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find datasource: %s"
,
err
)
}
...
...
@@ -208,20 +208,20 @@ func deleteDataSource(c *cli.Context) {
name
:=
c
.
Args
()
.
First
()
ds
:=
c
.
Args
()[
1
]
accountQuery
:=
m
.
GetAccount
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
account
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
account
: %s"
,
err
)
orgQuery
:=
m
.
GetOrg
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
org
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
organization
: %s"
,
err
)
}
accountId
:=
account
Query
.
Result
.
Id
orgId
:=
org
Query
.
Result
.
Id
query
:=
m
.
GetDataSourceByNameQuery
{
AccountId
:
account
Id
,
Name
:
ds
}
query
:=
m
.
GetDataSourceByNameQuery
{
OrgId
:
org
Id
,
Name
:
ds
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find datasource: %s"
,
err
)
}
datasource
:=
query
.
Result
cmd
:=
m
.
DeleteDataSourceCommand
{
AccountId
:
account
Id
,
Id
:
datasource
.
Id
}
cmd
:=
m
.
DeleteDataSourceCommand
{
OrgId
:
org
Id
,
Id
:
datasource
.
Id
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to delete datasource: %s"
,
err
)
}
...
...
pkg/cmd/
account
s.go
→
pkg/cmd/
org
s.go
View file @
26e4809e
...
...
@@ -13,49 +13,49 @@ import (
"github.com/grafana/grafana/pkg/setting"
)
var
List
Account
s
=
cli
.
Command
{
Name
:
"
account
s"
,
Usage
:
"list
account
s"
,
Description
:
"Lists the
account
s in the system"
,
Action
:
list
Account
s
,
var
List
Org
s
=
cli
.
Command
{
Name
:
"
org
s"
,
Usage
:
"list
organization
s"
,
Description
:
"Lists the
organization
s in the system"
,
Action
:
list
Org
s
,
}
var
Create
Account
=
cli
.
Command
{
Name
:
"
account
s:create"
,
Usage
:
"
create a new account
"
,
Description
:
"Creates a new
account
"
,
Action
:
create
Account
,
var
Create
Org
=
cli
.
Command
{
Name
:
"
org
s:create"
,
Usage
:
"
Creates a new organization
"
,
Description
:
"Creates a new
organization
"
,
Action
:
create
Org
,
}
var
Delete
Account
=
cli
.
Command
{
Name
:
"
account
s:delete"
,
Usage
:
"
delete an existing account
"
,
Description
:
"Deletes an existing
account
"
,
Action
:
delete
Account
,
var
Delete
Org
=
cli
.
Command
{
Name
:
"
org
s:delete"
,
Usage
:
"
Delete an existing organization
"
,
Description
:
"Deletes an existing
organization
"
,
Action
:
delete
Org
,
}
func
list
Account
s
(
c
*
cli
.
Context
)
{
func
list
Org
s
(
c
*
cli
.
Context
)
{
initRuntime
(
c
)
accountsQuery
:=
m
.
GetAccounts
Query
{}
if
err
:=
bus
.
Dispatch
(
&
account
sQuery
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
account
s: %s"
,
err
)
orgsQuery
:=
m
.
GetOrgList
Query
{}
if
err
:=
bus
.
Dispatch
(
&
org
sQuery
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
organization
s: %s"
,
err
)
}
w
:=
tabwriter
.
NewWriter
(
os
.
Stdout
,
8
,
1
,
4
,
' '
,
0
)
fmt
.
Fprintf
(
w
,
"ID
\t
NAME
\n
"
)
for
_
,
account
:=
range
account
sQuery
.
Result
{
fmt
.
Fprintf
(
w
,
"%d
\t
%s
\n
"
,
account
.
Id
,
account
.
Name
)
for
_
,
org
:=
range
org
sQuery
.
Result
{
fmt
.
Fprintf
(
w
,
"%d
\t
%s
\n
"
,
org
.
Id
,
org
.
Name
)
}
w
.
Flush
()
}
func
create
Account
(
c
*
cli
.
Context
)
{
func
create
Org
(
c
*
cli
.
Context
)
{
initRuntime
(
c
)
if
!
c
.
Args
()
.
Present
()
{
log
.
ConsoleFatal
(
"
Account
name arg is required"
)
log
.
ConsoleFatal
(
"
Organization
name arg is required"
)
}
name
:=
c
.
Args
()
.
First
()
...
...
@@ -68,32 +68,32 @@ func createAccount(c *cli.Context) {
adminUser
:=
adminQuery
.
Result
cmd
:=
m
.
Create
Account
Command
{
Name
:
name
,
UserId
:
adminUser
.
Id
}
cmd
:=
m
.
Create
Org
Command
{
Name
:
name
,
UserId
:
adminUser
.
Id
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to create
account
: %s"
,
err
)
log
.
ConsoleFatalf
(
"Failed to create
organization
: %s"
,
err
)
}
log
.
ConsoleInfof
(
"
Account
%s created for admin user %s
\n
"
,
name
,
adminUser
.
Email
)
log
.
ConsoleInfof
(
"
Organization
%s created for admin user %s
\n
"
,
name
,
adminUser
.
Email
)
}
func
delete
Account
(
c
*
cli
.
Context
)
{
func
delete
Org
(
c
*
cli
.
Context
)
{
initRuntime
(
c
)
if
!
c
.
Args
()
.
Present
()
{
log
.
ConsoleFatal
(
"
Account
name arg is required"
)
log
.
ConsoleFatal
(
"
Organization
name arg is required"
)
}
name
:=
c
.
Args
()
.
First
()
accountQuery
:=
m
.
GetAccount
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
account
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
account
: %s"
,
err
)
orgQuery
:=
m
.
GetOrg
ByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
org
Query
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find
organization
: %s"
,
err
)
}
accountId
:=
account
Query
.
Result
.
Id
cmd
:=
m
.
Delete
AccountCommand
{
Id
:
account
Id
}
orgId
:=
org
Query
.
Result
.
Id
cmd
:=
m
.
Delete
OrgCommand
{
Id
:
org
Id
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to delete
account
: %s"
,
err
)
log
.
ConsoleFatalf
(
"Failed to delete
organization
: %s"
,
err
)
}
log
.
ConsoleInfof
(
"
Account
%s deleted"
,
name
)
log
.
ConsoleInfof
(
"
Organization
%s deleted"
,
name
)
}
pkg/events/events.go
View file @
26e4809e
...
...
@@ -50,13 +50,13 @@ func ToOnWriteEvent(event interface{}) (*OnTheWireEvent, error) {
return
&
wireEvent
,
nil
}
type
Account
Created
struct
{
type
Org
Created
struct
{
Timestamp
time
.
Time
`json:"timestamp"`
Id
int64
`json:"id"`
Name
string
`json:"name"`
}
type
Account
Updated
struct
{
type
Org
Updated
struct
{
Timestamp
time
.
Time
`json:"timestamp"`
Id
int64
`json:"id"`
Name
string
`json:"name"`
...
...
pkg/middleware/auth.go
View file @
26e4809e
...
...
@@ -56,7 +56,7 @@ func RoleAuth(roles ...m.RoleType) macaron.Handler {
return
func
(
c
*
Context
)
{
ok
:=
false
for
_
,
role
:=
range
roles
{
if
role
==
c
.
Account
Role
{
if
role
==
c
.
Org
Role
{
ok
=
true
break
}
...
...
pkg/middleware/middleware.go
View file @
26e4809e
...
...
@@ -56,22 +56,23 @@ func GetContextHandler() macaron.Handler {
ctx
.
SignedInUser
=
&
m
.
SignedInUser
{}
// TODO: fix this
ctx
.
Account
Role
=
keyInfo
.
Role
ctx
.
Org
Role
=
keyInfo
.
Role
ctx
.
ApiKeyId
=
keyInfo
.
Id
ctx
.
AccountId
=
keyInfo
.
Account
Id
ctx
.
OrgId
=
keyInfo
.
Org
Id
}
}
else
if
setting
.
AnonymousEnabled
{
accountQuery
:=
m
.
GetAccountByNameQuery
{
Name
:
setting
.
AnonymousAccount
Name
}
if
err
:=
bus
.
Dispatch
(
&
account
Query
);
err
!=
nil
{
if
err
==
m
.
Err
Account
NotFound
{
log
.
Error
(
3
,
"Anonymous access
account
name does not exist"
,
nil
)
orgQuery
:=
m
.
GetOrgByNameQuery
{
Name
:
setting
.
AnonymousOrg
Name
}
if
err
:=
bus
.
Dispatch
(
&
org
Query
);
err
!=
nil
{
if
err
==
m
.
Err
Org
NotFound
{
log
.
Error
(
3
,
"Anonymous access
organization
name does not exist"
,
nil
)
}
}
else
{
ctx
.
IsSignedIn
=
false
ctx
.
HasAnonymousAccess
=
true
ctx
.
SignedInUser
=
&
m
.
SignedInUser
{}
ctx
.
AccountRole
=
m
.
RoleType
(
setting
.
AnonymousAccountRole
)
ctx
.
AccountId
=
accountQuery
.
Result
.
Id
ctx
.
OrgRole
=
m
.
RoleType
(
setting
.
AnonymousOrgRole
)
ctx
.
OrgId
=
orgQuery
.
Result
.
Id
ctx
.
OrgName
=
orgQuery
.
Result
.
Name
}
}
...
...
pkg/models/apikey.go
View file @
26e4809e
...
...
@@ -8,22 +8,22 @@ import (
var
ErrInvalidApiKey
=
errors
.
New
(
"Invalid API Key"
)
type
ApiKey
struct
{
Id
int64
AccountId
int64
Name
string
Key
string
Role
RoleType
Created
time
.
Time
Updated
time
.
Time
Id
int64
OrgId
int64
Name
string
Key
string
Role
RoleType
Created
time
.
Time
Updated
time
.
Time
}
// ---------------------
// COMMANDS
type
AddApiKeyCommand
struct
{
Name
string
`json:"name" binding:"Required"`
Role
RoleType
`json:"role" binding:"Required"`
Account
Id
int64
`json:"-"`
Key
string
`json:"-"`
Name
string
`json:"name" binding:"Required"`
Role
RoleType
`json:"role" binding:"Required"`
Org
Id
int64
`json:"-"`
Key
string
`json:"-"`
Result
*
ApiKey
`json:"-"`
}
...
...
@@ -33,20 +33,20 @@ type UpdateApiKeyCommand struct {
Name
string
`json:"name"`
Role
RoleType
`json:"role"`
Account
Id
int64
`json:"-"`
Org
Id
int64
`json:"-"`
}
type
DeleteApiKeyCommand
struct
{
Id
int64
`json:"id"`
Account
Id
int64
`json:"-"`
Id
int64
`json:"id"`
Org
Id
int64
`json:"-"`
}
// ----------------------
// QUERIES
type
GetApiKeysQuery
struct
{
AccountId
int64
Result
[]
*
ApiKey
OrgId
int64
Result
[]
*
ApiKey
}
type
GetApiKeyByKeyQuery
struct
{
...
...
pkg/models/dashboards.go
View file @
26e4809e
...
...
@@ -14,10 +14,10 @@ var (
)
type
Dashboard
struct
{
Id
int64
Slug
string
AccountId
int64
Version
int
Id
int64
Slug
string
OrgId
int64
Version
int
Created
time
.
Time
Updated
time
.
Time
...
...
@@ -53,7 +53,7 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
dash
:=
&
Dashboard
{}
dash
.
Data
=
cmd
.
Dashboard
dash
.
Title
=
dash
.
Data
[
"title"
]
.
(
string
)
dash
.
AccountId
=
cmd
.
Account
Id
dash
.
OrgId
=
cmd
.
Org
Id
dash
.
UpdateSlug
()
if
dash
.
Data
[
"id"
]
!=
nil
{
...
...
@@ -80,14 +80,14 @@ func (dash *Dashboard) UpdateSlug() {
type
SaveDashboardCommand
struct
{
Dashboard
map
[
string
]
interface
{}
`json:"dashboard"`
AccountId
int64
`json:"-"`
OrgId
int64
`json:"-"`
Result
*
Dashboard
}
type
DeleteDashboardCommand
struct
{
Slug
string
Account
Id
int64
Slug
string
Org
Id
int64
}
//
...
...
@@ -95,8 +95,8 @@ type DeleteDashboardCommand struct {
//
type
GetDashboardQuery
struct
{
Slug
string
Account
Id
int64
Slug
string
Org
Id
int64
Result
*
Dashboard
}
pkg/models/datasource.go
View file @
26e4809e
...
...
@@ -23,9 +23,9 @@ type DsType string
type
DsAccess
string
type
DataSource
struct
{
Id
int64
AccountId
int64
Version
int
Id
int64
OrgId
int64
Version
int
Name
string
Type
DsType
...
...
@@ -48,7 +48,7 @@ type DataSource struct {
// Also acts as api DTO
type
AddDataSourceCommand
struct
{
AccountId
int64
`json:"-"`
OrgId
int64
`json:"-"`
Name
string
Type
DsType
Access
DsAccess
...
...
@@ -64,7 +64,7 @@ type AddDataSourceCommand struct {
// Also acts as api DTO
type
UpdateDataSourceCommand
struct
{
Id
int64
AccountId
int64
OrgId
int64
Name
string
Type
DsType
Access
DsAccess
...
...
@@ -76,28 +76,28 @@ type UpdateDataSourceCommand struct {
}
type
DeleteDataSourceCommand
struct
{
Id
int64
Account
Id
int64
Id
int64
Org
Id
int64
}
// ---------------------
// QUERIES
type
GetDataSourcesQuery
struct
{
AccountId
int64
Result
[]
*
DataSource
OrgId
int64
Result
[]
*
DataSource
}
type
GetDataSourceByIdQuery
struct
{
Id
int64
AccountId
int64
Result
DataSource
Id
int64
OrgId
int64
Result
DataSource
}
type
GetDataSourceByNameQuery
struct
{
Name
string
AccountId
int64
Result
DataSource
Name
string
OrgId
int64
Result
DataSource
}
// ---------------------
...
...
pkg/models/
account
.go
→
pkg/models/
org
.go
View file @
26e4809e
...
...
@@ -7,10 +7,10 @@ import (
// Typed errors
var
(
Err
AccountNotFound
=
errors
.
New
(
"Account
not found"
)
Err
OrgNotFound
=
errors
.
New
(
"Organization
not found"
)
)
type
Account
struct
{
type
Org
struct
{
Id
int64
Version
int
Name
string
...
...
@@ -21,50 +21,45 @@ type Account struct {
// ---------------------
// COMMANDS
type
Create
Account
Command
struct
{
type
Create
Org
Command
struct
{
Name
string
`json:"name" binding:"Required"`
// initial admin user for account
UserId
int64
`json:"-"`
Result
Account
`json:"-"`
UserId
int64
`json:"-"`
Result
Org
`json:"-"`
}
type
Delete
Account
Command
struct
{
type
Delete
Org
Command
struct
{
Id
int64
}
type
Update
Account
Command
struct
{
Name
string
`json:"name" binding:"Required"`
Account
Id
int64
`json:"-"`
type
Update
Org
Command
struct
{
Name
string
`json:"name" binding:"Required"`
Org
Id
int64
`json:"-"`
}
type
GetUserAccountsQuery
struct
{
UserId
int64
Result
[]
*
UserAccountDTO
}
type
GetAccountByIdQuery
struct
{
type
GetOrgByIdQuery
struct
{
Id
int64
Result
*
Account
Result
*
Org
}
type
Get
Account
ByNameQuery
struct
{
type
Get
Org
ByNameQuery
struct
{
Name
string
Result
*
Account
Result
*
Org
}
type
Get
Accounts
Query
struct
{
Result
[]
*
Account
type
Get
OrgList
Query
struct
{
Result
[]
*
Org
}
type
Account
DTO
struct
{
type
Org
DTO
struct
{
Id
int64
`json:"id"`
Name
string
`json:"name"`
}
type
User
Account
DTO
struct
{
AccountId
int64
`json:"account
Id"`
Name
string
`json:"name"`
Role
RoleType
`json:"role"`
IsUsing
bool
`json:"isUsing"`
type
User
Org
DTO
struct
{
OrgId
int64
`json:"org
Id"`
Name
string
`json:"name"`
Role
RoleType
`json:"role"`
IsUsing
bool
`json:"isUsing"`
}
pkg/models/
account
_user.go
→
pkg/models/
org
_user.go
View file @
26e4809e
...
...
@@ -7,8 +7,8 @@ import (
// Typed errors
var
(
ErrInvalidRoleType
=
errors
.
New
(
"Invalid role type"
)
ErrLast
AccountAdmin
=
errors
.
New
(
"Cannot remove last account
admin"
)
ErrInvalidRoleType
=
errors
.
New
(
"Invalid role type"
)
ErrLast
OrgAdmin
=
errors
.
New
(
"Cannot remove last organization
admin"
)
)
type
RoleType
string
...
...
@@ -23,45 +23,45 @@ func (r RoleType) IsValid() bool {
return
r
==
ROLE_VIEWER
||
r
==
ROLE_ADMIN
||
r
==
ROLE_EDITOR
}
type
Account
User
struct
{
AccountId
int64
UserId
int64
Role
RoleType
Created
time
.
Time
Updated
time
.
Time
type
Org
User
struct
{
OrgId
int64
UserId
int64
Role
RoleType
Created
time
.
Time
Updated
time
.
Time
}
// ---------------------
// COMMANDS
type
Remove
Account
UserCommand
struct
{
UserId
int64
AccountId
int64
type
Remove
Org
UserCommand
struct
{
UserId
int64
OrgId
int64
}
type
Add
Account
UserCommand
struct
{
type
Add
Org
UserCommand
struct
{
LoginOrEmail
string
`json:"loginOrEmail" binding:"Required"`
Role
RoleType
`json:"role" binding:"Required"`
AccountId
int64
`json:"-"`
UserId
int64
`json:"-"`
OrgId
int64
`json:"-"`
UserId
int64
`json:"-"`
}
// ----------------------
// QUERIES
type
Get
Account
UsersQuery
struct
{
AccountId
int64
Result
[]
*
Account
UserDTO
type
Get
Org
UsersQuery
struct
{
OrgId
int64
Result
[]
*
Org
UserDTO
}
// ----------------------
// Projections and DTOs
type
Account
UserDTO
struct
{
AccountId
int64
`json:"account
Id"`
UserId
int64
`json:"userId"`
Email
string
`json:"email"`
Login
string
`json:"login"`
Role
string
`json:"role"`
type
Org
UserDTO
struct
{
OrgId
int64
`json:"org
Id"`
UserId
int64
`json:"userId"`
Email
string
`json:"email"`
Login
string
`json:"login"`
Role
string
`json:"role"`
}
pkg/models/search.go
View file @
26e4809e
...
...
@@ -22,7 +22,7 @@ type DashboardTagCloudItem struct {
type
SearchDashboardsQuery
struct
{
Title
string
Tag
string
AccountId
int64
OrgId
int64
UserId
int64
Limit
int
IsStarred
bool
...
...
@@ -31,6 +31,6 @@ type SearchDashboardsQuery struct {
}
type
GetDashboardTagsQuery
struct
{
AccountId
int64
Result
[]
*
DashboardTagCloudItem
OrgId
int64
Result
[]
*
DashboardTagCloudItem
}
pkg/models/user.go
View file @
26e4809e
...
...
@@ -23,8 +23,8 @@ type User struct {
EmailVerified
bool
Theme
string
IsAdmin
bool
AccountId
int64
IsAdmin
bool
OrgId
int64
Created
time
.
Time
Updated
time
.
Time
...
...
@@ -63,9 +63,9 @@ type DeleteUserCommand struct {
UserId
int64
}
type
SetUsing
Account
Command
struct
{
UserId
int64
AccountId
int64
type
SetUsing
Org
Command
struct
{
UserId
int64
OrgId
int64
}
// ----------------------
...
...
@@ -99,14 +99,19 @@ type SearchUsersQuery struct {
Result
[]
*
UserSearchHitDTO
}
type
GetUserOrgListQuery
struct
{
UserId
int64
Result
[]
*
UserOrgDTO
}
// ------------------------
// DTO & Projections
type
SignedInUser
struct
{
UserId
int64
AccountId
int64
AccountName
string
AccountRole
RoleType
OrgId
int64
OrgName
string
OrgRole
RoleType
Login
string
Name
string
Email
string
...
...
pkg/services/sqlstore/apikey.go
View file @
26e4809e
...
...
@@ -17,7 +17,7 @@ func init() {
}
func
GetApiKeys
(
query
*
m
.
GetApiKeysQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
account_id=?"
,
query
.
Account
Id
)
.
Asc
(
"name"
)
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
org_id=?"
,
query
.
Org
Id
)
.
Asc
(
"name"
)
query
.
Result
=
make
([]
*
m
.
ApiKey
,
0
)
return
sess
.
Find
(
&
query
.
Result
)
...
...
@@ -25,8 +25,8 @@ func GetApiKeys(query *m.GetApiKeysQuery) error {
func
DeleteApiKey
(
cmd
*
m
.
DeleteApiKeyCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
rawSql
=
"DELETE FROM api_key WHERE id=? and
account
_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Id
,
cmd
.
Account
Id
)
var
rawSql
=
"DELETE FROM api_key WHERE id=? and
org
_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Id
,
cmd
.
Org
Id
)
return
err
})
}
...
...
@@ -34,12 +34,12 @@ func DeleteApiKey(cmd *m.DeleteApiKeyCommand) error {
func
AddApiKey
(
cmd
*
m
.
AddApiKeyCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
t
:=
m
.
ApiKey
{
AccountId
:
cmd
.
Account
Id
,
Name
:
cmd
.
Name
,
Role
:
cmd
.
Role
,
Key
:
cmd
.
Key
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
OrgId
:
cmd
.
Org
Id
,
Name
:
cmd
.
Name
,
Role
:
cmd
.
Role
,
Key
:
cmd
.
Key
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
if
_
,
err
:=
sess
.
Insert
(
&
t
);
err
!=
nil
{
...
...
@@ -53,13 +53,13 @@ func AddApiKey(cmd *m.AddApiKeyCommand) error {
func
UpdateApiKey
(
cmd
*
m
.
UpdateApiKeyCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
t
:=
m
.
ApiKey
{
Id
:
cmd
.
Id
,
AccountId
:
cmd
.
Account
Id
,
Name
:
cmd
.
Name
,
Role
:
cmd
.
Role
,
Updated
:
time
.
Now
(),
Id
:
cmd
.
Id
,
OrgId
:
cmd
.
Org
Id
,
Name
:
cmd
.
Name
,
Role
:
cmd
.
Role
,
Updated
:
time
.
Now
(),
}
_
,
err
:=
sess
.
Where
(
"id=? and
account_id=?"
,
t
.
Id
,
t
.
Account
Id
)
.
Update
(
&
t
)
_
,
err
:=
sess
.
Where
(
"id=? and
org_id=?"
,
t
.
Id
,
t
.
Org
Id
)
.
Update
(
&
t
)
return
err
})
}
...
...
pkg/services/sqlstore/apikey_test.go
View file @
26e4809e
...
...
@@ -14,7 +14,7 @@ func TestApiKeyDataAccess(t *testing.T) {
InitTestDB
(
t
)
Convey
(
"Given saved api key"
,
func
()
{
cmd
:=
m
.
AddApiKeyCommand
{
Account
Id
:
1
,
Key
:
"hello"
}
cmd
:=
m
.
AddApiKeyCommand
{
Org
Id
:
1
,
Key
:
"hello"
}
err
:=
AddApiKey
(
&
cmd
)
So
(
err
,
ShouldBeNil
)
...
...
pkg/services/sqlstore/dashboard.go
View file @
26e4809e
...
...
@@ -22,7 +22,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
dash
:=
cmd
.
GetDashboardModel
()
// try get existing dashboard
existing
:=
m
.
Dashboard
{
Slug
:
dash
.
Slug
,
AccountId
:
dash
.
Account
Id
}
existing
:=
m
.
Dashboard
{
Slug
:
dash
.
Slug
,
OrgId
:
dash
.
Org
Id
}
hasExisting
,
err
:=
sess
.
Get
(
&
existing
)
if
err
!=
nil
{
return
err
...
...
@@ -61,7 +61,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
}
func
GetDashboard
(
query
*
m
.
GetDashboardQuery
)
error
{
dashboard
:=
m
.
Dashboard
{
Slug
:
query
.
Slug
,
AccountId
:
query
.
Account
Id
}
dashboard
:=
m
.
Dashboard
{
Slug
:
query
.
Slug
,
OrgId
:
query
.
Org
Id
}
has
,
err
:=
x
.
Get
(
&
dashboard
)
if
err
!=
nil
{
return
err
...
...
@@ -98,9 +98,9 @@ func SearchDashboards(query *m.SearchDashboardsQuery) error {
sql
.
WriteString
(
" INNER JOIN star on star.dashboard_id = dashboard.id"
)
}
sql
.
WriteString
(
` WHERE dashboard.
account
_id=?`
)
sql
.
WriteString
(
` WHERE dashboard.
org
_id=?`
)
params
=
append
(
params
,
query
.
Account
Id
)
params
=
append
(
params
,
query
.
Org
Id
)
if
query
.
IsStarred
{
sql
.
WriteString
(
` AND star.user_id=?`
)
...
...
@@ -158,11 +158,11 @@ func GetDashboardTags(query *m.GetDashboardTagsQuery) error {
term
FROM dashboard
INNER JOIN dashboard_tag on dashboard_tag.dashboard_id = dashboard.id
WHERE dashboard.
account
_id=?
WHERE dashboard.
org
_id=?
GROUP BY term`
query
.
Result
=
make
([]
*
m
.
DashboardTagCloudItem
,
0
)
sess
:=
x
.
Sql
(
sql
,
query
.
Account
Id
)
sess
:=
x
.
Sql
(
sql
,
query
.
Org
Id
)
err
:=
sess
.
Find
(
&
query
.
Result
)
return
err
}
...
...
@@ -171,8 +171,8 @@ func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
sess
:=
x
.
NewSession
()
defer
sess
.
Close
()
rawSql
:=
"DELETE FROM Dashboard WHERE
account
_id=? and slug=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Account
Id
,
cmd
.
Slug
)
rawSql
:=
"DELETE FROM Dashboard WHERE
org
_id=? and slug=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Org
Id
,
cmd
.
Slug
)
return
err
}
pkg/services/sqlstore/dashboard_test.go
View file @
26e4809e
...
...
@@ -8,9 +8,9 @@ import (
m
"github.com/grafana/grafana/pkg/models"
)
func
insertTestDashboard
(
title
string
,
account
Id
int64
,
tags
...
interface
{})
*
m
.
Dashboard
{
func
insertTestDashboard
(
title
string
,
org
Id
int64
,
tags
...
interface
{})
*
m
.
Dashboard
{
cmd
:=
m
.
SaveDashboardCommand
{
AccountId
:
account
Id
,
OrgId
:
org
Id
,
Dashboard
:
map
[
string
]
interface
{}{
"id"
:
nil
,
"title"
:
title
,
...
...
@@ -40,8 +40,8 @@ func TestDashboardDataAccess(t *testing.T) {
Convey
(
"Should be able to get dashboard"
,
func
()
{
query
:=
m
.
GetDashboardQuery
{
Slug
:
"test-dash-23"
,
Account
Id
:
1
,
Slug
:
"test-dash-23"
,
Org
Id
:
1
,
}
err
:=
GetDashboard
(
&
query
)
...
...
@@ -53,8 +53,8 @@ func TestDashboardDataAccess(t *testing.T) {
Convey
(
"Should be able to search for dashboard"
,
func
()
{
query
:=
m
.
SearchDashboardsQuery
{
Title
:
"test"
,
Account
Id
:
1
,
Title
:
"test"
,
Org
Id
:
1
,
}
err
:=
SearchDashboards
(
&
query
)
...
...
@@ -66,8 +66,8 @@ func TestDashboardDataAccess(t *testing.T) {
})
Convey
(
"Should be able to search for dashboards using tags"
,
func
()
{
query1
:=
m
.
SearchDashboardsQuery
{
Tag
:
"webapp"
,
Account
Id
:
1
}
query2
:=
m
.
SearchDashboardsQuery
{
Tag
:
"tagdoesnotexist"
,
Account
Id
:
1
}
query1
:=
m
.
SearchDashboardsQuery
{
Tag
:
"webapp"
,
Org
Id
:
1
}
query2
:=
m
.
SearchDashboardsQuery
{
Tag
:
"tagdoesnotexist"
,
Org
Id
:
1
}
err
:=
SearchDashboards
(
&
query1
)
err
=
SearchDashboards
(
&
query2
)
...
...
@@ -79,7 +79,7 @@ func TestDashboardDataAccess(t *testing.T) {
Convey
(
"Should not be able to save dashboard with same name"
,
func
()
{
cmd
:=
m
.
SaveDashboardCommand
{
Account
Id
:
1
,
Org
Id
:
1
,
Dashboard
:
map
[
string
]
interface
{}{
"id"
:
nil
,
"title"
:
"test dash 23"
,
...
...
@@ -92,7 +92,7 @@ func TestDashboardDataAccess(t *testing.T) {
})
Convey
(
"Should be able to get dashboard tags"
,
func
()
{
query
:=
m
.
GetDashboardTagsQuery
{
Account
Id
:
1
}
query
:=
m
.
GetDashboardTagsQuery
{
Org
Id
:
1
}
err
:=
GetDashboardTags
(
&
query
)
So
(
err
,
ShouldBeNil
)
...
...
@@ -113,7 +113,7 @@ func TestDashboardDataAccess(t *testing.T) {
})
Convey
(
"Should be able to search for starred dashboards"
,
func
()
{
query
:=
m
.
SearchDashboardsQuery
{
Account
Id
:
1
,
UserId
:
10
,
IsStarred
:
true
}
query
:=
m
.
SearchDashboardsQuery
{
Org
Id
:
1
,
UserId
:
10
,
IsStarred
:
true
}
err
:=
SearchDashboards
(
&
query
)
So
(
err
,
ShouldBeNil
)
...
...
pkg/services/sqlstore/datasource.go
View file @
26e4809e
...
...
@@ -19,7 +19,7 @@ func init() {
}
func
GetDataSourceById
(
query
*
m
.
GetDataSourceByIdQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
account_id=? AND id=?"
,
query
.
Account
Id
,
query
.
Id
)
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
org_id=? AND id=?"
,
query
.
Org
Id
,
query
.
Id
)
has
,
err
:=
sess
.
Get
(
&
query
.
Result
)
if
!
has
{
...
...
@@ -29,7 +29,7 @@ func GetDataSourceById(query *m.GetDataSourceByIdQuery) error {
}
func
GetDataSourceByName
(
query
*
m
.
GetDataSourceByNameQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
account_id=? AND name=?"
,
query
.
Account
Id
,
query
.
Name
)
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
org_id=? AND name=?"
,
query
.
Org
Id
,
query
.
Name
)
has
,
err
:=
sess
.
Get
(
&
query
.
Result
)
if
!
has
{
...
...
@@ -39,7 +39,7 @@ func GetDataSourceByName(query *m.GetDataSourceByNameQuery) error {
}
func
GetDataSources
(
query
*
m
.
GetDataSourcesQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
account_id=?"
,
query
.
Account
Id
)
.
Asc
(
"name"
)
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"
org_id=?"
,
query
.
Org
Id
)
.
Asc
(
"name"
)
query
.
Result
=
make
([]
*
m
.
DataSource
,
0
)
return
sess
.
Find
(
&
query
.
Result
)
...
...
@@ -47,8 +47,8 @@ func GetDataSources(query *m.GetDataSourcesQuery) error {
func
DeleteDataSource
(
cmd
*
m
.
DeleteDataSourceCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
rawSql
=
"DELETE FROM data_source WHERE id=? and
account
_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Id
,
cmd
.
Account
Id
)
var
rawSql
=
"DELETE FROM data_source WHERE id=? and
org
_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Id
,
cmd
.
Org
Id
)
return
err
})
}
...
...
@@ -57,7 +57,7 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error {
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
ds
:=
&
m
.
DataSource
{
AccountId
:
cmd
.
Account
Id
,
OrgId
:
cmd
.
Org
Id
,
Name
:
cmd
.
Name
,
Type
:
cmd
.
Type
,
Access
:
cmd
.
Access
,
...
...
@@ -85,8 +85,8 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error {
func
updateIsDefaultFlag
(
ds
*
m
.
DataSource
,
sess
*
xorm
.
Session
)
error
{
// Handle is default flag
if
ds
.
IsDefault
{
rawSql
:=
"UPDATE data_source SET is_default = 0 WHERE
account
_id=? AND id <> ?"
if
_
,
err
:=
sess
.
Exec
(
rawSql
,
ds
.
Account
Id
,
ds
.
Id
);
err
!=
nil
{
rawSql
:=
"UPDATE data_source SET is_default = 0 WHERE
org
_id=? AND id <> ?"
if
_
,
err
:=
sess
.
Exec
(
rawSql
,
ds
.
Org
Id
,
ds
.
Id
);
err
!=
nil
{
return
err
}
}
...
...
@@ -98,7 +98,7 @@ func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
ds
:=
&
m
.
DataSource
{
Id
:
cmd
.
Id
,
AccountId
:
cmd
.
Account
Id
,
OrgId
:
cmd
.
Org
Id
,
Name
:
cmd
.
Name
,
Type
:
cmd
.
Type
,
Access
:
cmd
.
Access
,
...
...
@@ -112,7 +112,7 @@ func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
sess
.
UseBool
(
"is_default"
)
_
,
err
:=
sess
.
Where
(
"id=? and
account_id=?"
,
ds
.
Id
,
ds
.
Account
Id
)
.
Update
(
ds
)
_
,
err
:=
sess
.
Where
(
"id=? and
org_id=?"
,
ds
.
Id
,
ds
.
Org
Id
)
.
Update
(
ds
)
if
err
!=
nil
{
return
err
}
...
...
pkg/services/sqlstore/datasource_test.go
View file @
26e4809e
...
...
@@ -42,16 +42,16 @@ func TestDataAccess(t *testing.T) {
Convey
(
"Can add datasource"
,
func
()
{
err
:=
AddDataSource
(
&
m
.
AddDataSourceCommand
{
AccountId
:
10
,
Type
:
m
.
DS_INFLUXDB
,
Access
:
m
.
DS_ACCESS_DIRECT
,
Url
:
"http://test"
,
Database
:
"site"
,
OrgId
:
10
,
Type
:
m
.
DS_INFLUXDB
,
Access
:
m
.
DS_ACCESS_DIRECT
,
Url
:
"http://test"
,
Database
:
"site"
,
})
So
(
err
,
ShouldBeNil
)
query
:=
m
.
GetDataSourcesQuery
{
Account
Id
:
10
}
query
:=
m
.
GetDataSourcesQuery
{
Org
Id
:
10
}
err
=
GetDataSources
(
&
query
)
So
(
err
,
ShouldBeNil
)
...
...
@@ -59,33 +59,33 @@ func TestDataAccess(t *testing.T) {
ds
:=
query
.
Result
[
0
]
So
(
ds
.
Account
Id
,
ShouldEqual
,
10
)
So
(
ds
.
Org
Id
,
ShouldEqual
,
10
)
So
(
ds
.
Database
,
ShouldEqual
,
"site"
)
})
Convey
(
"Given a datasource"
,
func
()
{
AddDataSource
(
&
m
.
AddDataSourceCommand
{
AccountId
:
10
,
Type
:
m
.
DS_GRAPHITE
,
Access
:
m
.
DS_ACCESS_DIRECT
,
Url
:
"http://test"
,
OrgId
:
10
,
Type
:
m
.
DS_GRAPHITE
,
Access
:
m
.
DS_ACCESS_DIRECT
,
Url
:
"http://test"
,
})
query
:=
m
.
GetDataSourcesQuery
{
Account
Id
:
10
}
query
:=
m
.
GetDataSourcesQuery
{
Org
Id
:
10
}
GetDataSources
(
&
query
)
ds
:=
query
.
Result
[
0
]
Convey
(
"Can delete datasource"
,
func
()
{
err
:=
DeleteDataSource
(
&
m
.
DeleteDataSourceCommand
{
Id
:
ds
.
Id
,
AccountId
:
ds
.
Account
Id
})
err
:=
DeleteDataSource
(
&
m
.
DeleteDataSourceCommand
{
Id
:
ds
.
Id
,
OrgId
:
ds
.
Org
Id
})
So
(
err
,
ShouldBeNil
)
GetDataSources
(
&
query
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
0
)
})
Convey
(
"Can not delete datasource with wrong
account
Id"
,
func
()
{
err
:=
DeleteDataSource
(
&
m
.
DeleteDataSourceCommand
{
Id
:
ds
.
Id
,
Account
Id
:
123123
})
Convey
(
"Can not delete datasource with wrong
org
Id"
,
func
()
{
err
:=
DeleteDataSource
(
&
m
.
DeleteDataSourceCommand
{
Id
:
ds
.
Id
,
Org
Id
:
123123
})
So
(
err
,
ShouldBeNil
)
GetDataSources
(
&
query
)
...
...
pkg/services/sqlstore/
account
.go
→
pkg/services/sqlstore/
org
.go
View file @
26e4809e
...
...
@@ -10,116 +10,115 @@ import (
)
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetAccountById
)
bus
.
AddHandler
(
"sql"
,
CreateAccount
)
bus
.
AddHandler
(
"sql"
,
SetUsingAccount
)
bus
.
AddHandler
(
"sql"
,
UpdateAccount
)
bus
.
AddHandler
(
"sql"
,
GetAccountByName
)
bus
.
AddHandler
(
"sql"
,
GetAccountsQuery
)
bus
.
AddHandler
(
"sql"
,
DeleteAccount
)
bus
.
AddHandler
(
"sql"
,
GetOrgById
)
bus
.
AddHandler
(
"sql"
,
CreateOrg
)
bus
.
AddHandler
(
"sql"
,
UpdateOrg
)
bus
.
AddHandler
(
"sql"
,
GetOrgByName
)
bus
.
AddHandler
(
"sql"
,
GetOrgList
)
bus
.
AddHandler
(
"sql"
,
DeleteOrg
)
}
func
Get
AccountsQuery
(
query
*
m
.
GetAccounts
Query
)
error
{
func
Get
OrgList
(
query
*
m
.
GetOrgList
Query
)
error
{
return
x
.
Find
(
&
query
.
Result
)
}
func
Get
AccountById
(
query
*
m
.
GetAccount
ByIdQuery
)
error
{
var
account
m
.
Account
exists
,
err
:=
x
.
Id
(
query
.
Id
)
.
Get
(
&
account
)
func
Get
OrgById
(
query
*
m
.
GetOrg
ByIdQuery
)
error
{
var
org
m
.
Org
exists
,
err
:=
x
.
Id
(
query
.
Id
)
.
Get
(
&
org
)
if
err
!=
nil
{
return
err
}
if
!
exists
{
return
m
.
Err
Account
NotFound
return
m
.
Err
Org
NotFound
}
query
.
Result
=
&
account
query
.
Result
=
&
org
return
nil
}
func
Get
AccountByName
(
query
*
m
.
GetAccount
ByNameQuery
)
error
{
var
account
m
.
Account
exists
,
err
:=
x
.
Where
(
"name=?"
,
query
.
Name
)
.
Get
(
&
account
)
func
Get
OrgByName
(
query
*
m
.
GetOrg
ByNameQuery
)
error
{
var
org
m
.
Org
exists
,
err
:=
x
.
Where
(
"name=?"
,
query
.
Name
)
.
Get
(
&
org
)
if
err
!=
nil
{
return
err
}
if
!
exists
{
return
m
.
Err
Account
NotFound
return
m
.
Err
Org
NotFound
}
query
.
Result
=
&
account
query
.
Result
=
&
org
return
nil
}
func
Create
Account
(
cmd
*
m
.
CreateAccount
Command
)
error
{
func
Create
Org
(
cmd
*
m
.
CreateOrg
Command
)
error
{
return
inTransaction2
(
func
(
sess
*
session
)
error
{
account
:=
m
.
Account
{
org
:=
m
.
Org
{
Name
:
cmd
.
Name
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
if
_
,
err
:=
sess
.
Insert
(
&
account
);
err
!=
nil
{
if
_
,
err
:=
sess
.
Insert
(
&
org
);
err
!=
nil
{
return
err
}
user
:=
m
.
Account
User
{
AccountId
:
account
.
Id
,
UserId
:
cmd
.
UserId
,
Role
:
m
.
ROLE_ADMIN
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
user
:=
m
.
Org
User
{
OrgId
:
org
.
Id
,
UserId
:
cmd
.
UserId
,
Role
:
m
.
ROLE_ADMIN
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
_
,
err
:=
sess
.
Insert
(
&
user
)
cmd
.
Result
=
account
cmd
.
Result
=
org
sess
.
publishAfterCommit
(
&
events
.
Account
Created
{
Timestamp
:
account
.
Created
,
Id
:
account
.
Id
,
Name
:
account
.
Name
,
sess
.
publishAfterCommit
(
&
events
.
Org
Created
{
Timestamp
:
org
.
Created
,
Id
:
org
.
Id
,
Name
:
org
.
Name
,
})
return
err
})
}
func
Update
Account
(
cmd
*
m
.
UpdateAccount
Command
)
error
{
func
Update
Org
(
cmd
*
m
.
UpdateOrg
Command
)
error
{
return
inTransaction2
(
func
(
sess
*
session
)
error
{
account
:=
m
.
Account
{
org
:=
m
.
Org
{
Name
:
cmd
.
Name
,
Updated
:
time
.
Now
(),
}
if
_
,
err
:=
sess
.
Id
(
cmd
.
AccountId
)
.
Update
(
&
account
);
err
!=
nil
{
if
_
,
err
:=
sess
.
Id
(
cmd
.
OrgId
)
.
Update
(
&
org
);
err
!=
nil
{
return
err
}
sess
.
publishAfterCommit
(
&
events
.
Account
Updated
{
Timestamp
:
account
.
Updated
,
Id
:
account
.
Id
,
Name
:
account
.
Name
,
sess
.
publishAfterCommit
(
&
events
.
Org
Updated
{
Timestamp
:
org
.
Updated
,
Id
:
org
.
Id
,
Name
:
org
.
Name
,
})
return
nil
})
}
func
Delete
Account
(
cmd
*
m
.
DeleteAccount
Command
)
error
{
func
Delete
Org
(
cmd
*
m
.
DeleteOrg
Command
)
error
{
return
inTransaction2
(
func
(
sess
*
session
)
error
{
deletes
:=
[]
string
{
"DELETE FROM star WHERE EXISTS (SELECT 1 FROM dashboard WHERE
account
_id = ?)"
,
"DELETE FROM dashboard_tag WHERE EXISTS (SELECT 1 FROM dashboard WHERE
account
_id = ?)"
,
"DELETE FROM dashboard WHERE
account
_id = ?"
,
"DELETE FROM api_key WHERE
account
_id = ?"
,
"DELETE FROM data_source WHERE
account
_id = ?"
,
"DELETE FROM
account_user WHERE account
_id = ?"
,
"DELETE FROM
account
WHERE id = ?"
,
"DELETE FROM star WHERE EXISTS (SELECT 1 FROM dashboard WHERE
org
_id = ?)"
,
"DELETE FROM dashboard_tag WHERE EXISTS (SELECT 1 FROM dashboard WHERE
org
_id = ?)"
,
"DELETE FROM dashboard WHERE
org
_id = ?"
,
"DELETE FROM api_key WHERE
org
_id = ?"
,
"DELETE FROM data_source WHERE
org
_id = ?"
,
"DELETE FROM
org_user WHERE org
_id = ?"
,
"DELETE FROM
org
WHERE id = ?"
,
}
for
_
,
sql
:=
range
deletes
{
...
...
pkg/services/sqlstore/
account
_test.go
→
pkg/services/sqlstore/
org
_test.go
View file @
26e4809e
...
...
@@ -15,11 +15,11 @@ func TestAccountDataAccess(t *testing.T) {
InitTestDB
(
t
)
Convey
(
"Given single account mode"
,
func
()
{
setting
.
Single
Account
Mode
=
true
setting
.
Default
Account
Name
=
"test"
setting
.
Default
Account
Role
=
"Viewer"
setting
.
Single
Org
Mode
=
true
setting
.
Default
Org
Name
=
"test"
setting
.
Default
Org
Role
=
"Viewer"
Convey
(
"Users should be added to default
account
"
,
func
()
{
Convey
(
"Users should be added to default
organization
"
,
func
()
{
ac1cmd
:=
m
.
CreateUserCommand
{
Login
:
"ac1"
,
Email
:
"ac1@test.com"
,
Name
:
"ac1 name"
}
ac2cmd
:=
m
.
CreateUserCommand
{
Login
:
"ac2"
,
Email
:
"ac2@test.com"
,
Name
:
"ac2 name"
}
...
...
@@ -33,15 +33,15 @@ func TestAccountDataAccess(t *testing.T) {
GetUserAccounts
(
&
q1
)
GetUserAccounts
(
&
q2
)
So
(
q1
.
Result
[
0
]
.
AccountId
,
ShouldEqual
,
q2
.
Result
[
0
]
.
Account
Id
)
So
(
q1
.
Result
[
0
]
.
OrgId
,
ShouldEqual
,
q2
.
Result
[
0
]
.
Org
Id
)
So
(
q1
.
Result
[
0
]
.
Role
,
ShouldEqual
,
"Viewer"
)
})
})
Convey
(
"Given two saved users"
,
func
()
{
setting
.
SingleAccountMode
=
false
setting
.
SingleOrgMode
=
false
setting
.
DefaultOrgName
=
"test"
setting
.
DefaultAccountName
=
"test"
ac1cmd
:=
m
.
CreateUserCommand
{
Login
:
"ac1"
,
Email
:
"ac1@test.com"
,
Name
:
"ac1 name"
}
ac2cmd
:=
m
.
CreateUserCommand
{
Login
:
"ac2"
,
Email
:
"ac2@test.com"
,
Name
:
"ac2 name"
,
IsAdmin
:
true
}
...
...
pkg/services/sqlstore/
account
_users.go
→
pkg/services/sqlstore/
org
_users.go
View file @
26e4809e
...
...
@@ -11,20 +11,20 @@ import (
)
func
init
()
{
bus
.
AddHandler
(
"sql"
,
Add
Account
User
)
bus
.
AddHandler
(
"sql"
,
Remove
Account
User
)
bus
.
AddHandler
(
"sql"
,
Get
Account
Users
)
bus
.
AddHandler
(
"sql"
,
Add
Org
User
)
bus
.
AddHandler
(
"sql"
,
Remove
Org
User
)
bus
.
AddHandler
(
"sql"
,
Get
Org
Users
)
}
func
Add
AccountUser
(
cmd
*
m
.
AddAccount
UserCommand
)
error
{
func
Add
OrgUser
(
cmd
*
m
.
AddOrg
UserCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
entity
:=
m
.
Account
User
{
AccountId
:
cmd
.
Account
Id
,
UserId
:
cmd
.
UserId
,
Role
:
cmd
.
Role
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
entity
:=
m
.
Org
User
{
OrgId
:
cmd
.
Org
Id
,
UserId
:
cmd
.
UserId
,
Role
:
cmd
.
Role
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
_
,
err
:=
sess
.
Insert
(
&
entity
)
...
...
@@ -32,34 +32,34 @@ func AddAccountUser(cmd *m.AddAccountUserCommand) error {
})
}
func
Get
AccountUsers
(
query
*
m
.
GetAccount
UsersQuery
)
error
{
query
.
Result
=
make
([]
*
m
.
Account
UserDTO
,
0
)
sess
:=
x
.
Table
(
"
account
_user"
)
func
Get
OrgUsers
(
query
*
m
.
GetOrg
UsersQuery
)
error
{
query
.
Result
=
make
([]
*
m
.
Org
UserDTO
,
0
)
sess
:=
x
.
Table
(
"
org
_user"
)
sess
.
Join
(
"INNER"
,
"user"
,
fmt
.
Sprintf
(
"account_user.user_id=%s.id"
,
x
.
Dialect
()
.
Quote
(
"user"
)))
sess
.
Where
(
"
account_user.account_id=?"
,
query
.
Account
Id
)
sess
.
Cols
(
"
account_user.account_id"
,
"account_user.user_id"
,
"user.email"
,
"user.login"
,
"account
_user.role"
)
sess
.
Where
(
"
org_user.org_id=?"
,
query
.
Org
Id
)
sess
.
Cols
(
"
org_user.org_id"
,
"org_user.user_id"
,
"user.email"
,
"user.login"
,
"org
_user.role"
)
sess
.
Asc
(
"user.email"
,
"user.login"
)
err
:=
sess
.
Find
(
&
query
.
Result
)
return
err
}
func
Remove
AccountUser
(
cmd
*
m
.
RemoveAccount
UserCommand
)
error
{
func
Remove
OrgUser
(
cmd
*
m
.
RemoveOrg
UserCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
rawSql
=
"DELETE FROM
account_user WHERE account
_id=? and user_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Account
Id
,
cmd
.
UserId
)
var
rawSql
=
"DELETE FROM
org_user WHERE org
_id=? and user_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Org
Id
,
cmd
.
UserId
)
if
err
!=
nil
{
return
err
}
// validate that there is an admin user left
res
,
err
:=
sess
.
Query
(
"SELECT 1 from
account_user WHERE account_id=? and role='Admin'"
,
cmd
.
Account
Id
)
res
,
err
:=
sess
.
Query
(
"SELECT 1 from
org_user WHERE org_id=? and role='Admin'"
,
cmd
.
Org
Id
)
if
err
!=
nil
{
return
err
}
if
len
(
res
)
==
0
{
return
m
.
ErrLast
Account
Admin
return
m
.
ErrLast
Org
Admin
}
return
err
...
...
pkg/services/sqlstore/user.go
View file @
26e4809e
...
...
@@ -20,58 +20,59 @@ func init() {
bus
.
AddHandler
(
"sql"
,
UpdateUser
)
bus
.
AddHandler
(
"sql"
,
ChangeUserPassword
)
bus
.
AddHandler
(
"sql"
,
GetUserByLogin
)
bus
.
AddHandler
(
"sql"
,
SetUsing
Account
)
bus
.
AddHandler
(
"sql"
,
SetUsing
Org
)
bus
.
AddHandler
(
"sql"
,
GetUserInfo
)
bus
.
AddHandler
(
"sql"
,
GetSignedInUser
)
bus
.
AddHandler
(
"sql"
,
SearchUsers
)
bus
.
AddHandler
(
"sql"
,
GetUser
Accounts
)
bus
.
AddHandler
(
"sql"
,
GetUser
OrgList
)
bus
.
AddHandler
(
"sql"
,
DeleteUser
)
bus
.
AddHandler
(
"sql"
,
SetUsingOrg
)
}
func
get
Account
IdForNewUser
(
userEmail
string
,
sess
*
session
)
(
int64
,
error
)
{
var
account
m
.
Account
func
get
Org
IdForNewUser
(
userEmail
string
,
sess
*
session
)
(
int64
,
error
)
{
var
org
m
.
Org
if
setting
.
Single
Account
Mode
{
has
,
err
:=
sess
.
Where
(
"name=?"
,
setting
.
Default
AccountName
)
.
Get
(
&
account
)
if
setting
.
Single
Org
Mode
{
has
,
err
:=
sess
.
Where
(
"name=?"
,
setting
.
Default
OrgName
)
.
Get
(
&
org
)
if
err
!=
nil
{
return
0
,
err
}
if
has
{
return
account
.
Id
,
nil
return
org
.
Id
,
nil
}
else
{
account
.
Name
=
setting
.
DefaultAccount
Name
org
.
Name
=
setting
.
DefaultOrg
Name
}
}
else
{
account
.
Name
=
userEmail
org
.
Name
=
userEmail
}
account
.
Created
=
time
.
Now
()
account
.
Updated
=
time
.
Now
()
org
.
Created
=
time
.
Now
()
org
.
Updated
=
time
.
Now
()
if
_
,
err
:=
sess
.
Insert
(
&
account
);
err
!=
nil
{
if
_
,
err
:=
sess
.
Insert
(
&
org
);
err
!=
nil
{
return
0
,
err
}
return
account
.
Id
,
nil
return
org
.
Id
,
nil
}
func
CreateUser
(
cmd
*
m
.
CreateUserCommand
)
error
{
return
inTransaction2
(
func
(
sess
*
session
)
error
{
accountId
,
err
:=
getAccount
IdForNewUser
(
cmd
.
Email
,
sess
)
orgId
,
err
:=
getOrg
IdForNewUser
(
cmd
.
Email
,
sess
)
if
err
!=
nil
{
return
err
}
// create user
user
:=
m
.
User
{
Email
:
cmd
.
Email
,
Name
:
cmd
.
Name
,
Login
:
cmd
.
Login
,
Company
:
cmd
.
Company
,
IsAdmin
:
cmd
.
IsAdmin
,
AccountId
:
account
Id
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
Email
:
cmd
.
Email
,
Name
:
cmd
.
Name
,
Login
:
cmd
.
Login
,
Company
:
cmd
.
Company
,
IsAdmin
:
cmd
.
IsAdmin
,
OrgId
:
org
Id
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
if
len
(
cmd
.
Password
)
>
0
{
...
...
@@ -86,20 +87,20 @@ func CreateUser(cmd *m.CreateUserCommand) error {
return
err
}
// create
account
user link
accountUser
:=
m
.
Account
User
{
AccountId
:
account
Id
,
UserId
:
user
.
Id
,
Role
:
m
.
ROLE_ADMIN
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
// create
org
user link
orgUser
:=
m
.
Org
User
{
OrgId
:
org
Id
,
UserId
:
user
.
Id
,
Role
:
m
.
ROLE_ADMIN
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
if
setting
.
Single
Account
Mode
&&
!
user
.
IsAdmin
{
accountUser
.
Role
=
m
.
RoleType
(
setting
.
DefaultAccount
Role
)
if
setting
.
Single
Org
Mode
&&
!
user
.
IsAdmin
{
orgUser
.
Role
=
m
.
RoleType
(
setting
.
DefaultOrg
Role
)
}
if
_
,
err
=
sess
.
Insert
(
&
account
User
);
err
!=
nil
{
if
_
,
err
=
sess
.
Insert
(
&
org
User
);
err
!=
nil
{
return
err
}
...
...
@@ -198,12 +199,12 @@ func ChangeUserPassword(cmd *m.ChangeUserPasswordCommand) error {
})
}
func
SetUsing
Account
(
cmd
*
m
.
SetUsingAccount
Command
)
error
{
func
SetUsing
Org
(
cmd
*
m
.
SetUsingOrg
Command
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
user
:=
m
.
User
{}
sess
.
Id
(
cmd
.
UserId
)
.
Get
(
&
user
)
user
.
AccountId
=
cmd
.
Account
Id
user
.
OrgId
=
cmd
.
Org
Id
_
,
err
:=
sess
.
Id
(
user
.
Id
)
.
Update
(
&
user
)
return
err
})
...
...
@@ -228,12 +229,12 @@ func GetUserInfo(query *m.GetUserInfoQuery) error {
return
err
}
func
GetUser
Accounts
(
query
*
m
.
GetUserAccounts
Query
)
error
{
query
.
Result
=
make
([]
*
m
.
User
Account
DTO
,
0
)
sess
:=
x
.
Table
(
"
account
_user"
)
sess
.
Join
(
"INNER"
,
"
account"
,
"account_user.account_id=account
.id"
)
sess
.
Where
(
"
account
_user.user_id=?"
,
query
.
UserId
)
sess
.
Cols
(
"
account.name"
,
"account_user.role"
,
"account
_user.account_id"
)
func
GetUser
OrgList
(
query
*
m
.
GetUserOrgList
Query
)
error
{
query
.
Result
=
make
([]
*
m
.
User
Org
DTO
,
0
)
sess
:=
x
.
Table
(
"
org
_user"
)
sess
.
Join
(
"INNER"
,
"
org"
,
"org_user.org_id=org
.id"
)
sess
.
Where
(
"
org
_user.user_id=?"
,
query
.
UserId
)
sess
.
Cols
(
"
org.name"
,
"org_user.role"
,
"org
_user.account_id"
)
err
:=
sess
.
Find
(
&
query
.
Result
)
return
err
}
...
...
@@ -245,12 +246,12 @@ func GetSignedInUser(query *m.GetSignedInUserQuery) error {
u.email as email,
u.login as login,
u.name as name,
account.name as account
_name,
account_user.role as account
_role,
account.id as account
_id
org.name as org
_name,
org_user.role as org
_role,
org.id as org
_id
FROM `
+
dialect
.
Quote
(
"user"
)
+
` as u
LEFT OUTER JOIN
account_user on account_user.account_id = u.account_id and account
_user.user_id = u.id
LEFT OUTER JOIN
account on account.id = u.account
_id
LEFT OUTER JOIN
org_user on org_user.org_id = u.org_id and org
_user.user_id = u.id
LEFT OUTER JOIN
org on org.id = u.org
_id
WHERE u.id=?`
var
user
m
.
SignedInUser
...
...
pkg/setting/setting.go
View file @
26e4809e
...
...
@@ -66,18 +66,18 @@ var (
CookieRememberName
string
DisableUserSignUp
bool
// single
account
Single
Account
Mode
bool
Default
Account
Name
string
Default
Account
Role
string
// single
organization
Single
Org
Mode
bool
Default
Org
Name
string
Default
Org
Role
string
// Http auth
AdminUser
string
AdminPassword
string
AnonymousEnabled
bool
Anonymous
Account
Name
string
Anonymous
Account
Role
string
AnonymousEnabled
bool
Anonymous
Org
Name
string
Anonymous
Org
Role
string
// Session settings.
SessionOptions
session
.
Options
...
...
@@ -220,14 +220,14 @@ func NewConfigContext(config string) {
AdminPassword
=
security
.
Key
(
"admin_password"
)
.
String
()
// single account
Single
AccountMode
=
Cfg
.
Section
(
"account
.single"
)
.
Key
(
"enabled"
)
.
MustBool
(
false
)
Default
AccountName
=
Cfg
.
Section
(
"account.single"
)
.
Key
(
"account
_name"
)
.
MustString
(
"main"
)
Default
AccountRole
=
Cfg
.
Section
(
"account
.single"
)
.
Key
(
"default_role"
)
.
In
(
"Editor"
,
[]
string
{
"Editor"
,
"Admin"
,
"Viewer"
})
Single
OrgMode
=
Cfg
.
Section
(
"organization
.single"
)
.
Key
(
"enabled"
)
.
MustBool
(
false
)
Default
OrgName
=
Cfg
.
Section
(
"organization.single"
)
.
Key
(
"org
_name"
)
.
MustString
(
"main"
)
Default
OrgRole
=
Cfg
.
Section
(
"organization
.single"
)
.
Key
(
"default_role"
)
.
In
(
"Editor"
,
[]
string
{
"Editor"
,
"Admin"
,
"Viewer"
})
// anonymous access
AnonymousEnabled
=
Cfg
.
Section
(
"auth.anonymous"
)
.
Key
(
"enabled"
)
.
MustBool
(
false
)
Anonymous
AccountName
=
Cfg
.
Section
(
"auth.anonymous"
)
.
Key
(
"account
_name"
)
.
String
()
Anonymous
AccountRole
=
Cfg
.
Section
(
"auth.anonymous"
)
.
Key
(
"account
_role"
)
.
String
()
Anonymous
OrgName
=
Cfg
.
Section
(
"auth.anonymous"
)
.
Key
(
"org
_name"
)
.
String
()
Anonymous
OrgRole
=
Cfg
.
Section
(
"auth.anonymous"
)
.
Key
(
"org
_role"
)
.
String
()
// PhantomJS rendering
ImagesDir
=
"data/png"
...
...
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