/// <summary>
        /// Get pending policies: policies for which the user has not yet expressed his opinion
        /// </summary>
        /// <param name="context">Context of the showing content</param>
        /// <param name="part">the PolicyPart that describe which policies should be checked</param>
        /// <returns>The IContentQuery that returns the list of the pending policies</returns>
        private IContentQuery GetPendingPolicies(LoadContentContext context, PolicyPart part)
        {
            var loggedUser = _workContext.GetContext().CurrentUser;
            int currentLanguageId;
            IContentQuery <PolicyTextInfoPart> query;

            if (context.ContentItem.As <LocalizationPart>() != null && context.ContentItem.As <LocalizationPart>().Culture != null && context.ContentItem.As <LocalizationPart>().Culture.Id > 0)
            {
                currentLanguageId = context.ContentItem.As <LocalizationPart>().Culture.Id;
            }
            else
            {
                //Nel caso di contenuto senza Localizationpart prendo la CurrentCulture
                currentLanguageId = _cultureManager.GetCultureByName(_workContext.GetContext().CurrentCulture).Id;
            }

            query = _contentManager.Query <PolicyTextInfoPart>().Join <LocalizationPartRecord>().Where(w => w.CultureId == currentLanguageId || w.CultureId == 0);

            //recupero solo le Policy Pendenti, alle quali l'utente non ha risposto ancora

            IContentQuery <PolicyTextInfoPart, PolicyTextInfoPartRecord> items;

            if (loggedUser != null)
            {
                var answeredIds = loggedUser.As <UserPolicyPart>().UserPolicyAnswers.Select(s => s.PolicyTextInfoPartRecord.Id).ToArray();
                items = query.Where <PolicyTextInfoPartRecord>(w => !answeredIds.Contains(w.Id));
            }
            else
            {
                IList <PolicyForUserViewModel> answers = _policyServices.GetCookieOrVolatileAnswers();
                var answeredIds = answers.Select(s => s.PolicyTextId).ToArray();
                items = query.Where <PolicyTextInfoPartRecord>(w => !answeredIds.Contains(w.Id));
            }

            var settings = part.Settings.GetModel <PolicyPartSettings>();

            if (!settings.PolicyTextReferences.Contains("{All}"))
            {
                int[] filterIds;

                string[] filterComplexIds = _policyServices.GetPoliciesForContent(part);

                if (filterComplexIds != null)
                {
                    if (filterComplexIds.Length == 1)
                    {
                        filterComplexIds = filterComplexIds[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    }
                    filterIds = filterComplexIds.Select(s => {
                        int id = 0;
                        int.TryParse(s.Replace("{", "").Replace("}", ""), out id);
                        return(id);
                    }).ToArray();

                    items = items.Where <PolicyTextInfoPartRecord>(w => filterIds.Contains(w.Id));
                }
            }

            return(items.OrderByDescending(x => x.Priority));
        }
        protected override DriverResult Display(PolicyPart part, string displayType, dynamic shapeHelper)
        {
            if (displayType == "Detail")
            {
                if (_policyServices.HasPendingPolicies(part.ContentItem) ?? false)
                {
                    var       language = _workContextAccessor.GetContext().CurrentCulture;
                    UrlHelper url      = new UrlHelper(_httpContextAccessor.Current().Request.RequestContext);

                    var associatedPolicies        = _policyServices.GetPoliciesForContent(part);
                    var encodedAssociatedPolicies = "";
                    if (associatedPolicies != null)
                    {
                        encodedAssociatedPolicies = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(",", associatedPolicies)));
                    }
                    else
                    {
                        encodedAssociatedPolicies = Convert.ToBase64String(Encoding.UTF8.GetBytes(""));
                    }

                    var fullUrl = url.Action("Index", "Policies", new { area      = "Laser.Orchard.Policy",
                                                                        lang      = language,
                                                                        policies  = encodedAssociatedPolicies,
                                                                        returnUrl = _httpContextAccessor.Current().Request.RawUrl,
                                                                        alias     = part.As <AutoroutePart>() != null? part.As <AutoroutePart>().DisplayAlias:"" });
                    var cookie = _httpContextAccessor.Current().Request.Cookies["PoliciesAnswers"];
                    if (cookie != null && cookie.Value != null)
                    {
                        _httpContextAccessor.Current().Response.Cookies.Add(_httpContextAccessor.Current().Request.Cookies["PoliciesAnswers"]);
                    }
                    _httpContextAccessor.Current().Response.Redirect(fullUrl, true);
                }
                else
                {
                }
            }
            else if (displayType == "SummaryAdmin")
            {
                return(ContentShape("Parts_Policy_SummaryAdmin",
                                    () => shapeHelper.Parts_Policy_SummaryAdmin(IncludePendingPolicy: part.IncludePendingPolicy)));
            }
            return(null);
        }
