mirror of https://github.com/grafana/grafana
parent
e9e2fa2927
commit
26e4809e2e
@ -1,50 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/bus" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
func GetOrg(c *middleware.Context) { |
||||
query := m.GetAccountByIdQuery{Id: c.AccountId} |
||||
|
||||
if err := bus.Dispatch(&query); err != nil { |
||||
if err == m.ErrAccountNotFound { |
||||
c.JsonApiErr(404, "Account not found", err) |
||||
return |
||||
} |
||||
|
||||
c.JsonApiErr(500, "Failed to get account", err) |
||||
return |
||||
} |
||||
|
||||
account := m.AccountDTO{ |
||||
Id: query.Result.Id, |
||||
Name: query.Result.Name, |
||||
} |
||||
|
||||
c.JSON(200, &account) |
||||
} |
||||
|
||||
func CreateOrg(c *middleware.Context, cmd m.CreateAccountCommand) { |
||||
cmd.UserId = c.UserId |
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
c.JsonApiErr(500, "Failed to create account", err) |
||||
return |
||||
} |
||||
|
||||
c.JsonOK("Account created") |
||||
} |
||||
|
||||
func UpdateOrg(c *middleware.Context, cmd m.UpdateAccountCommand) { |
||||
cmd.AccountId = c.AccountId |
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
c.JsonApiErr(500, "Failed to update account", err) |
||||
return |
||||
} |
||||
|
||||
c.JsonOK("Account updated") |
||||
} |
@ -0,0 +1,50 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/bus" |
||||
"github.com/grafana/grafana/pkg/middleware" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
func GetOrg(c *middleware.Context) { |
||||
query := m.GetOrgByIdQuery{Id: c.OrgId} |
||||
|
||||
if err := bus.Dispatch(&query); err != nil { |
||||
if err == m.ErrOrgNotFound { |
||||
c.JsonApiErr(404, "Organization not found", err) |
||||
return |
||||
} |
||||
|
||||
c.JsonApiErr(500, "Failed to get organization", err) |
||||
return |
||||
} |
||||
|
||||
org := m.OrgDTO{ |
||||
Id: query.Result.Id, |
||||
Name: query.Result.Name, |
||||
} |
||||
|
||||
c.JSON(200, &org) |
||||
} |
||||
|
||||
func CreateOrg(c *middleware.Context, cmd m.CreateOrgCommand) { |
||||
cmd.UserId = c.UserId |
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
c.JsonApiErr(500, "Failed to create organization", err) |
||||
return |
||||
} |
||||
|
||||
c.JsonOK("Organization created") |
||||
} |
||||
|
||||
func UpdateOrg(c *middleware.Context, cmd m.UpdateOrgCommand) { |
||||
cmd.OrgId = c.OrgId |
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
c.JsonApiErr(500, "Failed to update organization", err) |
||||
return |
||||
} |
||||
|
||||
c.JsonOK("Organization updated") |
||||
} |
@ -1,99 +0,0 @@ |
||||
package cmd |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"text/tabwriter" |
||||
|
||||
"github.com/codegangsta/cli" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
"github.com/grafana/grafana/pkg/log" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
var ListAccounts = cli.Command{ |
||||
Name: "accounts", |
||||
Usage: "list accounts", |
||||
Description: "Lists the accounts in the system", |
||||
Action: listAccounts, |
||||
} |
||||
|
||||
var CreateAccount = cli.Command{ |
||||
Name: "accounts:create", |
||||
Usage: "create a new account", |
||||
Description: "Creates a new account", |
||||
Action: createAccount, |
||||
} |
||||
|
||||
var DeleteAccount = cli.Command{ |
||||
Name: "accounts:delete", |
||||
Usage: "delete an existing account", |
||||
Description: "Deletes an existing account", |
||||
Action: deleteAccount, |
||||
} |
||||
|
||||
func listAccounts(c *cli.Context) { |
||||
initRuntime(c) |
||||
|
||||
accountsQuery := m.GetAccountsQuery{} |
||||
if err := bus.Dispatch(&accountsQuery); err != nil { |
||||
log.ConsoleFatalf("Failed to find accounts: %s", err) |
||||
} |
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 8, 1, 4, ' ', 0) |
||||
|
||||
fmt.Fprintf(w, "ID\tNAME\n") |
||||
for _, account := range accountsQuery.Result { |
||||
fmt.Fprintf(w, "%d\t%s\n", account.Id, account.Name) |
||||
} |
||||
w.Flush() |
||||
} |
||||
|
||||
func createAccount(c *cli.Context) { |
||||
initRuntime(c) |
||||
|
||||
if !c.Args().Present() { |
||||
log.ConsoleFatal("Account name arg is required") |
||||
} |
||||
|
||||
name := c.Args().First() |
||||
|
||||
adminQuery := m.GetUserByLoginQuery{LoginOrEmail: setting.AdminUser} |
||||
|
||||
if err := bus.Dispatch(&adminQuery); err == m.ErrUserNotFound { |
||||
log.ConsoleFatalf("Failed to find default admin user: %s", err) |
||||
} |
||||
|
||||
adminUser := adminQuery.Result |
||||
|
||||
cmd := m.CreateAccountCommand{Name: name, UserId: adminUser.Id} |
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
log.ConsoleFatalf("Failed to create account: %s", err) |
||||
} |
||||
|
||||
log.ConsoleInfof("Account %s created for admin user %s\n", name, adminUser.Email) |
||||
} |
||||
|
||||
func deleteAccount(c *cli.Context) { |
||||
initRuntime(c) |
||||
|
||||
if !c.Args().Present() { |
||||
log.ConsoleFatal("Account name arg is required") |
||||
} |
||||
|
||||
name := c.Args().First() |
||||
accountQuery := m.GetAccountByNameQuery{Name: name} |
||||
if err := bus.Dispatch(&accountQuery); err != nil { |
||||
log.ConsoleFatalf("Failed to find account: %s", err) |
||||
} |
||||
|
||||
accountId := accountQuery.Result.Id |
||||
cmd := m.DeleteAccountCommand{Id: accountId} |
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
log.ConsoleFatalf("Failed to delete account: %s", err) |
||||
} |
||||
|
||||
log.ConsoleInfof("Account %s deleted", name) |
||||
} |
@ -0,0 +1,99 @@ |
||||
package cmd |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"text/tabwriter" |
||||
|
||||
"github.com/codegangsta/cli" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
"github.com/grafana/grafana/pkg/log" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
var ListOrgs = cli.Command{ |
||||
Name: "orgs", |
||||
Usage: "list organizations", |
||||
Description: "Lists the organizations in the system", |
||||
Action: listOrgs, |
||||
} |
||||
|
||||
var CreateOrg = cli.Command{ |
||||
Name: "orgs:create", |
||||
Usage: "Creates a new organization", |
||||
Description: "Creates a new organization", |
||||
Action: createOrg, |
||||
} |
||||
|
||||
var DeleteOrg = cli.Command{ |
||||
Name: "orgs:delete", |
||||
Usage: "Delete an existing organization", |
||||
Description: "Deletes an existing organization", |
||||
Action: deleteOrg, |
||||
} |
||||
|
||||
func listOrgs(c *cli.Context) { |
||||
initRuntime(c) |
||||
|
||||
orgsQuery := m.GetOrgListQuery{} |
||||
if err := bus.Dispatch(&orgsQuery); err != nil { |
||||
log.ConsoleFatalf("Failed to find organizations: %s", err) |
||||
} |
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 8, 1, 4, ' ', 0) |
||||
|
||||
fmt.Fprintf(w, "ID\tNAME\n") |
||||
for _, org := range orgsQuery.Result { |
||||
fmt.Fprintf(w, "%d\t%s\n", org.Id, org.Name) |
||||
} |
||||
w.Flush() |
||||
} |
||||
|
||||
func createOrg(c *cli.Context) { |
||||
initRuntime(c) |
||||
|
||||
if !c.Args().Present() { |
||||
log.ConsoleFatal("Organization name arg is required") |
||||
} |
||||
|
||||
name := c.Args().First() |
||||
|
||||
adminQuery := m.GetUserByLoginQuery{LoginOrEmail: setting.AdminUser} |
||||
|
||||
if err := bus.Dispatch(&adminQuery); err == m.ErrUserNotFound { |
||||
log.ConsoleFatalf("Failed to find default admin user: %s", err) |
||||
} |
||||
|
||||
adminUser := adminQuery.Result |
||||
|
||||
cmd := m.CreateOrgCommand{Name: name, UserId: adminUser.Id} |
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
log.ConsoleFatalf("Failed to create organization: %s", err) |
||||
} |
||||
|
||||
log.ConsoleInfof("Organization %s created for admin user %s\n", name, adminUser.Email) |
||||
} |
||||
|
||||
func deleteOrg(c *cli.Context) { |
||||
initRuntime(c) |
||||
|
||||
if !c.Args().Present() { |
||||
log.ConsoleFatal("Organization name arg is required") |
||||
} |
||||
|
||||
name := c.Args().First() |
||||
orgQuery := m.GetOrgByNameQuery{Name: name} |
||||
if err := bus.Dispatch(&orgQuery); err != nil { |
||||
log.ConsoleFatalf("Failed to find organization: %s", err) |
||||
} |
||||
|
||||
orgId := orgQuery.Result.Id |
||||
cmd := m.DeleteOrgCommand{Id: orgId} |
||||
if err := bus.Dispatch(&cmd); err != nil { |
||||
log.ConsoleFatalf("Failed to delete organization: %s", err) |
||||
} |
||||
|
||||
log.ConsoleInfof("Organization %s deleted", name) |
||||
} |
@ -1,70 +0,0 @@ |
||||
package models |
||||
|
||||
import ( |
||||
"errors" |
||||
"time" |
||||
) |
||||
|
||||
// Typed errors
|
||||
var ( |
||||
ErrAccountNotFound = errors.New("Account not found") |
||||
) |
||||
|
||||
type Account struct { |
||||
Id int64 |
||||
Version int |
||||
Name string |
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type CreateAccountCommand struct { |
||||
Name string `json:"name" binding:"Required"` |
||||
|
||||
// initial admin user for account
|
||||
UserId int64 `json:"-"` |
||||
Result Account `json:"-"` |
||||
} |
||||
|
||||
type DeleteAccountCommand struct { |
||||
Id int64 |
||||
} |
||||
|
||||
type UpdateAccountCommand struct { |
||||
Name string `json:"name" binding:"Required"` |
||||
AccountId int64 `json:"-"` |
||||
} |
||||
|
||||
type GetUserAccountsQuery struct { |
||||
UserId int64 |
||||
Result []*UserAccountDTO |
||||
} |
||||
|
||||
type GetAccountByIdQuery struct { |
||||
Id int64 |
||||
Result *Account |
||||
} |
||||
|
||||
type GetAccountByNameQuery struct { |
||||
Name string |
||||
Result *Account |
||||
} |
||||
|
||||
type GetAccountsQuery struct { |
||||
Result []*Account |
||||
} |
||||
|
||||
type AccountDTO struct { |
||||
Id int64 `json:"id"` |
||||
Name string `json:"name"` |
||||
} |
||||
|
||||
type UserAccountDTO struct { |
||||
AccountId int64 `json:"accountId"` |
||||
Name string `json:"name"` |
||||
Role RoleType `json:"role"` |
||||
IsUsing bool `json:"isUsing"` |
||||
} |
@ -1,67 +0,0 @@ |
||||
package models |
||||
|
||||
import ( |
||||
"errors" |
||||
"time" |
||||
) |
||||
|
||||
// Typed errors
|
||||
var ( |
||||
ErrInvalidRoleType = errors.New("Invalid role type") |
||||
ErrLastAccountAdmin = errors.New("Cannot remove last account admin") |
||||
) |
||||
|
||||
type RoleType string |
||||
|
||||
const ( |
||||
ROLE_VIEWER RoleType = "Viewer" |
||||
ROLE_EDITOR RoleType = "Editor" |
||||
ROLE_ADMIN RoleType = "Admin" |
||||
) |
||||
|
||||
func (r RoleType) IsValid() bool { |
||||
return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR |
||||
} |
||||
|
||||
type AccountUser struct { |
||||
AccountId int64 |
||||
UserId int64 |
||||
Role RoleType |
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type RemoveAccountUserCommand struct { |
||||
UserId int64 |
||||
AccountId int64 |
||||
} |
||||
|
||||
type AddAccountUserCommand struct { |
||||
LoginOrEmail string `json:"loginOrEmail" binding:"Required"` |
||||
Role RoleType `json:"role" binding:"Required"` |
||||
|
||||
AccountId int64 `json:"-"` |
||||
UserId int64 `json:"-"` |
||||
} |
||||
|
||||
// ----------------------
|
||||
// QUERIES
|
||||
|
||||
type GetAccountUsersQuery struct { |
||||
AccountId int64 |
||||
Result []*AccountUserDTO |
||||
} |
||||
|
||||
// ----------------------
|
||||
// Projections and DTOs
|
||||
|
||||
type AccountUserDTO struct { |
||||
AccountId int64 `json:"accountId"` |
||||
UserId int64 `json:"userId"` |
||||
Email string `json:"email"` |
||||
Login string `json:"login"` |
||||
Role string `json:"role"` |
||||
} |
@ -0,0 +1,65 @@ |
||||
package models |
||||
|
||||
import ( |
||||
"errors" |
||||
"time" |
||||
) |
||||
|
||||
// Typed errors
|
||||
var ( |
||||
ErrOrgNotFound = errors.New("Organization not found") |
||||
) |
||||
|
||||
type Org struct { |
||||
Id int64 |
||||
Version int |
||||
Name string |
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type CreateOrgCommand struct { |
||||
Name string `json:"name" binding:"Required"` |
||||
|
||||
// initial admin user for account
|
||||
UserId int64 `json:"-"` |
||||
Result Org `json:"-"` |
||||
} |
||||
|
||||
type DeleteOrgCommand struct { |
||||
Id int64 |
||||
} |
||||
|
||||
type UpdateOrgCommand struct { |
||||
Name string `json:"name" binding:"Required"` |
||||
OrgId int64 `json:"-"` |
||||
} |
||||
|
||||
type GetOrgByIdQuery struct { |
||||
Id int64 |
||||
Result *Org |
||||
} |
||||
|
||||
type GetOrgByNameQuery struct { |
||||
Name string |
||||
Result *Org |
||||
} |
||||
|
||||
type GetOrgListQuery struct { |
||||
Result []*Org |
||||
} |
||||
|
||||
type OrgDTO struct { |
||||
Id int64 `json:"id"` |
||||
Name string `json:"name"` |
||||
} |
||||
|
||||
type UserOrgDTO struct { |
||||
OrgId int64 `json:"orgId"` |
||||
Name string `json:"name"` |
||||
Role RoleType `json:"role"` |
||||
IsUsing bool `json:"isUsing"` |
||||
} |
@ -0,0 +1,67 @@ |
||||
package models |
||||
|
||||
import ( |
||||
"errors" |
||||
"time" |
||||
) |
||||
|
||||
// Typed errors
|
||||
var ( |
||||
ErrInvalidRoleType = errors.New("Invalid role type") |
||||
ErrLastOrgAdmin = errors.New("Cannot remove last organization admin") |
||||
) |
||||
|
||||
type RoleType string |
||||
|
||||
const ( |
||||
ROLE_VIEWER RoleType = "Viewer" |
||||
ROLE_EDITOR RoleType = "Editor" |
||||
ROLE_ADMIN RoleType = "Admin" |
||||
) |
||||
|
||||
func (r RoleType) IsValid() bool { |
||||
return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR |
||||
} |
||||
|
||||
type OrgUser struct { |
||||
OrgId int64 |
||||
UserId int64 |
||||
Role RoleType |
||||
Created time.Time |
||||
Updated time.Time |
||||
} |
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type RemoveOrgUserCommand struct { |
||||
UserId int64 |
||||
OrgId int64 |
||||
} |
||||
|
||||
type AddOrgUserCommand struct { |
||||
LoginOrEmail string `json:"loginOrEmail" binding:"Required"` |
||||
Role RoleType `json:"role" binding:"Required"` |
||||
|
||||
OrgId int64 `json:"-"` |
||||
UserId int64 `json:"-"` |
||||
} |
||||
|
||||
// ----------------------
|
||||
// QUERIES
|
||||
|
||||
type GetOrgUsersQuery struct { |
||||
OrgId int64 |
||||
Result []*OrgUserDTO |
||||
} |
||||
|
||||
// ----------------------
|
||||
// Projections and DTOs
|
||||
|
||||
type OrgUserDTO struct { |
||||
OrgId int64 `json:"orgId"` |
||||
UserId int64 `json:"userId"` |
||||
Email string `json:"email"` |
||||
Login string `json:"login"` |
||||
Role string `json:"role"` |
||||
} |
@ -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 |
||||
}) |
||||
} |
@ -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 |
||||
}) |
||||
} |
@ -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 |
||||
}) |
||||
} |
Loading…
Reference in new issue