mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-04 16:26:23 +08:00
28 lines
675 B
Go
28 lines
675 B
Go
// Package viewer provides utility functions for passing the current user to a context.
|
|
package viewer
|
|
|
|
import "context"
|
|
|
|
type key int
|
|
|
|
const viewerKey key = 0
|
|
|
|
// Viewer represents a user performing an action on the server.
|
|
type Viewer struct {
|
|
UserID string
|
|
Username string
|
|
FullName string
|
|
SessionID string
|
|
}
|
|
|
|
// FromContext returns the current user information if present.
|
|
func FromContext(ctx context.Context) (Viewer, bool) {
|
|
v, ok := ctx.Value(viewerKey).(Viewer)
|
|
return v, ok
|
|
}
|
|
|
|
// NewContext updates the parent context with viewer information.
|
|
func NewContext(ctx context.Context, v Viewer) context.Context {
|
|
return context.WithValue(ctx, viewerKey, v)
|
|
}
|