Skip to content
Draft
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 @@ -7,6 +7,8 @@
import com.jamil.ahadith.core.security.RefreshTokenRevoker;
import com.jamil.ahadith.core.validation.EmailNormalizer;
import com.jamil.ahadith.core.web.dto.MessageResponseDto;
import com.jamil.ahadith.features.audit.service.AuditData;
import com.jamil.ahadith.features.audit.service.AuditEventPublisher;
import com.jamil.ahadith.features.account.dto.request.ResetPasswordRequestDto;
import com.jamil.ahadith.features.account.entity.PasswordResetToken;
import com.jamil.ahadith.features.account.event.AccountEmailEvent;
Expand All @@ -31,6 +33,7 @@ public class PasswordResetService {
private static final String INVALID_PASSWORD_RESET_TOKEN_MESSAGE = "Invalid or expired password reset token";
private static final String FORGOT_PASSWORD_MESSAGE =
"If the email is registered, password reset instructions have been sent";
private static final String USERS_TABLE_NAME = "users";

private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
Expand All @@ -43,6 +46,7 @@ public class PasswordResetService {
private final RateLimitKeyResolver rateLimitKeyResolver;
private final OneTimeTokenValidator oneTimeTokenValidator;
private final ApplicationEventPublisher eventPublisher;
private final AuditEventPublisher auditEventPublisher;

@Transactional
public MessageResponseDto forgotPassword(String email) {
Expand Down Expand Up @@ -86,6 +90,7 @@ public MessageResponseDto resetPassword(ResetPasswordRequestDto request) {

User user = resetToken.getUser();
requireActive(user);
var oldData = AuditData.snapshot(user);

String encodedPassword =
passwordEncoder.encode(request.getNewPassword());
Expand All @@ -99,6 +104,15 @@ public MessageResponseDto resetPassword(ResetPasswordRequestDto request) {

passwordResetTokenRepository.consumeActiveForUser(user, now);

auditEventPublisher.publishUpdateAs(
user,
USERS_TABLE_NAME,
user.getId(),
oldData,
AuditData.snapshot(user),
"password reset"
);

return new MessageResponseDto(
"Password has been reset"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.jamil.ahadith.core.security.RefreshTokenRevoker;
import com.jamil.ahadith.core.storage.dto.ProfileImageResponse;
import com.jamil.ahadith.core.web.dto.MessageResponseDto;
import com.jamil.ahadith.features.audit.service.AuditData;
import com.jamil.ahadith.features.audit.service.AuditEventPublisher;
import com.jamil.ahadith.features.account.service.PasswordPolicyService;
import com.jamil.ahadith.features.auth.dto.response.AuthUserDto;
import com.jamil.ahadith.features.auth.mapper.AuthUserMapper;
Expand All @@ -24,13 +26,15 @@
@Service
@RequiredArgsConstructor
public class UserProfileTransactionService {
private static final String USERS_TABLE_NAME = "users";

private final UserRepository userRepository;
private final ApplicationEventPublisher eventPublisher;
private final AuthUserMapper authUserMapper;
private final PasswordEncoder passwordEncoder;
private final PasswordPolicyService passwordPolicyService;
private final RefreshTokenRevoker refreshTokenRevoker;
private final AuditEventPublisher auditEventPublisher;

@Transactional
public void replaceProfileImage(
Expand Down Expand Up @@ -86,11 +90,21 @@ public AuthUserDto updateProfile(
User user = userRepository
.findByIdForUpdate(userId)
.orElseThrow(UserNotFoundException::new);
var oldData = AuditData.snapshot(user);

user.setName(request.getName().trim());
user.setGender(request.getGender());
user.setBirthDate(request.getBirthDate());

auditEventPublisher.publishUpdateAs(
user,
USERS_TABLE_NAME,
user.getId(),
oldData,
AuditData.snapshot(user),
"profile updated"
);

return authUserMapper.toDto(user);
}

Expand All @@ -106,6 +120,7 @@ public MessageResponseDto changePassword(
User user = userRepository
.findByIdForUpdate(userId)
.orElseThrow(UserNotFoundException::new);
var oldData = AuditData.snapshot(user);

if (user.getPassword() == null
|| !passwordEncoder.matches(
Expand Down Expand Up @@ -140,6 +155,15 @@ public MessageResponseDto changePassword(

refreshTokenRevoker.revokeAllForUser(user);

auditEventPublisher.publishUpdateAs(
user,
USERS_TABLE_NAME,
user.getId(),
oldData,
AuditData.snapshot(user),
"password changed"
);

return new MessageResponseDto(
"Password changed successfully"
);
Expand Down