using Lskj.Business; using Lskj.Business.Impl; using Lskj.Control; using Lskj.Core; using Lskj.Model; using Lskj.Util; using System; using System.Data; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; namespace Lskj.PubUpdatePwd { public partial class FrmMain : BaseForm { private static readonly Regex PasswordRegex = new Regex( @"(?=.*[0-9]) (?=.*[a-zA-Z]) (?=.*[^a-zA-Z0-9]) .{8,18}", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); public DynamicUpdatePwd Model; public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { try { txtNewPass.Text = string.Empty; txtPassConfirm.Text = string.Empty; txtNewPass.Properties.PasswordChar = '*'; txtPassConfirm.Properties.PasswordChar = '*'; BindUserSelectionEvents(); InitializeUsers(); UpdateConfirmButtonState(); } catch (Exception ex) { string message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(message, ex.Message); } } /// /// 绑定人员选择事件,兼容鼠标选择和键盘回车选择。 /// private void BindUserSelectionEvents() { lueUserName.TextChanged -= OnUserSelectionChanged; lueUserName.TextChanged += OnUserSelectionChanged; lueUserName.Popup.OnGridViewMouseClick -= OnUserSelectionChanged; lueUserName.Popup.OnGridViewMouseClick += OnUserSelectionChanged; lueUserName.Popup.OnGridViewEnter -= OnUserSelectionEnter; lueUserName.Popup.OnGridViewEnter += OnUserSelectionEnter; } private void OnUserSelectionChanged(object sender, EventArgs e) { UpdateConfirmButtonState(); } private void OnUserSelectionEnter(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { UpdateConfirmButtonState(); } } /// /// 管理员不能在本模块中修改自己的密码。 /// private void UpdateConfirmButtonState() { bool isAdministrator = string.Equals( ERPInfo.Instance.UserName, "管理员", StringComparison.OrdinalIgnoreCase); string personnelId = lueUserName.EditValue == null ? string.Empty : lueUserName.EditValue.Trim(); bool isCurrentUser = !string.IsNullOrWhiteSpace(personnelId) && string.Equals(personnelId, ERPInfo.Instance.UserId, StringComparison.OrdinalIgnoreCase); btnOK.Enabled = isAdministrator && !isCurrentUser; } /// /// 初始化可修改密码的人员列表。 /// private void InitializeUsers() { string displayName = "UserName"; string additionalFields = string.Empty; if (!string.IsNullOrWhiteSpace(SystemInfo.Instance.LoginUsername) && BaseImpl.HasExistsColumn("P_EmployeeTab", SystemInfo.Instance.LoginUsername)) { displayName = SystemInfo.Instance.LoginUsername; additionalFields = " ," + displayName; } lueUserName.ValueField = lueUserName.ValueMember = "UserId"; lueUserName.TextMember = displayName; lueUserName.SourceSQL = string.Format( "select EmployeeId UserId,LoginAccount UserCode,EmployeeName UserName {0} " + "from P_EmployeeTab where sign=0 and UseFlag=1", additionalFields); DataTable userTable; //if (SystemInfo.Instance.DisplayUserCode) //{ // lueUserName.SourceSQL = string.Format( // "select EmployeeId UserId,LoginAccount UserCode,EmployeeName UserName,LoginAccount 工号 {0} " // + "from P_EmployeeTab where sign=0 and UseFlag=1", // additionalFields); // userTable = MainImpl.GetEmployeeListNew(); //} //else //{ // userTable = MainImpl.GetEmployeeList(); //} userTable = MainImpl.GetEmployeeList(); lueUserName.DataSource = userTable; lueUserName.Properties.NullValuePrompt = "请选择人员"; string personnelId = Model == null ? string.Empty : Model.PersonnelID; if (string.IsNullOrWhiteSpace(personnelId)) { lueUserName.Focus(); return; } DataRow userRow = userTable.Rows.Cast().FirstOrDefault( row => string.Equals(row["UserId"] + string.Empty, personnelId, StringComparison.OrdinalIgnoreCase)); if (userRow == null) { lueUserName.Properties.ReadOnly = true; MessageUtil.Show("未找到指定人员,无法修改密码!", "提示"); return; } lueUserName.EditValue = userRow["UserId"] + string.Empty; lueUserName.Properties.ReadOnly = true; txtNewPass.Focus(); } /// /// 确定按钮点击事件。 /// private void btnOK_Click(object sender, EventArgs e) { string personnelId = lueUserName.EditValue == null ? string.Empty : lueUserName.EditValue.Trim(); string newPassword = txtNewPass.Text.Trim(); string confirmPassword = txtPassConfirm.Text.Trim(); if (string.IsNullOrWhiteSpace(personnelId)) { MessageUtil.Show("请选择需要修改密码的人员!", "提示"); lueUserName.Focus(); return; } if (string.Equals(personnelId, ERPInfo.Instance.UserId, StringComparison.OrdinalIgnoreCase)) { btnOK.Enabled = false; MessageUtil.Show("管理员不能在此模块中修改自己的密码!", "提示"); return; } if (string.IsNullOrWhiteSpace(newPassword)) { MessageUtil.Show("新密码不能为空!"); txtNewPass.Focus(); return; } if (string.IsNullOrWhiteSpace(confirmPassword)) { MessageUtil.Show("确认密码不能为空!"); txtPassConfirm.Focus(); return; } if (!newPassword.Equals(confirmPassword)) { MessageUtil.Show("两次输入的密码不一致!"); txtPassConfirm.Focus(); return; } if (SystemInfo.Instance.VerfyPasswordStrength && !PasswordRegex.IsMatch(confirmPassword)) { MessageUtil.Show("密码不符合要求!"); txtNewPass.Focus(); return; } try { btnOK.Enabled = false; string tipMessage; int result; bool useChangePasswordProcedure = BaseImpl.HasExistsStoredProcedure("pr_sytemchangepsw"); if (useChangePasswordProcedure) { result = MainImpl.ChangePassword(personnelId, newPassword, out tipMessage); } else { result = MainImpl.ForceUpdatePassword(personnelId, newPassword); tipMessage = result == 0 ? "用户不存在" : string.Empty; } if (result != 1) { MessageUtil.Show("修改失败:" + tipMessage); return; } if (!useChangePasswordProcedure && SystemInfo.Instance.UpdPwdAllSystem) { if (MainImpl.UpdatePasswordAllSubsystem(personnelId, newPassword)) { MessageUtil.Show("用户对应账套密码修改成功"); } else { MessageUtil.Show("当前账套密码修改成功,但用户对应账套密码修改失败"); } } MainImpl.RecordPassword(newPassword,"修改密码,"+ ERPInfo.Instance.UserName + "修改"+ lueUserName.Text); MessageUtil.Show("密码修改成功!"); if (Model.AfterClose.Equals("1")) { DialogResult = DialogResult.OK; Close(); } else { txtNewPass.Text = string.Empty; txtPassConfirm.Text = string.Empty; } } catch (Exception ex) { string message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show("修改密码出错!" + message); } finally { if (!IsDisposed) { UpdateConfirmButtonState(); } } } /// /// 取消按钮点击事件。 /// private void btnCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }