Exemplo n.º 1
0
// BaseCurrencyCode retrieves application base currency code
func BaseCurrencyCode(cr config.Reader) (language.Currency, error) {
	base, err := cr.GetString(config.Path(PathCurrencyBase))
	if config.NotKeyNotFoundError(err) {
		return language.Currency{}, err
	}
	return language.ParseCurrency(base)
}
Exemplo n.º 2
0
// WithPasswordFromConfig retrieves the password from the configuration with path
// as defined in constant PathJWTPassword
func WithPasswordFromConfig(cr config.Getter) Option {
	pw, err := cr.String(config.Path(PathJWTPassword))
	if config.NotKeyNotFoundError(err) {
		pw = string(uuid.NewRandom())
	}
	return WithPassword([]byte(pw))
}
Exemplo n.º 3
0
// AllowedCurrencies returns all installed currencies from global scope.
func AllowedCurrencies(cr config.Reader) ([]string, error) {
	installedCur, err := cr.GetString(config.Path(PathSystemCurrencyInstalled))
	if config.NotKeyNotFoundError(err) {
		return nil, err
	}
	// TODO use internal model of PathSystemCurrencyInstalled defined in package directory
	return strings.Split(installedCur, ","), nil
}
Exemplo n.º 4
0
// IsSingleStoreMode check if Single-Store mode is enabled in configuration and from Store count < 3.
// This flag only shows that admin does not want to show certain UI components at backend (like store switchers etc)
// if Magento has only one store view but it does not check the store view collection.
func (sm *Service) IsSingleStoreMode() bool {
	isEnabled, err := sm.cr.GetBool(config.Path(PathSingleStoreModeEnabled)) // default scope
	if config.NotKeyNotFoundError(err) {
		// TODO maybe log error here
		return false
	}
	return sm.HasSingleStore() && isEnabled
}
Exemplo n.º 5
0
// WithValidateBaseURL is a middleware which checks if the request base URL
// is equal to the one store in the configuration, if not
// i.e. redirect from http://example.com/store/ to http://www.example.com/store/
// @see app/code/Magento/Store/App/FrontController/Plugin/RequestPreprocessor.php
func WithValidateBaseURL(cr config.ReaderPubSuber) ctxhttp.Middleware {

	// Having the GetBool command here, means you must restart the app to take
	// changes in effect. @todo refactor and use pub/sub to automatically change
	// the isRedirectToBase value.
	checkBaseURL, err := cr.GetBool(config.Path(PathRedirectToBase)) // scope default
	if config.NotKeyNotFoundError(err) && PkgLog.IsDebug() {
		PkgLog.Debug("ctxhttp.WithValidateBaseUrl.GetBool", "err", err, "path", PathRedirectToBase)
	}

	redirectCode := http.StatusMovedPermanently
	if rc, err := cr.GetInt(config.Path(PathRedirectToBase)); rc != redirectCode && false == config.NotKeyNotFoundError(err) {
		redirectCode = http.StatusFound
	}

	return func(h ctxhttp.Handler) ctxhttp.Handler {
		return ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

			if checkBaseURL && r.Method != "POST" {

				_, requestedStore, err := FromContextReader(ctx)
				if err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("ctxhttp.WithValidateBaseUrl.FromContextServiceReader", "err", err, "ctx", ctx)
					}
					return errgo.Mask(err)
				}

				baseURL, err := requestedStore.BaseURL(config.URLTypeWeb, requestedStore.IsCurrentlySecure(r))
				if err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("ctxhttp.WithValidateBaseUrl.requestedStore.BaseURL", "err", err, "ctx", ctx)
					}
					return errgo.Mask(err)
				}

				if err := httputils.IsBaseURLCorrect(r, &baseURL); err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("store.WithValidateBaseUrl.IsBaseUrlCorrect.error", "err", err, "baseURL", baseURL, "request", r)
					}

					baseURL.Path = r.URL.Path
					baseURL.RawPath = r.URL.RawPath
					baseURL.RawQuery = r.URL.RawQuery
					baseURL.Fragment = r.URL.Fragment
					http.Redirect(w, r, (&baseURL).String(), redirectCode)
					return nil
				}
			}
			return h.ServeHTTPContext(ctx, w, r)
		})
	}
}
Exemplo n.º 6
0
// BaseURL returns a parsed and maybe cached URL from config.ScopedReader.
// It returns a copy of url.URL or an error. Possible URLTypes are:
//     - config.URLTypeWeb
//     - config.URLTypeStatic
//     - config.URLTypeMedia
func (s *Store) BaseURL(ut config.URLType, isSecure bool) (url.URL, error) {

	switch isSecure {
	case true:
		if pu := s.urlcache.secure.Get(ut); pu != nil {
			return *pu, nil
		}
	case false:
		if pu := s.urlcache.unsecure.Get(ut); pu != nil {
			return *pu, nil
		}
	}

	var p model.BaseURL
	switch ut {
	case config.URLTypeWeb:
		p = backend.Backend.WebUnsecureBaseURL
		if isSecure {
			p = backend.Backend.WebSecureBaseURL
		}
		break
	case config.URLTypeStatic:
		p = backend.Backend.WebUnsecureBaseStaticURL
		if isSecure {
			p = backend.Backend.WebSecureBaseStaticURL
		}
		break
	case config.URLTypeMedia:
		p = backend.Backend.WebUnsecureBaseMediaURL
		if isSecure {
			p = backend.Backend.WebSecureBaseMediaURL
		}
		break
	case config.URLTypeAbsent: // hack to clear the cache :-( refactor that
		_ = s.urlcache.unsecure.Clear()
		return url.URL{}, s.urlcache.secure.Clear()
	// TODO(cs) rethink that here and maybe add the other paths if needed.
	default:
		return url.URL{}, fmt.Errorf("Unsupported UrlType: %d", ut)
	}

	rawURL := p.Get(s.Config)

	if strings.Contains(rawURL, model.PlaceholderBaseURL) {
		// TODO(cs) replace placeholder with \Magento\Framework\App\Request\Http::getDistroBaseUrl()
		// getDistroBaseUrl will be generated from the $_SERVER variable,
		base, err := s.cr.String(config.Path(config.PathCSBaseURL))
		if config.NotKeyNotFoundError(err) {
			PkgLog.Debug("store.Store.BaseURL.String", "err", err, "path", config.PathCSBaseURL)
			base = config.CSBaseURL
		}
		rawURL = strings.Replace(rawURL, model.PlaceholderBaseURL, base, 1)
	}
	rawURL = strings.TrimRight(rawURL, "/") + "/"

	if isSecure {
		retURL, retErr := s.urlcache.secure.Set(ut, rawURL)
		return *retURL, retErr
	}
	retURL, retErr := s.urlcache.unsecure.Set(ut, rawURL)
	return *retURL, retErr
}