mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Worked on account update, moved collaborats to its own api url and files
This commit is contained in:
parent
804bff55ec
commit
04bbdbad12
2
grafana
2
grafana
@ -1 +1 @@
|
|||||||
Subproject commit 5b93e09714dbee6c1c181daf0182704334d8c6be
|
Subproject commit 9d1dacb8d417cac2cc66797e5dae6deba36c3080
|
@ -18,51 +18,22 @@ func GetAccount(c *middleware.Context) {
|
|||||||
c.JSON(200, query.Result)
|
c.JSON(200, query.Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddCollaborator(c *middleware.Context) {
|
func UpdateAccount(c *middleware.Context) {
|
||||||
var cmd m.AddCollaboratorCommand
|
cmd := m.UpdateAccountCommand{}
|
||||||
|
|
||||||
if !c.JsonBody(&cmd) {
|
if !c.JsonBody(&cmd) {
|
||||||
c.JsonApiErr(400, "Invalid request", nil)
|
c.JsonApiErr(400, "Invalid request", nil)
|
||||||
return
|
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.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 RemoveCollaborator(c *middleware.Context) {
|
|
||||||
collaboratorId := c.ParamsInt64(":id")
|
|
||||||
|
|
||||||
cmd := m.RemoveCollaboratorCommand{AccountId: c.UserAccount.Id, CollaboratorId: collaboratorId}
|
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
c.JsonApiErr(500, "Failed to remove collaborator", err)
|
c.JsonApiErr(400, "Failed to update account", nil)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JsonOK("Collaborator removed")
|
c.JsonOK("Account updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOtherAccounts(c *middleware.Context) {
|
func GetOtherAccounts(c *middleware.Context) {
|
||||||
|
@ -34,7 +34,9 @@ func Register(m *macaron.Macaron) {
|
|||||||
// account
|
// account
|
||||||
m.Group("/account", func() {
|
m.Group("/account", func() {
|
||||||
m.Get("/", GetAccount)
|
m.Get("/", GetAccount)
|
||||||
|
m.Post("/", UpdateAccount)
|
||||||
m.Put("/collaborators", AddCollaborator)
|
m.Put("/collaborators", AddCollaborator)
|
||||||
|
m.Get("/collaborators", GetCollaborators)
|
||||||
m.Delete("/collaborators/:id", RemoveCollaborator)
|
m.Delete("/collaborators/:id", RemoveCollaborator)
|
||||||
m.Post("/using/:id", SetUsingAccount)
|
m.Post("/using/:id", SetUsingAccount)
|
||||||
m.Get("/others", GetOtherAccounts)
|
m.Get("/others", GetOtherAccounts)
|
||||||
|
64
pkg/api/collaborators.go
Normal file
64
pkg/api/collaborators.go
Normal file
@ -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")
|
||||||
|
}
|
@ -42,6 +42,14 @@ type CreateAccountCommand struct {
|
|||||||
Result Account `json:"-"`
|
Result Account `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateAccountCommand struct {
|
||||||
|
Email string `json:"email" binding:"required"`
|
||||||
|
Login string `json:"login"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
|
||||||
|
AccountId int64 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
type SetUsingAccountCommand struct {
|
type SetUsingAccountCommand struct {
|
||||||
AccountId int64
|
AccountId int64
|
||||||
UsingAccountId int64
|
UsingAccountId int64
|
||||||
@ -88,12 +96,6 @@ type OtherAccountDTO struct {
|
|||||||
IsUsing bool `json:"isUsing"`
|
IsUsing bool `json:"isUsing"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollaboratorDTO struct {
|
|
||||||
CollaboratorId int64 `json:"id"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
Role string `json:"role"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AccountSearchHitDTO struct {
|
type AccountSearchHitDTO struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -103,7 +105,7 @@ type AccountSearchHitDTO struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AccountDTO struct {
|
type AccountDTO struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Collaborators []*CollaboratorDTO `json:"collaborators"`
|
Login string `json:"login"`
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,19 @@ type Collaborator struct {
|
|||||||
Updated time.Time
|
Updated time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewCollaborator(accountId int64, collaboratorId int64, role RoleType) *Collaborator {
|
||||||
|
return &Collaborator{
|
||||||
|
AccountId: accountId,
|
||||||
|
CollaboratorId: collaboratorId,
|
||||||
|
Role: role,
|
||||||
|
Created: time.Now(),
|
||||||
|
Updated: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------
|
||||||
|
// COMMANDS
|
||||||
|
|
||||||
type RemoveCollaboratorCommand struct {
|
type RemoveCollaboratorCommand struct {
|
||||||
CollaboratorId int64
|
CollaboratorId int64
|
||||||
AccountId int64
|
AccountId int64
|
||||||
@ -33,12 +46,20 @@ type AddCollaboratorCommand struct {
|
|||||||
Role RoleType `json:"-"`
|
Role RoleType `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCollaborator(accountId int64, collaboratorId int64, role RoleType) *Collaborator {
|
// ----------------------
|
||||||
return &Collaborator{
|
// QUERIES
|
||||||
AccountId: accountId,
|
|
||||||
CollaboratorId: collaboratorId,
|
type GetCollaboratorsQuery struct {
|
||||||
Role: role,
|
AccountId int64
|
||||||
Created: time.Now(),
|
Result []*CollaboratorDTO
|
||||||
Updated: time.Now(),
|
}
|
||||||
}
|
|
||||||
|
// ----------------------
|
||||||
|
// Projections and DTOs
|
||||||
|
|
||||||
|
type CollaboratorDTO struct {
|
||||||
|
CollaboratorId int64 `json:"id"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Login string `json:"login"`
|
||||||
|
Role string `json:"role"`
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,8 @@ func init() {
|
|||||||
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)
|
|
||||||
bus.AddHandler("sql", SearchAccounts)
|
bus.AddHandler("sql", SearchAccounts)
|
||||||
|
bus.AddHandler("sql", UpdateAccount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||||
@ -44,6 +43,21 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateAccount(cmd *m.UpdateAccountCommand) error {
|
||||||
|
return inTransaction(func(sess *xorm.Session) error {
|
||||||
|
|
||||||
|
account := m.Account{
|
||||||
|
Email: cmd.Email,
|
||||||
|
Login: cmd.Login,
|
||||||
|
Name: cmd.Name,
|
||||||
|
Updated: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := sess.Id(cmd.AccountId).Update(&account)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
|
||||||
return inTransaction(func(sess *xorm.Session) error {
|
return inTransaction(func(sess *xorm.Session) error {
|
||||||
account := m.Account{}
|
account := m.Account{}
|
||||||
@ -66,35 +80,14 @@ func GetAccountInfo(query *m.GetAccountInfoQuery) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query.Result = m.AccountDTO{
|
query.Result = m.AccountDTO{
|
||||||
Name: account.Name,
|
Name: account.Name,
|
||||||
Email: account.Email,
|
Email: account.Email,
|
||||||
Collaborators: make([]*m.CollaboratorDTO, 0),
|
Login: account.Login,
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := x.Table("collaborator")
|
|
||||||
sess.Join("INNER", "account", "account.id=collaborator.collaborator_id")
|
|
||||||
sess.Where("collaborator.account_id=?", query.Id)
|
|
||||||
err = sess.Find(&query.Result.Collaborators)
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
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 GetAccountById(query *m.GetAccountByIdQuery) error {
|
func GetAccountById(query *m.GetAccountByIdQuery) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@ -165,14 +158,6 @@ 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")
|
||||||
|
51
pkg/services/sqlstore/collaborators.go
Normal file
51
pkg/services/sqlstore/collaborators.go
Normal file
@ -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
Block a user