Api Key role is now correcty added do middleware context

This commit is contained in:
Torkel Ödegaard
2015-01-16 16:15:35 +01:00
parent 507bff8b59
commit 2b05dac071
6 changed files with 54 additions and 47 deletions

View File

@ -31,7 +31,7 @@ func GetContextHandler() macaron.Handler {
}
// try get account id from request
if accountId, err := getRequestAccountId(ctx); err == nil {
if accountId := getRequestAccountId(ctx); accountId != 0 {
query := m.GetSignedInUserQuery{AccountId: accountId}
if err := bus.Dispatch(&query); err != nil {
log.Error(3, "Failed to get user by id, %v, %v", accountId, err)
@ -39,6 +39,28 @@ func GetContextHandler() macaron.Handler {
ctx.IsSignedIn = true
ctx.SignInUser = query.Result
}
} else if token := getApiToken(ctx); token != "" {
// Try API Key auth
tokenQuery := m.GetTokenByTokenQuery{Token: token}
if err := bus.Dispatch(&tokenQuery); err != nil {
ctx.JsonApiErr(401, "Invalid token", err)
return
} else {
tokenInfo := tokenQuery.Result
query := m.GetSignedInUserQuery{AccountId: tokenInfo.AccountId}
if err := bus.Dispatch(&query); err != nil {
ctx.JsonApiErr(401, "Invalid token", err)
return
}
ctx.IsSignedIn = true
ctx.SignInUser = query.Result
// api key role
ctx.SignInUser.UserRole = tokenInfo.Role
ctx.SignInUser.UsingAccountId = ctx.SignInUser.AccountId
ctx.SignInUser.UsingAccountName = ctx.SignInUser.UserName
}
}
c.Map(ctx)