Ejemplo n.º 1
0
        void HandlePermissionsLinkUpdates()
        {
            if (!IsHttpPost) return;
            if (!AntiForgeryTokenValid) return;
            var Form = HttpContext.Request.Form;
            string Button = Form["permissionslinkbutton"];
            if (String.IsNullOrWhiteSpace(Button)) return;

            int CategoryID, PermissionsID, RoleID;

            try {
                CategoryID = Convert.ToInt32(Form["Category"]);
                PermissionsID = Convert.ToInt32(Form["PermissionSet"]);
                RoleID = Convert.ToInt32(Form["UserGroup"]);
            } catch (FormatException)
            {
                ModelState.AddModelError("InvalidData", "Numbers are invalid");
                return;
            } catch (OverflowException)
            {
                ModelState.AddModelError("InvalidData", "Numbers are too big");
                return;
            }

            using (ForumRespository db = new ForumRespository())
            {
                var PermissionsLink = db.GetPermissionLinks().SingleOrDefault(L => L.CategoryID == CategoryID && L.PermissionID == PermissionsID && L.RoleID == RoleID);

                if (Button == "Add")
                {
                    if (PermissionsLink != null)
                    {
                        ModelState.AddModelError("AddLinkError", "Permission link already exists");
                        return;
                    }

                    db.AddPermissionsLink(new Forum_PermissionsLink() { CategoryID = CategoryID, PermissionID = PermissionsID, RoleID = RoleID });

                    try
                    {
                        db.Save();
                    }
                    catch
                    {
                        ModelState.AddModelError("AddLinkError", "Unable to add new permissions link");
                    }
                }
                else if (Button == "Delete")
                {
                    if (PermissionsLink == null)
                    {
                        ModelState.AddModelError("DeleteLinkError", "Permission link doesn't exists");
                        return;
                    }

                    db.DeletePermissionsLink(PermissionsLink);
                    try
                    {
                        db.Save();
                    }
                    catch
                    {
                        ModelState.AddModelError("DeleteLinkError", "Unable to delete permissions link");
                    }
                }
            }
        }