2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
2018-01-24 16:35:09 +00:00
|
|
|
|
/// Copyright (c) 2017-2018 Sensus Slovensko a.s.
|
2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-03-15 11:56:53 +00:00
|
|
|
|
using System.Text;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
using NHibernate;
|
2017-03-16 20:53:12 +00:00
|
|
|
|
using Users.Entities;
|
2017-03-15 11:56:53 +00:00
|
|
|
|
using Users.Resources;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2017-03-15 11:56:53 +00:00
|
|
|
|
namespace Users.Forms
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
public partial class EditSelectedUser : Form
|
|
|
|
|
|
{
|
2018-01-24 16:35:09 +00:00
|
|
|
|
NHibernate.ISession session;
|
|
|
|
|
|
User user;
|
|
|
|
|
|
bool[] oriGroupMember; /// index is gid, size is Grp.Count
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
public EditSelectedUser()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
2017-03-15 11:56:53 +00:00
|
|
|
|
this.toolTip1.SetToolTip(this.txtName, string.Format(Strings.max_0_alphanum_chars, 20));
|
2018-01-24 16:35:09 +00:00
|
|
|
|
oriGroupMember = new bool[Grp.Count];
|
2014-07-28 07:54:35 +00:00
|
|
|
|
btnOk.Enabled = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
public EditSelectedUser(NHibernate.ISession session, User user)
|
|
|
|
|
|
: this()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.session = session;
|
|
|
|
|
|
this.user = user;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
private void EditSelectedUser_Load(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2015-10-07 19:33:37 +00:00
|
|
|
|
Localize();
|
|
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
txtName.Text = user.UserName;
|
|
|
|
|
|
txtCode.Text = user.Number.ToString();
|
|
|
|
|
|
txtDescription.Text = user.FullName;
|
2017-03-17 09:58:05 +00:00
|
|
|
|
txtPassword.Text = string.Empty;
|
|
|
|
|
|
txtRepeatPassword.Text = string.Empty;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
ShowGroups();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-07 19:33:37 +00:00
|
|
|
|
void Localize()
|
|
|
|
|
|
{
|
|
|
|
|
|
Text = Strings.Edit_User;
|
|
|
|
|
|
nameLabel.Text = Strings.Name;
|
|
|
|
|
|
passwordLabel.Text = Strings.Password;
|
2017-03-15 07:11:54 +00:00
|
|
|
|
repeatPasswordLabel.Text = Strings.Repeat_password;
|
2017-03-17 09:58:05 +00:00
|
|
|
|
codeLabel.Text = Strings.User_code;
|
2015-10-07 19:33:37 +00:00
|
|
|
|
descriptionLabel.Text = Strings.DescriptionColHdr;
|
|
|
|
|
|
groupsLabel.Text = Strings.Groups;
|
|
|
|
|
|
btnOk.Text = Strings.OkBtnText;
|
|
|
|
|
|
btnCancel.Text = Strings.CancelBtnText;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
public void ShowGroups()
|
|
|
|
|
|
{
|
|
|
|
|
|
checkedListBoxGroups.Items.Clear();
|
2018-01-24 16:35:09 +00:00
|
|
|
|
for (int i= 0; i < Grp.Count; i++)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
Group group = Grp.FromId((Grp.GID)i);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
bool boolIsMember = oriGroupMember[i] = user.IsMemberOf((Grp.GID)group.Gid);
|
|
|
|
|
|
|
2018-01-25 12:30:48 +00:00
|
|
|
|
checkedListBoxGroups.Items.Add(Grp.Gid2Str(i));
|
|
|
|
|
|
checkedListBoxGroups.SetItemCheckState(i, boolIsMember ? CheckState.Checked : CheckState.Unchecked);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
private void EditSelectedUser_Shown(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
txtName.Focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2015-12-04 16:17:20 +00:00
|
|
|
|
public bool Save()
|
2018-01-24 16:35:09 +00:00
|
|
|
|
{
|
2014-07-28 07:54:35 +00:00
|
|
|
|
// todo: check if this name is given to another user
|
|
|
|
|
|
if (IsUserNameAllreadyTaken(txtName.Text))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show(Strings.Username_taken);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
ITransaction transaction = session.BeginTransaction();
|
2017-03-17 09:58:05 +00:00
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
try
|
2017-03-17 09:58:05 +00:00
|
|
|
|
{
|
2018-01-24 16:35:09 +00:00
|
|
|
|
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();
|
2017-03-17 09:58:05 +00:00
|
|
|
|
}
|
2018-01-24 16:35:09 +00:00
|
|
|
|
catch (Exception exc)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2018-01-24 16:35:09 +00:00
|
|
|
|
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);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
2018-01-24 16:35:09 +00:00
|
|
|
|
|
|
|
|
|
|
if (transaction.WasCommitted) session.Flush();
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2018-01-24 16:35:09 +00:00
|
|
|
|
if (person.Id != user.Id)
|
2017-03-15 11:56:53 +00:00
|
|
|
|
{
|
2014-07-28 07:54:35 +00:00
|
|
|
|
// other user
|
2016-12-21 08:22:30 +00:00
|
|
|
|
if (person.UserName.ToLower() == Username.ToLower())
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-25 12:30:48 +00:00
|
|
|
|
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(); }
|
2018-01-24 16:35:09 +00:00
|
|
|
|
private void txtDescription_KeyPress(object sender, KeyPressEventArgs e) { UpdateOKButton(); }
|
2018-01-25 12:30:48 +00:00
|
|
|
|
private void checkedListBoxGroups_Click(object sender, EventArgs e) { UpdateOKButton(); }
|
|
|
|
|
|
private void txtCode_TextChanged(object sender, EventArgs e) { UpdateOKButton(); }
|
2017-03-17 09:58:05 +00:00
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
private void UpdateOKButton()
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2018-01-24 16:35:09 +00:00
|
|
|
|
int userCode;
|
2017-03-17 08:45:39 +00:00
|
|
|
|
|
2018-01-24 16:35:09 +00:00
|
|
|
|
btnOk.Enabled = (txtName.Text != string.Empty)
|
|
|
|
|
|
&& int.TryParse(txtCode.Text, out userCode)
|
|
|
|
|
|
&& (txtPassword.Text == txtRepeatPassword.Text);
|
2017-03-17 08:45:39 +00:00
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|