Changeset b7ec74e
- Timestamp:
- 07/27/22 18:26:49 (2 years ago)
- Branches:
- main
- Children:
- 0e62f4d
- Parents:
- 6c1d611
- Location:
- springapp/src/main/java/mk/profesori/springapp
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
springapp/src/main/java/mk/profesori/springapp/Model/ConfirmationToken.java
r6c1d611 rb7ec74e 38 38 private LocalDateTime confirmedAt; 39 39 40 @ManyToOne 40 @ManyToOne() 41 41 @JoinColumn(nullable = false, name = "custom_user_details_id") 42 42 private CustomUserDetails customUserDetails; -
springapp/src/main/java/mk/profesori/springapp/Model/CustomUserDetails.java
r6c1d611 rb7ec74e 3 3 import java.util.Collection; 4 4 import java.util.Collections; 5 import java.util.HashSet; 6 import java.util.Set; 5 7 8 import javax.persistence.CascadeType; 6 9 import javax.persistence.Entity; 7 10 import javax.persistence.EnumType; … … 10 13 import javax.persistence.GenerationType; 11 14 import javax.persistence.Id; 15 import javax.persistence.OneToMany; 12 16 import javax.persistence.SequenceGenerator; 13 17 … … 40 44 private Boolean locked = false; 41 45 private Boolean enabled = false; 46 @OneToMany(mappedBy = "customUserDetails", cascade = CascadeType.ALL) 47 private Set<ConfirmationToken> confirmationTokens = new HashSet<>(); 42 48 43 49 public CustomUserDetails(String fullName, String username, String email, String password, UserRole userRole) { -
springapp/src/main/java/mk/profesori/springapp/Service/CustomUserDetailsService.java
r6c1d611 rb7ec74e 38 38 39 39 public String signUp(CustomUserDetails customUserDetails) { 40 41 boolean emailExists = userRepository.findByEmail(customUserDetails.getEmail()).isPresent();42 if(emailExists) {43 throw new IllegalStateException("Email is taken");44 }45 46 boolean usernameExists = userRepository.findByUsername(customUserDetails.getUsername()).isPresent();47 if(usernameExists) {48 throw new IllegalStateException("Username is taken");49 }50 51 40 String encodedPassword = passwordEncoder().encode(customUserDetails.getPassword()); 52 41 customUserDetails.setPassword(encodedPassword); … … 54 43 userRepository.save(customUserDetails); 55 44 45 String token = createToken(customUserDetails); 46 47 return token; 48 } 49 50 public String createToken(CustomUserDetails customUserDetails) { 56 51 String token = UUID.randomUUID().toString(); 57 52 ConfirmationToken confirmationToken = new ConfirmationToken( -
springapp/src/main/java/mk/profesori/springapp/Service/EmailSender.java
r6c1d611 rb7ec74e 3 3 public interface EmailSender { 4 4 void send(String to, String email); 5 String buildEmail(String username, String link); 5 6 } -
springapp/src/main/java/mk/profesori/springapp/Service/EmailService.java
r6c1d611 rb7ec74e 37 37 } 38 38 39 @Override 40 public String buildEmail(String username, String link) { 41 return "<div style=\"font-family:Helvetica,Arial,sans-serif;font-size:16px;margin:0;color:#0b0c0c\">\n" + 42 "\n" + 43 "<span style=\"display:none;font-size:1px;color:#fff;max-height:0\"></span>\n" + 44 "\n" + 45 " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;min-width:100%;width:100%!important\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" + 46 " <tbody><tr>\n" + 47 " <td width=\"100%\" height=\"53\" bgcolor=\"#0b0c0c\">\n" + 48 " \n" + 49 " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;max-width:580px\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\">\n" + 50 " <tbody><tr>\n" + 51 " <td width=\"70\" bgcolor=\"#0b0c0c\" valign=\"middle\">\n" + 52 " <table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" + 53 " <tbody><tr>\n" + 54 " <td style=\"padding-left:10px\">\n" + 55 " \n" + 56 " </td>\n" + 57 " <td style=\"font-size:28px;line-height:1.315789474;Margin-top:4px;padding-left:10px\">\n" + 58 " <span style=\"font-family:Helvetica,Arial,sans-serif;font-weight:700;color:#ffffff;text-decoration:none;vertical-align:top;display:inline-block\">Активирај ја својата корисничка сметка</span>\n" + 59 " </td>\n" + 60 " </tr>\n" + 61 " </tbody></table>\n" + 62 " </a>\n" + 63 " </td>\n" + 64 " </tr>\n" + 65 " </tbody></table>\n" + 66 " \n" + 67 " </td>\n" + 68 " </tr>\n" + 69 " </tbody></table>\n" + 70 " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" + 71 " <tbody><tr>\n" + 72 " <td width=\"10\" height=\"10\" valign=\"middle\"></td>\n" + 73 " <td>\n" + 74 " \n" + 75 " <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" + 76 " <tbody><tr>\n" + 77 " <td bgcolor=\"#1D70B8\" width=\"100%\" height=\"10\"></td>\n" + 78 " </tr>\n" + 79 " </tbody></table>\n" + 80 " \n" + 81 " </td>\n" + 82 " <td width=\"10\" valign=\"middle\" height=\"10\"></td>\n" + 83 " </tr>\n" + 84 " </tbody></table>\n" + 85 "\n" + 86 "\n" + 87 "\n" + 88 " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" + 89 " <tbody><tr>\n" + 90 " <td height=\"30\"><br></td>\n" + 91 " </tr>\n" + 92 " <tr>\n" + 93 " <td width=\"10\" valign=\"middle\"><br></td>\n" + 94 " <td style=\"font-family:Helvetica,Arial,sans-serif;font-size:19px;line-height:1.315789474;max-width:560px\">\n" + 95 " \n" + 96 " <p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\">Здраво " + username + ",</p><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> Добредојде на profesori.mk!. Да ја активираш својата корисничка сметка, кликни на линкот долу: </p><blockquote style=\"Margin:0 0 20px 0;border-left:10px solid #b1b4b6;padding:15px 0 0.1px 15px;font-size:19px;line-height:25px\"><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> <a href=\"" + link + "\">Активирај</a> </p></blockquote>\n Линкот истекува за 10 минути. <p>Убав престој!</p>" + 97 " \n" + 98 " </td>\n" + 99 " <td width=\"10\" valign=\"middle\"><br></td>\n" + 100 " </tr>\n" + 101 " <tr>\n" + 102 " <td height=\"30\"><br></td>\n" + 103 " </tr>\n" + 104 " </tbody></table><div class=\"yj6qo\"></div><div class=\"adL\">\n" + 105 "\n" + 106 "</div></div>"; 107 } 108 39 109 } -
springapp/src/main/java/mk/profesori/springapp/Service/RegistrationService.java
r6c1d611 rb7ec74e 12 12 import mk.profesori.springapp.Model.RegistrationRequest; 13 13 import mk.profesori.springapp.Model.UserRole; 14 import mk.profesori.springapp.Repository.UserRepository; 14 15 15 16 @Service … … 23 24 private final UsernameValidator usernameValidator; 24 25 private final EmailSender emailSender; 26 private final UserRepository userRepository; 25 27 26 28 public String register(RegistrationRequest request) { … … 34 36 boolean isValidUsername = usernameValidator.test(request.getUsername()); 35 37 if(!isValidUsername) throw new IllegalStateException("Invalid username"); 38 39 boolean emailExists = userRepository.findByEmail(request.getEmail()).isPresent(); 40 if(emailExists) { 41 if(!userRepository.findByEmail(request.getEmail()).get().isEnabled()) { 42 String tokenToResend = customUserDetailsService.createToken(userRepository.findByEmail(request.getEmail()).get()); 43 String link = "http://192.168.0.17:8080/registration/confirm?token=" + tokenToResend; 44 emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link)); 45 return tokenToResend; 46 } else { 47 throw new IllegalStateException("Email is taken"); 48 } 49 } 50 51 boolean usernameExists = userRepository.findByUsername(request.getUsername()).isPresent(); 52 if(usernameExists) { 53 throw new IllegalStateException("Username is taken"); 54 } 36 55 37 56 String token = customUserDetailsService.signUp( … … 47 66 String link = "http://192.168.0.17:8080/registration/confirm?token=" + token; 48 67 49 emailSender.send(request.getEmail(), buildEmail(request.getUsername(), link));68 emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link)); 50 69 51 70 return token; … … 74 93 return "Confirmed"; 75 94 } 76 77 private String buildEmail(String username, String link) {78 return "<div style=\"font-family:Helvetica,Arial,sans-serif;font-size:16px;margin:0;color:#0b0c0c\">\n" +79 "\n" +80 "<span style=\"display:none;font-size:1px;color:#fff;max-height:0\"></span>\n" +81 "\n" +82 " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;min-width:100%;width:100%!important\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" +83 " <tbody><tr>\n" +84 " <td width=\"100%\" height=\"53\" bgcolor=\"#0b0c0c\">\n" +85 " \n" +86 " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;max-width:580px\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\">\n" +87 " <tbody><tr>\n" +88 " <td width=\"70\" bgcolor=\"#0b0c0c\" valign=\"middle\">\n" +89 " <table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +90 " <tbody><tr>\n" +91 " <td style=\"padding-left:10px\">\n" +92 " \n" +93 " </td>\n" +94 " <td style=\"font-size:28px;line-height:1.315789474;Margin-top:4px;padding-left:10px\">\n" +95 " <span style=\"font-family:Helvetica,Arial,sans-serif;font-weight:700;color:#ffffff;text-decoration:none;vertical-align:top;display:inline-block\">Активирај ја својата корисничка сметка</span>\n" +96 " </td>\n" +97 " </tr>\n" +98 " </tbody></table>\n" +99 " </a>\n" +100 " </td>\n" +101 " </tr>\n" +102 " </tbody></table>\n" +103 " \n" +104 " </td>\n" +105 " </tr>\n" +106 " </tbody></table>\n" +107 " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" +108 " <tbody><tr>\n" +109 " <td width=\"10\" height=\"10\" valign=\"middle\"></td>\n" +110 " <td>\n" +111 " \n" +112 " <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +113 " <tbody><tr>\n" +114 " <td bgcolor=\"#1D70B8\" width=\"100%\" height=\"10\"></td>\n" +115 " </tr>\n" +116 " </tbody></table>\n" +117 " \n" +118 " </td>\n" +119 " <td width=\"10\" valign=\"middle\" height=\"10\"></td>\n" +120 " </tr>\n" +121 " </tbody></table>\n" +122 "\n" +123 "\n" +124 "\n" +125 " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" +126 " <tbody><tr>\n" +127 " <td height=\"30\"><br></td>\n" +128 " </tr>\n" +129 " <tr>\n" +130 " <td width=\"10\" valign=\"middle\"><br></td>\n" +131 " <td style=\"font-family:Helvetica,Arial,sans-serif;font-size:19px;line-height:1.315789474;max-width:560px\">\n" +132 " \n" +133 " <p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\">Здраво " + username + ",</p><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> Добредојде на profesori.mk!. Да ја активираш својата корисничка сметка, кликни на линкот долу: </p><blockquote style=\"Margin:0 0 20px 0;border-left:10px solid #b1b4b6;padding:15px 0 0.1px 15px;font-size:19px;line-height:25px\"><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> <a href=\"" + link + "\">Активирај</a> </p></blockquote>\n Линкот истекува за 10 минути. <p>Убав престој!</p>" +134 " \n" +135 " </td>\n" +136 " <td width=\"10\" valign=\"middle\"><br></td>\n" +137 " </tr>\n" +138 " <tr>\n" +139 " <td height=\"30\"><br></td>\n" +140 " </tr>\n" +141 " </tbody></table><div class=\"yj6qo\"></div><div class=\"adL\">\n" +142 "\n" +143 "</div></div>";144 }145 95 }
Note:
See TracChangeset
for help on using the changeset viewer.