mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Moved add collaborator to command way of doing it
This commit is contained in:
parent
36c46112df
commit
ccba95542b
@ -21,14 +21,14 @@ func GetAccount(c *middleware.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AddCollaborator(c *middleware.Context) {
|
func AddCollaborator(c *middleware.Context) {
|
||||||
var model dtos.AddCollaboratorCommand
|
var cmd m.AddCollaboratorCommand
|
||||||
|
|
||||||
if !c.JsonBody(&model) {
|
if !c.JsonBody(&cmd) {
|
||||||
c.JsonApiErr(400, "Invalid request", nil)
|
c.JsonApiErr(400, "Invalid request", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
accountToAdd, err := m.GetAccountByLogin(model.Email)
|
accountToAdd, err := m.GetAccountByLogin(cmd.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(404, "Collaborator not found", nil)
|
c.JsonApiErr(404, "Collaborator not found", nil)
|
||||||
return
|
return
|
||||||
@ -39,15 +39,17 @@ func AddCollaborator(c *middleware.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var collaborator = m.NewCollaborator(accountToAdd.Id, c.UserAccount.Id, m.ROLE_READ_WRITE)
|
cmd.AccountId = accountToAdd.Id
|
||||||
|
cmd.ForAccountId = c.UserAccount.Id
|
||||||
|
cmd.Role = m.ROLE_READ_WRITE
|
||||||
|
|
||||||
err = m.AddCollaborator(collaborator)
|
err = bus.Dispatch(&cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "Could not add collaborator", err)
|
c.JsonApiErr(500, "Could not add collaborator", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Status(204)
|
c.JsonOK("Collaborator added")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOtherAccounts(c *middleware.Context) {
|
func GetOtherAccounts(c *middleware.Context) {
|
||||||
|
@ -6,12 +6,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
SaveAccount func(account *Account) error
|
SaveAccount func(account *Account) error
|
||||||
GetAccountByLogin func(emailOrName string) (*Account, error)
|
GetAccountByLogin func(emailOrName string) (*Account, error)
|
||||||
GetAccount func(accountId int64) (*Account, error)
|
GetAccount func(accountId int64) (*Account, error)
|
||||||
GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error)
|
GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error)
|
||||||
GetCollaboratorsForAccount func(accountId int64) ([]*CollaboratorInfo, error)
|
|
||||||
AddCollaborator func(collaborator *Collaborator) error
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Typed errors
|
// Typed errors
|
||||||
@ -30,7 +28,7 @@ type Account struct {
|
|||||||
Id int64
|
Id int64
|
||||||
Login string `xorm:"UNIQUE NOT NULL"`
|
Login string `xorm:"UNIQUE NOT NULL"`
|
||||||
Email string `xorm:"UNIQUE NOT NULL"`
|
Email string `xorm:"UNIQUE NOT NULL"`
|
||||||
Name string `xorm:"UNIQUE NOT NULL"`
|
Name string
|
||||||
FullName string
|
FullName string
|
||||||
Password string
|
Password string
|
||||||
IsAdmin bool
|
IsAdmin bool
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ROLE_READ_WRITE = "ReadWrite"
|
ROLE_READ_WRITE RoleType = "ReadWrite"
|
||||||
ROLE_READ = "Read"
|
ROLE_READ = "Read"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoleType string
|
type RoleType string
|
||||||
@ -16,15 +16,16 @@ type Collaborator struct {
|
|||||||
AccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"` // The account that can use another account
|
AccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"` // The account that can use another account
|
||||||
Role RoleType `xorm:"not null"` // Permission type
|
Role RoleType `xorm:"not null"` // Permission type
|
||||||
ForAccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"` // The account being given access to
|
ForAccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"` // The account being given access to
|
||||||
Created time.Time
|
|
||||||
Updated time.Time
|
Created time.Time
|
||||||
|
Updated time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// read only projection
|
type AddCollaboratorCommand struct {
|
||||||
type CollaboratorInfo struct {
|
Email string `json:"email" binding:"required"`
|
||||||
AccountId int64
|
AccountId int64 `json:"-"`
|
||||||
Role string
|
ForAccountId int64 `json:"-"`
|
||||||
Email string
|
Role RoleType `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCollaborator(accountId int64, forAccountId int64, role RoleType) *Collaborator {
|
func NewCollaborator(accountId int64, forAccountId int64, role RoleType) *Collaborator {
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
package sqlstore
|
package sqlstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-xorm/xorm"
|
||||||
|
|
||||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
bus.AddHandler("sql", GetAccountInfo)
|
bus.AddHandler("sql", GetAccountInfo)
|
||||||
|
bus.AddHandler("sql", AddCollaborator)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
||||||
@ -33,6 +38,22 @@ func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
|
||||||
|
return inTransaction(func(sess *xorm.Session) error {
|
||||||
|
|
||||||
|
entity := m.Collaborator{
|
||||||
|
AccountId: cmd.AccountId,
|
||||||
|
ForAccountId: cmd.ForAccountId,
|
||||||
|
Role: cmd.Role,
|
||||||
|
Created: time.Now(),
|
||||||
|
Updated: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := sess.Insert(&entity)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func SaveAccount(account *m.Account) error {
|
func SaveAccount(account *m.Account) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@ -93,37 +114,6 @@ func GetAccountByLogin(emailOrLogin string) (*m.Account, error) {
|
|||||||
return account, nil
|
return account, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCollaboratorsForAccount(accountId int64) ([]*m.CollaboratorInfo, error) {
|
|
||||||
collaborators := make([]*m.CollaboratorInfo, 0)
|
|
||||||
|
|
||||||
sess := x.Table("collaborator")
|
|
||||||
sess.Join("INNER", "account", "account.id=collaborator.account_Id")
|
|
||||||
sess.Where("collaborator.for_account_id=?", accountId)
|
|
||||||
err := sess.Find(&collaborators)
|
|
||||||
|
|
||||||
return collaborators, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddCollaborator(collaborator *m.Collaborator) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
sess := x.NewSession()
|
|
||||||
defer sess.Close()
|
|
||||||
|
|
||||||
if err = sess.Begin(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = sess.Insert(collaborator); err != nil {
|
|
||||||
sess.Rollback()
|
|
||||||
return err
|
|
||||||
} else if err = sess.Commit(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetOtherAccountsFor(accountId int64) ([]*m.OtherAccount, error) {
|
func GetOtherAccountsFor(accountId int64) ([]*m.OtherAccount, error) {
|
||||||
collaborators := make([]*m.OtherAccount, 0)
|
collaborators := make([]*m.OtherAccount, 0)
|
||||||
sess := x.Table("collaborator")
|
sess := x.Table("collaborator")
|
||||||
|
@ -13,20 +13,52 @@ func TestAccountDataAccess(t *testing.T) {
|
|||||||
Convey("Testing Account DB Access", t, func() {
|
Convey("Testing Account DB Access", t, func() {
|
||||||
InitTestDB(t)
|
InitTestDB(t)
|
||||||
|
|
||||||
Convey("Can save account", func() {
|
Convey("Given two saved accounts", func() {
|
||||||
account := m.Account{
|
ac1 := m.Account{
|
||||||
Login: "login",
|
Login: "ac1",
|
||||||
Email: "login@test.com",
|
Email: "ac1@test.com",
|
||||||
Name: "name",
|
Name: "ac1_name",
|
||||||
|
}
|
||||||
|
ac2 := m.Account{
|
||||||
|
Login: "ac2",
|
||||||
|
Email: "ac2@test.com",
|
||||||
|
Name: "ac2_name",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := SaveAccount(&account)
|
err := SaveAccount(&ac1)
|
||||||
|
err = SaveAccount(&ac2)
|
||||||
query := m.GetAccountInfoQuery{Id: account.Id}
|
|
||||||
err = GetAccountInfo(&query)
|
|
||||||
|
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(query.Result.Name, ShouldEqual, "name")
|
|
||||||
|
Convey("Should be able to read account info projection", func() {
|
||||||
|
query := m.GetAccountInfoQuery{Id: ac1.Id}
|
||||||
|
err = GetAccountInfo(&query)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(query.Result.Name, ShouldEqual, "ac1_name")
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Can add collaborator", func() {
|
||||||
|
cmd := m.AddCollaboratorCommand{
|
||||||
|
AccountId: ac2.Id,
|
||||||
|
ForAccountId: ac1.Id,
|
||||||
|
Role: m.ROLE_READ_WRITE,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := AddCollaborator(&cmd)
|
||||||
|
Convey("Saved without error", func() {
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Collaborator should be included in account info projection", func() {
|
||||||
|
query := m.GetAccountInfoQuery{Id: ac1.Id}
|
||||||
|
err = GetAccountInfo(&query)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(query.Result.Collaborators[0].AccountId, ShouldEqual, ac2.Id)
|
||||||
|
So(query.Result.Collaborators[0].Role, ShouldEqual, m.ROLE_READ_WRITE)
|
||||||
|
So(query.Result.Collaborators[0].Email, ShouldEqual, "ac2@test.com")
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -43,8 +43,6 @@ func Init() {
|
|||||||
m.SaveDashboard = SaveDashboard
|
m.SaveDashboard = SaveDashboard
|
||||||
m.SearchQuery = SearchQuery
|
m.SearchQuery = SearchQuery
|
||||||
m.DeleteDashboard = DeleteDashboard
|
m.DeleteDashboard = DeleteDashboard
|
||||||
m.GetCollaboratorsForAccount = GetCollaboratorsForAccount
|
|
||||||
m.AddCollaborator = AddCollaborator
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEngine() (err error) {
|
func NewEngine() (err error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user