Skip to content

Commit 4cbb06d

Browse files
committed
resolve merge conflicts and restore correct code from restore-hvanh
2 parents 5ad4503 + b496636 commit 4cbb06d

7 files changed

Lines changed: 124 additions & 11 deletions

File tree

src/java/com/library/controller/admin/book/AddBookController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
*
3333
* @author laptop gigabyte
3434
*/
35+
3536
@WebServlet(name = "saveBooks", urlPatterns = {"/admin/book/add"})
3637
@MultipartConfig
37-
public class AddBookController extends HttpServlet {
38-
38+
public class AddBookController extends HttpServlet {
3939
BookService bookService = ServiceFactory.getBookService();
4040
private static final Logger logger = LoggerFactory.getLogger(AddBookController.class);
4141

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
4+
*/
5+
package com.library.controller.user;
6+
7+
import com.library.factory.ServiceFactory;
8+
import com.library.service.UserService;
9+
import com.library.util.HashPassword;
10+
import java.io.IOException;
11+
import java.io.PrintWriter;
12+
import jakarta.servlet.ServletException;
13+
import jakarta.servlet.annotation.WebServlet;
14+
import jakarta.servlet.http.HttpServlet;
15+
import jakarta.servlet.http.HttpServletRequest;
16+
import jakarta.servlet.http.HttpServletResponse;
17+
import jakarta.servlet.http.HttpSession;
18+
19+
/**
20+
*
21+
* @author laptop gigabyte
22+
*/
23+
@WebServlet(name = "ChangePassword", urlPatterns = {"/user/change-password"})
24+
public class ChangePassword extends HttpServlet {
25+
26+
private UserService userService = ServiceFactory.getUserService();
27+
28+
@Override
29+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
30+
throws ServletException, IOException {
31+
String currentPassword = request.getParameter("currentPassword");
32+
String newPassword = request.getParameter("newPassword");
33+
String confirmPassword = request.getParameter("confirmPassword");
34+
HttpSession session = request.getSession(false);
35+
String account = (String) session.getAttribute("account");
36+
String getHashedPassword = userService.getHashedPassword(account);
37+
String hashedPassword = HashPassword.hash(newPassword);
38+
if (newPassword.trim().isEmpty()) {
39+
session.setAttribute("changePasswordError", "Mật khẩu không được bao gồm khoảng trắng!");
40+
response.sendRedirect(request.getContextPath() + "/user/setting");
41+
return;
42+
} else if (newPassword.contains(" ")) {
43+
session.setAttribute("changePasswordError", "Mật khẩu không được bao gồm khoảng trắng!");
44+
response.sendRedirect(request.getContextPath() + "/user/setting");
45+
return;
46+
}else if(!newPassword.equals(confirmPassword)){
47+
session.setAttribute("changePasswordError", "Mật khẩu xác nhận không giống mật khẩu mới!");
48+
response.sendRedirect(request.getContextPath() + "/user/setting");
49+
return;
50+
}
51+
if (HashPassword.checkPassword(currentPassword, getHashedPassword)) {
52+
if (userService.updatePassword(account, hashedPassword)) {
53+
session.setAttribute("changePasswordSuccess", "Cập nhật mật khẩu thành công!");
54+
response.sendRedirect(request.getContextPath() + "/user/setting");
55+
return;
56+
} else {
57+
session.setAttribute("changePasswordError", "Cập nhật mật khẩu không thành công!");
58+
response.sendRedirect(request.getContextPath() + "/user/setting");
59+
return;
60+
}
61+
} else {
62+
session.setAttribute("changePasswordError", "Mật khẩu hiện tại sai!");
63+
response.sendRedirect(request.getContextPath() + "/user/setting");
64+
return;
65+
}
66+
}
67+
68+
}

src/java/com/library/controller/user/SettingController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
4545
response.sendRedirect(request.getContextPath() + "/user/login");
4646
return;
4747
}
48+
String error = (String) session.getAttribute("changePasswordError");
49+
String success = (String) session.getAttribute("changePasswordSuccess");
50+
request.setAttribute("error", error);
51+
request.setAttribute("success", success);
52+
session.removeAttribute("changePasswordError");
53+
session.removeAttribute("changePasswordSuccess");
4854
session.setAttribute("user", dto);
4955
request.getRequestDispatcher("/WEB-INF/views/user/setting.jsp").forward(request, response);
5056
return;

src/java/com/library/dao/UserDao.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public interface UserDao {
4646

4747
String findHashedPassword(String account);
4848

49-
List<Integer> getAllUserID();
50-
49+
List<Integer> getAllUserID();
50+
51+
boolean updatePassword(String account,String password);
52+
5153

5254
}

src/java/com/library/dao/UserDaoImpl.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.sql.SQLException;
1515
import java.util.ArrayList;
1616
import java.util.List;
17+
import java.util.logging.Level;
1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
1920

@@ -251,16 +252,35 @@ public List<Integer> getAllUserID() {
251252
String sql = "select user_id from users ";
252253
List<Integer> list = new ArrayList<>();
253254
try (
254-
Connection conn = DBConnection.getInstance().getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
255+
Connection conn = DBConnection.getInstance().getConnection();
256+
PreparedStatement ps = conn.prepareStatement(sql)) {
255257
ResultSet rs = ps.executeQuery();
256258
while (rs.next()) {
257259
int id = rs.getInt("user_id");
258260
list.add(id);
259261
}
262+
} catch (SQLException ex) {
263+
ex.printStackTrace();
264+
}
265+
return list ;
266+
}
267+
268+
public boolean updatePassword(String account, String password) {
269+
String sql = "UPDATE users\n"
270+
+ "SET password = ?\n"
271+
+ "WHERE account = ?;";
272+
try (
273+
Connection conn = DBConnection.getInstance().getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
274+
ps.setString(1, password);
275+
ps.setString(2, account);
276+
int rs = ps.executeUpdate();
277+
if(rs > 0){
278+
return true;
279+
}
260280
} catch (SQLException s) {
261281
s.printStackTrace();
262282
}
263-
return list;
283+
return false ;
264284
}
265285

266286
@Override
@@ -272,7 +292,7 @@ public void setOfflineAll() {
272292
ps.executeUpdate();
273293
} catch (SQLException s) {
274294
s.printStackTrace();
275-
}
295+
}
276296
}
277297

