Skip to content

Commit

Permalink
Feature: Integrated reset password
Browse files Browse the repository at this point in the history
  • Loading branch information
ishika28 committed Jul 11, 2020
1 parent f50a445 commit 5e85d26
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class UserSpec implements Specification<User> {

private static final String FILTER_BY_NAME = "name";
private static final String FILTER_BY_USER_TYPE = "userType";
public static final String FILTER_BY_EMAIL = "email";

private final String property;
private final String value;
Expand All @@ -34,6 +35,8 @@ public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery,
return criteriaBuilder.like(root.get(FILTER_BY_NAME), "%" + value + "%");
case FILTER_BY_USER_TYPE:
return criteriaBuilder.equal(root.get(FILTER_BY_USER_TYPE), UserType.valueOf(value));
case FILTER_BY_EMAIL:
return criteriaBuilder.equal(root.get(FILTER_BY_EMAIL), value);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public final class EmailConstant {

public enum Template {
REGISTRATION_CREDENTIALS("Registration Credentials!!!"),
PATIENT_REGISTRATION("Patient Registration Credentials!!!");
PATIENT_REGISTRATION("Patient Registration Credentials!!!"),
RESET_PASSWORD("Reset your password!!!");

private final String subject;

Expand All @@ -27,6 +28,7 @@ public String get() {
public static final Map<Template, String> MAIL = ImmutableMap.<Template, String>builder()
.put(Template.REGISTRATION_CREDENTIALS, "/mail/registration-credentials")
.put(Template.PATIENT_REGISTRATION, "/mail/patient-registration")
.put(Template.RESET_PASSWORD, "/mail/reset-password.html")
.build();

private EmailConstant() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EmailDto {
private String username;
private String webUrl;
private String patientId;
private String resetToken;

private Template template;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public void configure(HttpSecurity http) throws Exception {
.permitAll()
.antMatchers(HttpMethod.POST, "/v1/patient")
.anonymous()
.antMatchers(HttpMethod.POST, "/v1/users/resetPassword/verify")
.permitAll()
.antMatchers(HttpMethod.POST, "/v1/users/resetPassword")
.permitAll()
.antMatchers("/v1/**")
.authenticated()
.and()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.pemits.webcare.web.user;

import static com.pemits.webcare.api.user.repository.spec.UserSpec.FILTER_BY_EMAIL;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -15,9 +19,13 @@

import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.api.user.service.UserService;
import com.pemits.webcare.core.constant.EmailConstant.Template;
import com.pemits.webcare.core.controller.BaseController;
import com.pemits.webcare.core.dto.EmailDto;
import com.pemits.webcare.core.dto.RestResponseDto;
import com.pemits.webcare.core.enums.Status;
import com.pemits.webcare.core.service.EmailService;
import com.pemits.webcare.core.utils.PasswordGenerator;
import com.pemits.webcare.web.user.dto.ChangePasswordDto;

/**
Expand All @@ -31,13 +39,17 @@ public class UserController extends BaseController<User, Long> {
static final String URL = "/v1/users";
private final UserService service;
private final PasswordEncoder passwordEncoder;
private final EmailService emailService;

protected UserController(
UserService service,
PasswordEncoder passwordEncoder) {
PasswordEncoder passwordEncoder,
EmailService emailService
) {
super(service, log.getClass());
this.service = service;
this.passwordEncoder = passwordEncoder;
this.emailService = emailService;
}

@GetMapping("/authenticated")
Expand Down Expand Up @@ -71,7 +83,54 @@ public ResponseEntity<?> changePassword(@RequestBody ChangePasswordDto dto) {
User updatedUser = service.save(user);
if (updatedUser == null) {
return new RestResponseDto()
.fail(HttpStatus.INTERNAL_SERVER_ERROR, Optional.of("Could not update password!!!"));
.fail(HttpStatus.INTERNAL_SERVER_ERROR,
Optional.of("Could not update password!!!"));
}
return new RestResponseDto().success(dto);
}

@PostMapping("/resetPassword/verify")
public ResponseEntity<?> verifyResetPassword(@RequestBody ChangePasswordDto dto) {
Map<String, String> filter = new HashMap<>();
filter.put(FILTER_BY_EMAIL, dto.getEmail());
Optional<User> user = service.findOneBySpec(filter);

if (!user.isPresent()) {
return new RestResponseDto().fail(HttpStatus.NOT_FOUND, Optional.of("User not found"));
}

PasswordGenerator passwordGenerator = new PasswordGenerator.PasswordGeneratorBuilder()
.useDigits(true)
.build();
dto.setToken(passwordGenerator.generate(4));
dto.setUserId(user.get().getId());

// send email
EmailDto emailDto = EmailDto.builder()
.template(Template.RESET_PASSWORD)
.to(user.get().getEmail())
.toName(user.get().getName())
.resetToken(dto.getToken())
.build();
emailService.send(emailDto);

return new RestResponseDto().success(dto);
}

@PostMapping("/resetPassword")
public ResponseEntity<?> resetPassword(@RequestBody ChangePasswordDto dto) {
User user = service.findOne(dto.getUserId()).orElse(null);

if (user == null) {
return new RestResponseDto()
.fail(HttpStatus.NOT_FOUND, Optional.of("User not found!!!"));
}
user.setPassword(passwordEncoder.encode(dto.getNewPassword()));
User updatedUser = service.save(user);
if (updatedUser == null) {
return new RestResponseDto()
.fail(HttpStatus.INTERNAL_SERVER_ERROR,
Optional.of("Could not reset password!!!"));
}
return new RestResponseDto().success(dto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class ChangePasswordDto {
private String oldPassword;
private String newPassword;
private String confirmPassword;
private String email;
private String token;
}
146 changes: 146 additions & 0 deletions pemits-web/src/main/resources/templates/mail/reset-password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!DOCTYPE html>
<html xmlns:th="http:https://www.thymeleaf.org">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
</head>
<style>
.custom-para {
margin: 0;
color: #4c4c4c;
font-weight: 400;
font-size: 16px;
line-height: 1.25
}
</style>
<body>

<div
style="padding:0;margin:0 auto;width:100%!important;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif">

<table align="center" bgcolor="#EDF0F3" border="0" cellpadding="0" cellspacing="0"
style="background-color:#edf0f3;table-layout:fixed" width="100%">
<tbody>
<tr>
<td align="center">
<table bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0"
style="background-color:#ffffff;margin:0 auto;max-width:512px;width:inherit"
width="512">
<tbody>
<tr>
<td bgcolor="#F6F8FA"
style="background-color:#f6f8fa;padding:12px;border-bottom:1px solid #ececec">
<table border="0" cellpadding="0" cellspacing="0"
style="width:100%!important;min-width:100%!important"
width="100%">
<tbody>
<tr>
<td align="left" valign="middle"><a
style="color:#008cc9;display:inline-block;text-decoration:none">
<!--<img alt=""
border="0"
height="50"
src=""
style="outline:none;color:#ffffff;text-decoration:none"
width="150">-->
</a></td>
<td align="right" style="padding:0 0 0 10px" width="100%"><a
style="margin:0;color:#008cc9;display:inline-block;text-decoration:none">
<span
style="word-wrap:break-word;color:#4c4c4c;word-break:break-word;font-weight:400;font-size:14px;line-height:1.429"><span
th:text="${data.getTo()}"></span></span>
</a></td>
<td width="1">&nbsp;</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td style="padding:20px 24px 32px 24px">
<table border="0" cellpadding="0" cellspacing="0"
width="100%">
<tbody>
<tr>
<td style="padding-bottom:20px"><h2
style="margin:0;color:#262626;font-weight:700;font-size:20px;line-height:1.2">
Hello <span
th:text="${data.getToName()}"></span>,</h2></td>
</tr>
<tr>
<td style="padding-bottom:20px">
<p class="custom-para">
Please use this code for resetting your password.
</p>
<p class="custom-para">
Token: <strong><span th:text="${data.getResetToken()}"></span></strong>
</p>
<hr>
<p class="custom-para" style="color: red;">
This token will expire as soon as you leave the form.
</p>
<hr>
<p class="custom-para">
Regards, <br/>
<em>The PEMITS Team</em>
</p>
<hr/>
<p class="custom-para">
Note: Please do not reply to this email, as
this is an automated email
and any email sent to
this email will not be answered.
</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table align="center" bgcolor="#EDF0F3" border="0" cellpadding="0"
cellspacing="0"
style="background-color:#edf0f3;padding:0 24px;color:#6a6c6d;text-align:center"
width="100%">
<tbody>
<tr>
<td align="center"
style="padding:16px 0 0 0;text-align:center"></td>
</tr>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0"
width="100%">
<tbody>
<tr>
<td align="center"
style="padding:0 0 12px 0;text-align:center"><p
style="margin:0;color:#6a6c6d;font-weight:400;font-size:12px;line-height:1.333"></p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>

</body>
</html>

0 comments on commit 5e85d26

Please sign in to comment.