tbf/Users/Forms/EditSelectedUser.cs

205 lines
6.7 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2017-2018 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using Users.Entities;
using Users.Resources;
namespace Users.Forms
{
public partial class EditSelectedUser : Form
{
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 = user.UserName;
txtCode.Text = user.Number.ToString();
txtDescription.Text = user.FullName;
txtPassword.Text = string.Empty;
txtRepeatPassword.Text = string.Empty;
ShowGroups();
}
void Localize()
{
Text = Strings.Edit_User;
nameLabel.Text = Strings.Name;
passwordLabel.Text = Strings.Password;
repeatPasswordLabel.Text = Strings.Repeat_password;
codeLabel.Text = Strings.User_code;
descriptionLabel.Text = Strings.DescriptionColHdr;
groupsLabel.Text = Strings.Groups;
btnOk.Text = Strings.OkBtnText;
btnCancel.Text = Strings.CancelBtnText;
}
public void ShowGroups()
{
checkedListBoxGroups.Items.Clear();
for (int i= 0; i < Grp.Count; i++)
{
Group group = Grp.FromId((Grp.GID)i);
bool boolIsMember = oriGroupMember[i] = user.IsMemberOf((Grp.GID)group.Gid);
checkedListBoxGroups.Items.Add(Grp.Gid2Str(i));
checkedListBoxGroups.SetItemCheckState(i, boolIsMember ? CheckState.Checked : 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))
{
MessageBox.Show(Strings.Username_taken);
return false;
}
ITransaction transaction = session.BeginTransaction();
try
{
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<Group> groups = user.Groups; /// = session.QueryOver<Group>().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();
}
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;
}
/// <summary>
/// returns true if the username allready exists for another user
/// </summary>
private bool IsUserNameAllreadyTaken(string Username)
{
// have a look into all other users
IList<User> ListOfUsers = User.GetAllUsers();
foreach (var person in ListOfUsers)
{
if (person.Id != user.Id)
{
// other user
if (person.UserName.ToLower() == Username.ToLower())
{
// Name is equal, so allready taken
return true;
}
}
}
return false;
}
private void btnOk_Click(object sender, EventArgs e)
{
if (Save())
{
this.Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
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;
btnOk.Enabled = (txtName.Text != string.Empty)
&& int.TryParse(txtCode.Text, out userCode)
&& (txtPassword.Text == txtRepeatPassword.Text);
}
}
}