Big Backend Refatoring: Renamed Account -> Org
This commit is contained in:
@@ -1,135 +0,0 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func GetAccountsQuery(query *m.GetAccountsQuery) error {
|
||||
return x.Find(&query.Result)
|
||||
}
|
||||
|
||||
func GetAccountById(query *m.GetAccountByIdQuery) error {
|
||||
var account m.Account
|
||||
exists, err := x.Id(query.Id).Get(&account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return m.ErrAccountNotFound
|
||||
}
|
||||
|
||||
query.Result = &account
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAccountByName(query *m.GetAccountByNameQuery) error {
|
||||
var account m.Account
|
||||
exists, err := x.Where("name=?", query.Name).Get(&account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return m.ErrAccountNotFound
|
||||
}
|
||||
|
||||
query.Result = &account
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
account := m.Account{
|
||||
Name: cmd.Name,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
if _, err := sess.Insert(&account); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user := m.AccountUser{
|
||||
AccountId: account.Id,
|
||||
UserId: cmd.UserId,
|
||||
Role: m.ROLE_ADMIN,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&user)
|
||||
cmd.Result = account
|
||||
|
||||
sess.publishAfterCommit(&events.AccountCreated{
|
||||
Timestamp: account.Created,
|
||||
Id: account.Id,
|
||||
Name: account.Name,
|
||||
})
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateAccount(cmd *m.UpdateAccountCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
account := m.Account{
|
||||
Name: cmd.Name,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
if _, err := sess.Id(cmd.AccountId).Update(&account); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sess.publishAfterCommit(&events.AccountUpdated{
|
||||
Timestamp: account.Updated,
|
||||
Id: account.Id,
|
||||
Name: account.Name,
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteAccount(cmd *m.DeleteAccountCommand) 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 = ?",
|
||||
}
|
||||
|
||||
for _, sql := range deletes {
|
||||
log.Trace(sql)
|
||||
_, err := sess.Exec(sql, cmd.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", AddAccountUser)
|
||||
bus.AddHandler("sql", RemoveAccountUser)
|
||||
bus.AddHandler("sql", GetAccountUsers)
|
||||
}
|
||||
|
||||
func AddAccountUser(cmd *m.AddAccountUserCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
|
||||
entity := m.AccountUser{
|
||||
AccountId: cmd.AccountId,
|
||||
UserId: cmd.UserId,
|
||||
Role: cmd.Role,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func GetAccountUsers(query *m.GetAccountUsersQuery) error {
|
||||
query.Result = make([]*m.AccountUserDTO, 0)
|
||||
sess := x.Table("account_user")
|
||||
sess.Join("INNER", "user", fmt.Sprintf("account_user.user_id=%s.id", x.Dialect().Quote("user")))
|
||||
sess.Where("account_user.account_id=?", query.AccountId)
|
||||
sess.Cols("account_user.account_id", "account_user.user_id", "user.email", "user.login", "account_user.role")
|
||||
sess.Asc("user.email", "user.login")
|
||||
|
||||
err := sess.Find(&query.Result)
|
||||
return err
|
||||
}
|
||||
|
||||
func RemoveAccountUser(cmd *m.RemoveAccountUserCommand) 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.AccountId, 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.AccountId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(res) == 0 {
|
||||
return m.ErrLastAccountAdmin
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -17,7 +17,7 @@ func init() {
|
||||
}
|
||||
|
||||
func GetApiKeys(query *m.GetApiKeysQuery) error {
|
||||
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId).Asc("name")
|
||||
sess := x.Limit(100, 0).Where("org_id=?", query.OrgId).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.AccountId)
|
||||
var rawSql = "DELETE FROM api_key WHERE id=? and org_id=?"
|
||||
_, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId)
|
||||
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.AccountId,
|
||||
Name: cmd.Name,
|
||||
Role: cmd.Role,
|
||||
Key: cmd.Key,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
OrgId: cmd.OrgId,
|
||||
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.AccountId,
|
||||
Name: cmd.Name,
|
||||
Role: cmd.Role,
|
||||
Updated: time.Now(),
|
||||
Id: cmd.Id,
|
||||
OrgId: cmd.OrgId,
|
||||
Name: cmd.Name,
|
||||
Role: cmd.Role,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
_, err := sess.Where("id=? and account_id=?", t.Id, t.AccountId).Update(&t)
|
||||
_, err := sess.Where("id=? and org_id=?", t.Id, t.OrgId).Update(&t)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func TestApiKeyDataAccess(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
|
||||
Convey("Given saved api key", func() {
|
||||
cmd := m.AddApiKeyCommand{AccountId: 1, Key: "hello"}
|
||||
cmd := m.AddApiKeyCommand{OrgId: 1, Key: "hello"}
|
||||
err := AddApiKey(&cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
|
||||
@@ -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.AccountId}
|
||||
existing := m.Dashboard{Slug: dash.Slug, OrgId: dash.OrgId}
|
||||
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.AccountId}
|
||||
dashboard := m.Dashboard{Slug: query.Slug, OrgId: query.OrgId}
|
||||
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.AccountId)
|
||||
params = append(params, query.OrgId)
|
||||
|
||||
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.AccountId)
|
||||
sess := x.Sql(sql, query.OrgId)
|
||||
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.AccountId, cmd.Slug)
|
||||
rawSql := "DELETE FROM Dashboard WHERE org_id=? and slug=?"
|
||||
_, err := sess.Exec(rawSql, cmd.OrgId, cmd.Slug)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func insertTestDashboard(title string, accountId int64, tags ...interface{}) *m.Dashboard {
|
||||
func insertTestDashboard(title string, orgId int64, tags ...interface{}) *m.Dashboard {
|
||||
cmd := m.SaveDashboardCommand{
|
||||
AccountId: accountId,
|
||||
OrgId: orgId,
|
||||
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",
|
||||
AccountId: 1,
|
||||
Slug: "test-dash-23",
|
||||
OrgId: 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",
|
||||
AccountId: 1,
|
||||
Title: "test",
|
||||
OrgId: 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", AccountId: 1}
|
||||
query2 := m.SearchDashboardsQuery{Tag: "tagdoesnotexist", AccountId: 1}
|
||||
query1 := m.SearchDashboardsQuery{Tag: "webapp", OrgId: 1}
|
||||
query2 := m.SearchDashboardsQuery{Tag: "tagdoesnotexist", OrgId: 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{
|
||||
AccountId: 1,
|
||||
OrgId: 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{AccountId: 1}
|
||||
query := m.GetDashboardTagsQuery{OrgId: 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{AccountId: 1, UserId: 10, IsStarred: true}
|
||||
query := m.SearchDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
|
||||
err := SearchDashboards(&query)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
@@ -19,7 +19,7 @@ func init() {
|
||||
}
|
||||
|
||||
func GetDataSourceById(query *m.GetDataSourceByIdQuery) error {
|
||||
sess := x.Limit(100, 0).Where("account_id=? AND id=?", query.AccountId, query.Id)
|
||||
sess := x.Limit(100, 0).Where("org_id=? AND id=?", query.OrgId, 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.AccountId, query.Name)
|
||||
sess := x.Limit(100, 0).Where("org_id=? AND name=?", query.OrgId, 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.AccountId).Asc("name")
|
||||
sess := x.Limit(100, 0).Where("org_id=?", query.OrgId).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.AccountId)
|
||||
var rawSql = "DELETE FROM data_source WHERE id=? and org_id=?"
|
||||
_, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error {
|
||||
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
ds := &m.DataSource{
|
||||
AccountId: cmd.AccountId,
|
||||
OrgId: cmd.OrgId,
|
||||
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.AccountId, ds.Id); err != nil {
|
||||
rawSql := "UPDATE data_source SET is_default = 0 WHERE org_id=? AND id <> ?"
|
||||
if _, err := sess.Exec(rawSql, ds.OrgId, 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.AccountId,
|
||||
OrgId: cmd.OrgId,
|
||||
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.AccountId).Update(ds)
|
||||
_, err := sess.Where("id=? and org_id=?", ds.Id, ds.OrgId).Update(ds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -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{AccountId: 10}
|
||||
query := m.GetDataSourcesQuery{OrgId: 10}
|
||||
err = GetDataSources(&query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
@@ -59,33 +59,33 @@ func TestDataAccess(t *testing.T) {
|
||||
|
||||
ds := query.Result[0]
|
||||
|
||||
So(ds.AccountId, ShouldEqual, 10)
|
||||
So(ds.OrgId, 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{AccountId: 10}
|
||||
query := m.GetDataSourcesQuery{OrgId: 10}
|
||||
GetDataSources(&query)
|
||||
ds := query.Result[0]
|
||||
|
||||
Convey("Can delete datasource", func() {
|
||||
err := DeleteDataSource(&m.DeleteDataSourceCommand{Id: ds.Id, AccountId: ds.AccountId})
|
||||
err := DeleteDataSource(&m.DeleteDataSourceCommand{Id: ds.Id, OrgId: ds.OrgId})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
GetDataSources(&query)
|
||||
So(len(query.Result), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("Can not delete datasource with wrong accountId", func() {
|
||||
err := DeleteDataSource(&m.DeleteDataSourceCommand{Id: ds.Id, AccountId: 123123})
|
||||
Convey("Can not delete datasource with wrong orgId", func() {
|
||||
err := DeleteDataSource(&m.DeleteDataSourceCommand{Id: ds.Id, OrgId: 123123})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
GetDataSources(&query)
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
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 GetOrgList(query *m.GetOrgListQuery) error {
|
||||
return x.Find(&query.Result)
|
||||
}
|
||||
|
||||
func GetOrgById(query *m.GetOrgByIdQuery) error {
|
||||
var org m.Org
|
||||
exists, err := x.Id(query.Id).Get(&org)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return m.ErrOrgNotFound
|
||||
}
|
||||
|
||||
query.Result = &org
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetOrgByName(query *m.GetOrgByNameQuery) error {
|
||||
var org m.Org
|
||||
exists, err := x.Where("name=?", query.Name).Get(&org)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return m.ErrOrgNotFound
|
||||
}
|
||||
|
||||
query.Result = &org
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateOrg(cmd *m.CreateOrgCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
org := m.Org{
|
||||
Name: cmd.Name,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
if _, err := sess.Insert(&org); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user := m.OrgUser{
|
||||
OrgId: org.Id,
|
||||
UserId: cmd.UserId,
|
||||
Role: m.ROLE_ADMIN,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&user)
|
||||
cmd.Result = org
|
||||
|
||||
sess.publishAfterCommit(&events.OrgCreated{
|
||||
Timestamp: org.Created,
|
||||
Id: org.Id,
|
||||
Name: org.Name,
|
||||
})
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateOrg(cmd *m.UpdateOrgCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
org := m.Org{
|
||||
Name: cmd.Name,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
if _, err := sess.Id(cmd.OrgId).Update(&org); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sess.publishAfterCommit(&events.OrgUpdated{
|
||||
Timestamp: org.Updated,
|
||||
Id: org.Id,
|
||||
Name: org.Name,
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteOrg(cmd *m.DeleteOrgCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
deletes := []string{
|
||||
"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 {
|
||||
log.Trace(sql)
|
||||
_, err := sess.Exec(sql, cmd.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -15,11 +15,11 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
|
||||
Convey("Given single account mode", func() {
|
||||
setting.SingleAccountMode = true
|
||||
setting.DefaultAccountName = "test"
|
||||
setting.DefaultAccountRole = "Viewer"
|
||||
setting.SingleOrgMode = true
|
||||
setting.DefaultOrgName = "test"
|
||||
setting.DefaultOrgRole = "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].AccountId)
|
||||
So(q1.Result[0].OrgId, ShouldEqual, q2.Result[0].OrgId)
|
||||
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}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", AddOrgUser)
|
||||
bus.AddHandler("sql", RemoveOrgUser)
|
||||
bus.AddHandler("sql", GetOrgUsers)
|
||||
}
|
||||
|
||||
func AddOrgUser(cmd *m.AddOrgUserCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
|
||||
entity := m.OrgUser{
|
||||
OrgId: cmd.OrgId,
|
||||
UserId: cmd.UserId,
|
||||
Role: cmd.Role,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func GetOrgUsers(query *m.GetOrgUsersQuery) error {
|
||||
query.Result = make([]*m.OrgUserDTO, 0)
|
||||
sess := x.Table("org_user")
|
||||
sess.Join("INNER", "user", fmt.Sprintf("account_user.user_id=%s.id", x.Dialect().Quote("user")))
|
||||
sess.Where("org_user.org_id=?", query.OrgId)
|
||||
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 RemoveOrgUser(cmd *m.RemoveOrgUserCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "DELETE FROM org_user WHERE org_id=? and user_id=?"
|
||||
_, err := sess.Exec(rawSql, cmd.OrgId, cmd.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// validate that there is an admin user left
|
||||
res, err := sess.Query("SELECT 1 from org_user WHERE org_id=? and role='Admin'", cmd.OrgId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(res) == 0 {
|
||||
return m.ErrLastOrgAdmin
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -20,58 +20,59 @@ func init() {
|
||||
bus.AddHandler("sql", UpdateUser)
|
||||
bus.AddHandler("sql", ChangeUserPassword)
|
||||
bus.AddHandler("sql", GetUserByLogin)
|
||||
bus.AddHandler("sql", SetUsingAccount)
|
||||
bus.AddHandler("sql", SetUsingOrg)
|
||||
bus.AddHandler("sql", GetUserInfo)
|
||||
bus.AddHandler("sql", GetSignedInUser)
|
||||
bus.AddHandler("sql", SearchUsers)
|
||||
bus.AddHandler("sql", GetUserAccounts)
|
||||
bus.AddHandler("sql", GetUserOrgList)
|
||||
bus.AddHandler("sql", DeleteUser)
|
||||
bus.AddHandler("sql", SetUsingOrg)
|
||||
}
|
||||
|
||||
func getAccountIdForNewUser(userEmail string, sess *session) (int64, error) {
|
||||
var account m.Account
|
||||
func getOrgIdForNewUser(userEmail string, sess *session) (int64, error) {
|
||||
var org m.Org
|
||||
|
||||
if setting.SingleAccountMode {
|
||||
has, err := sess.Where("name=?", setting.DefaultAccountName).Get(&account)
|
||||
if setting.SingleOrgMode {
|
||||
has, err := sess.Where("name=?", setting.DefaultOrgName).Get(&org)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if has {
|
||||
return account.Id, nil
|
||||
return org.Id, nil
|
||||
} else {
|
||||
account.Name = setting.DefaultAccountName
|
||||
org.Name = setting.DefaultOrgName
|
||||
}
|
||||
} 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 := getAccountIdForNewUser(cmd.Email, sess)
|
||||
orgId, err := getOrgIdForNewUser(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: accountId,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
Email: cmd.Email,
|
||||
Name: cmd.Name,
|
||||
Login: cmd.Login,
|
||||
Company: cmd.Company,
|
||||
IsAdmin: cmd.IsAdmin,
|
||||
OrgId: orgId,
|
||||
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.AccountUser{
|
||||
AccountId: accountId,
|
||||
UserId: user.Id,
|
||||
Role: m.ROLE_ADMIN,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
// create org user link
|
||||
orgUser := m.OrgUser{
|
||||
OrgId: orgId,
|
||||
UserId: user.Id,
|
||||
Role: m.ROLE_ADMIN,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
if setting.SingleAccountMode && !user.IsAdmin {
|
||||
accountUser.Role = m.RoleType(setting.DefaultAccountRole)
|
||||
if setting.SingleOrgMode && !user.IsAdmin {
|
||||
orgUser.Role = m.RoleType(setting.DefaultOrgRole)
|
||||
}
|
||||
|
||||
if _, err = sess.Insert(&accountUser); err != nil {
|
||||
if _, err = sess.Insert(&orgUser); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -198,12 +199,12 @@ func ChangeUserPassword(cmd *m.ChangeUserPasswordCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
||||
func SetUsingOrg(cmd *m.SetUsingOrgCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
user := m.User{}
|
||||
sess.Id(cmd.UserId).Get(&user)
|
||||
|
||||
user.AccountId = cmd.AccountId
|
||||
user.OrgId = cmd.OrgId
|
||||
_, err := sess.Id(user.Id).Update(&user)
|
||||
return err
|
||||
})
|
||||
@@ -228,12 +229,12 @@ func GetUserInfo(query *m.GetUserInfoQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func GetUserAccounts(query *m.GetUserAccountsQuery) error {
|
||||
query.Result = make([]*m.UserAccountDTO, 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 GetUserOrgList(query *m.GetUserOrgListQuery) error {
|
||||
query.Result = make([]*m.UserOrgDTO, 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
|
||||
|
||||
Reference in New Issue
Block a user