Skip to content

Commit

Permalink
Change Password
Browse files Browse the repository at this point in the history
  • Loading branch information
BimsaraBodaragama committed May 29, 2022
1 parent 49052e6 commit 95e9f8d
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 117 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/trb/utils/PasswordUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ public class PasswordUpdater {

private String confirmedNewPassword;

public PasswordUpdater() {
}

public PasswordUpdater(long userId) {
this.userId = userId;
}

public long getUserId() {
return userId;
}
Expand Down
107 changes: 55 additions & 52 deletions src/main/java/org/trb/web/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.trb.model.User;
import org.trb.repository.UserRepository;
import org.trb.service.UserService;
Expand All @@ -14,17 +11,19 @@
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.trb.utils.AdminLock;
import org.trb.utils.PasswordUpdater;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.security.SecureRandom;

import java.security.Principal;
import java.security.SecureRandom;
import java.util.Optional;

@Controller
@RequestMapping("/user")
public class UserController {

Logger log = LoggerFactory.getLogger(UserController.class);
private static final Logger log = LoggerFactory.getLogger(UserController.class);

@Autowired
private UserService userService;
Expand All @@ -37,7 +36,7 @@ public class UserController {
@RequestMapping(value = "/profile", method = RequestMethod.GET)
public String profile(Principal principal, Model model) {
User user = userService.findByUsername(principal.getName());
PasswordUpdater passwordUpdater = new PasswordUpdater();
PasswordUpdater passwordUpdater = new PasswordUpdater(user.getUserId());

model.addAttribute("user", user);
model.addAttribute("passwordUpdater", passwordUpdater);
Expand All @@ -47,79 +46,63 @@ public String profile(Principal principal, Model model) {

@RequestMapping(value = "/profile", method = RequestMethod.POST)
public String profilePost(@ModelAttribute("user") User newUser, Model model) {
log.info("bbbbb");
Optional<User> userOptional = userService.findByID(newUser.getUserId());

if (!userOptional.isPresent()){
log.info("qqqqq");
}
User user = userOptional.get();

log.info("ddddd");
User user = userService.findByUsername(newUser.getUsername());
user.setUsername(newUser.getUsername());
log.info("eeee");
user.setFirstName(newUser.getFirstName());
log.info("ffff");
user.setLastName(newUser.getLastName());
log.info("ggggg");
user.setEmail(newUser.getEmail());
log.info("hhhh");
user.setPhone(newUser.getPhone());
log.info("iiii");

model.addAttribute("user", user);
log.info("jjjjj");
userService.saveUser(user);
log.info("kkkkk");
PasswordUpdater passwordUpdater = new PasswordUpdater(user.getUserId());

return "profile";
}
model.addAttribute("user", user);
model.addAttribute("passwordUpdater", passwordUpdater);

public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12, new SecureRandom(SALT.getBytes()));
}
userService.saveUser(user);

@RequestMapping(value = "/profile-change-password", method = RequestMethod.GET)
public String profileChangePasswordGet(@ModelAttribute("passwordUpdater") PasswordUpdater passwordUpdater, BindingResult result, Model model) {
return "profile";
}

@RequestMapping(value = "/profile-change-password", method = RequestMethod.POST)
public String profileChangePasswordPost(@ModelAttribute("passwordUpdater") PasswordUpdater passwordUpdater, BindingResult result, Model model) {
@RequestMapping(value = "/profile/changePassword", method = RequestMethod.POST)
public String profileChangePasswordPost(@ModelAttribute("passwordUpdater") PasswordUpdater newPasswordUpdater, Model model) {

log.info("AAAAAAAAAAAAAAAAAAAAAAA");
PasswordUpdater passwordUpdater = newPasswordUpdater;

long userId = passwordUpdater.getUserId();
String oldPassword = passwordUpdater.getOldPassword();
String newPassword = passwordUpdater.getNewPassword();
String confirmedNewPassword = passwordUpdater.getConfirmedNewPassword();

if(!newPassword.equals(confirmedNewPassword)){
log.info("New Password Mismatched");
result.addError(new FieldError("passwordUpdater",
"confirmedNewPassword", "New Password Mismatched"));
model.addAttribute("msg", "New Password Mismatched");
return "profile";
}

BCryptPasswordEncoder bCryptPasswordEncoder = passwordEncoder();
String encryptedOldPassword = bCryptPasswordEncoder.encode(oldPassword);

Optional<User> byId = userrepository.findById(userId);
if (!byId.isPresent()){
log.info("Unknown Error. User not found.");
log.info("Unknown Error. User not found.User Id: " + userId + " not found.");
model.addAttribute("msg", "Unknown Error Occurred: User NOt Found" +
". Please contact the bank.");
PasswordUpdater passwordUpdaterReBound = new PasswordUpdater();
model.addAttribute("passwordUpdater", passwordUpdaterReBound);
return "profile";
}

User user = byId.get();

if(!user.getPassword().equals(oldPassword)){
if(!newPassword.equals(confirmedNewPassword)){
log.info("New Password Mismatched");
model.addAttribute("msg", "New Password Mismatched");
PasswordUpdater passwordUpdaterReBound = new PasswordUpdater(userId);
model.addAttribute("user", user);
model.addAttribute("passwordUpdater", passwordUpdaterReBound);
return "profile";
}

BCryptPasswordEncoder bCryptPasswordEncoder = passwordEncoder();
/*String encryptedOldPassword = bCryptPasswordEncoder.encode(oldPassword);
log.info("/////////////////===== " + encryptedOldPassword + " =============");*/

if(!passwordChecker(passwordUpdater, user)){
log.info("Incorrect Old Password");
result.addError(new FieldError("passwordUpdater",
"oldPassword", "Incorrect Old Password"));
model.addAttribute("msg", "Incorrect Old Password");
PasswordUpdater passwordUpdaterReBound = new PasswordUpdater(userId);
model.addAttribute("user", user);
model.addAttribute("passwordUpdater", passwordUpdaterReBound);
return "profile";
}

Expand All @@ -129,11 +112,31 @@ public String profileChangePasswordPost(@ModelAttribute("passwordUpdater") Passw

userService.saveUser(user);

return "profile";
return "redirect:/user/profile";

}

public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12, new SecureRandom(SALT.getBytes()));
}

public boolean passwordChecker(PasswordUpdater passwordUpdater, User user) {

BCryptPasswordEncoder bCryptPasswordEncoder = passwordEncoder();
String encryptedAdminPassword = bCryptPasswordEncoder.encode(passwordUpdater.getOldPassword());

}
log.info("OLDPASSWORD====" + encryptedAdminPassword + "====");

User adminTRB = user;
if(adminTRB.getPassword().equals(encryptedAdminPassword)){
log.info("Matching Old Password.");
return true;

}

log.info("Old Password Mismatched.");
return false;

}

}
8 changes: 5 additions & 3 deletions src/main/resources/templates/admin-lock-signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
<img src="/images/bann.png" alt="bann" class="center">

<div class="container">
<div th:if="${msg}">
<div class="alert alert-danger" th:text="${msg}"></div>
<div class="text-center">
<div th:if="${msg}">
<div class="alert alert-danger" th:text="${msg}"></div>
</div>
</div>
<div class="row ">
<div class="col-md-6 col-md-offset-3">
<div class="bg-danger" th:if="${param.error}">
Invalid username and secret.
Invalid username or password.
</div>
<div class="bg-danger" th:if="${param.logout}">
You have been logged out.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<div class="row ">
<div class="col-md-6 col-md-offset-3">
<div class="bg-danger" th:if="${param.error}">
Invalid username and secret.
Invalid username or password.
</div>
<div class="bg-danger" th:if="${param.logout}">
You have been logged out.
Expand Down
54 changes: 26 additions & 28 deletions src/main/resources/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
layout:decorator="common/header">
layout:decorator="common/header" xmlns="http://www.w3.org/1999/html">

<head>
<title>Home</title>
Expand Down Expand Up @@ -50,12 +50,15 @@ <h1>DEPOSIT</h1>
</div>
</div>-->

<div class="d-sm-flex align-items-center justify-content-between mb-4">
<div th:if="${msg}">
<div class="alert alert-danger" th:text="${msg}"></div>
</div>
<div class="d-sm-flex align-items-center justify-content-between mb-12">
<div class="text-center">
<div th:if="${msg}">
<div class="alert alert-danger" th:text="${msg}"></div>
</div>
<div class="text-center">
</div>


<!-- Content Row 1 -->
<div class="row">

Expand Down Expand Up @@ -89,14 +92,14 @@ <h1>DEPOSIT</h1>
<h6 class="m-0 font-weight-bold text-secondary">My Profile</h6>
</div>
<div class="d-sm-flex align-items-center justify-content-between mb-4">

</div>
<form class="form-user" method="post" th:object="${user}" th:action="@{/user/profile}">
<form method="post" th:action="@{/user/profile}">
<input type="hidden" name="id" th:value="${user.userId}"/>

<div class="form-group">
<label for="firstName">First Name</label>
<div class="cols-sm-8">
<div class="cols-sm-10">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>-->
<input type="text" class="form-control" th:value="${user.firstName}" id="firstName"
Expand All @@ -108,7 +111,7 @@ <h6 class="m-0 font-weight-bold text-secondary">My Profile</h6>

<div class="form-group">
<label for="lastName" class="cols-sm-2 control-label">Last Name</label>
<div class="cols-sm-8">
<div class="cols-sm-10">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>-->
<input type="text" class="form-control" th:value="${user.lastName}" id="lastName"
Expand All @@ -120,7 +123,7 @@ <h6 class="m-0 font-weight-bold text-secondary">My Profile</h6>

<div class="form-group">
<label for="phone" class="cols-sm-2 control-label">Phone</label>
<div class="cols-sm-8">
<div class="cols-sm-10">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>-->
<input type="text" class="form-control" th:value="${user.phone}" id="phone" name="phone"
Expand All @@ -132,10 +135,9 @@ <h6 class="m-0 font-weight-bold text-secondary">My Profile</h6>
<div class="form-group">
<label for="email" class="cols-sm-2 control-label">Your Email</label><span
class="bg-danger pull-right" th:if="${emailExists}">Email already exists</span>
<div class="cols-sm-8">
<div class="cols-sm-10">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-envelope fa"
aria-hidden="true"></i></span>-->
<!--<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>-->
<input type="text" class="form-control" th:value="${user.email}" id="email" name="email"
roleId="email" placeholder="Enter your Email" required="required"/>
</div>
Expand All @@ -145,12 +147,12 @@ <h6 class="m-0 font-weight-bold text-secondary">My Profile</h6>
<div class="form-group">
<label for="username" class="cols-sm-2 control-label">Username</label><span
class="bg-danger pull-right" th:if="${usernameExists}">Username already exists</span>
<div class="cols-sm-8">
<div class="cols-sm-10">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-users fa" aria-hidden="true"></i></span>-->
<input type="text" class="form-control" th:value="${user.username}" id="username"
name="username" roleId="username" placeholder="Enter your Username"
required="required" disabled/>
required="required" readonly/>
</div>
</div>
</div>
Expand Down Expand Up @@ -196,19 +198,17 @@ <h1 class="title">Your Account Information</h1>

</div>

<form method="post" th:object="${passwordUpdater}" th:action="@{/user/profile-change-password}"></form>
<div id="errormsg" style="display:none"></div>

<input type="hidden" name="userId" th:value="${user.userId}"/>
<form method="post" th:action="@{/user/profile/changePassword}">
<input type="hidden" name="userId" th:value="${passwordUpdater.userId}"/>

<div id="errormsg" style="display:none"></div>
<div class="form-group">
<label for="phone" class="cols-sm-2 control-label">Old Password</label>
<div class="cols-sm-4">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>-->
<input type="password" class="form-control" th:value="${passwordUpdater.oldPassword}" id="oldPassword" name="oldPassword"
<input type="password" class="form-control" id="oldPassword" name="oldPassword"
roleId="oldPassword" placeholder="" required="required"/>
<!--<span class="text-danger" th:if="${#fields.hasErrors('oldPassword')}" th:errors="*{oldPassword}"></span>-->
</div>
</div>
</div>
Expand All @@ -217,9 +217,8 @@ <h1 class="title">Your Account Information</h1>
<div class="cols-sm-4">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>-->
<input type="password" class="form-control" th:value="${passwordUpdater.newPassword}" id="newPassword" name="newPassword"
<input type="password" class="form-control" id="newPassword" name="newPassword"
roleId="newPassword" placeholder="" required="required"/>
<!--<span class="text-danger" th:if="${#fields.hasErrors('newPassword')}" th:errors="*{newPassword}"></span>-->
</div>
</div>
</div>
Expand All @@ -228,13 +227,12 @@ <h1 class="title">Your Account Information</h1>
<div class="cols-sm-4">
<div class="input-group">
<!--<span class="input-group-addon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>-->
<input type="password" class="form-control" th:value="${passwordUpdater.confirmedNewPassword}" id="confirmedNewPassword" name="confirmedNewPassword"
<input type="password" class="form-control" id="confirmedNewPassword" name="confirmedNewPassword"
roleId="confirmedNewPassword" placeholder="" required="required"/>
<!--<span class="text-danger" th:if="${#fields.hasErrors('confirmedNewPassword')}" th:errors="*{confirmedNewPassword}"></span>-->
</div>
</div>
</div>
<span id="error" style="display:none">Password mismatch</span>
<!--<span id="error" style="display:none">Password mismatch</span>-->
<div class="form-group ">
<button type="submit" class="btn btn-primary btn-block">Change Password
</button>
Expand All @@ -244,8 +242,8 @@ <h1 class="title">Your Account Information</h1>
</form>


<script src="jquery.min.js"></script>
<script type="text/javascript">
<script src="jquery.min.js"></script>
<script type="text/javascript">
var serverContext = [[@{/}]];
function savePass(){
var pass = $("#pass").val();
Expand Down
Loading

0 comments on commit 95e9f8d

Please sign in to comment.