context

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserIDKey        contextKey = "user_id"
	TenantIDKey      contextKey = "tenant_id"
	AppIDKey         contextKey = "app_id"
	RolesKey         contextKey = "roles"
	PermissionsKey   contextKey = "permissions"
	EmailKey         contextKey = "email"
	CorrelationIDKey contextKey = "correlation_id"
	TenantDomainKey  contextKey = "tenant_domain"
)
View Source
const GinContextKey = "request_context"

GinContextKey is the key for storing context in gin.Context

Variables

View Source
var (
	ErrUserNotFound        = errors.New("user not found in context")
	ErrTenantNotFound      = errors.New("tenant not found in context")
	ErrPermissionsNotFound = errors.New("permissions not found in context")
)

Functions

func GetAppID

func GetAppID(ctx context.Context) string

GetAppID retrieves application ID from context

func GetAppIDFromGin

func GetAppIDFromGin(c *gin.Context) string

GetAppIDFromGin retrieves app ID from gin context

func GetCorrelationID

func GetCorrelationID(ctx context.Context) string

GetCorrelationID retrieves correlation ID from context

func GetCorrelationIDFromGin

func GetCorrelationIDFromGin(c *gin.Context) string

GetCorrelationIDFromGin retrieves correlation ID from gin context

func GetEmail

func GetEmail(ctx context.Context) string

GetEmail retrieves email from context

func GetEmailFromGin

func GetEmailFromGin(c *gin.Context) string

GetEmailFromGin retrieves email from gin context

func GetPermissions

func GetPermissions(ctx context.Context) ([]string, error)

GetPermissions retrieves permissions from context

func GetPermissionsFromGin

func GetPermissionsFromGin(c *gin.Context) []string

GetPermissionsFromGin retrieves permissions from gin context

func GetRoles

func GetRoles(ctx context.Context) []string

GetRoles retrieves roles from context

func GetRolesFromGin

func GetRolesFromGin(c *gin.Context) []string

GetRolesFromGin retrieves roles from gin context

func GetTenantDomain

func GetTenantDomain(ctx context.Context) string

GetTenantDomain retrieves tenant domain from context

func GetTenantID

func GetTenantID(ctx context.Context) (string, error)

GetTenantID retrieves tenant ID from context

func GetTenantIDFromGin

func GetTenantIDFromGin(c *gin.Context) string

GetTenantIDFromGin retrieves tenant ID from gin context

func GetUserID

func GetUserID(ctx context.Context) (string, error)

GetUserID retrieves user ID from context

func GetUserIDFromGin

func GetUserIDFromGin(c *gin.Context) string

GetUserIDFromGin retrieves user ID from gin context

func GinToStdContext

func GinToStdContext(c *gin.Context) context.Context

GinToStdContext converts gin.Context to standard context.Context with request context

func HasAllRolesFromGin

func HasAllRolesFromGin(c *gin.Context, roles ...string) bool

HasAllRolesFromGin checks if user has all of the specified roles in gin context

func HasAnyRole

func HasAnyRole(c *gin.Context, roles ...string) bool

HasAnyRole checks if user has any of the roles in Gin context

func HasAnyRoleFromGin

func HasAnyRoleFromGin(c *gin.Context, roles ...string) bool

HasAnyRoleFromGin checks if user has any of the specified roles in gin context

func HasRole

func HasRole(c *gin.Context, role string) bool

HasRole checks if user has specific role in Gin context

func HasRoleFromGin

func HasRoleFromGin(c *gin.Context, role string) bool

HasRoleFromGin checks if user has a specific role in gin context

func MustGetTenantID

func MustGetTenantID(ctx context.Context) string

MustGetTenantID retrieves tenant ID or panics

func MustGetUserID

func MustGetUserID(ctx context.Context) string

MustGetUserID retrieves user ID or panics

func SetUserContext

func SetUserContext(c *gin.Context, user *UserContext)

SetUserContext sets user context in Gin context

func ToGinContext

func ToGinContext(c *gin.Context, rc *RequestContext)

ToGinContext stores request context in gin.Context

func WithAppID

func WithAppID(ctx context.Context, appID string) context.Context

WithAppID adds application ID to context

func WithCorrelationID

func WithCorrelationID(ctx context.Context, correlationID string) context.Context

WithCorrelationID adds correlation ID to context

func WithEmail

func WithEmail(ctx context.Context, email string) context.Context

WithEmail adds email to context

func WithPermissions

func WithPermissions(ctx context.Context, permissions []string) context.Context

WithPermissions adds permissions to context

func WithRequestContext

func WithRequestContext(ctx context.Context, rc *RequestContext) context.Context

WithRequestContext adds full request context

func WithRoles

func WithRoles(ctx context.Context, roles []string) context.Context

WithRoles adds roles to context

func WithTenantDomain

func WithTenantDomain(ctx context.Context, domain string) context.Context

WithTenantDomain adds tenant domain to context

func WithTenantID

func WithTenantID(ctx context.Context, tenantID string) context.Context

WithTenantID adds tenant ID to context

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID adds user ID to context

Types

type RequestContext

type RequestContext struct {
	UserID        string
	TenantID      string
	AppID         string
	Email         string
	Roles         []string
	Permissions   []string
	CorrelationID string
	TenantDomain  string
}

RequestContext holds all context information for a request

func FromGinContext

func FromGinContext(c *gin.Context) *RequestContext

FromGinContext retrieves request context from gin.Context

func GetRequestContext

func GetRequestContext(ctx context.Context) *RequestContext

GetRequestContext retrieves full request context

type UserContext

type UserContext = RequestContext

UserContext is an alias for RequestContext for backward compatibility

func GetUserContext

func GetUserContext(c *gin.Context) (*UserContext, error)

GetUserContext extracts user context from Gin context

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL