protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                string roleName = ViewState["RoleName"].ToString();
                if (roleName == "")
                {
                    return;
                }

                string[] userNames = Roles.GetUsersInRole(roleName);
                if (userNames.Length > 0)
                {
                    return;
                }

                //Roles.RemoveUsersFromRole(userNames, roleName);

                Roles.DeleteRole(roleName);

                RolePermissions        roleController = new RolePermissions();
                List <RolePermissions> list           = roleController.GetByRolePermissionName(roleName);

                if (list != null)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        RolePermissions role = list[i];
                        roleController.Delete(role.RolePermissionID);
                    }
                }

                //WebHelper.LogAction("dbo.RolePermissions", ViewState["RoleName"].ToString(), ActionLog.LogAction.Delete);

                this.Redirect();
            }
            catch (Exception ex)
            {
            }
        }
        private void SavePermissions(string roleName)
        {
            RolePermissions        roleController = new RolePermissions();
            List <RolePermissions> list           = roleController.GetByRolePermissionName(roleName);

            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    RolePermissions role = list[i];
                    roleController.Delete(role.RolePermissionID);
                }
                list.Clear();
            }
            else
            {
                list = new List <RolePermissions>();
            }

            foreach (GridViewRow row in permissionGridView.Rows)
            {
                CheckBox accessableCheckBox = (CheckBox)row.FindControl("accessableCheckBox");
                if (accessableCheckBox == null || !accessableCheckBox.Checked)
                {
                    continue;
                }

                RolePermissions item = new RolePermissions();
                item.RoleName   = roleName;
                item.PageName   = row.Cells[0].Text;
                item.Accessable = accessableCheckBox.Checked;

                list.Add(item);
                roleController.Add(item);
            }

            UpdateAllowedPagesInSession(list, roleName);
        }
        private void ShowRoleDetail()
        {
            try
            {
                string userName = WebHelper.GetCurrentUserName().ToLower();
                if (userName == "admin" && ViewState["RoleName"].ToString() == "")
                {
                    deleteButton.Visible = false;
                    return;
                }
                else if (ViewState["RoleName"].ToString() == "")
                {
                    deleteButton.Visible    = false;
                    saveButton.Enabled      = false;
                    roleNameTextBox.Enabled = false;
                    roleNameTextBox.Focus();

                    return;
                }

                if (WebHelper.GetCurrentUserName().ToLower() == "admin")
                {
                    deleteButton.Visible = true;
                }
                else
                {
                    deleteButton.Visible = false;
                }
                roleNameTextBox.Text    = ViewState["RoleName"].ToString();
                roleNameTextBox.Enabled = false;

                if (!Roles.RoleExists(roleNameTextBox.Text))
                {
                    deleteButton.Visible = false;
                    saveButton.Enabled   = false;
                    return;
                }

                RolePermissions        roleController = new RolePermissions();
                List <RolePermissions> list           = roleController.GetByRolePermissionName(roleNameTextBox.Text.Trim());
                if (list == null)
                {
                    return;
                }

                for (int i = 0; i < list.Count; i++)
                {
                    if (pageList[list[i].PageName] == null)
                    {
                        continue;
                    }

                    int pageIndex = int.Parse(pageList[list[i].PageName].ToString());

                    GridViewRow row = permissionGridView.Rows[pageIndex];
                    CheckBox    accessableCheckBox = (CheckBox)row.FindControl("accessableCheckBox");

                    if (accessableCheckBox == null)
                    {
                        continue;
                    }
                    accessableCheckBox.Checked = (bool)list[i].Accessable;
                }
            }
            catch (Exception ex)
            {
            }
        }
Esempio n. 4
0
        public static bool AuthorizeUser(string userName, string pageName)
        {
            try
            {
                /*if (userName.ToLower().Trim() == "admin")
                 * {
                 *
                 *  return true;
                 * }*/

                if (pageName.Contains("login") || pageName.Contains("permissiondenied"))
                {
                    return(true);
                }

                string[] roleName = Roles.GetRolesForUser(userName);
                if (roleName == null || roleName.Length == 0)
                {
                    return(false);
                }
                RolePermissions        roleController = new RolePermissions();
                List <RolePermissions> allowedPages   = null;


                if (HttpContext.Current.Session["AllowedPages"] != null)
                {
                    allowedPages = (List <RolePermissions>)HttpContext.Current.Session["AllowedPages"];
                }
                else
                {
                    allowedPages = new List <RolePermissions>();
                    for (int i = 0; i < roleName.Length; i++)
                    {
                        List <RolePermissions> allowedPage = roleController.GetByRolePermissionName(roleName[i]);
                        if (allowedPage != null || allowedPage.Count > 0)
                        {
                            allowedPages.AddRange(allowedPage);
                        }
                    }
                    HttpContext.Current.Session.Add("AllowedPages", allowedPages);
                    Constants.SessionKeys.My_Session = Guid.NewGuid().ToString();
                }

                if (allowedPages != null && allowedPages.Count > 0)
                {
                    foreach (RolePermissions allowedPage in allowedPages)
                    {
                        if (allowedPage.PageName.ToLower().Trim().Contains(pageName))
                        {
                            return((bool)allowedPage.Accessable);
                        }
                    }
                    return(false);
                }


                return((bool)allowedPages[0].Accessable);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }