Exemplo n.º 1
0
        protected ContentResult GetCleanRegistrationPoliciesLogic(string lang = null)
        {
            var sb       = new StringBuilder();
            var policies = _usersExtensionsServices.GetUserLinkedPolicies(lang);

            //policy.PendingPolicies
            sb.Insert(0, "{");
            sb.Append("\"Policies\":[");

            int i = 0;

            foreach (var item in policies)
            {
                if (i > 0)
                {
                    sb.Append(",");
                }
                sb.Append("{");
                sb.Append("\"PolicyId\":" + item.Id.ToString() + ",");
                sb.Append("\"Title\":\"" + item.ContentItem.As <TitlePart>().Title.Replace("\"", "\\\"") + "\",");
                sb.Append("\"Body\":\"" + item.ContentItem.As <BodyPart>().Text.Replace("\"", "\\\"").Replace("\r\n", "\\r\\n") + "\",");
                sb.Append("\"PolicyType\":\"" + item.PolicyType.ToString() + "\",");
                sb.Append("\"UserHaveToAccept\":" + item.UserHaveToAccept.ToString().ToLowerInvariant() + "");
                sb.Append("}");
                i++;
            }
            sb.Append("]");
            sb.Append("}");
            return(new ContentResult {
                Content = sb.ToString(), ContentType = "application/json"
            });
        }
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            bool isAdminService = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AdminServiceAttribute), false).Any();

            var fullActionName = filterContext.Controller.GetType().FullName + "." + filterContext.ActionDescriptor.ActionName;

            if (_workContext.GetContext().CurrentUser != null &&
                !allowedControllers.Contains(filterContext.Controller.GetType().FullName) &&
                !allowedControllers.Contains(fullActionName) &&
                !AdminFilter.IsApplied(filterContext.RequestContext) &&
                !isAdminService)
            {
                var language = _workContext.GetContext().CurrentCulture;
                IEnumerable <PolicyTextInfoPart> neededPolicies = _userExtensionServices.GetUserLinkedPolicies(language);

                if (neededPolicies.Count() > 0)
                {
                    var missingPolicies = MissingRegistrationPolices();
                    if (missingPolicies.Count() > 0)
                    {
                        if (filterContext.Controller.GetType().FullName == "Laser.Orchard.WebServices.Controllers.JsonController")
                        {
                            string data = _policyServices.PoliciesLMNVSerialization(neededPolicies.Where(w => missingPolicies.Any(a => a == w.Id)));

                            filterContext.Result = new ContentResult {
                                Content = data, ContentType = "application/json"
                            };
                        }
                        else if (filterContext.Controller.GetType().FullName == "Laser.Orchard.WebServices.Controllers.WebApiController")
                        {
                            string data = _policyServices.PoliciesPureJsonSerialization(neededPolicies.Where(w => missingPolicies.Any(a => a == w.Id)));

                            filterContext.Result = new ContentResult {
                                Content = data, ContentType = "application/json"
                            };
                        }
                        else
                        {
                            string outputFormat = _workContext.GetContext().HttpContext.Request.Headers["OutputFormat"];

                            if (String.Equals(outputFormat, "LMNV", StringComparison.OrdinalIgnoreCase))
                            {
                                string   data     = _policyServices.PoliciesLMNVSerialization(neededPolicies.Where(w => missingPolicies.Any(a => a == w.Id)));
                                Response response = _utilsServices.GetResponse(ResponseType.MissingPolicies, "", Newtonsoft.Json.JsonConvert.DeserializeObject(data));

                                filterContext.Result = new ContentResult {
                                    Content = Newtonsoft.Json.JsonConvert.SerializeObject(response), ContentType = "application/json"
                                };
                            }
                            else if (String.Equals(outputFormat, "PureJson", StringComparison.OrdinalIgnoreCase))
                            {
                                string   data     = _policyServices.PoliciesPureJsonSerialization(neededPolicies.Where(w => missingPolicies.Any(a => a == w.Id)));
                                Response response = _utilsServices.GetResponse(ResponseType.MissingPolicies, "", Newtonsoft.Json.JsonConvert.DeserializeObject(data));

                                filterContext.Result = new ContentResult {
                                    Content = Newtonsoft.Json.JsonConvert.SerializeObject(response), ContentType = "application/json"
                                };
                            }
                            else
                            {
                                var returnType = ((ReflectedActionDescriptor)filterContext.ActionDescriptor).MethodInfo.ReturnType;

                                if (returnType == typeof(JsonResult))
                                {
                                    string   data     = _policyServices.PoliciesPureJsonSerialization(neededPolicies.Where(w => missingPolicies.Any(a => a == w.Id)));
                                    Response response = _utilsServices.GetResponse(ResponseType.MissingPolicies, "", Newtonsoft.Json.JsonConvert.DeserializeObject(data));

                                    filterContext.Result = new ContentResult {
                                        Content = Newtonsoft.Json.JsonConvert.SerializeObject(response), ContentType = "application/json"
                                    };
                                }
                                else
                                {
                                    var encodedAssociatedPolicies = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(",", missingPolicies)));

                                    UrlHelper urlHelper = new UrlHelper(_httpContextAccessor.Current().Request.RequestContext);
                                    var       url       = urlHelper.Action("Index", "Policies", new { area = "Laser.Orchard.Policy", lang = language, policies = encodedAssociatedPolicies, returnUrl = _httpContextAccessor.Current().Request.RawUrl });

                                    filterContext.Result = new RedirectResult(url);
                                }
                            }
                        }
                    }
                }
            }

            return;
        }