mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 15:22:47 +01:00
39 lines
963 B
Go
39 lines
963 B
Go
package busdb
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/SecurityBrewery/catalyst/generated/model"
|
|
"github.com/SecurityBrewery/catalyst/role"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const (
|
|
userContextKey contextKey = "user"
|
|
groupContextKey contextKey = "groups"
|
|
)
|
|
|
|
func SetContext(r *http.Request, user *model.UserResponse) *http.Request {
|
|
user.Roles = role.Strings(role.Explodes(user.Roles))
|
|
|
|
return r.WithContext(context.WithValue(r.Context(), userContextKey, user))
|
|
}
|
|
|
|
func SetGroupContext(r *http.Request, groups []string) *http.Request {
|
|
return r.WithContext(context.WithValue(r.Context(), groupContextKey, groups))
|
|
}
|
|
|
|
func UserContext(ctx context.Context, user *model.UserResponse) context.Context {
|
|
user.Roles = role.Strings(role.Explodes(user.Roles))
|
|
|
|
return context.WithValue(ctx, userContextKey, user)
|
|
}
|
|
|
|
func UserFromContext(ctx context.Context) (*model.UserResponse, bool) {
|
|
u, ok := ctx.Value(userContextKey).(*model.UserResponse)
|
|
|
|
return u, ok
|
|
}
|