Esempio n. 1
0
        public ActionResult CountryRolePageElementSave(FormCollection permissions)
        {
            if (!Permission.CanUpdatePermissions)
            {
                return(new HttpUnauthorizedResult("User does not have permission to update Page Element permissions"));
            }

            if (!long.TryParse(permissions.Get("PageElementID"), out long pageElementID))
            {
                return(HttpNotFound("Could not find Page Element"));
            }

            HashSet <int> handledNewLines      = new HashSet <int>();
            HashSet <int> handledExistingLines = new HashSet <int>();
            Context       context     = new Context();
            PageElement   pageElement = context.PageElements.FirstOrDefault(pe => pe.PageElementID == pageElementID);

            pageElement.ForAuthenticatedOnly = bool.Parse(permissions["IsForAuthenticated"]);
            foreach (string key in permissions.AllKeys)
            {
                string[] parts = key.Split('-');
                int      countryRolePageElementID;
                switch (parts[0])
                {
                case "new":
                    if (!int.TryParse(parts[2], out countryRolePageElementID))
                    {
                        continue;
                    }

                    if (!handledNewLines.Add(countryRolePageElementID))
                    {
                        continue;
                    }

                    string countryRoleIDString = permissions[string.Format("new-perm-{0}-countryroleid", countryRolePageElementID)];
                    string canViewString       = permissions[string.Format("new-perm-{0}-canview", countryRolePageElementID)];
                    string canEditString       = permissions[string.Format("new-perm-{0}-canedit", countryRolePageElementID)];

                    if (!int.TryParse(countryRoleIDString, out int countryRoleID) ||
                        !bool.TryParse(canViewString, out bool canView) ||
                        !bool.TryParse(canEditString, out bool canEdit))
                    {
                        continue;
                    }

                    CountryRolePageElement newCountryRolePageElement = new CountryRolePageElement();
                    newCountryRolePageElement.CountryRoleID = countryRoleID;
                    newCountryRolePageElement.PageElementID = pageElementID;
                    newCountryRolePageElement.CanView       = canView;
                    newCountryRolePageElement.CanEdit       = canEdit;

                    context.CountryRolePageElements.Add(newCountryRolePageElement);
                    break;

                case "removed":
                    if (!int.TryParse(parts[2], out countryRolePageElementID))
                    {
                        continue;
                    }

                    if (!handledExistingLines.Add(countryRolePageElementID))
                    {
                        continue;
                    }

                    CountryRolePageElement countryRolePageElementToDelete = context.CountryRolePageElements.FirstOrDefault(crpe => crpe.CountryRolePageElementID == countryRolePageElementID);
                    if (countryRolePageElementToDelete != null)
                    {
                        context.CountryRolePageElements.Remove(countryRolePageElementToDelete);
                    }

                    break;

                case "perm":
                    if (!int.TryParse(parts[1], out countryRolePageElementID))
                    {
                        continue;
                    }

                    if (!handledExistingLines.Add(countryRolePageElementID))
                    {
                        continue;
                    }

                    CountryRolePageElement existingCountryRolePageElement = context.CountryRolePageElements.FirstOrDefault(crpe => crpe.CountryRolePageElementID == countryRolePageElementID);
                    if (existingCountryRolePageElement == null)
                    {
                        continue;
                    }

                    string updateCanViewString = permissions[string.Format("perm-{0}-canview", countryRolePageElementID)];
                    string updateCanEditString = permissions[string.Format("perm-{0}-canedit", countryRolePageElementID)];

                    if (!bool.TryParse(updateCanViewString, out bool updateCanView) ||
                        !bool.TryParse(updateCanEditString, out bool updateCanEdit))
                    {
                        continue;
                    }

                    existingCountryRolePageElement.CanView = updateCanView;
                    existingCountryRolePageElement.CanEdit = updateCanEdit;
                    break;
                }
            }

            try
            {
                context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                return(ModalSaveFailedResult(ex));
            }

            return(Json(new { success = true }));
        }
Esempio n. 2
0
        public ActionResult PageElementAdd(FormCollection collection)
        {
            if (!Permission.CanAddPages)
            {
                return(new HttpUnauthorizedResult("User does not have permission to Add Page Elements"));
            }

            Dictionary <string, string> errors = new Dictionary <string, string>();

            if (!collection.AllKeys.Contains("ElementType"))
            {
                errors.Add("ElementType", "Element Type is a required field");
            }

            int pageID = -1;

            if (!collection.AllKeys.Contains("PageID") || !int.TryParse(collection["PageID"], out pageID))
            {
                errors.Add("ElementType", "Page ID is required");
            }

            if (errors.Any())
            {
                return(Json(new { success = false, errors }));
            }

            string elementType = collection["ElementType"];

            Type pageContentViewPartType = typeof(PageContentViewPart);
            Type selectedType            = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).FirstOrDefault(t => t != pageContentViewPartType && pageContentViewPartType.IsAssignableFrom(t) && t.Name == elementType);

            if (selectedType == null)
            {
                errors.Add("ElementType", "Element Type is a required field");

                return(Json(new { success = false, errors }));
            }

            PageContentViewPart pageContentViewPart = (PageContentViewPart)Activator.CreateInstance(selectedType);
            XElement            data = new XElement("element", new XAttribute("type", selectedType.FullName));

            pageContentViewPart.WriteToXML(data);

            Context context = new Context();

            PageElement newPageElement = new PageElement();

            newPageElement.PageID                  = pageID;
            newPageElement.ElementXML              = data.ToString();
            newPageElement.DisplayOrder            = (byte)(context.PageElements.Count() + 1);
            newPageElement.CountryRolePageElements = new List <CountryRolePageElement>();

            foreach (CountryRole countryRole in context.CountryRoles.Where(cr => cr.CountryID == CountryID))
            {
                CountryRolePageElement countryRolePageElement = new CountryRolePageElement();
                countryRolePageElement.CountryRoleID = countryRole.CountryRoleID.Value;
                countryRolePageElement.CanView       = true;
                countryRolePageElement.CanEdit       = true;
                newPageElement.CountryRolePageElements.Add(countryRolePageElement);
            }

            context.PageElements.Add(newPageElement);

            try
            {
                context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                return(ModalSaveFailedResult(ex));
            }

            return(Json(new { success = true }));
        }
