protected void RemoveUserFromAllTemplateRoles(String username) { if (RoleAdapter.IsUserInRole(username, "db_Custom")) { RoleAdapter.RemoveUserFromRole(username, "db_Custom"); } // Ensure Admin always stays admin when editing their own permissions if (!(RoleAdapter.IsUserInRole("db_Admin") && dd_user.SelectedItem.Text == User.Identity.Name) || User.Identity.Name == "jpickering") { if (RoleAdapter.IsUserInRole(username, "db_Admin")) { RoleAdapter.RemoveUserFromRole(username, "db_Admin"); } } if (RoleAdapter.IsUserInRole(username, "db_HoS")) { RoleAdapter.RemoveUserFromRole(username, "db_HoS"); } if (RoleAdapter.IsUserInRole(username, "db_TeamLeader")) { RoleAdapter.RemoveUserFromRole(username, "db_TeamLeader"); } if (RoleAdapter.IsUserInRole(username, "db_Finance")) { RoleAdapter.RemoveUserFromRole(username, "db_Finance"); } if (RoleAdapter.IsUserInRole(username, "db_GroupUser")) { RoleAdapter.RemoveUserFromRole(username, "db_GroupUser"); } if (RoleAdapter.IsUserInRole(username, "db_User")) { RoleAdapter.RemoveUserFromRole(username, "db_User"); } if (RoleAdapter.IsUserInRole(username, "db_CCA")) { RoleAdapter.RemoveUserFromRole(username, "db_CCA"); } if (RoleAdapter.IsUserInRole(username, "db_SalesBookOfficeAdmin")) { RoleAdapter.RemoveUserFromRole(username, "db_SalesBookOfficeAdmin"); } if (RoleAdapter.IsUserInRole(username, "db_SalesBookDesign")) { RoleAdapter.RemoveUserFromRole(username, "db_SalesBookDesign"); } }
// Save protected void SaveRoles(object sender, EventArgs e) { if (dd_user.Items.Count > 0 && dd_user.SelectedItem != null) { String username = dd_user.SelectedItem.Text; RemoveUserFromAllTemplateRoles(username); // Add new template role if (!RoleAdapter.IsUserInRole(username, dd_templates.SelectedItem.Value)) { Roles.AddUserToRole(username, dd_templates.SelectedItem.Value); } // Iterate roles and save foreach (HtmlTableRow row in tbl_main.Controls) { foreach (HtmlTableCell cell in row.Controls) { for (int i = 0; i < cell.Controls.Count; i++) { Control c = cell.Controls[i] as Control; if (c is CheckBox) { CheckBox cb = c as CheckBox; String role = cb.ToolTip; if (cb.Checked) { cb.BackColor = Color.Green; if (!RoleAdapter.IsUserInRole(username, role)) { RoleAdapter.AddUserToRole(username, role); } } else { cb.BackColor = Color.Red; if (RoleAdapter.IsUserInRole(username, role)) { RoleAdapter.RemoveUserFromRole(username, role); } } // Save territory limited roles if (cb.ID.Substring(cb.ID.Length - 2, 2) == "tl") { HtmlTableRow tr_tl = (HtmlTableRow)cb.Parent.Parent.FindControl("tr_tl_" + cb.ToolTip); if (tr_tl != null) { foreach (HtmlTableCell tl_cell in tr_tl.Controls) { for (int z = 0; z < tl_cell.Controls.Count; z++) { c = tl_cell.Controls[z] as Control; if (c is CheckBox && tr_tl.Visible) { cb = c as CheckBox; role = cb.ToolTip; if (cb.Checked) { if (!RoleAdapter.IsUserInRole(username, role)) { RoleAdapter.AddUserToRole(username, role); } } else { if (RoleAdapter.IsUserInRole(username, role)) { RoleAdapter.RemoveUserFromRole(username, role); } } } } } } } } else if (c is RadioButtonList) { RadioButtonList rbl = c as RadioButtonList; if (rbl.Visible) { foreach (ListItem li in rbl.Items) { if (li.Value != String.Empty) { if (li.Selected) { if (!RoleAdapter.IsUserInRole(username, li.Value)) { RoleAdapter.AddUserToRole(username, li.Value); } } else { // Ensure Admin always stays admin when editing own permissions if (!(RoleAdapter.IsUserInRole("db_Admin") && dd_user.SelectedItem.Text == HttpContext.Current.User.Identity.Name && li.Value == "db_Admin")) { if (RoleAdapter.IsUserInRole(username, li.Value)) { RoleAdapter.RemoveUserFromRole(username, li.Value); } } } } } if (rbl.SelectedIndex == -1) { rbl.SelectedIndex = 0; } } } } } } Util.PageMessage(this, "User permissions saved."); Util.WriteLogWithDetails("Roles saved for " + dd_user.SelectedItem.Text + ".", "rolesmanagement_log"); } }