Commit 9feb8a73 by Torkel Ödegaard

Added binding to tokens api and role validation

parent f858f6b6
Subproject commit 500e00066139b861a2898db6ef80ef87b8b8daa6
Subproject commit d3cc6e518dfd2ceb26c0e568bc603b4473b11a02
......@@ -46,7 +46,10 @@ func Register(r *macaron.Macaron) {
})
// Token
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)
})
// Data sources
......
......@@ -7,6 +7,10 @@ import (
)
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}
err := bus.Dispatch(&userQuery)
......
......@@ -41,19 +41,12 @@ func DeleteToken(c *middleware.Context) {
c.JsonOK("Token deleted")
}
func AddToken(c *middleware.Context) {
cmd := m.AddTokenCommand{}
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation failed", nil)
func AddToken(c *middleware.Context, cmd m.AddTokenCommand) {
if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil)
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.Token = util.GetRandomString(64)
......@@ -61,20 +54,20 @@ func AddToken(c *middleware.Context) {
c.JsonApiErr(500, "Failed to add token", err)
return
}
result := &m.TokenDTO{
Id: cmd.Result.Id,
Name: cmd.Result.Name,
Role: cmd.Result.Role,
Token: cmd.Result.Token,
}
c.JSON(200, result)
}
func UpdateToken(c *middleware.Context) {
cmd := m.UpdateTokenCommand{}
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation failed", nil)
func UpdateToken(c *middleware.Context, cmd m.UpdateTokenCommand) {
if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil)
return
}
......
......@@ -19,12 +19,8 @@ const (
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
func (r RoleType) IsValid() bool {
return r == ROLE_VIEWER || r == ROLE_ADMIN || r == ROLE_EDITOR
}
type Collaborator struct {
......
......@@ -25,11 +25,12 @@ type AddTokenCommand struct {
}
type UpdateTokenCommand struct {
Id int64 `json:"id"`
Name string `json:"name"`
AccountId int64 `json:"-"`
Role RoleType `json:"role"`
Result *Token `json:"-"`
Id int64 `json:"id"`
Name string `json:"name"`
Role RoleType `json:"role"`
AccountId int64 `json:"-"`
Result *Token `json:"-"`
}
type DeleteTokenCommand struct {
......
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