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/middleware"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
"github.com/torkelo/grafana-pro/pkg/util"
|
||||
)
|
||||
|
||||
func GetAccount(c *middleware.Context) {
|
||||
@ -41,8 +40,8 @@ func AddCollaborator(c *middleware.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
cmd.AccountId = accountToAdd.Id
|
||||
cmd.ForAccountId = c.UserAccount.Id
|
||||
cmd.AccountId = c.UserAccount.Id
|
||||
cmd.CollaboratorId = accountToAdd.Id
|
||||
cmd.Role = m.ROLE_READ_WRITE
|
||||
|
||||
err = bus.Dispatch(&cmd)
|
||||
@ -54,23 +53,35 @@ func AddCollaborator(c *middleware.Context) {
|
||||
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) {
|
||||
query := m.GetOtherAccountsQuery{AccountId: c.UserAccount.Id}
|
||||
err := bus.Dispatch(&query)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, util.DynMap{"message": err.Error()})
|
||||
c.JsonApiErr(500, "Failed to get other accounts", err)
|
||||
return
|
||||
}
|
||||
|
||||
result := append(query.Result, &m.OtherAccountDTO{
|
||||
Id: c.UserAccount.Id,
|
||||
Role: "owner",
|
||||
Email: c.UserAccount.Email,
|
||||
AccountId: c.UserAccount.Id,
|
||||
Role: "owner",
|
||||
Email: c.UserAccount.Email,
|
||||
})
|
||||
|
||||
for _, ac := range result {
|
||||
if ac.Id == c.UserAccount.UsingAccountId {
|
||||
if ac.AccountId == c.UserAccount.UsingAccountId {
|
||||
ac.IsUsing = true
|
||||
break
|
||||
}
|
||||
@ -93,7 +104,7 @@ func validateUsingAccount(accountId int64, otherId int64) bool {
|
||||
// validate that the account id in the list
|
||||
valid := false
|
||||
for _, other := range query.Result {
|
||||
if other.Id == otherId {
|
||||
if other.AccountId == otherId {
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,8 @@ func Register(m *macaron.Macaron) {
|
||||
// account
|
||||
m.Group("/account", func() {
|
||||
m.Get("/", GetAccount)
|
||||
m.Post("/collaborators/add", AddCollaborator)
|
||||
m.Put("/collaborators", AddCollaborator)
|
||||
m.Delete("/collaborators/:id", RemoveCollaborator)
|
||||
m.Post("/using/:id", SetUsingAccount)
|
||||
m.Get("/others", GetOtherAccounts)
|
||||
})
|
||||
|
@ -72,16 +72,16 @@ type GetAccountByLoginQuery struct {
|
||||
// DTO & Projections
|
||||
|
||||
type OtherAccountDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
IsUsing bool `json:"isUsing"`
|
||||
}
|
||||
|
||||
type CollaboratorDTO struct {
|
||||
AccountId int64 `json:"accountId"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
IsUsing bool `json:"isUsing"`
|
||||
}
|
||||
|
||||
type CollaboratorDTO struct {
|
||||
CollaboratorId int64 `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
type AccountDTO struct {
|
||||
|
@ -12,28 +12,33 @@ const (
|
||||
type RoleType string
|
||||
|
||||
type Collaborator struct {
|
||||
Id int64
|
||||
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
|
||||
ForAccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"` // The account being given access to
|
||||
Id int64
|
||||
AccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"`
|
||||
Role RoleType `xorm:"not null"`
|
||||
CollaboratorId int64 `xorm:"not null unique(uix_account_id_for_account_id)"`
|
||||
|
||||
Created time.Time
|
||||
Updated time.Time
|
||||
}
|
||||
|
||||
type AddCollaboratorCommand struct {
|
||||
Email string `json:"email" binding:"required"`
|
||||
AccountId int64 `json:"-"`
|
||||
ForAccountId int64 `json:"-"`
|
||||
Role RoleType `json:"-"`
|
||||
type RemoveCollaboratorCommand struct {
|
||||
CollaboratorId int64
|
||||
AccountId int64
|
||||
}
|
||||
|
||||
func NewCollaborator(accountId int64, forAccountId int64, role RoleType) *Collaborator {
|
||||
type AddCollaboratorCommand struct {
|
||||
Email string `json:"email" binding:"required"`
|
||||
AccountId int64 `json:"-"`
|
||||
CollaboratorId int64 `json:"-"`
|
||||
Role RoleType `json:"-"`
|
||||
}
|
||||
|
||||
func NewCollaborator(accountId int64, collaboratorId int64, role RoleType) *Collaborator {
|
||||
return &Collaborator{
|
||||
AccountId: accountId,
|
||||
ForAccountId: forAccountId,
|
||||
Role: role,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
AccountId: accountId,
|
||||
CollaboratorId: collaboratorId,
|
||||
Role: role,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
}
|
||||
|
@ -11,13 +11,14 @@ import (
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetAccountInfo)
|
||||
bus.AddHandler("sql", AddCollaborator)
|
||||
bus.AddHandler("sql", GetOtherAccounts)
|
||||
bus.AddHandler("sql", CreateAccount)
|
||||
bus.AddHandler("sql", SetUsingAccount)
|
||||
bus.AddHandler("sql", GetAccountById)
|
||||
bus.AddHandler("sql", GetAccountByLogin)
|
||||
bus.AddHandler("sql", GetAccountByToken)
|
||||
bus.AddHandler("sql", AddCollaborator)
|
||||
bus.AddHandler("sql", RemoveCollaborator)
|
||||
}
|
||||
|
||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
@ -66,8 +67,8 @@ func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
||||
}
|
||||
|
||||
sess := x.Table("collaborator")
|
||||
sess.Join("INNER", "account", "account.id=collaborator.account_Id")
|
||||
sess.Where("collaborator.for_account_id=?", query.Id)
|
||||
sess.Join("INNER", "account", "account.id=collaborator.collaborator_id")
|
||||
sess.Where("collaborator.account_id=?", query.Id)
|
||||
err = sess.Find(&query.Result.Collaborators)
|
||||
|
||||
return err
|
||||
@ -77,11 +78,11 @@ 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(),
|
||||
AccountId: cmd.AccountId,
|
||||
CollaboratorId: cmd.CollaboratorId,
|
||||
Role: cmd.Role,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
@ -116,7 +117,7 @@ func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
|
||||
var account m.Account
|
||||
sess := x.Join("INNER", "token", "token.account_id = account.id")
|
||||
sess.Omit("token.id", "token.account_id", "token.name", "token.token",
|
||||
"token.role", "token.updated", "token.created")
|
||||
"token.role", "token.updated", "token.created")
|
||||
has, err := sess.Where("token.token=?", query.Token).Get(&account)
|
||||
|
||||
if err != nil {
|
||||
@ -155,12 +156,20 @@ func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
|
||||
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 {
|
||||
query.Result = make([]*m.OtherAccountDTO, 0)
|
||||
sess := x.Table("collaborator")
|
||||
sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
|
||||
sess.Where("account_id=?", query.AccountId)
|
||||
sess.Cols("collaborator.id", "collaborator.role", "account.email")
|
||||
sess.Join("INNER", "account", "collaborator.account_id=account.id")
|
||||
sess.Where("collaborator_id=?", query.AccountId)
|
||||
sess.Cols("collaborator.account_id", "collaborator.role", "account.email")
|
||||
err := sess.Find(&query.Result)
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user