Commit 6f63d63e by Torkel Ödegaard

Working on account collaborators

parent 50a5355d
Subproject commit 733a9af6294c87acc72d1a519791e2be7d0f594f Subproject commit 961ebbde6b6540f03d3fb5a1741722614166099f
...@@ -4,7 +4,6 @@ import ( ...@@ -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) { ...@@ -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) { ...@@ -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 { ...@@ -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) { ...@@ -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,16 +72,16 @@ type GetAccountByLoginQuery struct { ...@@ -72,16 +72,16 @@ type GetAccountByLoginQuery struct {
// DTO & Projections // DTO & Projections
type OtherAccountDTO struct { 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"` AccountId int64 `json:"accountId"`
Email string `json:"email"` Email string `json:"email"`
Role string `json:"role"` 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 { type AccountDTO struct {
......
...@@ -12,28 +12,33 @@ const ( ...@@ -12,28 +12,33 @@ const (
type RoleType string 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 ( ...@@ -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 { ...@@ -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
...@@ -77,11 +78,11 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error { ...@@ -77,11 +78,11 @@ func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
return inTransaction(func(sess *xorm.Session) error { return inTransaction(func(sess *xorm.Session) 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(),
} }
_, err := sess.Insert(&entity) _, err := sess.Insert(&entity)
...@@ -116,7 +117,7 @@ func GetAccountByToken(query *m.GetAccountByTokenQuery) error { ...@@ -116,7 +117,7 @@ func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
var account m.Account var account m.Account
sess := x.Join("INNER", "token", "token.account_id = account.id") sess := x.Join("INNER", "token", "token.account_id = account.id")
sess.Omit("token.id", "token.account_id", "token.name", "token.token", 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) has, err := sess.Where("token.token=?", query.Token).Get(&account)
if err != nil { if err != nil {
...@@ -155,12 +156,20 @@ func GetAccountByLogin(query *m.GetAccountByLoginQuery) error { ...@@ -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
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment