Added binding to tokens api and role validation

This commit is contained in:
Torkel Ödegaard 2015-01-16 12:06:24 +01:00
parent f858f6b621
commit 9feb8a73fd
6 changed files with 25 additions and 28 deletions

@ -1 +1 @@
Subproject commit 500e00066139b861a2898db6ef80ef87b8b8daa6 Subproject commit d3cc6e518dfd2ceb26c0e568bc603b4473b11a02

View File

@ -46,7 +46,10 @@ func Register(r *macaron.Macaron) {
}) })
// Token // Token
r.Group("/tokens", func() { r.Group("/tokens", func() {
r.Combo("/").Get(GetTokens).Put(AddToken).Post(UpdateToken) r.Combo("/").
Get(GetTokens).
Put(bind(m.AddTokenCommand{}), AddToken).
Post(bind(m.UpdateTokenCommand{}), UpdateToken)
r.Delete("/:id", DeleteToken) r.Delete("/:id", DeleteToken)
}) })
// Data sources // Data sources

View File

@ -7,6 +7,10 @@ import (
) )
func AddCollaborator(c *middleware.Context, cmd m.AddCollaboratorCommand) { func AddCollaborator(c *middleware.Context, cmd m.AddCollaboratorCommand) {
if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil)
return
}
userQuery := m.GetAccountByLoginQuery{LoginOrEmail: cmd.LoginOrEmail} userQuery := m.GetAccountByLoginQuery{LoginOrEmail: cmd.LoginOrEmail}
err := bus.Dispatch(&userQuery) err := bus.Dispatch(&userQuery)

View File

@ -41,19 +41,12 @@ func DeleteToken(c *middleware.Context) {
c.JsonOK("Token deleted") c.JsonOK("Token deleted")
} }
func AddToken(c *middleware.Context) { func AddToken(c *middleware.Context, cmd m.AddTokenCommand) {
cmd := m.AddTokenCommand{} if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil)
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation failed", nil)
return return
} }
// if cmd.Role != m.ROLE_READ_WRITE && cmd.Role != m.ROLE_READ {
// c.JsonApiErr(400, "Invalid role specified", nil)
// return
// }
cmd.AccountId = c.Account.Id cmd.AccountId = c.Account.Id
cmd.Token = util.GetRandomString(64) cmd.Token = util.GetRandomString(64)
@ -61,20 +54,20 @@ func AddToken(c *middleware.Context) {
c.JsonApiErr(500, "Failed to add token", err) c.JsonApiErr(500, "Failed to add token", err)
return return
} }
result := &m.TokenDTO{ result := &m.TokenDTO{
Id: cmd.Result.Id, Id: cmd.Result.Id,
Name: cmd.Result.Name, Name: cmd.Result.Name,
Role: cmd.Result.Role, Role: cmd.Result.Role,
Token: cmd.Result.Token, Token: cmd.Result.Token,
} }
c.JSON(200, result) c.JSON(200, result)
} }
func UpdateToken(c *middleware.Context) { func UpdateToken(c *middleware.Context, cmd m.UpdateTokenCommand) {
cmd := m.UpdateTokenCommand{} if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil)
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation failed", nil)
return return
} }

View File

@ -19,12 +19,8 @@ const (
ROLE_ADMIN RoleType = "Admin" ROLE_ADMIN RoleType = "Admin"
) )
func (r RoleType) Validate() error { func (r RoleType) IsValid() bool {
if r == ROLE_OWNER || r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR { return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR
return nil
}
return ErrInvalidRoleType
} }
type Collaborator struct { type Collaborator struct {

View File

@ -27,8 +27,9 @@ type AddTokenCommand struct {
type UpdateTokenCommand struct { type UpdateTokenCommand struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Name string `json:"name"` Name string `json:"name"`
AccountId int64 `json:"-"`
Role RoleType `json:"role"` Role RoleType `json:"role"`
AccountId int64 `json:"-"`
Result *Token `json:"-"` Result *Token `json:"-"`
} }