Example #1
0
        private void pic_Import_Click(object sender, EventArgs e)
        {
            openFileDialog_Import.InitialDirectory = SpecialDirectories.MyComputer;

            DialogResult Result = openFileDialog_Import.ShowDialog();

            using (Database DB = new Database())
                if (Result == DialogResult.OK)
                {
                    try
                    {
                        string ImportDatabasePath = openFileDialog_Import.FileName;
                        ImportDatabasePath = Path.GetFullPath(ImportDatabasePath);
                        long ImportedResults = DB.Import(ImportDatabasePath);
                        MaterialMessageBox.Show("Database has been imported " + ImportedResults + " results successfully!", "Easy Survey - Database Import", MaterialMessageBox.MessageBoxButtons.OK, MaterialMessageBox.MessageBoxIcon.Information);
                        // Program.frm_MainForm.Close();
                        // base.Close();
                    }
                    catch (Exception ex)
                    {
                        MaterialMessageBox.Show(ex.Message, "Easy Survey - Import Database", MaterialMessageBox.MessageBoxButtons.OK, MaterialMessageBox.MessageBoxIcon.Error);
                    }
                }
        }
Example #2
0
        // Promote / Demote users
        private void promoteDemoteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int SelectedUsersCount = listView_Users.SelectedItems.Count;

            if (SelectedUsersCount == 0)
            {
                return;
            }

            long AdminCount;
            long SelectedAdminCount;

            long AdminRoleID;
            long UserRoleID;

            using (UserController userController = new UserController())
                using (RoleController roleController = new RoleController())
                {
                    AdminRoleID = roleController.GetRoleID("Admin");
                    UserRoleID  = roleController.GetRoleID("User");

                    // Check if Admin want to demote all Admins
                    List <UserModelDataTransferObject> AdminsList = userController.GetUsersByRoleID(AdminRoleID);
                    AdminCount         = AdminsList.Count;
                    SelectedAdminCount = 0;

                    foreach (ListViewItem item in listView_Users.SelectedItems)
                    {
                        long UserID = Convert.ToInt64(item.Tag);
                        UserModelDataTransferObject CurrentUser = userController.GetUserByID(UserID);
                        if (CurrentUser.IsAdministrator())
                        {
                            ++SelectedAdminCount;
                        }
                    }
                }

            if (AdminCount == SelectedAdminCount && AdminCount == listView_Users.SelectedItems.Count)
            {
                MaterialMessageBox.Show("You cannot demote all Administrators.", "Easy Survey - Demote Users", MaterialMessageBox.MessageBoxButtons.OK, MaterialMessageBox.MessageBoxIcon.Error);
                return;
            }


            // Check if Admin wants to demote himself
            bool AdminSelectedHimself = false;

            foreach (ListViewItem item in listView_Users.SelectedItems)
            {
                long UserID = Convert.ToInt64(item.Tag);
                if (LoggedUser.UserID == UserID)
                {
                    AdminSelectedHimself = true; break;
                }
            }
            if (AdminSelectedHimself)
            {
                MaterialMessageBox.Show("You cannot demote yourself.", "Easy Survey - Demote Users", MaterialMessageBox.MessageBoxButtons.OK, MaterialMessageBox.MessageBoxIcon.Error);
                return;
            }

            // Demote / Promote selected users.
            using (UserController userController = new UserController())
                using (UserRoleController userRoleController = new UserRoleController())
                    foreach (ListViewItem item in listView_Users.SelectedItems)
                    {
                        long UserID = Convert.ToInt64(item.Tag);
                        UserModelDataTransferObject CurrentUser = userController.GetUserByID(UserID);
                        if (CurrentUser.IsAdministrator()) // Demote to standard user.
                        {
                            userRoleController.SetUserRole(UserID, UserRoleID);
                            item.Group = listView_Users.Groups["User"];
                        }
                        else if (!CurrentUser.IsAdministrator()) // Promote to admin.
                        {
                            userRoleController.SetUserRole(UserID, AdminRoleID);
                            item.Group = listView_Users.Groups["Administrator"];
                        }
                    }
        }
Example #3
0
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int SelectedUsersCount = listView_Users.SelectedItems.Count;

            if (SelectedUsersCount == 0)
            {
                return;
            }

            IEnumerable <ListViewItem> SelectedUsers = listView_Users.SelectedItems.Cast <ListViewItem>();

            using (UserController userController = new UserController())
                using (RoleController roleController = new RoleController())
                {
                    // Check if Admin selected all Admins to be deleted.
                    long AdminRoleID = roleController.GetRoleID("Admin");
                    List <UserModelDataTransferObject> AdminsList = userController.GetUsersByRoleID(AdminRoleID);
                    long AdminCount         = AdminsList.Count;
                    long SelectedAdminCount = 0;
                    foreach (ListViewItem item in SelectedUsers)
                    {
                        long UserID = Convert.ToInt64(item.Tag);
                        UserModelDataTransferObject SelectedUser = userController.GetUserByID(UserID);
                        if (SelectedUser.IsAdministrator())
                        {
                            ++SelectedAdminCount;
                        }
                    }
                    if (AdminCount == SelectedAdminCount)
                    {
                        MaterialMessageBox.Show("You cannot delete all Administrators.", "Easy Survey - Delete Users", MaterialMessageBox.MessageBoxButtons.OK, MaterialMessageBox.MessageBoxIcon.Error);
                        return;
                    }

                    // Check if Admin selected his account to de deleted.
                    bool SelectedSelfAccount = false;
                    foreach (ListViewItem item in SelectedUsers)
                    {
                        long UserID = Convert.ToInt64(item.Tag);
                        if (LoggedUser.UserID == UserID)
                        {
                            SelectedSelfAccount = true; break;
                        }
                    }
                    if (SelectedSelfAccount)
                    {
                        MaterialMessageBox.Show("You cannot delete your own account.", "Easy Survey - Delete Users", MaterialMessageBox.MessageBoxButtons.OK, MaterialMessageBox.MessageBoxIcon.Error);
                        return;
                    }

                    // Delete selected users.
                    MaterialMessageBox.MessageBoxResult result = MaterialMessageBox.MessageBoxResult.None;
                    result = MaterialMessageBox.Show("Are you sure you want to delete all " + SelectedUsersCount + " selected users?", "Easy Survey - Delete Users", MaterialMessageBox.MessageBoxButtons.YesNo, MaterialMessageBox.MessageBoxIcon.Warning);

                    if (result == MaterialMessageBox.MessageBoxResult.Yes)
                    {
                        foreach (ListViewItem item in SelectedUsers)
                        {
                            long UserID = Convert.ToInt64(item.Tag);

                            // Delete from Database
                            userController.Delete(UserID);

                            // Delete from ListView
                            foreach (ListViewItem listViewItemToDelete in listView_Users.SelectedItems)
                            {
                                long ListViewUserID = Convert.ToInt64(listViewItemToDelete.Tag);
                                if (UserID == ListViewUserID)
                                {
                                    listView_Users.Items.Remove(listViewItemToDelete);
                                }
                            }
                        }
                    }
                }
        }