User / Account model split, User and account now seperate entities, collaborators are now AccountUsers

This commit is contained in:
Torkel Ödegaard
2015-01-19 18:01:04 +01:00
parent f1996a9f1f
commit 90925273a0
29 changed files with 592 additions and 705 deletions

86
pkg/api/user.go Normal file
View File

@@ -0,0 +1,86 @@
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 GetUser(c *middleware.Context) {
query := m.GetUserInfoQuery{UserId: c.UserId}
if err := bus.Dispatch(&query); err != nil {
c.JsonApiErr(500, "Failed to get account", err)
return
}
c.JSON(200, query.Result)
}
func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) {
cmd.UserId = c.UserId
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(400, "Failed to update account", err)
return
}
c.JsonOK("Account updated")
}
func GetUserAccounts(c *middleware.Context) {
query := m.GetUserAccountsQuery{UserId: c.UserId}
if err := bus.Dispatch(&query); err != nil {
c.JsonApiErr(500, "Failed to get user accounts", err)
return
}
for _, ac := range query.Result {
if ac.AccountId == c.AccountId {
ac.IsUsing = true
break
}
}
c.JSON(200, query.Result)
}
func validateUsingAccount(userId int64, accountId int64) bool {
query := m.GetUserAccountsQuery{UserId: userId}
if err := bus.Dispatch(&query); err != nil {
return false
}
// validate that the account id in the list
valid := false
for _, other := range query.Result {
if other.AccountId == accountId {
valid = true
}
}
return valid
}
func SetUsingAccount(c *middleware.Context) {
usingAccountId := c.ParamsInt64(":id")
if !validateUsingAccount(c.AccountId, usingAccountId) {
c.JsonApiErr(401, "Not a valid account", nil)
return
}
cmd := m.SetUsingAccountCommand{
UserId: c.UserId,
AccountId: usingAccountId,
}
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed change active account", err)
return
}
c.JsonOK("Active account changed")
}