2025-06-13 17:23:16 +08:00

76 lines
1.4 KiB
Go

package mid
import (
"context"
"management/internal/erpserver/model/dto"
"github.com/a-h/templ"
)
type userKey struct{}
func setUser(ctx context.Context, usr dto.AuthorizeUser) context.Context {
return context.WithValue(ctx, userKey{}, usr)
}
// GetUser returns the user from the context.
func GetUser(ctx context.Context) dto.AuthorizeUser {
v, ok := ctx.Value(userKey{}).(dto.AuthorizeUser)
if !ok {
return dto.AuthorizeUser{}
}
return v
}
type menuKey struct{}
func setCurMenus(ctx context.Context, ms []dto.OwnerMenuDto) context.Context {
return context.WithValue(ctx, menuKey{}, ms)
}
func GetCurMenus(ctx context.Context) []dto.OwnerMenuDto {
v, ok := ctx.Value(menuKey{}).([]dto.OwnerMenuDto)
if !ok {
return []dto.OwnerMenuDto{}
}
return v
}
type NoSurfToken struct {
Token string
HtmlToken string
}
type csrfKey struct{}
func setCsrfToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, csrfKey{}, token)
}
func GetCsrfToken(ctx context.Context) string {
v, ok := ctx.Value(csrfKey{}).(string)
if !ok {
return ""
}
return v
}
type htmlCsrfKey struct{}
func setHtmlCsrfToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, htmlCsrfKey{}, templ.Raw(token))
}
func GetHtmlCsrfToken(ctx context.Context) templ.Component {
v, ok := ctx.Value(htmlCsrfKey{}).(templ.Component)
if !ok {
return templ.Raw("")
}
return v
}