密码重置功能完成

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-15 11:36:02 +08:00
parent 1da899ccf7
commit d113a8c415
10 changed files with 60 additions and 3 deletions

View File

@ -8,6 +8,11 @@
FROM user FROM user
WHERE username = #{username} WHERE username = #{username}
</select> </select>
<select id="getUserById" parameterType="int" resultType="cyou.chenx221.pojo.User">
SELECT *
FROM user
WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="cyou.chenx221.pojo.User"> <insert id="insertUser" parameterType="cyou.chenx221.pojo.User">
INSERT INTO user (username, password) INSERT INTO user (username, password)
VALUES (#{username}, #{password}) VALUES (#{username}, #{password})
@ -20,4 +25,9 @@
INSERT INTO user (username, password, roles, detail) INSERT INTO user (username, password, roles, detail)
VALUES (#{username}, #{password}, #{roles}, #{detail}) VALUES (#{username}, #{password}, #{roles}, #{detail})
</insert> </insert>
<update id="updateUserPassword" parameterType="cyou.chenx221.pojo.User">
UPDATE user
SET password = #{password}
WHERE id = #{id}
</update>
</mapper> </mapper>

View File

@ -261,7 +261,7 @@
<div class="tab-pane fade" id="ex1-tabs-2" role="tabpanel" <div class="tab-pane fade" id="ex1-tabs-2" role="tabpanel"
aria-labelledby="ex1-tab-2"> aria-labelledby="ex1-tab-2">
<div class="card-body" style="min-width: 175px"> <div class="card-body" style="min-width: 175px">
<form action="" method="post"> <form action="reset" method="post">
<div class="row mb-2"> <div class="row mb-2">
<div class="col-12"> <div class="col-12">
<div class="form-outline"> <div class="form-outline">

View File

@ -136,7 +136,7 @@ public class UserController {
// start to create // start to create
Password pw = new Password(password); Password pw = new Password(password);
System.out.println("username:" + username + " password:" + pw.getEncryptedPassword() + " role:" + role + " roleid:" + roleid); //debug System.out.println("username:" + username + " password:" + pw.getEncryptedPassword() + " role:" + role + " roleid:" + roleid); //debug
user = new User(username, pw.getEncryptedPassword(), role, (roleid==-1?null:roleid)); user = new User(username, pw.getEncryptedPassword(), role, (roleid == -1 ? null : roleid));
int status_code = userMapper.insertUserv2(user); int status_code = userMapper.insertUserv2(user);
if (status_code == 0) { if (status_code == 0) {
model.addAttribute("errorMessage", "创建用户失败,请重试。"); model.addAttribute("errorMessage", "创建用户失败,请重试。");
@ -150,4 +150,32 @@ public class UserController {
return "redirect:/user/usermanage"; return "redirect:/user/usermanage";
} }
} }
@RequestMapping(path = "/user/reset", method = {RequestMethod.POST})
public String resetpassword(@RequestParam("id") int id,
@RequestParam("password") String password,
@RequestParam("re_password") String repassword,
Model model) throws NoSuchAlgorithmException {
//check password
if (!password.equals(repassword)) {
model.addAttribute("errorMessage", "两次输入的密码不一致,请重新输入。");
return "redirect:/user/usermanage";
}
//check if user exists
User user = userMapper.getUserById(id);
if (user == null) {
model.addAttribute("errorMessage", "ID为" + id + "的用户不存在,请重新输入。");
return "redirect:/user/usermanage";
} else {
Password pw = new Password(password);
User user1 = new User(id, pw.getEncryptedPassword());
int status_code = userMapper.updateUserPassword(user1);
if (status_code == 0) {
model.addAttribute("errorMessage", "重置密码失败,请重试。");
} else {
model.addAttribute("successMessage", "重置密码成功");
}
}
return "redirect:/user/usermanage";
}
} }

View File

@ -15,4 +15,8 @@ public interface UserMapper {
int insertUserv2(User user); int insertUserv2(User user);
User getUserById(int id);
int updateUserPassword(User user);
} }

View File

@ -58,6 +58,11 @@ public class User {
this.password = password; this.password = password;
} }
public User(Integer id, String password) {
this.id = id;
this.password = password;
}
public User(String username, String password) { public User(String username, String password) {
this.username = username; this.username = username;
this.password = password; this.password = password;

View File

@ -8,6 +8,11 @@
FROM user FROM user
WHERE username = #{username} WHERE username = #{username}
</select> </select>
<select id="getUserById" parameterType="int" resultType="cyou.chenx221.pojo.User">
SELECT *
FROM user
WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="cyou.chenx221.pojo.User"> <insert id="insertUser" parameterType="cyou.chenx221.pojo.User">
INSERT INTO user (username, password) INSERT INTO user (username, password)
VALUES (#{username}, #{password}) VALUES (#{username}, #{password})
@ -20,4 +25,9 @@
INSERT INTO user (username, password, roles, detail) INSERT INTO user (username, password, roles, detail)
VALUES (#{username}, #{password}, #{roles}, #{detail}) VALUES (#{username}, #{password}, #{roles}, #{detail})
</insert> </insert>
<update id="updateUserPassword" parameterType="cyou.chenx221.pojo.User">
UPDATE user
SET password = #{password}
WHERE id = #{id}
</update>
</mapper> </mapper>

View File

@ -261,7 +261,7 @@
<div class="tab-pane fade" id="ex1-tabs-2" role="tabpanel" <div class="tab-pane fade" id="ex1-tabs-2" role="tabpanel"
aria-labelledby="ex1-tab-2"> aria-labelledby="ex1-tab-2">
<div class="card-body" style="min-width: 175px"> <div class="card-body" style="min-width: 175px">
<form action="" method="post"> <form action="reset" method="post">
<div class="row mb-2"> <div class="row mb-2">
<div class="col-12"> <div class="col-12">
<div class="form-outline"> <div class="form-outline">