Commit f858f6b6 by Torkel Ödegaard

Add collaborator now handles role, added macaron-contrib/binding for binding and validation

parent cd5843e9
Subproject commit 9d1dacb8d417cac2cc66797e5dae6deba36c3080 Subproject commit 500e00066139b861a2898db6ef80ef87b8b8daa6
...@@ -2,76 +2,79 @@ package api ...@@ -2,76 +2,79 @@ package api
import ( import (
"github.com/Unknwon/macaron" "github.com/Unknwon/macaron"
"github.com/macaron-contrib/binding"
"github.com/torkelo/grafana-pro/pkg/api/dtos" "github.com/torkelo/grafana-pro/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/middleware" "github.com/torkelo/grafana-pro/pkg/middleware"
m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting" "github.com/torkelo/grafana-pro/pkg/setting"
) )
// Register adds http routes // Register adds http routes
func Register(m *macaron.Macaron) { func Register(r *macaron.Macaron) {
reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true}) reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
reqAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqAdmin: true}) reqAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqAdmin: true})
bind := binding.Bind
// not logged in views // not logged in views
m.Get("/", reqSignedIn, Index) r.Get("/", reqSignedIn, Index)
m.Post("/logout", LogoutPost) r.Post("/logout", LogoutPost)
m.Post("/login", LoginPost) r.Post("/login", LoginPost)
m.Get("/login/:name", OAuthLogin) r.Get("/login/:name", OAuthLogin)
m.Get("/login", Index) r.Get("/login", Index)
// authed views // authed views
m.Get("/account/", reqSignedIn, Index) r.Get("/account/", reqSignedIn, Index)
m.Get("/account/datasources/", reqSignedIn, Index) r.Get("/account/datasources/", reqSignedIn, Index)
m.Get("/admin", reqSignedIn, Index) r.Get("/admin", reqSignedIn, Index)
m.Get("/dashboard/*", reqSignedIn, Index) r.Get("/dashboard/*", reqSignedIn, Index)
// sign up // sign up
m.Get("/signup", Index) r.Get("/signup", Index)
m.Post("/api/account/signup", SignUp) r.Post("/api/account/signup", SignUp)
// authed api // authed api
m.Group("/api", func() { r.Group("/api", func() {
// account // account
m.Group("/account", func() { r.Group("/account", func() {
m.Get("/", GetAccount) r.Get("/", GetAccount)
m.Post("/", UpdateAccount) r.Post("/", UpdateAccount)
m.Put("/collaborators", AddCollaborator) r.Put("/collaborators", bind(m.AddCollaboratorCommand{}), AddCollaborator)
m.Get("/collaborators", GetCollaborators) r.Get("/collaborators", GetCollaborators)
m.Delete("/collaborators/:id", RemoveCollaborator) r.Delete("/collaborators/:id", RemoveCollaborator)
m.Post("/using/:id", SetUsingAccount) r.Post("/using/:id", SetUsingAccount)
m.Get("/others", GetOtherAccounts) r.Get("/others", GetOtherAccounts)
}) })
// Token // Token
m.Group("/tokens", func() { r.Group("/tokens", func() {
m.Combo("/").Get(GetTokens).Put(AddToken).Post(UpdateToken) r.Combo("/").Get(GetTokens).Put(AddToken).Post(UpdateToken)
m.Delete("/:id", DeleteToken) r.Delete("/:id", DeleteToken)
}) })
// Data sources // Data sources
m.Group("/datasources", func() { r.Group("/datasources", func() {
m.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource) r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
m.Delete("/:id", DeleteDataSource) r.Delete("/:id", DeleteDataSource)
m.Any("/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest) r.Any("/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
}) })
// Dashboard // Dashboard
m.Group("/dashboard", func() { r.Group("/dashboard", func() {
m.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard) r.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard)
m.Post("/", PostDashboard) r.Post("/", PostDashboard)
}) })
// Search // Search
m.Get("/search/", Search) r.Get("/search/", Search)
// metrics // metrics
m.Get("/metrics/test", GetTestMetrics) r.Get("/metrics/test", GetTestMetrics)
}, reqSignedIn) }, reqSignedIn)
// admin api // admin api
m.Group("/api/admin", func() { r.Group("/api/admin", func() {
m.Get("/accounts", AdminSearchAccounts) r.Get("/accounts", AdminSearchAccounts)
}, reqAdmin) }, reqAdmin)
// rendering // rendering
m.Get("/render/*", reqSignedIn, RenderToPng) r.Get("/render/*", reqSignedIn, RenderToPng)
m.NotFound(NotFound) r.NotFound(NotFound)
} }
func setIndexViewData(c *middleware.Context) error { func setIndexViewData(c *middleware.Context) error {
......
...@@ -6,15 +6,9 @@ import ( ...@@ -6,15 +6,9 @@ import (
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
) )
func AddCollaborator(c *middleware.Context) { func AddCollaborator(c *middleware.Context, cmd m.AddCollaboratorCommand) {
var cmd m.AddCollaboratorCommand
if !c.JsonBody(&cmd) { userQuery := m.GetAccountByLoginQuery{LoginOrEmail: cmd.LoginOrEmail}
c.JsonApiErr(400, "Invalid request", nil)
return
}
userQuery := m.GetAccountByLoginQuery{Login: cmd.Email}
err := bus.Dispatch(&userQuery) err := bus.Dispatch(&userQuery)
if err != nil { if err != nil {
c.JsonApiErr(404, "Collaborator not found", nil) c.JsonApiErr(404, "Collaborator not found", nil)
...@@ -30,7 +24,6 @@ func AddCollaborator(c *middleware.Context) { ...@@ -30,7 +24,6 @@ func AddCollaborator(c *middleware.Context) {
cmd.AccountId = c.UserAccount.Id cmd.AccountId = c.UserAccount.Id
cmd.CollaboratorId = accountToAdd.Id cmd.CollaboratorId = accountToAdd.Id
cmd.Role = m.ROLE_READ_WRITE
err = bus.Dispatch(&cmd) err = bus.Dispatch(&cmd)
if err != nil { if err != nil {
......
...@@ -23,7 +23,7 @@ func LoginPost(c *middleware.Context) { ...@@ -23,7 +23,7 @@ func LoginPost(c *middleware.Context) {
return return
} }
userQuery := m.GetAccountByLoginQuery{Login: loginModel.Email} userQuery := m.GetAccountByLoginQuery{LoginOrEmail: loginModel.Email}
err := bus.Dispatch(&userQuery) err := bus.Dispatch(&userQuery)
if err != nil { if err != nil {
......
...@@ -51,7 +51,7 @@ func OAuthLogin(ctx *middleware.Context) { ...@@ -51,7 +51,7 @@ func OAuthLogin(ctx *middleware.Context) {
log.Info("login.OAuthLogin(social login): %s", userInfo) log.Info("login.OAuthLogin(social login): %s", userInfo)
userQuery := m.GetAccountByLoginQuery{Login: userInfo.Email} userQuery := m.GetAccountByLoginQuery{LoginOrEmail: userInfo.Email}
err = bus.Dispatch(&userQuery) err = bus.Dispatch(&userQuery)
// create account if missing // create account if missing
......
...@@ -49,10 +49,10 @@ func AddToken(c *middleware.Context) { ...@@ -49,10 +49,10 @@ func AddToken(c *middleware.Context) {
return return
} }
if cmd.Role != m.ROLE_READ_WRITE && cmd.Role != m.ROLE_READ { // if cmd.Role != m.ROLE_READ_WRITE && cmd.Role != m.ROLE_READ {
c.JsonApiErr(400, "Invalid role specified", nil) // c.JsonApiErr(400, "Invalid role specified", nil)
return // return
} // }
cmd.AccountId = c.Account.Id cmd.AccountId = c.Account.Id
cmd.Token = util.GetRandomString(64) cmd.Token = util.GetRandomString(64)
......
...@@ -74,8 +74,8 @@ type GetAccountByIdQuery struct { ...@@ -74,8 +74,8 @@ type GetAccountByIdQuery struct {
} }
type GetAccountByLoginQuery struct { type GetAccountByLoginQuery struct {
Login string LoginOrEmail string
Result *Account Result *Account
} }
type SearchAccountsQuery struct { type SearchAccountsQuery struct {
......
package models package models
import ( import (
"errors"
"time" "time"
) )
const ( // Typed errors
ROLE_READ_WRITE RoleType = "ReadWrite" var (
ROLE_READ = "Read" ErrInvalidRoleType = errors.New("Invalid role type")
) )
type RoleType string type RoleType string
const (
ROLE_OWNER RoleType = "Owner"
ROLE_VIEWER RoleType = "Viewer"
ROLE_EDITOR RoleType = "Editor"
ROLE_ADMIN RoleType = "Admin"
)
func (r RoleType) Validate() error {
if r == ROLE_OWNER || r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR {
return nil
}
return ErrInvalidRoleType
}
type Collaborator struct { type Collaborator struct {
Id int64 Id int64
AccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"` AccountId int64 `xorm:"not null unique(uix_account_id_for_account_id)"`
...@@ -21,16 +37,6 @@ type Collaborator struct { ...@@ -21,16 +37,6 @@ 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 // COMMANDS
...@@ -40,10 +46,10 @@ type RemoveCollaboratorCommand struct { ...@@ -40,10 +46,10 @@ type RemoveCollaboratorCommand struct {
} }
type AddCollaboratorCommand struct { type AddCollaboratorCommand struct {
Email string `json:"email" binding:"required"` LoginOrEmail string `json:"loginOrEmail" binding:"Required"`
Role RoleType `json:"role" binding:"Required"`
AccountId int64 `json:"-"` AccountId int64 `json:"-"`
CollaboratorId int64 `json:"-"` CollaboratorId int64 `json:"-"`
Role RoleType `json:"-"`
} }
// ---------------------- // ----------------------
......
...@@ -134,11 +134,15 @@ func GetAccountByToken(query *m.GetAccountByTokenQuery) error { ...@@ -134,11 +134,15 @@ func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
} }
func GetAccountByLogin(query *m.GetAccountByLoginQuery) error { func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
if query.LoginOrEmail == "" {
return m.ErrAccountNotFound
}
account := new(m.Account) account := new(m.Account)
if strings.Contains(query.Login, "@") { if strings.Contains(query.LoginOrEmail, "@") {
account = &m.Account{Email: query.Login} account = &m.Account{Email: query.LoginOrEmail}
} else { } else {
account = &m.Account{Login: strings.ToLower(query.Login)} account = &m.Account{Login: strings.ToLower(query.LoginOrEmail)}
} }
has, err := x.Get(account) has, err := x.Get(account)
......
...@@ -46,7 +46,7 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -46,7 +46,7 @@ func TestAccountDataAccess(t *testing.T) {
cmd := m.AddCollaboratorCommand{ cmd := m.AddCollaboratorCommand{
AccountId: ac1.Id, AccountId: ac1.Id,
CollaboratorId: ac2.Id, CollaboratorId: ac2.Id,
Role: m.ROLE_READ_WRITE, Role: m.ROLE_VIEWER,
} }
err := AddCollaborator(&cmd) err := AddCollaborator(&cmd)
......
...@@ -45,7 +45,7 @@ func init() { ...@@ -45,7 +45,7 @@ func init() {
} }
func EnsureAdminUser() { func EnsureAdminUser() {
adminQuery := m.GetAccountByLoginQuery{Login: setting.AdminUser} adminQuery := m.GetAccountByLoginQuery{LoginOrEmail: setting.AdminUser}
if err := bus.Dispatch(&adminQuery); err == m.ErrAccountNotFound { if err := bus.Dispatch(&adminQuery); err == m.ErrAccountNotFound {
cmd := m.CreateAccountCommand{} cmd := m.CreateAccountCommand{}
......
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