Пример #1
0
    // 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");
        }
    }
Пример #2
0
 public ServiceResult <object> AddUserToRole(string roleId, string account)
 {
     RoleAdapter.AddUserToRole(roleId, account);
     return(new ServiceResult <object>(null));
 }