Esempio n. 1
0
        private void AddOrRemoveUserToRoles(UserToRoles addOrRemove)
        {
            try
            {
                string   username           = allUsersInUserToRole_comboBox.SelectedItem.ToString();
                string[] checkedRolesArray  = RolesHelper.GetAllCheckedItemsArray(this.userToRole_groupBox);
                string   checkedRolesString = RolesHelper.GetAllCheckedItems(this.userToRole_groupBox);

                switch (addOrRemove)
                {
                case UserToRoles.Add:
                {
                    try
                    {
                        Roles.AddUserToRoles(username, checkedRolesArray);
                        SampleMessageBox.Show(String.Format("Successfully add {0} to  {1} role(s)", username, checkedRolesString));
                    }
                    catch (ProviderException)
                    {
                        if (checkedRolesArray.Length == 1)
                        {
                            SampleMessageBox.Show(String.Format("{0} is already in {1} role", username, checkedRolesString));
                        }
                        else
                        {
                            SampleMessageBox.Show(String.Format("{0} is already in some of these roles:\n{1} ", username, checkedRolesString));
                        }
                    }
                    catch (ArgumentException)
                    {
                        SampleMessageBox.Show("Please check some of roles");
                    }
                    break;
                }

                case UserToRoles.Remove:
                {
                    try
                    {
                        Roles.RemoveUserFromRoles(username, checkedRolesArray);
                        SampleMessageBox.Show(String.Format("Successfully remove {0} from {1} role(s)", username, checkedRolesString));
                    }
                    catch (ArgumentException)
                    {
                        SampleMessageBox.Show("Please check some of roles");
                    }
                    break;
                }
                }
                DisplayUsers(Display.FirstPage);
                RolesHelper.UncheckedAllCheckboxes(this.userToRole_groupBox);
                allUsersInUserToRole_comboBox.SelectedIndex = -1;
            }
            catch (NullReferenceException)
            {
                SampleMessageBox.Show("Please, select user!");
            }
        }
        private void deleteRoles_btn_Click(object sender, EventArgs e)
        {
            string[] checkedRolesArray  = RolesHelper.GetAllCheckedItemsArray(this.deleteRoles_groupBox);
            string   checkedRolesString = RolesHelper.GetAllCheckedItems(this.deleteRoles_groupBox);
            int      counter            = 0;

            for (int i = 0; i < checkedRolesArray.Length; i++)
            {
                try
                {
                    string[] usersInRole = Roles.GetUsersInRole(checkedRolesArray[i]);

                    if (usersInRole.Length > 0)
                    {
                        Roles.RemoveUsersFromRole(usersInRole, checkedRolesArray[i]);
                    }

                    if (Roles.DeleteRole(checkedRolesArray[i], true))
                    {
                        counter++;
                    }
                }
                catch (Exception ex)
                {
                    SampleMessageBox.Show(ex.Message);
                }
            }

            if (counter != 0)
            {
                SampleMessageBox.Show(String.Format("Successfully deleted {0} role(s)", checkedRolesString));
                RolesHelper.UncheckedAllCheckboxes(this.deleteRoles_groupBox);
                AddCheckBoxesRolesInAllContainers();
                DisplayUsers(Display.FirstPage);
            }
            else
            {
                SampleMessageBox.Show("Please,check some of roles to delete it");
            }
        }
        private void AddOrRemoveRolesToForm(RolesToForm addOrRemove)
        {
            string[] checkedRolesArray  = RolesHelper.GetAllCheckedItemsArray(this.rolesToForm_groupBox);
            string   checkedRolesString = RolesHelper.GetAllCheckedItems(this.rolesToForm_groupBox);

            int  addCounter        = 0;
            int  removeCounter     = 0;
            bool isRoleExistToForm = false;

            try
            {
                string selectedForm = this.formsRolesCollections_comboBox.SelectedItem.ToString();

                if (checkedRolesArray.Length != 0)
                {
                    foreach (SettingsProperty currentProperty in RolesForSpecificForm.Default.Properties)
                    {
                        if (currentProperty.Name.Equals(selectedForm))
                        {
                            foreach (string role in checkedRolesArray)
                            {
                                if (currentProperty.PropertyType == typeof(StringCollection))
                                {
                                    if (RolesForSpecificForm.Default[currentProperty.Name] == null)
                                    {
                                        RolesForSpecificForm.Default[currentProperty.Name] = new StringCollection();
                                    }
                                    switch (addOrRemove)
                                    {
                                    case RolesToForm.Add:
                                    {
                                        if (!(RolesForSpecificForm.Default[currentProperty.Name] as StringCollection).Contains(role))
                                        {
                                            try
                                            {
                                                (RolesForSpecificForm.Default[currentProperty.Name] as StringCollection).Add(role);
                                                RolesForSpecificForm.Default.Save();
                                                addCounter++;
                                            }
                                            catch (Exception ex)
                                            {
                                                SampleMessageBox.Show(ex.Message);
                                            }
                                        }
                                        else
                                        {
                                            isRoleExistToForm = true;
                                        }
                                        break;
                                    }

                                    case RolesToForm.Remove:
                                    {
                                        try
                                        {
                                            (RolesForSpecificForm.Default[currentProperty.Name] as StringCollection).Remove(role);
                                            RolesForSpecificForm.Default.Save();
                                            removeCounter++;
                                        }
                                        catch (Exception ex)
                                        {
                                            SampleMessageBox.Show(ex.Message);
                                        }
                                        break;
                                    }
                                    }
                                }
                            }
                        }
                    }
                    if (isRoleExistToForm)
                    {
                        SampleMessageBox.Show(String.Format("{0} role(s) already to {1} form", checkedRolesString, selectedForm));
                    }
                    if (addCounter == checkedRolesArray.Length)
                    {
                        SampleMessageBox.Show(String.Format("Successfully add {0} role(s) to {1} form", checkedRolesString, selectedForm));
                    }
                    if (removeCounter == checkedRolesArray.Length)
                    {
                        SampleMessageBox.Show(String.Format("Successfully remove {0} role(s) from {1} form", checkedRolesString, selectedForm));
                    }
                }
                else
                {
                    SampleMessageBox.Show(String.Format("Please first check some of roles"));
                }
            }
            catch (NullReferenceException)
            {
                SampleMessageBox.Show("Please first select FormsRolesCollection");
            }

            DisplayFormsRoles(Display.FirstPage);
            RolesHelper.UncheckedAllCheckboxes(this.rolesToForm_groupBox);
            this.formsRolesCollections_comboBox.SelectedIndex = -1;
        }