94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package webcontext
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// UserIsLogged returns a boolean indicating if the user is authenticated or not
|
|
func (c *WebContext) UserIsLogged() bool {
|
|
return c.GetUserID() != ""
|
|
}
|
|
|
|
// GetUserID retrieves the user ID from the request context
|
|
func (c *WebContext) GetUserID() string {
|
|
if userID := c.request.Context().Value(userIDKey); userID != nil {
|
|
return userID.(string)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// SetUserID stores the user ID in the request context
|
|
func (c *WebContext) SetUserID(userID string) {
|
|
ctx := WithUserID(c.request.Context(), userID)
|
|
c.request = c.request.WithContext(ctx)
|
|
}
|
|
|
|
// GetUserEmail retrieves the user email from the request context
|
|
func (c *WebContext) GetUserEmail() string {
|
|
if email := c.request.Context().Value(userEmailKey); email != nil {
|
|
return email.(string)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// SetUserEmail stores the user email in the request context
|
|
func (c *WebContext) SetUserEmail(email string) {
|
|
ctx := WithUserEmail(c.request.Context(), email)
|
|
c.request = c.request.WithContext(ctx)
|
|
}
|
|
|
|
// GetUserRole retrieves the user role from the request context
|
|
func (c *WebContext) GetUserRole() string {
|
|
if role := c.request.Context().Value(userRoleKey); role != nil {
|
|
return role.(string)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// SetUserRole stores the user role in the request context
|
|
func (c *WebContext) SetUserRole(role string) {
|
|
ctx := WithUserRole(c.request.Context(), role)
|
|
c.request = c.request.WithContext(ctx)
|
|
}
|
|
|
|
// SetUser stores both user ID and email in the request context
|
|
func (c *WebContext) SetUser(userID, email string) {
|
|
ctx := WithUser(c.request.Context(), userID, email)
|
|
c.request = c.request.WithContext(ctx)
|
|
}
|
|
|
|
// SetUserWithRole stores user ID, email, and role in the request context
|
|
func (c *WebContext) SetUserWithRole(userID, email, role string) {
|
|
ctx := WithUserWithRole(c.request.Context(), userID, email, role)
|
|
c.request = c.request.WithContext(ctx)
|
|
}
|
|
|
|
// WithUserID creates a new context with the user ID
|
|
func WithUserID(ctx context.Context, userID string) context.Context {
|
|
return context.WithValue(ctx, userIDKey, userID)
|
|
}
|
|
|
|
// WithUserEmail creates a new context with the user email
|
|
func WithUserEmail(ctx context.Context, email string) context.Context {
|
|
return context.WithValue(ctx, userEmailKey, email)
|
|
}
|
|
|
|
// WithUserRole creates a new context with the user role
|
|
func WithUserRole(ctx context.Context, role string) context.Context {
|
|
return context.WithValue(ctx, userRoleKey, role)
|
|
}
|
|
|
|
// WithUser creates a new context with both user ID and email
|
|
func WithUser(ctx context.Context, userID, email string) context.Context {
|
|
ctx = WithUserID(ctx, userID)
|
|
ctx = WithUserEmail(ctx, email)
|
|
return ctx
|
|
}
|
|
|
|
// WithUserWithRole creates a new context with user ID, email, and role
|
|
func WithUserWithRole(ctx context.Context, userID, email, role string) context.Context {
|
|
ctx = WithUserID(ctx, userID)
|
|
ctx = WithUserEmail(ctx, email)
|
|
ctx = WithUserRole(ctx, role)
|
|
return ctx
|
|
}
|