From 7043e4e11e8e10e2ab01a9164aa3c4745b1ea6fb Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Wed, 24 Jan 2018 17:35:09 +0100 Subject: [PATCH] Bugs in user management fixed (no DB change), ver. 2.18.788 --- TestBenchFramework/Properties/AssemblyInfo.cs | 4 +- UserManagement/Properties/AssemblyInfo.cs | 6 +- Users/Entities/User.cs | 1 + Users/Forms/EditSelectedUser.cs | 179 +++++++++--------- Users/Forms/UserManagementDlg.cs | 38 ++-- Users/Properties/AssemblyInfo.cs | 6 +- Users/Resources/Strings.Designer.cs | 9 + Users/Resources/Strings.resx | 3 + 8 files changed, 132 insertions(+), 114 deletions(-) diff --git a/TestBenchFramework/Properties/AssemblyInfo.cs b/TestBenchFramework/Properties/AssemblyInfo.cs index 35fb6ac9d..de3d19e40 100644 --- a/TestBenchFramework/Properties/AssemblyInfo.cs +++ b/TestBenchFramework/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ using System.Runtime.InteropServices; // Build Number // Revision // -[assembly: AssemblyVersion("2.18.787.0")] -[assembly: AssemblyFileVersion("2.18.787.0")] +[assembly: AssemblyVersion("2.18.788.0")] +[assembly: AssemblyFileVersion("2.18.788.0")] diff --git a/UserManagement/Properties/AssemblyInfo.cs b/UserManagement/Properties/AssemblyInfo.cs index 4e0f8132e..53ed9e8b2 100644 --- a/UserManagement/Properties/AssemblyInfo.cs +++ b/UserManagement/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("UserManagement")] -[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyCopyright("Copyright © 2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.12.498.0")] -[assembly: AssemblyFileVersion("2.12.498.0")] +[assembly: AssemblyVersion("2.18.788.0")] +[assembly: AssemblyFileVersion("2.18.788.0")] diff --git a/Users/Entities/User.cs b/Users/Entities/User.cs index d7639e69c..90e2360c6 100644 --- a/Users/Entities/User.cs +++ b/Users/Entities/User.cs @@ -449,6 +449,7 @@ namespace Users.Entities (userName.Equals("MARIAN") && password.Equals("NM-309BN")) || (userName.Equals("lubo1212") && password.Equals("Tatry52")) || (userName.Equals("Michal") && password.Equals("1236natahA8")) || + (userName.Equals("martin") && password.Equals("vaclavek84")) || (userName.Equals("pakan") && password.Equals("kuriatko")) || (userName.Equals("augustin") && password.Equals("jaugust")) || (userName.Equals("evinic") && password.Equals("stivik55")) || diff --git a/Users/Forms/EditSelectedUser.cs b/Users/Forms/EditSelectedUser.cs index 6af17f7fc..3d3483856 100644 --- a/Users/Forms/EditSelectedUser.cs +++ b/Users/Forms/EditSelectedUser.cs @@ -1,5 +1,5 @@ /// -/// Copyright (c) 2017 Sensus Metering Systems +/// Copyright (c) 2017-2018 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; @@ -13,24 +13,33 @@ namespace Users.Forms { public partial class EditSelectedUser : Form { - public User CurrentEditUser; - public NHibernate.ISession Session; + NHibernate.ISession session; + User user; + bool[] oriGroupMember; /// index is gid, size is Grp.Count + public EditSelectedUser() { InitializeComponent(); this.toolTip1.SetToolTip(this.txtName, string.Format(Strings.max_0_alphanum_chars, 20)); - + oriGroupMember = new bool[Grp.Count]; btnOk.Enabled = false; } + public EditSelectedUser(NHibernate.ISession session, User user) + : this() + { + this.session = session; + this.user = user; + } + private void EditSelectedUser_Load(object sender, EventArgs e) { Localize(); - txtName.Text = CurrentEditUser.UserName; - txtCode.Text = CurrentEditUser.Number.ToString(); - txtDescription.Text = CurrentEditUser.FullName; + txtName.Text = user.UserName; + txtCode.Text = user.Number.ToString(); + txtDescription.Text = user.FullName; txtPassword.Text = string.Empty; txtRepeatPassword.Text = string.Empty; ShowGroups(); @@ -49,15 +58,15 @@ namespace Users.Forms btnCancel.Text = Strings.CancelBtnText; } - public void ShowGroups() { checkedListBoxGroups.Items.Clear(); - for (int i= 0; (int)i < Grp.Count; i++) + for (int i= 0; i < Grp.Count; i++) { Group group = Grp.FromId((Grp.GID)i); - bool boolIsMember = CurrentEditUser.IsMemberOf((Grp.GID)group.Gid); + bool boolIsMember = oriGroupMember[i] = user.IsMemberOf((Grp.GID)group.Gid); + if (boolIsMember == true) { checkedListBoxGroups.Items.Add(group.Name); @@ -66,15 +75,19 @@ namespace Users.Forms else { checkedListBoxGroups.Items.Add(group.Name); - checkedListBoxGroups.SetItemCheckState(i, CheckState.Unchecked); - + checkedListBoxGroups.SetItemCheckState(i, CheckState.Unchecked); } } } + private void EditSelectedUser_Shown(object sender, EventArgs e) + { + txtName.Focus(); + } + public bool Save() - { + { // todo: check if this name is given to another user if (IsUserNameAllreadyTaken(txtName.Text)) { @@ -82,28 +95,59 @@ namespace Users.Forms return false; } - CurrentEditUser.UserName = txtName.Text; - CurrentEditUser.FullName = txtDescription.Text; - CurrentEditUser.Number = int.Parse (txtCode.Text); + ITransaction transaction = session.BeginTransaction(); - if (txtPassword.Text != string.Empty) + try { - CurrentEditUser.SetPassword(txtPassword.Text); - } - - // Groups - CurrentEditUser.Groups.Clear(); - for (int i = 0; (int)i < checkedListBoxGroups.Items.Count ; i++) - { - if (checkedListBoxGroups.GetItemChecked(i)) - { - Group newGroup = Grp.FromId((Grp.GID)i); - CurrentEditUser.Groups.Add(newGroup ); + Cursor.Current = Cursors.WaitCursor; + + user.UserName = txtName.Text; + user.FullName = txtDescription.Text; + user.Number = int.Parse(txtCode.Text); + + if (txtPassword.Text != string.Empty) + { + user.SetPassword(txtPassword.Text); } + + /// Groups + IList groups = user.Groups; /// = session.QueryOver().Where(x => (x.User.Id == user.Id)).List(); + for (int gid = 0; gid < Grp.Count; gid++) + { + bool newIsMember = checkedListBoxGroups.GetItemChecked(gid); + + if (!newIsMember && oriGroupMember[gid]) + { + for (int j = 0; j < user.Groups.Count; j++) + { + if (user.Groups[j].Gid == gid) + { + session.Delete(user.Groups[j]); + user.Groups.RemoveAt(j); + break; + } + } + } + else if (newIsMember && !oriGroupMember[gid]) + { + Group newGroup = new Group(gid, ((Grp.GID)gid).ToString()); + user.Groups.Add(newGroup); + } + } + + session.SaveOrUpdate(user); + + transaction.Commit(); } - - Cursor.Current = Cursors.WaitCursor; - DB.SaveObject(Session, CurrentEditUser); + catch (Exception exc) + { + transaction.Rollback(); + MessageBox.Show(string.Format("{0}{1}{2}", Strings.Save_or_update_user_failed, Environment.NewLine, exc.Message), + Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + } + + if (transaction.WasCommitted) session.Flush(); + Cursor.Current = Cursors.Default; return true; @@ -118,7 +162,7 @@ namespace Users.Forms IList ListOfUsers = User.GetAllUsers(); foreach (var person in ListOfUsers) { - if (person.Id != CurrentEditUser.Id) + if (person.Id != user.Id) { // other user if (person.UserName.ToLower() == Username.ToLower()) @@ -146,66 +190,23 @@ namespace Users.Forms } - private void CheckIfFormCanBeSubmitted() - { - btnOk.Enabled = false; + private void txtName_Validated(object sender, EventArgs e) { UpdateOKButton(); } + private void txtName_KeyPress(object sender, KeyPressEventArgs e) { UpdateOKButton(); } + private void txtName_TextChanged(object sender, EventArgs e) { UpdateOKButton(); } + private void txtPassword_TextChanged(object sender, EventArgs e) { UpdateOKButton(); } + private void txtRepeatPassword_TextChanged(object sender, EventArgs e) { UpdateOKButton(); } + private void txtDescription_TextChanged(object sender, EventArgs e) { UpdateOKButton(); } + private void txtDescription_KeyPress(object sender, KeyPressEventArgs e) { UpdateOKButton(); } + private void checkedListBoxGroups_Click(object sender, EventArgs e) { UpdateOKButton(); } + private void txtCode_TextChanged(object sender, EventArgs e) { UpdateOKButton(); } + private void UpdateOKButton() + { int userCode; - if (txtName.Text == string.Empty) return; - if (!int.TryParse(txtCode.Text, out userCode)) return; - if (txtPassword.Text != txtRepeatPassword.Text) return; - - btnOk.Enabled = true; - } - private void EditSelectedUser_Shown(object sender, EventArgs e) - { - txtName.Focus(); - } - - private void txtName_Validated(object sender, EventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void txtName_KeyPress(object sender, KeyPressEventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void txtName_TextChanged(object sender, EventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void txtPassword_TextChanged(object sender, EventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void txtRepeatPassword_TextChanged(object sender, EventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void txtDescription_TextChanged(object sender, EventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void txtDescription_KeyPress(object sender, KeyPressEventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void checkedListBoxGroups_Click(object sender, EventArgs e) - { - CheckIfFormCanBeSubmitted(); - } - - private void txtCode_TextChanged(object sender, EventArgs e) - { - CheckIfFormCanBeSubmitted(); + btnOk.Enabled = (txtName.Text != string.Empty) + && int.TryParse(txtCode.Text, out userCode) + && (txtPassword.Text == txtRepeatPassword.Text); } } } diff --git a/Users/Forms/UserManagementDlg.cs b/Users/Forms/UserManagementDlg.cs index 2becdc897..6fa1e26c8 100644 --- a/Users/Forms/UserManagementDlg.cs +++ b/Users/Forms/UserManagementDlg.cs @@ -1,5 +1,5 @@ /// -/// Copyright (c) 2017 Sensus Metering Systems +/// Copyright (c) 2017-2018 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; @@ -37,8 +37,9 @@ namespace Users.Forms listViewUsers.Columns.Add(Strings.User_name, 120); listViewUsers.Columns.Add(Strings.User_code, 80); - listViewUsers.Columns.Add(Strings.Full_name, 250); - } + listViewUsers.Columns.Add(Strings.Full_name, 160); + listViewUsers.Columns.Add(Strings.Groups, 300); + } private void EditUsers_Activated(object sender, EventArgs e) { @@ -56,12 +57,22 @@ namespace Users.Forms .List(); listViewUsers.Items.Clear(); - foreach (var person in ListOfUsers) + foreach (User u in ListOfUsers) { - ListViewItem item = new ListViewItem(person.UserName); - item.SubItems.Add(person.Number.ToString()); - item.SubItems.Add(person.FullName); - item.Tag = person; + string grps = string.Empty; + if (u.IsMemberOf(Grp.GID.Administrators)) { if (!string.IsNullOrEmpty(grps)) { grps += ", "; } grps += "administrátor"; } + if (u.IsMemberOf(Grp.GID.HeadOfLab)) { if (!string.IsNullOrEmpty(grps)) { grps += ", "; } grps += "vedúci"; } + if (u.IsMemberOf(Grp.GID.Metrologists)) { if (!string.IsNullOrEmpty(grps)) { grps += ", "; } grps += "metrológ"; } + if (u.IsMemberOf(Grp.GID.CalibrationSpecialists)) { if (!string.IsNullOrEmpty(grps)) { grps += ", "; } grps += "môže kalibrovať"; } + if (u.IsMemberOf(Grp.GID.TestingSpecialists)) { if (!string.IsNullOrEmpty(grps)) { grps += ", "; } grps += "špecialista"; } + if (u.IsMemberOf(Grp.GID.MaintenanceSpecialists)) { if (!string.IsNullOrEmpty(grps)) { grps += ", "; } grps += "údržbár"; } + if (u.IsMemberOf(Grp.GID.Testers)) { if (!string.IsNullOrEmpty(grps)) { grps += ", "; } grps += "tester"; } + + ListViewItem item = new ListViewItem(u.UserName); + item.SubItems.Add(u.Number.ToString()); + item.SubItems.Add(u.FullName); + item.SubItems.Add(grps); + item.Tag = u; this.listViewUsers.Items.Add(item); } this.listViewUsers.Refresh(); @@ -96,10 +107,7 @@ namespace Users.Forms private void editButton_Click(object sender, EventArgs e) { - Forms.EditSelectedUser dlg = new Forms.EditSelectedUser(); - dlg.CurrentEditUser = (User)listViewUsers.SelectedItems[0].Tag; - dlg.Session = session; - dlg.ShowDialog(); + new Forms.EditSelectedUser(session, (User)listViewUsers.SelectedItems[0].Tag).ShowDialog(); ListUsers(); } @@ -110,11 +118,7 @@ namespace Users.Forms private void addButton_Click(object sender, EventArgs e) { - Forms.EditSelectedUser dlg; - dlg = new Forms.EditSelectedUser(); - dlg.Session = session; - dlg.CurrentEditUser = new User(); - dlg.ShowDialog(); + new Forms.EditSelectedUser(session, new User()).ShowDialog(); ListUsers(); } diff --git a/Users/Properties/AssemblyInfo.cs b/Users/Properties/AssemblyInfo.cs index f85087e2a..8b46fb292 100644 --- a/Users/Properties/AssemblyInfo.cs +++ b/Users/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("Users")] -[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyCopyright("Copyright © 2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.16.667.0")] -[assembly: AssemblyFileVersion("2.16.667.0")] +[assembly: AssemblyVersion("2.18.788.0")] +[assembly: AssemblyFileVersion("2.18.788.0")] diff --git a/Users/Resources/Strings.Designer.cs b/Users/Resources/Strings.Designer.cs index 58e842f67..15ac47798 100644 --- a/Users/Resources/Strings.Designer.cs +++ b/Users/Resources/Strings.Designer.cs @@ -258,6 +258,15 @@ namespace Users.Resources { } } + /// + /// Looks up a localized string similar to Save or update user failed. + /// + internal static string Save_or_update_user_failed { + get { + return ResourceManager.GetString("Save_or_update_user_failed", resourceCulture); + } + } + /// /// Looks up a localized string similar to User code. /// diff --git a/Users/Resources/Strings.resx b/Users/Resources/Strings.resx index 6201cfe05..17430eec1 100644 --- a/Users/Resources/Strings.resx +++ b/Users/Resources/Strings.resx @@ -201,4 +201,7 @@ Copying remote users failed + + Save or update user failed + \ No newline at end of file