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

Chat 209 bug fix status 200 returned when attempting to change password without special symbol #59

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand Down Expand Up @@ -43,9 +44,8 @@ public class ChangePasswordController {
content = @Content(schema = @Schema(implementation = ChangePasswordDto.class),
examples = @ExampleObject(value = OpenApiExamples.CHANGE_PASSWORD,
description = "Old and new passwords"))))
@PatchMapping(path = "/password",
consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public void changePassword(@RequestBody ChangePasswordDto request,
@PatchMapping(path = "/password", consumes = APPLICATION_JSON_VALUE)
public void changePassword(@Valid @RequestBody ChangePasswordDto request,
@AuthenticationPrincipal UserDetails userDetails) {
changePasswordService.changePassword(request, userDetails);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.chat.yourway.dto.request;

import com.chat.yourway.annotation.PasswordValidation;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ChangePasswordDto {
@PasswordValidation
private String oldPassword;

@PasswordValidation
private String newPassword;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void changePassword(ChangePasswordDto request, UserDetails userDetails) {
}

@Override
@Transactional
public void sendEmailToRestorePassword(String email, String clientHost) {

var contact = contactService.findByEmail(email);
Expand Down
74 changes: 51 additions & 23 deletions src/main/java/com/chat/yourway/service/ContactServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import com.chat.yourway.dto.request.ContactRequestDto;
import com.chat.yourway.dto.request.EditContactProfileRequestDto;
import com.chat.yourway.dto.response.ContactProfileResponseDto;
import com.chat.yourway.exception.*;
import com.chat.yourway.exception.ContactNotFoundException;
import com.chat.yourway.exception.PasswordsAreNotEqualException;
import com.chat.yourway.exception.ValueNotUniqException;
import com.chat.yourway.model.Contact;
import com.chat.yourway.model.Role;
import com.chat.yourway.repository.ContactRepository;
import com.chat.yourway.service.interfaces.ContactService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand All @@ -27,13 +30,15 @@ public class ContactServiceImpl implements ContactService {
@Transactional
@Override
public Contact create(ContactRequestDto contactRequestDto) {
log.trace("Started create contact, contact email: {}", contactRequestDto.getEmail());
log.trace("Started create contact, contact email: [{}]", contactRequestDto.getEmail());

if (isEmailExists(contactRequestDto.getEmail())) {
log.warn("Email [{}] already in use", contactRequestDto.getEmail());
throw new ValueNotUniqException(
String.format("Email %s already in use", contactRequestDto.getEmail()));
String.format("Email [%s] already in use", contactRequestDto.getEmail()));
}

return contactRepository.save(
Contact contact = contactRepository.save(
Contact.builder()
.nickname(contactRequestDto.getNickname())
.avatarId(contactRequestDto.getAvatarId())
Expand All @@ -43,86 +48,109 @@ public Contact create(ContactRequestDto contactRequestDto) {
.isPrivate(true)
.role(Role.USER)
.build());

log.info("New contact with email [{}] was created", contactRequestDto.getEmail());
return contact;
}

@Override
@Cacheable("contacts")
@Transactional(readOnly = true)
@Cacheable(value = "contacts", key = "#email")
public Contact findByEmail(String email) {
log.trace("Started findByEmail: {}", email);
return contactRepository
log.trace("Started findByEmail: [{}]", email);
Contact contact = contactRepository
.findByEmailIgnoreCase(email)
.orElseThrow(
() -> new ContactNotFoundException(String.format("Email %s wasn't found", email)));
.orElseThrow(() -> {
log.warn("Email [{}] wasn't found", email);
return new ContactNotFoundException(String.format("Email [%s] wasn't found", email));
});

log.info("Contact was found by email [{}]", email);
return contact;
}

@Override
@Transactional
@CacheEvict(value = "contacts", key = "#email")
public void changePasswordByEmail(String password, String email) {
log.trace("Started change password by email [{}]", email);
contactRepository.changePasswordByEmail(passwordEncoder.encode(password), email);
log.info("Password was changed by email [{}]", email);
}

@Override
public void verifyPassword(String password, String encodedPassword) {
log.trace("Started verify password");

if (!passwordEncoder.matches(password, encodedPassword)) {
log.warn("Password was not verify");
throw new PasswordsAreNotEqualException();
}
log.info("Password was verified");
}

@Transactional
@Override
@Transactional
@CacheEvict(value = "contacts", key = "#userDetails.getUsername()")
public void updateContactProfile(
EditContactProfileRequestDto editContactProfileRequestDto, UserDetails userDetails) {
log.trace("Started updating contact profile: {}", editContactProfileRequestDto);
log.trace("Started updating contact profile: [{}]", editContactProfileRequestDto);

String email = userDetails.getUsername();
Contact contact =
contactRepository
.findByEmailIgnoreCase(email)
.orElseThrow(
() -> new ContactNotFoundException(String.format("Email %s wasn't found", email)));

Contact contact = findByEmail(email);

contact.setNickname(editContactProfileRequestDto.getNickname());
contact.setAvatarId(editContactProfileRequestDto.getAvatarId());

contact = contactRepository.save(contact);
log.trace("Updated contact: {}", contact);
contactRepository.save(contact);

log.info("Updated contact by email [{}]", email);
}

@Override
public boolean isEmailExists(String email) {
log.trace("Started check is email exists in repository");
return contactRepository.existsByEmailIgnoreCase(email);
}

@Override
public ContactProfileResponseDto getContactProfile(UserDetails userDetails) {
String email = userDetails.getUsername();
Contact contact =
contactRepository
.findByEmailIgnoreCase(email)
.orElseThrow(
() -> new ContactNotFoundException(String.format("Email %s wasn't found", email)));
log.trace("Started get contact profile by email [{}]", email);

Contact contact = findByEmail(email);
ContactProfileResponseDto responseDto = new ContactProfileResponseDto();

responseDto.setNickname(contact.getNickname());
responseDto.setAvatarId(contact.getAvatarId());
responseDto.setEmail(email);
responseDto.setHasPermissionSendingPrivateMessage(contact.isPermittedSendingPrivateMessage());

log.info("Contact profile was got by email [{}]", email);
return responseDto;
}

@Override
@Transactional
@CacheEvict(value = "contacts", key = "#userDetails.getUsername()")
public void permitSendingPrivateMessages(UserDetails userDetails) {
log.trace("Started permit sending private messages by email [{}]", userDetails.getUsername());
boolean isPermittedSendingPrivateMessage = true;

changePermissionSendingPrivateMessages(userDetails, isPermittedSendingPrivateMessage);
log.info("Permitted sending private messages by email [{}]", userDetails.getUsername());
}

@Override
@Transactional
@CacheEvict(value = "contacts", key = "#userDetails.getUsername()")
public void prohibitSendingPrivateMessages(UserDetails userDetails) {
log.trace("Started prohibit sending private messages by email [{}]", userDetails.getUsername());
boolean isPermittedSendingPrivateMessage = false;

changePermissionSendingPrivateMessages(userDetails, isPermittedSendingPrivateMessage);
log.info("Prohibited sending private messages by email [{}]", userDetails.getUsername());
}

private void changePermissionSendingPrivateMessages(
Expand Down
Loading