narsil/Tanshu.Accounts.PointOfSale/User Management/UserForm.cs

97 lines
2.8 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
using System;
2018-08-24 10:41:33 +00:00
using System.Linq;
2010-03-02 17:56:21 +00:00
using System.Windows.Forms;
2018-08-24 10:41:33 +00:00
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Repository;
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.PointOfSale
{
public partial class UserForm : Form
{
User _user;
public UserForm(Guid? userID)
2010-03-02 17:56:21 +00:00
{
InitializeComponent();
if (userID.HasValue)
2018-08-24 10:41:33 +00:00
_user = UserBI.Get(userID.Value);
2010-03-02 17:56:21 +00:00
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnDelete_Click(object sender, EventArgs e)
2010-03-02 17:56:21 +00:00
{
2018-08-24 10:41:33 +00:00
UserBI.Delete(_user.UserID);
2010-03-02 17:56:21 +00:00
btnCancel_Click(sender, e);
}
private void UserForm_Load(object sender, EventArgs e)
2010-03-02 17:56:21 +00:00
{
2018-08-24 10:41:33 +00:00
var roles = RoleBI.List();
clbRoles.DataSource = roles;
if (_user == null)
return;
txtUsername.Text = _user.Name;
txtPassword.Text = _user.Password;
txtRetype.Text = _user.Password;
chkLocked.Checked = _user.LockedOut;
2018-08-24 10:41:33 +00:00
for (int i = 0; i < clbRoles.Items.Count; i++)
{
var item = (Role)clbRoles.Items[i];
clbRoles.SetItemChecked(i, _user.Roles.Any(x => x.RoleID == item.RoleID));
}
2010-03-02 17:56:21 +00:00
}
private void Save()
2010-03-02 17:56:21 +00:00
{
2018-08-24 10:41:33 +00:00
if (_user == null)
_user = new User();
else
_user = UserBI.Get(_user.UserID);
2018-08-24 10:41:33 +00:00
_user.Name = txtUsername.Text.Trim();
if (_user.Password != txtPassword.Text.Trim())
_user.Password = txtPassword.Text.Trim();
_user.LockedOut = (chkLocked.Checked == true ? true : false);
_user.Roles = clbRoles.CheckedItems.OfType<Role>().ToList();
2018-08-24 10:41:33 +00:00
if (_user.UserID == Guid.Empty)
UserBI.Insert(_user);
else
UserBI.Update(_user);
2010-03-02 17:56:21 +00:00
}
private void btnSave_Click(object sender, EventArgs e)
{
2018-08-24 10:41:33 +00:00
if (string.IsNullOrEmpty(txtUsername.Text.Trim()))
2010-03-02 17:56:21 +00:00
{
MessageBox.Show("Username cannot be blank.");
2010-03-02 17:56:21 +00:00
txtUsername.Focus();
}
else if (txtPassword.Text.Trim() != txtRetype.Text.Trim())
{
MessageBox.Show("Passwords do not match.");
txtPassword.Focus();
}
2018-08-24 10:41:33 +00:00
else if (_user == null && string.IsNullOrEmpty(txtPassword.Text.Trim()))
{
MessageBox.Show("Password cannot be empty.");
txtPassword.Focus();
}
2010-03-02 17:56:21 +00:00
else
{
Save();
2018-08-24 10:41:33 +00:00
this.Close();
2010-03-02 17:56:21 +00:00
}
}
2018-08-24 10:41:33 +00:00
private void clbRoles_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = ((Role)e.ListItem).Name;
}
2010-03-02 17:56:21 +00:00
}
}