Exemplo n.º 3
0
        public IList <UserPolicyAnswerWithContent> BuildEditorForPolicies(PolicyPart policyPart)
        {
            var associatedPolicies = _policyServices.GetPoliciesForContent(policyPart); //Reading policies Ids for that content. Ids are in strings i.e. "{12}"

            if (associatedPolicies.Count() == 0)
            {
                return(new List <UserPolicyAnswerWithContent>());
            }

            var contentPolicies = _policyServices.GetPolicies(null, associatedPolicies.Select(x => Convert.ToInt32(x.Substring(1, x.Length - 2) /*Strips {} chars*/)).ToArray());

            return(contentPolicies.Select(x => new UserPolicyAnswerWithContent {
                PolicyAnswer = false,
                PolicyId = x.Id,
                UserHaveToAccept = x.UserHaveToAccept,
                PolicyText = x.ContentItem,
                Policy = new PolicyTextViewModel {
                    Type = x.PolicyType,
                    Title = x.As <TitlePart>()?.Title,
                    Body = x.As <BodyPart>()?.Text
                }
            }).ToList());
        }
Exemplo n.º 4
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var    routeData      = HttpContext.Current.Request.RequestContext.RouteData;
            string areaName       = (routeData.Values["area"] ?? string.Empty).ToString();
            string controllerName = (routeData.Values["controller"] ?? string.Empty).ToString();
            string actionName     = (routeData.Values["action"] ?? string.Empty).ToString();

            if (areaName.Equals("Laser.Orchard.WebServices", StringComparison.InvariantCultureIgnoreCase) &&
                controllerName.Equals("WebApi", StringComparison.InvariantCultureIgnoreCase) &&
                actionName.Equals("display", StringComparison.InvariantCultureIgnoreCase))
            {
                SetPendingPolicies();
                if (pendingPolicies != null && pendingPolicies.Count() > 0)
                {
                    JObject json;
                    json = new JObject();
                    var resultArray = new JArray();
                    foreach (var pendingPolicy in pendingPolicies)
                    {
                        resultArray.Add(_contentSerializationServices.GetJson((ContentItem)pendingPolicy, 0));
                    }
                    json.Add("PendingPolicies", resultArray);
                    //_contentSerializationServices.NormalizeSingleProperty(json);
                    filterContext.Result = new ContentResult {
                        Content = json.ToString(Newtonsoft.Json.Formatting.None), ContentType = "application/json"
                    };
                    //return GetJson(content, page, pageSize);
                }
            }
            else if (areaName.Equals("Orchard.CustomForms", StringComparison.InvariantCultureIgnoreCase) &&
                     controllerName.Equals("Item", StringComparison.InvariantCultureIgnoreCase) &&
                     actionName.Equals("Create", StringComparison.InvariantCultureIgnoreCase))
            {
                // When we are trying to "protect" access to a CustomForm, we need to do it through a filter
                // because displaying it goes through its own controller rather than the Display Action.
                // Get the content correspnding to the CustomForm
                int contentId;
                if (int.TryParse(routeData.Values["id"]?.ToString(), out contentId))
                {
                    var content = _contentManager.Get(contentId);
                    if (content != null)
                    {
                        var policyPart = content.As <PolicyPart>();
                        if (policyPart != null &&
                            (_policyServices.HasPendingPolicies(content) ?? false))
                        {
                            // Replicates the logic we have in PolicyPartDriver to redirect the user
                            // to the action to accept the required policies
                            var language = _workContextAccessor.GetContext().CurrentCulture;

                            var associatedPolicies        = _policyServices.GetPoliciesForContent(policyPart);
                            var encodedAssociatedPolicies = "";
                            if (associatedPolicies != null)
                            {
                                encodedAssociatedPolicies = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(",", associatedPolicies)));
                            }
                            else
                            {
                                encodedAssociatedPolicies = Convert.ToBase64String(Encoding.UTF8.GetBytes(""));
                            }

                            var cookie = HttpContext.Current.Request.Cookies["PoliciesAnswers"];
                            if (cookie != null && cookie.Value != null)
                            {
                                HttpContext.Current.Response.Cookies.Add(HttpContext.Current.Request.Cookies["PoliciesAnswers"]);
                            }
                            var rvd = new RouteValueDictionary();
                            rvd["area"]       = "Laser.Orchard.Policy";
                            rvd["controller"] = "Policies";
                            rvd["action"]     = "Index";
                            rvd["lang"]       = language;
                            rvd["policies"]   = encodedAssociatedPolicies;
                            rvd["returnUrl"]  = HttpContext.Current.Request.RawUrl;
                            var autoroutePart = policyPart.As <AutoroutePart>();
                            if (autoroutePart != null && !string.IsNullOrWhiteSpace(autoroutePart.DisplayAlias))
                            {
                                rvd["alias"] = autoroutePart.DisplayAlias;
                            }

                            filterContext.Result = new RedirectToRouteResult(rvd);
                        }
                    }
                }
            }
        }