worked on account creation

This commit is contained in:
Torkel Ödegaard 2015-01-20 16:29:48 +01:00
parent eec178458b
commit 9e6df378c3
5 changed files with 38 additions and 3 deletions

@ -1 +1 @@
Subproject commit d9a33680a686ac6bc1239797278ad03b3c784d1c
Subproject commit 9667f324f1db66f7cdfa7ce5b6c9b38c92f41d53

18
pkg/api/account.go Normal file
View File

@ -0,0 +1,18 @@
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 CreateAccount(c *middleware.Context, cmd m.CreateAccountCommand) {
cmd.UserId = c.UserId
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to create account", nil)
return
}
c.JsonOK("Account created")
}

View File

@ -49,6 +49,7 @@ func Register(r *macaron.Macaron) {
// account
r.Group("/account", func() {
r.Put("/", bind(m.CreateAccountCommand{}), CreateAccount)
r.Put("/users", bind(m.AddAccountUserCommand{}), AddAccountUser)
r.Get("/users", GetAccountUsers)
r.Delete("/users/:id", RemoveAccountUser)

View File

@ -21,8 +21,10 @@ type Account struct {
// COMMANDS
type CreateAccountCommand struct {
Name string `json:"name"`
Name string `json:"name" binding:"Required"`
// initial admin user for account
UserId int64 `json:"-"`
Result Account `json:"-"`
}

View File

@ -24,8 +24,22 @@ func CreateAccount(cmd *m.CreateAccountCommand) error {
Updated: time.Now(),
}
_, err := sess.Insert(&account)
if _, err := sess.Insert(&account); err != nil {
return err
}
// create inital admin account user
user := m.AccountUser{
AccountId: account.Id,
UserId: cmd.UserId,
Role: m.ROLE_ADMIN,
Created: time.Now(),
Updated: time.Now(),
}
_, err := sess.Insert(&user)
cmd.Result = account
return err
})
}