278298
}

src/java/com/library/service/UserService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public int getUserIDByAccount(String account){
9999
public String getHashedPassword(String account){
100100
return this.userDao.findHashedPassword(account);
101101
}
102+
102103

103104
public void logoutAllUser(){
104105
Collection<HttpSession> session = SessionTracker.getAllValue();
@@ -110,4 +111,14 @@ public void logoutAllUser(){
110111

111112
}
112113

114+
115+
public boolean updatePassword(String account,String password){
116+
if(userDao.updatePassword(account, password)){
117+
logger.info("update completed ()",account);
118+
return true;
119+
}
120+
logger.info("update failed!");
121+
return false;
122+
}
123+
113124
}

web/WEB-INF/views/user/setting.jsp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@
472472
</style>
473473

474474
<script>
475-
475+
476476
function toggleEdit(fieldId) {
477477
const input = document.getElementById(fieldId);
478478
const saveBtn = document.getElementById('saveProfileBtn');
@@ -491,16 +491,16 @@
491491
input.focus();
492492
}
493493
494-
494+
495495
saveBtn.style.display = 'inline-flex';
496496
} else {
497-
497+
498498
if (isFile) {
499499
input.disabled = true;
500500
} else {
501501
input.readOnly = true;
502502
}
503-
503+
504504
}
505505
}
506506
@@ -645,6 +645,12 @@
645645
</h3>
646646

647647
<form action="${pageContext.request.contextPath}/user/change-password" method="post">
648+
<% if (request.getAttribute("error") != null) { %>
649+
<p style="color:red"><%= request.getAttribute("error") %></p>
650+
<% } %>
651+
<% if (request.getAttribute("success") != null) { %>
652+
<p style="color:#28a745"><%= request.getAttribute("success") %></p>
653+
<% } %>
648654
<div class="form-group">
649655
<label for="currentPassword">Current Password</label>
650656
<input type="password" id="currentPassword" name="currentPassword"

0 commit comments

Comments
 (0)