Added binding to tokens api and role validation
This commit is contained in:
+4
-1
@@ -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)
|
||||
|
||||
+8
-15
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user