lang
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"prestaproxy/internal/prestashop/cookie"
|
||||
"git.ma-al.com/goc_marek/ps_shop/internal/prestashop/cookie"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
@@ -12,8 +13,8 @@ import (
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
psconfig "prestaproxy/internal/prestashop/config"
|
||||
pscookie "prestaproxy/internal/prestashop/cookie"
|
||||
psconfig "git.ma-al.com/goc_marek/ps_shop/internal/prestashop/config"
|
||||
pscookie "git.ma-al.com/goc_marek/ps_shop/internal/prestashop/cookie"
|
||||
)
|
||||
|
||||
type AnonymousSessionInitializer interface {
|
||||
@@ -62,6 +63,7 @@ func Session(cfg psconfig.Config, codec pscookie.Codec, initializer AnonymousSes
|
||||
}
|
||||
if ownedRoute {
|
||||
applyRequestLanguage(session, resolveRequestLanguageID(c.Request().Context(), c.Request(), session, languageResolver))
|
||||
applyRequestMarket(session, requestMarketSelection(c.Request()))
|
||||
}
|
||||
if ownedRoute && shouldSetSessionCookie(rawCookie, session) {
|
||||
encoded, err := codec.Encode(session)
|
||||
@@ -70,6 +72,9 @@ func Session(cfg psconfig.Config, codec pscookie.Codec, initializer AnonymousSes
|
||||
}
|
||||
session.RawCookie = encoded
|
||||
setPrestaShopCookie(c.Request(), c.Response(), ownership.ProductPrefixes, cookieName, encoded)
|
||||
if redirectURL, ok := clearMarketSelectionURL(c.Request()); ok {
|
||||
return c.Redirect(http.StatusSeeOther, redirectURL)
|
||||
}
|
||||
}
|
||||
|
||||
SetSession(c, session)
|
||||
@@ -78,6 +83,12 @@ func Session(cfg psconfig.Config, codec pscookie.Codec, initializer AnonymousSes
|
||||
}
|
||||
}
|
||||
|
||||
type marketSelection struct {
|
||||
CountryID int64
|
||||
CountryISO string
|
||||
CurrencyID int64
|
||||
}
|
||||
|
||||
func resolveRequestLanguageID(ctx context.Context, req *http.Request, session *pscookie.SessionContext, resolver LanguageResolver) int64 {
|
||||
if resolver == nil {
|
||||
return 0
|
||||
@@ -177,6 +188,50 @@ func applyRequestLanguage(session *pscookie.SessionContext, languageID int64) {
|
||||
session.RawCookie = ""
|
||||
}
|
||||
|
||||
func applyRequestMarket(session *pscookie.SessionContext, selection marketSelection) {
|
||||
if session == nil || selection.CountryISO == "" || selection.CurrencyID == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
currentCountry := ""
|
||||
currentCurrency := int64(0)
|
||||
currentCountryID := int64(0)
|
||||
if session.Values != nil {
|
||||
currentCountry = strings.ToUpper(strings.TrimSpace(session.Values["iso_code_country"]))
|
||||
if session.CurrencyID != nil {
|
||||
currentCurrency = *session.CurrencyID
|
||||
}
|
||||
currentCountryID, _ = strconv.ParseInt(session.Values["id_country"], 10, 64)
|
||||
}
|
||||
if currentCountry == selection.CountryISO && currentCurrency == selection.CurrencyID && currentCountryID == selection.CountryID {
|
||||
return
|
||||
}
|
||||
|
||||
if session.Values == nil {
|
||||
session.Values = map[string]string{}
|
||||
}
|
||||
|
||||
session.CurrencyID = int64Ptr(selection.CurrencyID)
|
||||
session.Values["iso_code_country"] = selection.CountryISO
|
||||
if selection.CountryID > 0 {
|
||||
session.Values["id_country"] = strconv.FormatInt(selection.CountryID, 10)
|
||||
session.OrderedKeys = ensureOrderedKey(session.OrderedKeys, "id_country", 5)
|
||||
}
|
||||
session.Values["id_currency"] = strconv.FormatInt(selection.CurrencyID, 10)
|
||||
session.OrderedKeys = ensureOrderedKey(session.OrderedKeys, "iso_code_country", 4)
|
||||
session.OrderedKeys = ensureOrderedKey(session.OrderedKeys, "id_currency", 6)
|
||||
|
||||
if !session.IsLoggedIn {
|
||||
if checksum := anonymousSessionChecksum(session, sessionLanguageID(session)); checksum != "" {
|
||||
session.Values["checksum"] = checksum
|
||||
session.OrderedKeys = ensureOrderedKey(session.OrderedKeys, "checksum", len(session.OrderedKeys))
|
||||
}
|
||||
}
|
||||
|
||||
session.Plaintext = ""
|
||||
session.RawCookie = ""
|
||||
}
|
||||
|
||||
func sessionLanguageID(session *pscookie.SessionContext) int64 {
|
||||
if session == nil || session.LanguageID == nil {
|
||||
return 0
|
||||
@@ -237,6 +292,67 @@ func int64Ptr(value int64) *int64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func requestMarketSelection(req *http.Request) marketSelection {
|
||||
if req == nil || req.URL == nil {
|
||||
return marketSelection{}
|
||||
}
|
||||
raw := strings.TrimSpace(req.URL.Query().Get("market"))
|
||||
if raw == "" {
|
||||
return marketSelection{}
|
||||
}
|
||||
parts := strings.Split(raw, ":")
|
||||
if len(parts) != 2 && len(parts) != 3 {
|
||||
return marketSelection{}
|
||||
}
|
||||
|
||||
selection := marketSelection{}
|
||||
var countryISO string
|
||||
var currencyValue string
|
||||
if len(parts) == 3 {
|
||||
countryID, err := strconv.ParseInt(strings.TrimSpace(parts[0]), 10, 64)
|
||||
if err != nil || countryID == 0 {
|
||||
return marketSelection{}
|
||||
}
|
||||
selection.CountryID = countryID
|
||||
countryISO = strings.ToUpper(strings.TrimSpace(parts[1]))
|
||||
currencyValue = parts[2]
|
||||
} else {
|
||||
countryISO = strings.ToUpper(strings.TrimSpace(parts[0]))
|
||||
currencyValue = parts[1]
|
||||
}
|
||||
|
||||
currencyID, err := strconv.ParseInt(strings.TrimSpace(currencyValue), 10, 64)
|
||||
if err != nil || currencyID == 0 {
|
||||
return marketSelection{}
|
||||
}
|
||||
if len(countryISO) < 2 || len(countryISO) > 5 {
|
||||
return marketSelection{}
|
||||
}
|
||||
|
||||
selection.CountryISO = countryISO
|
||||
selection.CurrencyID = currencyID
|
||||
return selection
|
||||
}
|
||||
|
||||
func clearMarketSelectionURL(req *http.Request) (string, bool) {
|
||||
if req == nil || req.URL == nil {
|
||||
return "", false
|
||||
}
|
||||
query := req.URL.Query()
|
||||
if query.Get("market") == "" {
|
||||
return "", false
|
||||
}
|
||||
query.Del("market")
|
||||
cleanPath := req.URL.Path
|
||||
if cleanPath == "" {
|
||||
cleanPath = "/"
|
||||
}
|
||||
if encoded := query.Encode(); encoded != "" {
|
||||
return cleanPath + "?" + encoded, true
|
||||
}
|
||||
return cleanPath, true
|
||||
}
|
||||
|
||||
func setPrestaShopCookie(req *http.Request, res *echo.Response, ownedPrefixes []string, name, value string) {
|
||||
http.SetCookie(res.Writer, &http.Cookie{
|
||||
Name: name,
|
||||
@@ -277,7 +393,11 @@ func requestCookieDomain(req *http.Request) string {
|
||||
return ""
|
||||
}
|
||||
if parsed, err := url.Parse("http://" + host); err == nil {
|
||||
return parsed.Hostname()
|
||||
host = parsed.Hostname()
|
||||
}
|
||||
host = strings.TrimSpace(strings.TrimPrefix(host, "."))
|
||||
if host == "" || strings.EqualFold(host, "localhost") || net.ParseIP(host) != nil {
|
||||
return ""
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user