mirror of https://github.com/grafana/grafana
parent
804bff55ec
commit
04bbdbad12
@ -1 +1 @@ |
||||
Subproject commit 5b93e09714dbee6c1c181daf0182704334d8c6be |
||||
Subproject commit 9d1dacb8d417cac2cc66797e5dae6deba36c3080 |
@ -0,0 +1,64 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"github.com/torkelo/grafana-pro/pkg/bus" |
||||
"github.com/torkelo/grafana-pro/pkg/middleware" |
||||
m "github.com/torkelo/grafana-pro/pkg/models" |
||||
) |
||||
|
||||
func AddCollaborator(c *middleware.Context) { |
||||
var cmd m.AddCollaboratorCommand |
||||
|
||||
if !c.JsonBody(&cmd) { |
||||
c.JsonApiErr(400, "Invalid request", nil) |
||||
return |
||||
} |
||||
|
||||
userQuery := m.GetAccountByLoginQuery{Login: cmd.Email} |
||||
err := bus.Dispatch(&userQuery) |
||||
if err != nil { |
||||
c.JsonApiErr(404, "Collaborator not found", nil) |
||||
return |
||||
} |
||||
|
||||
accountToAdd := userQuery.Result |
||||
|
||||
if accountToAdd.Id == c.UserAccount.Id { |
||||
c.JsonApiErr(400, "Cannot add yourself as collaborator", nil) |
||||
return |
||||
} |
||||
|
||||
cmd.AccountId = c.UserAccount.Id |
||||
cmd.CollaboratorId = accountToAdd.Id |
||||
cmd.Role = m.ROLE_READ_WRITE |
||||
|
||||
err = bus.Dispatch(&cmd) |
||||
if err != nil { |
||||
c.JsonApiErr(500, "Could not add collaborator", err) |
||||
return |
||||
} |
||||
|
||||
c.JsonOK("Collaborator added") |
||||
} |
||||
|
||||
func GetCollaborators(c *middleware.Context) { |
||||
query := m.GetCollaboratorsQuery{AccountId: c.UserAccount.Id} |
||||
if err := bus.Dispatch(&query); err != nil { |
||||
c.JsonApiErr(500, "Failed to get collaborators", err) |
||||
return |
||||
} |
||||
|
||||
c.JSON(200, query.Result) |
||||
} |
||||
|
||||
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") |
||||
} |
@ -0,0 +1,51 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"github.com/go-xorm/xorm" |
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus" |
||||
m "github.com/torkelo/grafana-pro/pkg/models" |
||||
) |
||||
|
||||
func init() { |
||||
bus.AddHandler("sql", AddCollaborator) |
||||
bus.AddHandler("sql", RemoveCollaborator) |
||||
bus.AddHandler("sql", GetCollaborators) |
||||
} |
||||
|
||||
func AddCollaborator(cmd *m.AddCollaboratorCommand) error { |
||||
return inTransaction(func(sess *xorm.Session) error { |
||||
|
||||
entity := m.Collaborator{ |
||||
AccountId: cmd.AccountId, |
||||
CollaboratorId: cmd.CollaboratorId, |
||||
Role: cmd.Role, |
||||
Created: time.Now(), |
||||
Updated: time.Now(), |
||||
} |
||||
|
||||
_, err := sess.Insert(&entity) |
||||
return err |
||||
}) |
||||
} |
||||
|
||||
func GetCollaborators(query *m.GetCollaboratorsQuery) error { |
||||
query.Result = make([]*m.CollaboratorDTO, 0) |
||||
sess := x.Table("collaborator") |
||||
sess.Join("INNER", "account", "collaborator.collaborator_id=account.id") |
||||
sess.Where("collaborator.account_id=?", query.AccountId) |
||||
sess.Cols("collaborator.collaborator_id", "collaborator.role", "account.email", "account.login") |
||||
|
||||
err := sess.Find(&query.Result) |
||||
return err |
||||
} |
||||
|
||||
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 |
||||
}) |
||||
} |
Loading…
Reference in new issue