mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Working on account collaborators
This commit is contained in:
parent
50a5355de0
commit
6f63d63ee0
2
grafana
2
grafana
@ -1 +1 @@
|
|||||||
Subproject commit 733a9af6294c87acc72d1a519791e2be7d0f594f
|
Subproject commit 961ebbde6b6540f03d3fb5a1741722614166099f
|
@ -4,7 +4,6 @@ import (
|
|||||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||||
"github.com/torkelo/grafana-pro/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetAccount(c *middleware.Context) {
|
func GetAccount(c *middleware.Context) {
|
||||||
@ -41,8 +40,8 @@ func AddCollaborator(c *middleware.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AccountId = accountToAdd.Id
|
cmd.AccountId = c.UserAccount.Id
|
||||||
cmd.ForAccountId = c.UserAccount.Id
|
cmd.CollaboratorId = accountToAdd.Id
|
||||||
cmd.Role = m.ROLE_READ_WRITE
|
cmd.Role = m.ROLE_READ_WRITE
|
||||||
|
|
||||||
err = bus.Dispatch(&cmd)
|
err = bus.Dispatch(&cmd)
|
||||||
@ -54,23 +53,35 @@ func AddCollaborator(c *middleware.Context) {
|
|||||||
c.JsonOK("Collaborator added")
|
c.JsonOK("Collaborator added")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveCollaborator(c *middleware.Context) {
|
||||||
|
collaboratorId := c.ParamsInt64(":id")
|
||||||
|
|
||||||
|
cmd := m.RemoveCollaboratorCommand{AccountId: c.UserAccount.Id, CollaboratorId: collaboratorId}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
|
c.JsonApiErr(500, "Failed to remove collaborator", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JsonOK("Collaborator removed")
|
||||||
|
}
|
||||||
|
|
||||||
func GetOtherAccounts(c *middleware.Context) {
|
func GetOtherAccounts(c *middleware.Context) {
|
||||||
query := m.GetOtherAccountsQuery{AccountId: c.UserAccount.Id}
|
query := m.GetOtherAccountsQuery{AccountId: c.UserAccount.Id}
|
||||||
err := bus.Dispatch(&query)
|
err := bus.Dispatch(&query)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(500, util.DynMap{"message": err.Error()})
|
c.JsonApiErr(500, "Failed to get other accounts", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result := append(query.Result, &m.OtherAccountDTO{
|
result := append(query.Result, &m.OtherAccountDTO{
|
||||||
Id: c.UserAccount.Id,
|
AccountId: c.UserAccount.Id,
|
||||||
Role: "owner",
|
Role: "owner",
|
||||||
Email: c.UserAccount.Email,
|
Email: c.UserAccount.Email,
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, ac := range result {
|
for _, ac := range result {
|
||||||
if ac.Id == c.UserAccount.UsingAccountId {
|
if ac.AccountId == c.UserAccount.UsingAccountId {
|
||||||
ac.IsUsing = true
|
ac.IsUsing = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -93,7 +104,7 @@ func validateUsingAccount(accountId int64, otherId int64) bool {
|
|||||||
// validate that the account id in the list
|
// validate that the account id in the list
|
||||||
valid := false
|
valid := false
|
||||||
for _, other := range query.Result {
|
for _, other := range query.Result {
|
||||||
if other.Id == otherId {
|
if other.AccountId == otherId {
|
||||||
valid = true
|
valid = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,8 @@ func Register(m *macaron.Macaron) {
|
|||||||
// account
|
// account
|
||||||
m.Group("/account", func() {
|
m.Group("/account", func() {
|
||||||
m.Get("/", GetAccount)
|
m.Get("/", GetAccount)
|
||||||
m.Post("/collaborators/add", AddCollaborator)
|
m.Put("/collaborators", AddCollaborator)
|
||||||
|
m.Delete("/collaborators/:id", RemoveCollaborator)
|
||||||
m.Post("/using/:id", SetUsingAccount)
|
m.Post("/using/:id", SetUsingAccount)
|
||||||
m.Get("/others", GetOtherAccounts)
|
m.Get("/others", GetOtherAccounts)
|
||||||
})
|
})
|
||||||
|
@ -72,14 +72,14 @@ type GetAccountByLoginQuery struct {
|
|||||||
// DTO & Projections
|
// DTO & Projections
|
||||||
|
|
||||||
type OtherAccountDTO struct {
|
type OtherAccountDTO struct {
|
||||||
Id int64 `json:"id"`
|
AccountId int64 `json:"accountId"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
IsUsing bool `json:"isUsing"`
|
IsUsing bool `json:"isUsing"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollaboratorDTO struct {
|
type CollaboratorDTO struct {
|
||||||
AccountId int64 `json:"accountId"`
|
CollaboratorId int64 `json:"id"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
}
|
}
|
||||||
|
@ -13,25 +13,30 @@ type RoleType string
|
|||||||
|
|
||||||
type Collaborator struct {
|
type Collaborator struct {
|
||||||
Id int64
|
Id int64
|
||||||
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)"`
|
||||||
Role RoleType `xorm:"not null"` // Permission type
|
Role RoleType `xorm:"not null"`
|
||||||
ForAccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"` // The account being given access to
|
CollaboratorId int64 `xorm:"not null unique(uix_account_id_for_account_id)"`
|
||||||
|
|
||||||
Created time.Time
|
Created time.Time
|
||||||
Updated time.Time
|
Updated time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RemoveCollaboratorCommand struct {
|
||||||
|
CollaboratorId int64
|
||||||
|
AccountId int64
|
||||||
|
}
|
||||||
|
|
||||||
type AddCollaboratorCommand struct {
|
type AddCollaboratorCommand struct {
|
||||||
Email string `json:"email" binding:"required"`
|
Email string `json:"email" binding:"required"`
|
||||||
AccountId int64 `json:"-"`
|
AccountId int64 `json:"-"`
|
||||||
ForAccountId int64 `json:"-"`
|
CollaboratorId int64 `json:"-"`
|
||||||
Role RoleType `json:"-"`
|
Role RoleType `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCollaborator(accountId int64, forAccountId int64, role RoleType) *Collaborator {
|
func NewCollaborator(accountId int64, collaboratorId int64, role RoleType) *Collaborator {
|
||||||
return &Collaborator{
|
return &Collaborator{
|
||||||
AccountId: accountId,
|
AccountId: accountId,
|
||||||
ForAccountId: forAccountId,
|
CollaboratorId: collaboratorId,
|
||||||
Role: role,
|
Role: role,
|
||||||
Created: time.Now(),
|
Created: time.Now(),
|
||||||
Updated: time.Now(),
|
Updated: time.Now(),
|
||||||
|
@ -11,13 +11,14 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
bus.AddHandler("sql", GetAccountInfo)
|
bus.AddHandler("sql", GetAccountInfo)
|
||||||
bus.AddHandler("sql", AddCollaborator)
|
|
||||||
bus.AddHandler("sql", GetOtherAccounts)
|
bus.AddHandler("sql", GetOtherAccounts)
|
||||||
bus.AddHandler("sql", CreateAccount)
|
bus.AddHandler("sql", CreateAccount)
|
||||||
bus.AddHandler("sql", SetUsingAccount)
|
bus.AddHandler("sql", SetUsingAccount)
|
||||||
bus.AddHandler("sql", GetAccountById)
|
bus.AddHandler("sql", GetAccountById)
|
||||||
bus.AddHandler("sql", GetAccountByLogin)
|
bus.AddHandler("sql", GetAccountByLogin)
|
||||||
bus.AddHandler("sql", GetAccountByToken)
|
bus.AddHandler("sql", GetAccountByToken)
|
||||||
|
bus.AddHandler("sql", AddCollaborator)
|
||||||
|
bus.AddHandler("sql", RemoveCollaborator)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||||
@ -66,8 +67,8 @@ func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sess := x.Table("collaborator")
|
sess := x.Table("collaborator")
|
||||||
sess.Join("INNER", "account", "account.id=collaborator.account_Id")
|
sess.Join("INNER", "account", "account.id=collaborator.collaborator_id")
|
||||||
sess.Where("collaborator.for_account_id=?", query.Id)
|
sess.Where("collaborator.account_id=?", query.Id)
|
||||||
err = sess.Find(&query.Result.Collaborators)
|
err = sess.Find(&query.Result.Collaborators)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@ -78,7 +79,7 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
|
|||||||
|
|
||||||
entity := m.Collaborator{
|
entity := m.Collaborator{
|
||||||
AccountId: cmd.AccountId,
|
AccountId: cmd.AccountId,
|
||||||
ForAccountId: cmd.ForAccountId,
|
CollaboratorId: cmd.CollaboratorId,
|
||||||
Role: cmd.Role,
|
Role: cmd.Role,
|
||||||
Created: time.Now(),
|
Created: time.Now(),
|
||||||
Updated: time.Now(),
|
Updated: time.Now(),
|
||||||
@ -155,12 +156,20 @@ func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveCollaborator(cmd *m.RemoveCollaboratorCommand) error {
|
||||||
|
return inTransaction(func(sess *xorm.Session) error {
|
||||||
|
var rawSql = "DELETE FROM collaborator WHERE collaborator_id=? and account_id=?"
|
||||||
|
_, err := sess.Exec(rawSql, cmd.CollaboratorId, cmd.AccountId)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
|
func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
|
||||||
query.Result = make([]*m.OtherAccountDTO, 0)
|
query.Result = make([]*m.OtherAccountDTO, 0)
|
||||||
sess := x.Table("collaborator")
|
sess := x.Table("collaborator")
|
||||||
sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
|
sess.Join("INNER", "account", "collaborator.account_id=account.id")
|
||||||
sess.Where("account_id=?", query.AccountId)
|
sess.Where("collaborator_id=?", query.AccountId)
|
||||||
sess.Cols("collaborator.id", "collaborator.role", "account.email")
|
sess.Cols("collaborator.account_id", "collaborator.role", "account.email")
|
||||||
err := sess.Find(&query.Result)
|
err := sess.Find(&query.Result)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user