diff --git a/grafana b/grafana index 733a9af6294..961ebbde6b6 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit 733a9af6294c87acc72d1a519791e2be7d0f594f +Subproject commit 961ebbde6b6540f03d3fb5a1741722614166099f diff --git a/pkg/api/account.go b/pkg/api/account.go index 778bda40fc4..5a871ac852d 100644 --- a/pkg/api/account.go +++ b/pkg/api/account.go @@ -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 } } diff --git a/pkg/api/api.go b/pkg/api/api.go index 01f8befbfd7..5d1898e33e0 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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) }) diff --git a/pkg/models/account.go b/pkg/models/account.go index 9ba74f0c7f7..1172d2740c5 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -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 { diff --git a/pkg/models/collaborator.go b/pkg/models/collaborator.go index 39ffaeefa15..7babd1e95d6 100644 --- a/pkg/models/collaborator.go +++ b/pkg/models/collaborator.go @@ -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(), } } diff --git a/pkg/stores/sqlstore/accounts.go b/pkg/stores/sqlstore/accounts.go index 07a85fdaef6..9072f0cc2db 100644 --- a/pkg/stores/sqlstore/accounts.go +++ b/pkg/stores/sqlstore/accounts.go @@ -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 }