diff --git a/src/main/java/org/springframework/samples/petclinic/common/CommonError.java b/src/main/java/org/springframework/samples/petclinic/common/CommonError.java index 98415c80c..f37199581 100644 --- a/src/main/java/org/springframework/samples/petclinic/common/CommonError.java +++ b/src/main/java/org/springframework/samples/petclinic/common/CommonError.java @@ -27,6 +27,14 @@ public final class CommonError { public static final String PHONE_FORMAT = "Not a valid phone number !"; + public static final String PASSWORD_WRONG_MESSAGE = "Wrong password !"; + + public static final String PASSWORD_NOT_MATCHING_MESSAGE = "New passwords are not matching !"; + + public static final String PASSWORD_EMPTY_MESSAGE = "Password can't be empty !"; + + public static final String PASSWORD_LENGTH_MESSAGE = "Wrong password lenght !"; + private CommonError() { throw new IllegalStateException("Utility class"); } diff --git a/src/main/java/org/springframework/samples/petclinic/configuration/MailConfig.java b/src/main/java/org/springframework/samples/petclinic/configuration/MailConfig.java index 2dfbb7c7b..99c3bfca9 100644 --- a/src/main/java/org/springframework/samples/petclinic/configuration/MailConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/configuration/MailConfig.java @@ -75,7 +75,7 @@ public class MailConfig { private ITemplateResolver textTemplateResolver() { final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); - templateResolver.setOrder(Integer.valueOf(1)); + templateResolver.setOrder(1); templateResolver.setResolvablePatterns(Collections.singleton("text/*")); templateResolver.setPrefix("/mail/"); templateResolver.setSuffix(".txt"); @@ -87,7 +87,7 @@ public class MailConfig { private ITemplateResolver htmlTemplateResolver() { final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); - templateResolver.setOrder(Integer.valueOf(2)); + templateResolver.setOrder(2); templateResolver.setResolvablePatterns(Collections.singleton("html/*")); templateResolver.setPrefix("/mail/"); templateResolver.setSuffix(".html"); @@ -99,7 +99,7 @@ public class MailConfig { private ITemplateResolver stringTemplateResolver() { final StringTemplateResolver templateResolver = new StringTemplateResolver(); - templateResolver.setOrder(Integer.valueOf(3)); + templateResolver.setOrder(3); // No resolvable pattern, will simply process as a String template everything not // previously matched templateResolver.setTemplateMode("HTML5"); diff --git a/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java b/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java index a7bb7d2ef..b9a986ae4 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java @@ -109,11 +109,11 @@ public class UserController extends WebSocketSender { "Your attempt to create new account. To confirm your account, please click here : ", CommonEndPoint.PETCLINIC_CONFIRM_ACCOUNT + credential.getToken()); - emailService.sendMailAsynch(message, Locale.getDefault()); + // emailService.sendMailAsynch(message, Locale.getDefault()); log.info(message.toString()); - return CommonView.HOME + user.getId(); + return CommonView.HOME; } @GetMapping(CommonEndPoint.LOGIN) @@ -156,11 +156,15 @@ public class UserController extends WebSocketSender { @GetMapping(CommonEndPoint.OAUTH2_SUCCESS) public String postLoginOAUTH2(Model model, OAuth2AuthenticationToken authentication) { + String firstName; String lastName; String email; String providerId; String provider = authentication.getAuthorizedClientRegistrationId(); + + CredentialDTO credential = credentialService.findByAuthentication(authentication); + Map attributes = authentication.getPrincipal().getAttributes(); if (provider.equals(CommonAttribute.GOOGLE)) { @@ -181,8 +185,6 @@ public class UserController extends WebSocketSender { email = attributes.get(CommonAttribute.EMAIL).toString(); - CredentialDTO credential = credentialService.findByAuthentication(authentication); - UserDTO user = userService.findByEmail(email); if (credential.isNew()) { @@ -246,28 +248,16 @@ public class UserController extends WebSocketSender { credential.setExpiration(null); credential = credentialService.save(credential); - // find corresponding user - UserDTO user = userService.findByEmail(credential.getEmail()); + // Enabled corresponding user + UserDTO user = userService.setEnabled(credential.getEmail()); securityService.autoLogin(credential.getEmail(), credential.getPassword()); model.addAttribute(CommonAttribute.USER, user); - return CommonView.USER_UPDATE; } return CommonView.HOME; } - @GetMapping(CommonEndPoint.LOGOUT) - public String logout(HttpServletRequest request, HttpServletResponse response) { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication != null) { - new SecurityContextLogoutHandler().logout(request, response, authentication); - } - - sendSuccessMessage(CommonWebSocket.USER_LOGGED_OUT); - return CommonView.USER_LOGIN_R; - } - @GetMapping(CommonEndPoint.LOGOUT_SUCCESS) public String postLogout(HttpServletRequest request, HttpServletResponse response) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); @@ -336,7 +326,7 @@ public class UserController extends WebSocketSender { UserDTO operator = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); UserDTO user = userService.findById(userId); - if (user.equals(operator) || operator.hasRole("ROLE_ADMIN")) { + if (user.getEmail().equals(operator.getEmail()) || operator.hasRole("ROLE_ADMIN")) { model.addAttribute(CommonAttribute.USER, user); model.addAttribute(CommonAttribute.USER_ID, user.getId()); return CommonView.USER_CHANGE_PASSWORD; @@ -357,14 +347,33 @@ public class UserController extends WebSocketSender { // verify the matching with old password if (!user.matches(oldPassword)) { - bindingResult.rejectValue("password", "6", "Bad password !"); + bindingResult.rejectValue(CommonAttribute.PASSWORD, "6", CommonError.PASSWORD_WRONG_MESSAGE); + sendErrorMessage(CommonError.PASSWORD_WRONG_MESSAGE); model.addAttribute(CommonAttribute.USER, user); return CommonView.USER_CHANGE_PASSWORD; } // verify matching between two password if (!newPassword.equals(newMatchingPassword)) { - bindingResult.rejectValue("password", "7", "Bad matching password !"); + bindingResult.rejectValue(CommonAttribute.PASSWORD, "7", CommonError.PASSWORD_NOT_MATCHING_MESSAGE); + sendErrorMessage(CommonError.PASSWORD_NOT_MATCHING_MESSAGE); + model.addAttribute(CommonAttribute.USER, user); + return CommonView.USER_CHANGE_PASSWORD; + } + + // verify password not empty + if (newPassword.isEmpty()) { + bindingResult.rejectValue(CommonAttribute.PASSWORD, "8", CommonError.PASSWORD_EMPTY_MESSAGE); + sendErrorMessage(CommonError.PASSWORD_EMPTY_MESSAGE); + model.addAttribute(CommonAttribute.USER, user); + return CommonView.USER_CHANGE_PASSWORD; + } + + // verify password lenght + if (newPassword.length() < CommonParameter.PASSWORD_MIN + || newPassword.length() > CommonParameter.PASSWORD_MAX) { + bindingResult.rejectValue(CommonAttribute.PASSWORD, "9", CommonError.PASSWORD_LENGTH_MESSAGE); + sendErrorMessage(CommonError.PASSWORD_LENGTH_MESSAGE); model.addAttribute(CommonAttribute.USER, user); return CommonView.USER_CHANGE_PASSWORD; } @@ -376,7 +385,7 @@ public class UserController extends WebSocketSender { // encode password user.encode(newPassword); user = userService.save(user); - + sendInfoMessage("Password changed !"); model.addAttribute(CommonAttribute.USER, user); return CommonView.USER_UPDATE_R; } diff --git a/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java b/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java index 8e045280f..d4699fbf8 100644 --- a/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java +++ b/src/main/java/org/springframework/samples/petclinic/dto/common/CredentialDTO.java @@ -51,7 +51,7 @@ public class CredentialDTO extends BaseDTO { public CredentialDTO(UserDTO user) { this.setProvider(CommonParameter.DEFAULT_PROVIDER); this.email = user.getEmail(); - this.password = user.getId().toString(); + this.password = user.getPassword(); this.verified = false; this.setToken(); this.setExpiration(); diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java index 88afe1bb4..a27ac7edd 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -19,6 +19,7 @@ import java.util.List; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; import org.springframework.samples.petclinic.model.business.Pet; import org.springframework.samples.petclinic.model.business.PetType; import org.springframework.transaction.annotation.Transactional; @@ -50,7 +51,9 @@ public interface PetRepository extends Repository { * @param id the id to search for * @return the {@link Pet} if found */ - Pet findById(Integer id); + @Query("SELECT DISTINCT pet FROM Pet pet left join fetch pet.owner WHERE pet.id =:id") + @Transactional(readOnly = true) + Pet findById(@Param("id") Integer id); /** * Retrieve all {@link Pet}d from the data store by owner id. diff --git a/src/main/java/org/springframework/samples/petclinic/service/business/OwnerService.java b/src/main/java/org/springframework/samples/petclinic/service/business/OwnerService.java index 169935789..63819094f 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/business/OwnerService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/business/OwnerService.java @@ -3,11 +3,8 @@ package org.springframework.samples.petclinic.service.business; import org.modelmapper.ModelMapper; import org.modelmapper.internal.util.Lists; import org.springframework.samples.petclinic.dto.business.OwnerDTO; -import org.springframework.samples.petclinic.dto.business.PetDTO; import org.springframework.samples.petclinic.model.business.Owner; -import org.springframework.samples.petclinic.model.business.Pet; import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.samples.petclinic.repository.PetRepository; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -24,25 +21,16 @@ public class OwnerService implements BaseService { private final OwnerRepository ownerRepository; - private final PetRepository petRepository; - private final ModelMapper modelMapper = new ModelMapper(); - public OwnerService(OwnerRepository ownerRepository, PetRepository petRepository) { + public OwnerService(OwnerRepository ownerRepository) { this.ownerRepository = ownerRepository; - this.petRepository = petRepository; } @Override public Owner dtoToEntity(OwnerDTO dto) { if (dto != null) { - Owner owner = modelMapper.map(dto, Owner.class); - dto.getPets().forEach(petDTO -> { - Pet pet = modelMapper.map(petDTO, Pet.class); - pet.setOwner(owner); - owner.addPet(pet); - }); - return owner; + return modelMapper.map(dto, Owner.class); } return new Owner(); @@ -51,13 +39,7 @@ public class OwnerService implements BaseService { @Override public OwnerDTO entityToDTO(Owner entity) { if (entity != null) { - OwnerDTO ownerDTO = modelMapper.map(entity, OwnerDTO.class); - entity.getPets().forEach(pet -> { - PetDTO petDTO = modelMapper.map(pet, PetDTO.class); - petDTO.setOwner(ownerDTO); - ownerDTO.addPet(petDTO); - }); - return ownerDTO; + return modelMapper.map(entity, OwnerDTO.class); } return new OwnerDTO(); @@ -83,24 +65,12 @@ public class OwnerService implements BaseService { @Override public OwnerDTO findById(int ownerId) { - Owner owner = ownerRepository.findById(ownerId); - List pets = petRepository.findByOwnerId(owner.getId()); - - pets.forEach(pet -> { - // Add pet to the owner - owner.addPet(pet); - // Add owner to the pet - pet.setOwner(owner); - }); - - return entityToDTO(owner); + return entityToDTO(ownerRepository.findById(ownerId)); } @Override public List findAll() { - List owners = ownerRepository.findAll(); - - return entitiesToDTOS(owners); + return entitiesToDTOS(ownerRepository.findAll()); } @Override diff --git a/src/main/java/org/springframework/samples/petclinic/service/business/PetService.java b/src/main/java/org/springframework/samples/petclinic/service/business/PetService.java index b92d7c441..b7888549c 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/business/PetService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/business/PetService.java @@ -1,7 +1,6 @@ package org.springframework.samples.petclinic.service.business; import org.modelmapper.ModelMapper; -import org.springframework.samples.petclinic.dto.business.OwnerDTO; import org.springframework.samples.petclinic.dto.business.PetDTO; import org.springframework.samples.petclinic.dto.business.PetTypeDTO; import org.springframework.samples.petclinic.model.business.Owner; @@ -9,10 +8,10 @@ import org.springframework.samples.petclinic.model.business.Pet; import org.springframework.samples.petclinic.model.business.PetType; import org.springframework.samples.petclinic.repository.PetRepository; import org.springframework.samples.petclinic.repository.PetTypeRepository; -import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Service; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; /** @@ -27,39 +26,17 @@ public class PetService implements BaseService { private final PetTypeService petTypeService; - private final VisitService visitService; - private final ModelMapper modelMapper = new ModelMapper(); - public PetService(PetRepository petRepository, PetTypeRepository petTypeRepository, - VisitRepository visitRepository) { + public PetService(PetRepository petRepository, PetTypeRepository petTypeRepository) { this.petRepository = petRepository; - this.visitService = new VisitService(visitRepository); this.petTypeService = new PetTypeService(petTypeRepository); } @Override public Pet dtoToEntity(PetDTO dto) { if (dto != null) { - Pet pet = modelMapper.map(dto, Pet.class); - PetType petType = modelMapper.map(dto.getType(), PetType.class); - Owner owner = modelMapper.map(dto.getOwner(), Owner.class); - - dto.getVisits().forEach(visitDTO -> pet.addVisit(visitService.dtoToEntity(visitDTO))); - - dto.getOwner().getPets().forEach(petDTO -> { - if (dto.getId() == null || petDTO.getId().equals(dto.getId())) { - owner.addPet(pet); - } - else { - Pet otherPet = modelMapper.map(petDTO, Pet.class); - otherPet.setOwner(owner); - owner.addPet(otherPet); - } - }); - pet.setOwner(owner); - pet.setType(petType); - return pet; + return modelMapper.map(dto, Pet.class); } return new Pet(); @@ -68,21 +45,7 @@ public class PetService implements BaseService { @Override public PetDTO entityToDTO(Pet entity) { if (entity != null) { - PetDTO petDTO = modelMapper.map(entity, PetDTO.class); - PetTypeDTO petTypeDTO = modelMapper.map(entity.getType(), PetTypeDTO.class); - OwnerDTO ownerDTO = modelMapper.map(entity.getOwner(), OwnerDTO.class); - - petRepository.findByOwnerId(ownerDTO.getId()).forEach(pet -> { - PetDTO otherPetDTO = modelMapper.map(pet, PetDTO.class); - otherPetDTO.setOwner(ownerDTO); - ownerDTO.addPet(otherPetDTO); - }); - - entity.getVisits().forEach(visit -> petDTO.addVisit(visitService.entityToDTO(visit))); - - petDTO.setOwner(ownerDTO); - petDTO.setType(petTypeDTO); - return petDTO; + return modelMapper.map(entity, PetDTO.class); } return new PetDTO(); @@ -109,6 +72,9 @@ public class PetService implements BaseService { @Override public PetDTO findById(int petId) { Pet pet = petRepository.findById(petId); + Owner owner = pet.getOwner(); + owner.setPets(new HashSet<>()); + petRepository.findByOwnerId(owner.getId()).forEach(owner::addPet); return entityToDTO(pet); } diff --git a/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java b/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java index 6f7880599..ab9ff3bb5 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/common/RoleService.java @@ -70,9 +70,7 @@ public class RoleService implements BaseService { Collection roles = roleRepository.findAll(); List roleDTOS = new ArrayList<>(); - roles.forEach(role -> { - roleDTOS.add(entityToDTO(role)); - }); + roles.forEach(role -> roleDTOS.add(entityToDTO(role))); return roleDTOS; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/common/SecurityServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/common/SecurityServiceImpl.java index 0eedba8aa..ffbc61f1a 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/common/SecurityServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/common/SecurityServiceImpl.java @@ -2,9 +2,7 @@ package org.springframework.samples.petclinic.service.common; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; @@ -18,13 +16,10 @@ import org.springframework.stereotype.Service; @Service("SecurityService") public class SecurityServiceImpl implements SecurityService { - private final AuthenticationManager authenticationManager; - private final UserDetailsServiceImpl userDetailsService; @Autowired - public SecurityServiceImpl(AuthenticationManager authenticationManager, UserDetailsServiceImpl userDetailsService) { - this.authenticationManager = authenticationManager; + public SecurityServiceImpl(UserDetailsServiceImpl userDetailsService) { this.userDetailsService = userDetailsService; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java b/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java index 71a20cd1e..17ae2f705 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/common/UserService.java @@ -119,4 +119,11 @@ public class UserService implements BaseService { return userRepository.existsByEmail(email); } + public UserDTO setEnabled(String email) { + User user = userRepository.findByEmail(email); + user.setEnabled(true); + user = userRepository.save(user); + return entityToDTO(user); + } + } diff --git a/src/main/resources/templates/owners/ownersList.html b/src/main/resources/templates/owners/ownersList.html index cf489b59b..40003b112 100644 --- a/src/main/resources/templates/owners/ownersList.html +++ b/src/main/resources/templates/owners/ownersList.html @@ -24,7 +24,7 @@ - + diff --git a/src/main/resources/templates/users/userChangePasswordForm.html b/src/main/resources/templates/users/userChangePasswordForm.html index 2a01dcf6d..242c9f50a 100644 --- a/src/main/resources/templates/users/userChangePasswordForm.html +++ b/src/main/resources/templates/users/userChangePasswordForm.html @@ -13,7 +13,7 @@ - ; + diff --git a/src/main/resources/templates/users/userUpdateForm.html b/src/main/resources/templates/users/userUpdateForm.html index 3b5395f0b..a6b64be27 100644 --- a/src/main/resources/templates/users/userUpdateForm.html +++ b/src/main/resources/templates/users/userUpdateForm.html @@ -41,7 +41,7 @@ diff --git a/src/test/java/org/springframework/samples/petclinic/controller/common/UserControllerTest.java b/src/test/java/org/springframework/samples/petclinic/controller/common/UserControllerTest.java index 01a3393bc..bd819e92d 100644 --- a/src/test/java/org/springframework/samples/petclinic/controller/common/UserControllerTest.java +++ b/src/test/java/org/springframework/samples/petclinic/controller/common/UserControllerTest.java @@ -223,7 +223,7 @@ class UserControllerTest extends WebSocketSender { given(credentialService.save(any(CredentialDTO.class))).willReturn(credentialDTO); mockMvc.perform(post(CommonEndPoint.REGISTER).flashAttr(CommonAttribute.USER, userDTO)) - .andExpect(status().is3xxRedirection()).andExpect(view().name(CommonView.HOME + user.getId())); + .andExpect(status().is3xxRedirection()).andExpect(view().name(CommonView.HOME)); } @Test diff --git a/src/test/java/org/springframework/samples/petclinic/service/business/OwnerServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/business/OwnerServiceTest.java index 3568b1e91..aa7331a84 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/business/OwnerServiceTest.java +++ b/src/test/java/org/springframework/samples/petclinic/service/business/OwnerServiceTest.java @@ -78,8 +78,8 @@ class OwnerServiceTest { @BeforeEach void beforeEach() { - petService = new PetService(petRepository, petTypeRepository, visitRepository); - ownerService = new OwnerService(ownerRepository, petRepository); + petService = new PetService(petRepository, petTypeRepository); + ownerService = new OwnerService(ownerRepository); PetTypeService petTypeService = new PetTypeService(petTypeRepository); Collection petTypeDTOS = petService.findPetTypes(); PetTypeDTO petTypeDTO = petTypeDTOS.stream().findFirst().get(); diff --git a/src/test/java/org/springframework/samples/petclinic/service/business/PetServiceTest.java b/src/test/java/org/springframework/samples/petclinic/service/business/PetServiceTest.java index 6e60d5946..62a4444bc 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/business/PetServiceTest.java +++ b/src/test/java/org/springframework/samples/petclinic/service/business/PetServiceTest.java @@ -67,8 +67,8 @@ class PetServiceTest { @BeforeEach void beforeEach() { - this.petService = new PetService(petRepository, petTypeRepository, visitRepository); - this.ownerService = new OwnerService(ownerRepository, petRepository); + this.petService = new PetService(petRepository, petTypeRepository); + this.ownerService = new OwnerService(ownerRepository); PetTypeService petTypeService = new PetTypeService(petTypeRepository); Collection petTypeDTOS = petService.findPetTypes();