* 修改用户密码
(username, currentPassword, newPassword)
| 123 | * 修改用户密码 |
| 124 | */ |
| 125 | async changePassword(username, currentPassword, newPassword) { |
| 126 | try { |
| 127 | const user = await this.getUserByUsername(username); |
| 128 | |
| 129 | if (!user) { |
| 130 | throw new Error('用户不存在'); |
| 131 | } |
| 132 | |
| 133 | // 验证当前密码 |
| 134 | const isMatch = await bcrypt.compare(currentPassword, user.password); |
| 135 | if (!isMatch) { |
| 136 | throw new Error('当前密码不正确'); |
| 137 | } |
| 138 | |
| 139 | // 验证新密码复杂度 |
| 140 | if (!this.isPasswordComplex(newPassword)) { |
| 141 | throw new Error('新密码不符合复杂度要求'); |
| 142 | } |
| 143 | |
| 144 | // 更新密码 |
| 145 | const hashedNewPassword = await bcrypt.hash(newPassword, 10); |
| 146 | await database.run( |
| 147 | 'UPDATE users SET password = ?, updated_at = ? WHERE username = ?', |
| 148 | [hashedNewPassword, new Date().toISOString(), username] |
| 149 | ); |
| 150 | |
| 151 | logger.info(`用户 ${username} 密码已成功修改`); |
| 152 | } catch (error) { |
| 153 | logger.error('修改密码失败:', error); |
| 154 | throw error; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * 修改用户名 |
no test coverage detected