Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/BE-42-applicant-change-password #181

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hydraulic.applyforme.config.filter;


import com.hydraulic.applyforme.model.security.UserDetailsImpl;
import com.hydraulic.applyforme.service.impl.UserDetailsServiceImpl;
import com.hydraulic.applyforme.util.JwtUtil;
Expand Down Expand Up @@ -47,14 +48,17 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
return;
}


System.out.println("header " + header);

final String token = header.substring(7);
String emailAddress = null;

try {
emailAddress = jwtUtil.getUsernameFromToken(token);

System.out.println("Email Address " + emailAddress);

} catch (IllegalArgumentException e) {
System.out.println("Unable to get JWT Token");
} catch (ExpiredJwtException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public WebSecurityCustomizer webSecurityCustomizer() {
@Bean
public AuthenticationManager authenticationManagerBean(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
authenticationManagerBuilder.authenticationProvider(authenticationProvider);
return authenticationManagerBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.hydraulic.applyforme.controller;

import com.hydraulic.applyforme.model.domain.Member;
import com.hydraulic.applyforme.model.dto.password.ChangePasswordDto;
import com.hydraulic.applyforme.service.ChangePasswordService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(
value = "password",
produces = { MediaType.APPLICATION_JSON_VALUE }
)
public class ChangePasswordController {

private ChangePasswordService service;

public ChangePasswordController(ChangePasswordService passwordService) {
this.service = passwordService;
}

@PostMapping("/change-password/{id}")
public Member changePassword(@Validated @RequestBody ChangePasswordDto password, @PathVariable(name = "id") Long id){
return service.changePassword(id, password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
Expand Down Expand Up @@ -204,7 +203,6 @@ public Object forbidden(AccessDeniedException ex, HttpServletRequest request) {
return body;
}


@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(AuthenticationException.class)
public Object unauthorized(AuthenticationException ex, HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hydraulic.applyforme.model.dto.authentication;



import com.fasterxml.jackson.annotation.JsonProperty;
import com.hydraulic.applyforme.annotation.PhoneNumberConstraint;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hydraulic.applyforme.model.dto.password;

import com.hydraulic.applyforme.model.domain.Member;
import lombok.Data;

@Data
public class ChangePasswordDto {

private String password;
private String oldPassword;
private String newPassword;
private String reNewPassword;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hydraulic.applyforme.model.exception;

public class InvalidOldPasswordException extends ApplyForMeException{

private static final long serialVersionUID = 1L;
public static final String ENTITY_NAME = "Password";

@Override
public String getMessage() {
return String.format("%s entry does not match old password.", ENTITY_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hydraulic.applyforme.repository.impl;

import com.hydraulic.applyforme.model.domain.Member;
import com.hydraulic.applyforme.repository.UpdateMemberRepository;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;

@Repository
public class UpdateMemberRepositoryImpl implements UpdateMemberRepository {


@PersistenceContext
private EntityManager entityManager;

public Member getOne(Long id) {
return entityManager.find(Member.class, id);
}

public Member updateOne(Member body) {
return entityManager.merge(body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.hydraulic.applyforme.repository.jpa;

import com.hydraulic.applyforme.model.domain.Member;
import com.hydraulic.applyforme.model.dto.password.ChangePasswordDto;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface PasswordJpaRepository extends JpaRepository<Member, String> {

Optional<Member> findById(ChangePasswordDto id);

Member findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
public interface AuthenticationService {

void resetPassword(ResetPasswordDto resetPasswordDto);

void authenticate(String username, String password) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hydraulic.applyforme.service;

import com.hydraulic.applyforme.model.domain.Member;
import com.hydraulic.applyforme.model.dto.password.ChangePasswordDto;

public interface ChangePasswordService {

Member changePassword(Long Id, ChangePasswordDto password);

boolean checkIfValidOldPassword(Member member, String password);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public AuthenticationServiceImpl(MemberSecretJpaRepository secretJpaRepository,
this.memberJpaRepository = memberJpaRepository;
this.memberRepository = memberRepository;
}

public void resetPassword(ResetPasswordDto dto) {
MemberSecretCode secretCodeExists = secretJpaRepository.findByForgotPasswordCode(dto.getToken());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.hydraulic.applyforme.service.impl;

import com.hydraulic.applyforme.model.domain.Member;
import com.hydraulic.applyforme.model.dto.password.ChangePasswordDto;
import com.hydraulic.applyforme.model.exception.InvalidOldPasswordException;
import com.hydraulic.applyforme.repository.jpa.PasswordJpaRepository;
import com.hydraulic.applyforme.service.ChangePasswordService;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import static java.util.regex.Pattern.matches;

@Slf4j
@Service
public class ChangePasswordServiceImpl implements ChangePasswordService {

@Autowired
private ModelMapper modelMapper;

@Autowired
private PasswordEncoder passwordEncoder;

private final PasswordJpaRepository jpaRepository;

public ChangePasswordServiceImpl(PasswordJpaRepository jpaRepository) {
this.jpaRepository = jpaRepository;
}

@Override
@Transactional
public boolean checkIfValidOldPassword(Member member, String oldPassword) {
return matches(oldPassword, member.getPassword());
}

@Override
@Transactional
public Member changePassword(Long id, ChangePasswordDto password) {
final Member member = jpaRepository.findById(id);

Member mapper = new Member();
mapper = modelMapper.map(id, Member.class);

if (checkIfValidOldPassword(member, member.getPassword())){
if (password.getNewPassword().equals(password.getReNewPassword())) {
member.setPassword(password.getNewPassword());
jpaRepository.save(member);
}
}else{
throw new InvalidOldPasswordException();
}

return member;

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;

@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;

Expand Down Expand Up @@ -113,5 +112,4 @@ else if (role.getCode().equals("Professional")) {
}
claimsMap.put("memberType", memberType);
}

}
1 change: 1 addition & 0 deletions applyforme.web
Submodule applyforme.web added at 165250