1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#region
 
using System;
using System.Windows.Forms;
using CSFrameworkV5.Business;
using CSFrameworkV5.Business.BLL_Permission;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Core.Common;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Models;
 
#endregion
 
namespace CSFrameworkV5.Library.PermissionForms
{
    /// <summary>
    ///     修改密码
    /// </summary>
    public partial class frmModifyPwd : frmBaseDialog
    {
        private ModifyPwdType _type;
        private LoginUser _user;
 
        //私有构造器
        private frmModifyPwd()
        {
            InitializeComponent();
        }
 
        private void BindingDataSet()
        {
            var data = CommonData.GetSystemDataSet();
            DataBinder.BindingLookupEditDataSource(txtDataset, data,
                "DataSetName", "DataSetID");
            txtDataset.EditValue = _user.DBID;
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtAccount.Text.Trim() == string.Empty)
            {
                Msg.Warning("请输入登录帐号!");
                txtAccount.Focus();
                return;
            }
 
            if (txtOldPwd.EditValue == null ||
                txtOldPwd.EditValue.ToStringEx().Trim() == string.Empty)
            {
                Msg.Warning("请输入旧密码!");
                txtOldPwd.Focus();
                return;
            }
 
            //取用户的资料
            var dt = new bllUser().GetUser(txtAccount.Text);
            if (dt.Rows.Count > 0)
            {
                var pwd = dt.Rows[0][tb_MyUser.Password].ToStringEx();
                pwd = KeyProvider.Default.Decrypt(pwd);
 
                //管理员修改用户密码不需要验证旧密码
                if (Loginer.CurrentUser.IsAdmin())
                {
                    txtOldPwd.Text = pwd;
                }
                else
                {
                    if (pwd != txtOldPwd.Text.Trim())
                    {
                        Msg.Warning("所输入旧密码不正确!");
                        txtOldPwd.Focus();
                        return;
                    }
                }
            }
            else
            {
                Msg.Warning("登录帐号'" + txtAccount.Text + "'不存在!");
                return;
            }
 
            if (txtPwdF.EditValue == null ||
                txtPwdF.EditValue.ToStringEx().Trim() == string.Empty)
            {
                Msg.Warning("请输入新密码!");
                txtPwdF.Focus();
                return;
            }
 
            if (txtPwdF.Text.Trim().Length < 3)
            {
                Msg.Warning("密码长度必须大于2位!");
                txtPwdF.Focus();
                return;
            }
 
            if (txtPwdS.EditValue == null ||
                txtPwdS.EditValue.ToStringEx().Trim() == string.Empty)
            {
                Msg.Warning("请确认新密码!");
                txtPwdS.Focus();
                return;
            }
 
            if (txtPwdF.EditValue.ToStringEx() !=
                txtPwdS.EditValue.ToStringEx())
            {
                Msg.Warning("两次新密码不相同!");
                txtPwdF.Focus();
                return;
            }
 
            if (Msg.AskQuestion("要修改密码?"))
                try
                {
                    frmWaitingEx.ShowMe(this);
 
                    var pwd1 = KeyProvider.Default.Encrypt(txtOldPwd.Text);
                    var pwd2 = KeyProvider.Default.Encrypt(txtPwdF.Text);
 
                    var success =
                        new bllUser().ModifyPassword(txtAccount.Text, pwd1,
                            pwd2);
                    if (success)
                    {
                        _user.Password = txtPwdF.Text;
                        Msg.ShowInformation("修改成功!");
                        Close();
                    }
                    else
                    {
                        Msg.Warning("修改出错!");
                    }
                }
                finally
                {
                    frmWaitingEx.HideMe(this);
                }
        }
 
        /// <summary>
        ///     执行窗体.参数:loginInfo为登录对象ss
        /// </summary>
        public static void Execute(LoginUser user, ModifyPwdType type,
            Form owner)
        {
            var form = new frmModifyPwd();
            form.txtAccount.Text = user.Account;
            form._user = user;
            form._type = type;
            form.Owner = owner;
            form.ShowDialog();
        }
 
        private void frmModifyPwd_Activated(object sender, EventArgs e)
        {
            if (txtAccount.CanFocus)
                txtAccount.Focus();
            else
                txtOldPwd.Focus();
        }
 
        private void frmModifyPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) SendKeys.Send("{Tab}"); //发送Tab键
        }
 
        private void frmModifyPwd_Load(object sender, EventArgs e)
        {
            BindingDataSet();
 
            //此段代码仅用户管理窗体修改密码有效
            if (ModifyPwdType.UserManage == _type)
            {
                if (Loginer.CurrentUser.IsAdmin())
                {
                    txtOldPwd.Text = "************"; //仅用于显示
                    txtOldPwd.Enabled = false;
                }
 
                //普通用户仅修改本人的密码
                if (false == Loginer.CurrentUser.IsAdmin())
                    //如果已经登录,修改当前用户的密码
                    if (Loginer.CurrentUser.Account != string.Empty)
                    {
                        txtAccount.Text = Loginer.CurrentUser.Account;
                        txtAccount.Enabled = false;
                    }
            }
        }
    }
 
    /// <summary>
    ///     修改密码类型
    /// </summary>
    public enum ModifyPwdType
    {
        /// <summary>
        ///     登录窗体直接修改密码
        /// </summary>
        LoginWindowDirect,
 
        /// <summary>
        ///     用户管理窗体修改密码
        /// </summary>
        UserManage
    }
}