using System; using System.Linq; using System.Windows.Forms; using Tanshu.Accounts.Entities; using Tanshu.Accounts.Repository; namespace Tanshu.Accounts.PointOfSale { public partial class UserForm : Form { User _user; public UserForm(Guid? userID) { InitializeComponent(); if (userID.HasValue) _user = UserBI.Get(userID.Value); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void btnDelete_Click(object sender, EventArgs e) { UserBI.Delete(_user.UserID); btnCancel_Click(sender, e); } private void UserForm_Load(object sender, EventArgs e) { 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; 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)); } } private void Save() { if (_user == null) _user = new User(); else _user = UserBI.Get(_user.UserID); _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().ToList(); if (_user.UserID == Guid.Empty) UserBI.Insert(_user); else UserBI.Update(_user); } private void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtUsername.Text.Trim())) { MessageBox.Show("Username cannot be blank."); txtUsername.Focus(); } else if (txtPassword.Text.Trim() != txtRetype.Text.Trim()) { MessageBox.Show("Passwords do not match."); txtPassword.Focus(); } else if (_user == null && string.IsNullOrEmpty(txtPassword.Text.Trim())) { MessageBox.Show("Password cannot be empty."); txtPassword.Focus(); } else { Save(); this.Close(); } } private void clbRoles_Format(object sender, ListControlConvertEventArgs e) { e.Value = ((Role)e.ListItem).Name; } } }