Esempio n. 3
0
        private IEnumerable <ViewPart> GetPageElementPermCountryRolesViewParts(PageElement pageElement)
        {
            yield return(new Header()
            {
                HeaderType = Header.HeaderTypes.H4,
                Text = "Who can make edits to this Element?"
            });

            Table table = new Table();

            Table.Row    headerRow  = table.Header.CreateRow();
            Table.Column nameHeader = headerRow.CreateColumn();
            nameHeader.Class.Add("col-sm-11");
            nameHeader.Parts.Add(new Literal()
            {
                LiteralString = "Country Role"
            });

            Table.Column buttonHeader = headerRow.CreateColumn();
            buttonHeader.Class.Add("col-sm-1");
            buttonHeader.Parts.Add(new Literal()
            {
                LiteralString = "Delete"
            });

            for (int i = 0; i < pageElement.CountryRolePageElements.Count; i++)
            {
                CountryRolePageElement perm = pageElement.CountryRolePageElements.ElementAt(i);
                string idPrefix             = string.Format("perm-{0}-", perm.CountryRolePageElementID.Value);

                Table.Row    row     = table.Body.CreateRow();
                Table.Column nameCol = row.CreateColumn();
                nameCol.Class.Add("col-sm-11");
                nameCol.Parts.Add(new Literal()
                {
                    LiteralString = perm.CountryRole.Name
                });
                nameCol.Parts.Add(new Checkbox()
                {
                    Id      = idPrefix + "canview",
                    Text    = "Can View?",
                    Checked = perm.CanView
                });
                nameCol.Parts.Add(new Checkbox()
                {
                    Id      = idPrefix + "canedit",
                    Text    = "Can Edit?",
                    Checked = perm.CanEdit
                });
                nameCol.Parts.Add(new Hidden()
                {
                    Id    = idPrefix + "countryrolepagelementid",
                    Value = perm.CountryRolePageElementID.Value.ToString()
                });

                Table.Column removeCol = row.CreateColumn();
                removeCol.Class.Add("col-sm-1");
                Button removeButton = new Button()
                {
                    Type  = "button",
                    Class = { "btn", "btn-danger" },
                    ViewPartsBeforeText = new List <ViewPart>()
                    {
                        new Span()
                        {
                            Class = { "fas", "fa-trash" }
                        }
                    },
                    Text    = " Delete",
                    OnClick = "managePageElementPermissionsRemoveCountryRole($(this));"
                };
                removeCol.Parts.Add(removeButton);
            }

            Table.Row templateRow = table.Body.CreateRow();
            templateRow.Class.Add("d-none");
            templateRow.Class.Add("templateRow");

            string templateIdPrefix = "template-perm-";

            Table.Column nameColumn   = templateRow.CreateColumn();
            ComboBox     nameDropDown = new ComboBox();

            foreach (CountryRole role in pageElement.Page.Country.CountryRoles)
            {
                nameDropDown.SelectItems.Add(new ComboBox.Item()
                {
                    Text  = role.Name,
                    Value = role.CountryRoleID?.ToString()
                });
            }
            nameDropDown.Class.Add("form-control");
            nameDropDown.Id = templateIdPrefix + "countryroleid";
            nameColumn.Parts.Add(nameDropDown);
            nameColumn.Parts.Add(new Label()
            {
                Class = { "text-danger" },
                Data  = { { "validate-message-for", "CountryRoleID" } }
            });
            nameColumn.Parts.Add(new Checkbox()
            {
                Id   = templateIdPrefix + "canview",
                Text = "Can View?"
            });
            nameColumn.Parts.Add(new Checkbox()
            {
                Id   = templateIdPrefix + "canedit",
                Text = "Can Edit?"
            });

            Table.Column deleteCol = templateRow.CreateColumn();
            deleteCol.Class.Add("col-sm-1");
            Button deleteTemplate = new Button()
            {
                Type  = "button",
                Class = { "btn", "btn-danger" },
                ViewPartsBeforeText = new List <ViewPart>()
                {
                    new Span()
                    {
                        Class = { "fas", "fa-trash" }
                    }
                },
                Text    = " Delete",
                OnClick = "managePageElementPermissionsRemoveCountryRole($(this));"
            };

            deleteCol.Parts.Add(deleteTemplate);

            yield return(table);

            Button add = new Button()
            {
                Type  = "button",
                Class = { "btn", "btn-primary" },
                ViewPartsBeforeText = new List <ViewPart>()
                {
                    new Span()
                    {
                        Class = { "fas", "fa-plus-circle" }
                    }
                },
                Text    = " Add",
                OnClick = "managePageElementPermissionsAddCountryRole($(this));"
            };

            yield return(add);
        }