Skip to content
Snippets Groups Projects
Commit b6ff4327d8a3 authored by Florent Aide's avatar Florent Aide
Browse files

rendering engines receive a context containing the username

parent f6ef74a5e274
No related branches found
No related tags found
No related merge requests found
......@@ -20,4 +20,6 @@
const LogRenderBatchSize = 1000
type contextKey int
var (
......@@ -23,5 +25,6 @@
var (
ErrInvalidInput = errors.New("invalid input")
userNameContextKey contextKey = 1
ErrInvalidInput = errors.New("invalid input")
renderPipelineDuration = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
......@@ -228,7 +231,8 @@
reader := rendering.NewDocumentReader(document)
writer := rendering.NewDocumentWriter(&output)
if err := pipeline.Render(ctx, writer, reader); err != nil {
userContext := SetUsername(ctx, username)
if err := pipeline.Render(userContext, writer, reader); err != nil {
return nil, err
}
......@@ -262,6 +266,28 @@
return result, nil
}
// GetUsername allows to extract the username from a context
// this is intended to be used by engines to extract the username
// info.
func GetUsername(ctx context.Context) string {
v := ctx.Value(userNameContextKey)
if v == nil {
return ""
}
res, ok := v.(string)
if !ok {
panic("could not type assert on the userName from context")
}
return res
}
// SetUsername set the given username in the given context and returns
// a derived context.
func SetUsername(ctx context.Context, username string) context.Context {
return context.WithValue(ctx, userNameContextKey, username)
}
func init() {
prometheus.MustRegister(renderPipelineDuration)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment