1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

Add Context.SetUser and Context.User methods

relative to: https://github.com/iris-contrib/middleware/issues/63
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-10-12 15:52:53 +03:00
parent dfe27567ae
commit 8e51a296b9
7 changed files with 242 additions and 19 deletions

View File

@@ -2133,12 +2133,12 @@ const disableRequestBodyConsumptionContextKey = "iris.request.body.record"
// but acts for the current request.
// It makes the request body readable more than once.
func (ctx *Context) RecordBody() {
ctx.Values().Set(disableRequestBodyConsumptionContextKey, true)
ctx.values.Set(disableRequestBodyConsumptionContextKey, true)
}
// IsRecordingBody reports whether the request body can be readen multiple times.
func (ctx *Context) IsRecordingBody() bool {
return ctx.Values().GetBoolDefault(disableRequestBodyConsumptionContextKey,
return ctx.values.GetBoolDefault(disableRequestBodyConsumptionContextKey,
ctx.app.ConfigurationReadOnly().GetDisableBodyConsumptionOnUnmarshal())
}
@@ -5253,6 +5253,28 @@ func (ctx *Context) Logout(args ...interface{}) error {
return err
}
const userContextKey = "iris.user"
// SetUser sets a User for this request.
// It's used by auth middlewares as a common
// method to provide user information to the
// next handlers in the chain.
func (ctx *Context) SetUser(u User) {
ctx.values.Set(userContextKey, u)
}
// User returns the registered User of this request.
// See `SetUser` too.
func (ctx *Context) User() User {
if v := ctx.values.Get(userContextKey); v != nil {
if u, ok := v.(User); ok {
return u
}
}
return nil
}
const idContextKey = "iris.context.id"
// SetID sets an ID, any value, to the Request Context.