Exemplo n.º 1
0
            /// <summary>
            /// Called before the action executes, after model binding is complete
            /// </summary>
            /// <param name="context">A context for action filters</param>
            public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
            {
                await next();

                if (context == null || context.HttpContext == null || context.HttpContext.Request == null)
                {
                    return;
                }

                //check request query parameters
                var request = context.HttpContext.Request;

                if (request?.Query == null || !request.Query.Any())
                {
                    return;
                }

                if (!DataSettingsHelper.DatabaseIsInstalled())
                {
                    return;
                }

                //try to find by ID
                var affiliateIds = request.Query[ID_QUERY_PARAMETER_NAME];

                if (affiliateIds.Any())
                {
                    string affiliateId = affiliateIds.FirstOrDefault();
                    if (!string.IsNullOrEmpty(affiliateId))
                    {
                        await SetCustomerAffiliateId(await _affiliateService.GetAffiliateById(affiliateId));
                    }
                    return;
                }

                //try to find by friendly name
                var affiliateNames = request.Query[FRIENDLYURLNAME_QUERY_PARAMETER_NAME];

                if (affiliateNames.Any())
                {
                    var affiliateName = affiliateNames.FirstOrDefault();
                    if (!string.IsNullOrEmpty(affiliateName))
                    {
                        await SetCustomerAffiliateId(await _affiliateService.GetAffiliateByFriendlyUrlName(affiliateName));
                    }
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Validate friendly URL name
        /// </summary>
        /// <param name="affiliate">Affiliate</param>
        /// <param name="friendlyUrlName">Friendly URL name</param>
        /// <returns>Valid friendly name</returns>
        public static async Task <string> ValidateFriendlyUrlName(this Affiliate affiliate, IAffiliateService affiliateService, SeoSettings seoSettings, string friendlyUrlName)
        {
            if (affiliate == null)
            {
                throw new ArgumentNullException("affiliate");
            }

            //ensure we have only valid chars
            friendlyUrlName = SeoExtensions.GetSeName(friendlyUrlName, seoSettings);


            //max length
            //For long URLs we can get the following error:
            //"the specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters"
            //that's why we limit it to 200 here (consider a store URL + probably added {0}-{1} below)
            friendlyUrlName = CommonHelper.EnsureMaximumLength(friendlyUrlName, 200);


            //ensure this name is not reserved yet
            //empty? nothing to check
            if (String.IsNullOrEmpty(friendlyUrlName))
            {
                return(friendlyUrlName);
            }
            //check whether such friendly URL name already exists (and that is not the current affiliate)
            int i        = 2;
            var tempName = friendlyUrlName;

            while (true)
            {
                var affiliateByFriendlyUrlName = await affiliateService.GetAffiliateByFriendlyUrlName(tempName);

                bool reserved = affiliateByFriendlyUrlName != null && affiliateByFriendlyUrlName.Id != affiliate.Id;
                if (!reserved)
                {
                    break;
                }

                tempName = string.Format("{0}-{1}", friendlyUrlName, i);
                i++;
            }
            friendlyUrlName = tempName;

            return(friendlyUrlName);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validate friendly URL name
        /// </summary>
        /// <param name="affiliate">Affiliate</param>
        /// <param name="friendlyUrlName">Friendly URL name</param>
        /// <returns>Valid friendly name</returns>
        public static async Task <string> ValidateFriendlyUrlName(this Affiliate affiliate, IAffiliateService affiliateService, SeoSettings seoSettings, string friendlyUrlName, string name)
        {
            if (affiliate == null)
            {
                throw new ArgumentNullException(nameof(affiliate));
            }

            if (string.IsNullOrEmpty(friendlyUrlName))
            {
                friendlyUrlName = name;
            }

            //ensure we have only valid chars
            friendlyUrlName = SeoExtensions.GetSeName(friendlyUrlName, seoSettings.ConvertNonWesternChars, seoSettings.AllowUnicodeCharsInUrls, seoSettings.SeoCharConversion);

            //max length
            friendlyUrlName = CommonHelper.EnsureMaximumLength(friendlyUrlName, 200);

            if (String.IsNullOrEmpty(friendlyUrlName))
            {
                return(friendlyUrlName);
            }
            //check whether such friendly URL name already exists (and that is not the current affiliate)
            int i        = 2;
            var tempName = friendlyUrlName;

            while (true)
            {
                var affiliateByFriendlyUrlName = await affiliateService.GetAffiliateByFriendlyUrlName(tempName);

                bool reserved = affiliateByFriendlyUrlName != null && affiliateByFriendlyUrlName.Id != affiliate.Id;
                if (!reserved)
                {
                    break;
                }

                tempName = string.Format("{0}-{1}", friendlyUrlName, i);
                i++;
            }
            friendlyUrlName = tempName;

            return(friendlyUrlName);
        }
Exemplo n.º 4
0
            /// <summary>
            /// Called before the action executes, after model binding is complete
            /// </summary>
            /// <param name="context">A context for action filters</param>
            public void OnActionExecuting(ActionExecutingContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                //check request query parameters
                var request = context.HttpContext.Request;

                if (request?.Query == null || !request.Query.Any())
                {
                    return;
                }

                if (!DataSettingsManager.DatabaseIsInstalled)
                {
                    return;
                }

                //try to find by ID
                var affiliateIds = request.Query[AFFILIATE_ID_QUERY_PARAMETER_NAME];

                if (affiliateIds.Any() && int.TryParse(affiliateIds.FirstOrDefault(), out int affiliateId) &&
                    affiliateId > 0 && affiliateId != _workContext.CurrentCustomer.AffiliateId)
                {
                    SetCustomerAffiliateId(_affiliateService.GetAffiliateById(affiliateId));
                    return;
                }

                //try to find by friendly name
                var affiliateNames = request.Query[AFFILIATE_FRIENDLYURLNAME_QUERY_PARAMETER_NAME];

                if (affiliateNames.Any())
                {
                    var affiliateName = affiliateNames.FirstOrDefault();
                    if (!string.IsNullOrEmpty(affiliateName))
                    {
                        SetCustomerAffiliateId(_affiliateService.GetAffiliateByFriendlyUrlName(affiliateName));
                    }
                }
            }
Exemplo n.º 5
0
 /// <summary>
 /// Gets an affiliate by friendly url name
 /// </summary>
 /// <param name="friendlyUrlName">Friendly url name</param>
 /// <returns>Affiliate</returns>
 public Affiliate GetAffiliateByFriendlyUrlName(string friendlyUrlName)
 {
     return(_affiliateService.GetAffiliateByFriendlyUrlName(friendlyUrlName));
 }