void ListBoxUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (ListBoxUsers.SelectedIndex >= 0)
     {
         m_selectedUser = (User)ListBoxUsers.SelectedItem;
         GridManageUsers.DataContext = m_selectedUser;
         if (!m_selectedUser.UseADAuthentication)
             DatePickerPasswordExpiry.SelectedDate = m_selectedUser.ChangePasswordOn;
         if (m_selectedUser.UseADAuthentication)
             ComboBoxAuthentication.SelectedItem = "Windows Authentication";
         else
             ComboBoxAuthentication.SelectedItem = "Database Authentication";
         ButtonSaveUser.Tag = "Update";
         m_editUserMode = true;
     }
     else
         ClearUserInformation();
 }
        void ButtonSaveUser_Click(object sender, RoutedEventArgs e)
        {
            SystemMessages sm;
            try
            {
                if (ValidateUserInfo())
                {
                    string result;
                    User user = new User();
                    user.Name = TextBoxUsername.Text.CleanText();
                    user.DefaultNodeID = ((App)Application.Current).NodeValue;
                    user.LockedOut = (bool)CheckBoxLockedOut.IsChecked;
                    user.UseADAuthentication = true;
                    user.ChangePasswordOn = DateTime.MinValue;
                    user.UpdatedBy = ((App)Application.Current).Principal.Identity.Name;
                    user.UpdatedOn = DateTime.UtcNow;
                    if (ComboBoxAuthentication.SelectedValue.ToString() == "Database Authentication")
                    {

                        user.FirstName = TextBoxFirstName.Text.CleanText();
                        user.LastName = TextBoxLastName.Text.CleanText();
                        user.Phone = TextBoxPhone.Text.CleanText();
                        user.Email = TextBoxEmail.Text.CleanText();
                        user.UseADAuthentication = false;
                        user.ChangePasswordOn = DatePickerPasswordExpiry.SelectedDate ?? DateTime.MinValue;
                    }
                    else
                    {
                        user.FirstName = string.Empty;
                        user.LastName = string.Empty;
                        user.Phone = string.Empty;
                        user.Email = string.Empty;
                        user.UseADAuthentication = true;
                    }

                    if (m_editUserMode)
                    {
                        if (string.IsNullOrEmpty(TextBoxPassword.Password))
                            user.Password = m_selectedUser.Password;    //keep existing password.
                        else
                            user.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(@"O3990\P78f9E66b:a35_V©6M13©6~2&[" + TextBoxPassword.Password, "SHA1");
                        user.ID = m_selectedUser.ID;
                        user.CreatedBy = m_selectedUser.CreatedBy;
                        user.CreatedOn = m_selectedUser.CreatedOn;
                        result = CommonFunctions.SaveUser(null, user, false);
                    }
                    else
                    {
                        //don't need password for active directory users.
                        if (ComboBoxAuthentication.SelectedValue.ToString() == "Database Authentication")
                        {
                            if (string.IsNullOrEmpty(TextBoxPassword.Password))
                                throw new Exception(m_invalidPasswordMessage.ToString());
                            user.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(@"O3990\P78f9E66b:a35_V©6M13©6~2&[" + TextBoxPassword.Password, "SHA1");
                        }
                        else
                            user.Password = string.Empty;
                        user.CreatedBy = ((App)Application.Current).Principal.Identity.Name;
                        user.CreatedOn = DateTime.UtcNow;
                        result = CommonFunctions.SaveUser(null, user, true);
                    }

                    sm = new SystemMessages(new Message() { UserMessage = result, SystemMessage = string.Empty, UserMessageType = MessageType.Success },
                        ButtonType.OkOnly);
                    sm.Owner = Window.GetWindow(this);
                    sm.ShowPopup();

                    GetUsers();
                    ClearUserInformation();

                    //If group is selected when a user was being added or updated, then refresh current users list and possible users  list.
                    if (m_selectedGroup != null && m_editGroupMode)
                    {
                        ListBoxCurrentGroupUsers.ItemsSource = m_selectedGroup.CurrentGroupUsers = CommonFunctions.GetCurrentGroupUsers(null, m_selectedGroup.ID);
                        ListBoxPossibleGroupUsers.ItemsSource = m_selectedGroup.PossibleGroupUsers = CommonFunctions.GetPossibleGroupUsers(null, m_selectedGroup.ID);
                    }

                    //If role is selected when a user was being added or updated then refresh current users, possible users list for role.
                    if (m_selectedRole != null && m_editRoleMode)
                    {
                        ListBoxCurrentRoleUsers.ItemsSource = m_selectedRole.CurrentRoleUsers = CommonFunctions.GetCurrentRoleUsers(null, m_selectedRole.ID);
                        ListBoxPossibleRoleUsers.ItemsSource = m_selectedRole.PossibleRoleUsers = CommonFunctions.GetPossibleRoleUsers(null, m_selectedRole.ID);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonFunctions.LogException(null, "WPF.SaveUser", ex);
                sm = new SystemMessages(new Message() { UserMessage = "Failed to Save User Information", SystemMessage = ex.Message, UserMessageType = MessageType.Error },
                        ButtonType.OkOnly);
                sm.Owner = Window.GetWindow(this);
                sm.ShowPopup();
            }
        }
 void ClearUserInformation()
 {
     m_selectedUser = null;
     GridManageUsers.DataContext = new User() { LockedOut = false };
     ComboBoxAuthentication.SelectedIndex = 0;
     TextBoxPassword.Password = string.Empty;
     DatePickerPasswordExpiry.SelectedDate = DateTime.Now.AddDays(90);
     ButtonSaveUser.Tag = "Add";
     m_editUserMode = false;
     ListBoxUsers.SelectedIndex = -1;
 }
Example #4
0
        public static string SaveUser(DataConnection connection, User user, bool isNew)
        {
            bool createdConnection = false;
            try
            {
                if (connection == null)
                {
                    connection = new DataConnection();
                    createdConnection = true;
                }
                IDbCommand command = connection.Connection.CreateCommand();
                command.CommandType = CommandType.Text;

                if (isNew)
                    if (command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB"))
                        command.CommandText = "Insert Into UserAccount (Name, [Password], FirstName, LastName, DefaultNodeID, Phone, Email, LockedOut, UseADAuthentication, ChangePasswordOn, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) " +
                            "Values (@name, @password, @firstName, @lastName, @defaultNodeID, @phone, @email, @lockedOut, @useADAuthentication, @changePasswordOn, @updatedBy, @updatedOn, @createdBy, @createdOn)";
                    else
                        command.CommandText = "Insert Into UserAccount (Name, Password, FirstName, LastName, DefaultNodeID, Phone, Email, LockedOut, UseADAuthentication, ChangePasswordOn, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) " +
                            "Values (@name, @password, @firstName, @lastName, @defaultNodeID, @phone, @email, @lockedOut, @useADAuthentication, @changePasswordOn, @updatedBy, @updatedOn, @createdBy, @createdOn)";
                else
                    if (command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB"))
                        command.CommandText = "Update UserAccount Set Name = @name, [Password] = @password, FirstName = @firstName, LastName = @lastName, DefaultNodeID = @defaultNodeID, Phone = @phone, " +
                            "Email = @email, LockedOut = @lockedOut, UseADAuthentication = @useADAuthentication, ChangePasswordOn = @changePasswordOn, UpdatedBy = @updatedBy, UpdatedOn = @updatedOn Where ID = @id";
                    else
                        command.CommandText = "Update UserAccount Set Name = @name, Password = @password, FirstName = @firstName, LastName = @lastName, DefaultNodeID = @defaultNodeID, Phone = @phone, Email = @email, " +
                            "LockedOut = @lockedOut, UseADAuthentication = @useADAuthentication, ChangePasswordOn = @changePasswordOn, UpdatedBy = @updatedBy, UpdatedOn = @updatedOn Where ID = @id";

                command.Parameters.Add(AddWithValue(command, "@name", user.Name));
                command.Parameters.Add(AddWithValue(command, "@password", user.Password));
                command.Parameters.Add(AddWithValue(command, "@firstName", user.FirstName));
                command.Parameters.Add(AddWithValue(command, "@lastName", user.LastName));
                if (command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB"))
                    command.Parameters.Add(AddWithValue(command, "@defaultNodeID", "{" + user.DefaultNodeID + "}"));
                else
                    command.Parameters.Add(AddWithValue(command, "@defaultNodeID", user.DefaultNodeID));
                command.Parameters.Add(AddWithValue(command, "@phone", user.Phone));
                command.Parameters.Add(AddWithValue(command, "@email", user.Email));
                command.Parameters.Add(AddWithValue(command, "@lockedOut", user.LockedOut));
                command.Parameters.Add(AddWithValue(command, "@useADAuthentication", user.UseADAuthentication));
                if (command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB"))
                    command.Parameters.Add(AddWithValue(command, "@changePasswordOn", user.ChangePasswordOn == DateTime.MinValue ? DateTime.UtcNow.Date : user.ChangePasswordOn.Date));
                else
                    command.Parameters.Add(AddWithValue(command, "@changePasswordOn", user.ChangePasswordOn == DateTime.MinValue ? (object)DBNull.Value : user.ChangePasswordOn));

                command.Parameters.Add(AddWithValue(command, "@updatedBy", s_currentUser));
                command.Parameters.Add(AddWithValue(command, "@updatedOn", command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB") ? DateTime.UtcNow.Date : DateTime.UtcNow));

                if (isNew)
                {
                    command.Parameters.Add(AddWithValue(command, "@createdBy", s_currentUser));
                    command.Parameters.Add(AddWithValue(command, "@createdOn", command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB") ? DateTime.UtcNow.Date : DateTime.UtcNow));
                }
                else
                {
                    if (command.Connection.ConnectionString.Contains("Microsoft.Jet.OLEDB"))
                        command.Parameters.Add(AddWithValue(command, "@id", "{" + user.ID + "}"));
                    else
                        command.Parameters.Add(AddWithValue(command, "@id", user.ID));
                }

                command.ExecuteNonQuery();

                return "User Information Saved Successfully";
            }
            finally
            {
                if (createdConnection && connection != null)
                    connection.Dispose();
            }
        }