Add user helper methods to context
This commit is contained in:
@@ -19,6 +19,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
stderrs "errors"
|
stderrs "errors"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/user"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,6 +34,9 @@ type key int
|
|||||||
// namespaceKey is the context key for the request namespace.
|
// namespaceKey is the context key for the request namespace.
|
||||||
const namespaceKey key = 0
|
const namespaceKey key = 0
|
||||||
|
|
||||||
|
// userKey is the context key for the request user.
|
||||||
|
const userKey key = 1
|
||||||
|
|
||||||
// NewContext instantiates a base context object for request flows.
|
// NewContext instantiates a base context object for request flows.
|
||||||
func NewContext() Context {
|
func NewContext() Context {
|
||||||
return context.TODO()
|
return context.TODO()
|
||||||
@@ -86,3 +90,14 @@ func WithNamespaceDefaultIfNone(parent Context) Context {
|
|||||||
}
|
}
|
||||||
return parent
|
return parent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithUser returns a copy of parent in which the user value is set
|
||||||
|
func WithUser(parent Context, user user.Info) Context {
|
||||||
|
return WithValue(parent, userKey, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserFrom returns the value of the user key on the ctx
|
||||||
|
func UserFrom(ctx Context) (user.Info, bool) {
|
||||||
|
user, ok := ctx.Value(userKey).(user.Info)
|
||||||
|
return user, ok
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user