-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/controller/user-controller' into dev
- Loading branch information
Showing
14 changed files
with
301 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/org/teapot/backend/model/VerificationToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.teapot.backend.model; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Table(name = "verification_token") | ||
public class VerificationToken { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(nullable = false, unique = true, length = 32, updatable = false) | ||
private String token; | ||
|
||
@Column(nullable = false) | ||
private LocalDateTime expireDateTime = LocalDateTime.now().plusDays(1); | ||
|
||
@Column(name = "is_activated") | ||
private Boolean isActivated = false; | ||
|
||
@OneToOne(fetch = FetchType.LAZY) | ||
private User user; | ||
|
||
public VerificationToken() { | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public LocalDateTime getExpireDateTime() { | ||
return expireDateTime; | ||
} | ||
|
||
public Boolean isActivated() { | ||
return isActivated; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
public void setExpireDateTime(LocalDateTime expireDateTime) { | ||
this.expireDateTime = expireDateTime; | ||
} | ||
|
||
public void setActivated(Boolean activated) { | ||
isActivated = activated; | ||
} | ||
|
||
public void setUser(User user) { | ||
this.user = user; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/teapot/backend/repository/VerificationTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.teapot.backend.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.teapot.backend.model.VerificationToken; | ||
|
||
@Transactional | ||
public interface VerificationTokenRepository extends JpaRepository<VerificationToken, Long> { | ||
|
||
VerificationToken findByToken(String token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/org/teapot/backend/util/RandomSequenceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.teapot.backend.util; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.security.SecureRandom; | ||
|
||
@Component | ||
public final class RandomSequenceGenerator { | ||
|
||
public static final String CHARACTER_STRING = "0123456789abcdefghijklmnopqrstuvwxyz"; | ||
public static final int MIN_LENGTH = 1; | ||
public static final int MAX_LENGTH = 256; | ||
public static final int MIN_NOTATION = 2; | ||
public static final int MAX_NOTATION = CHARACTER_STRING.length(); | ||
|
||
public String generateSequence(int length, int notation, boolean caseSensitive) { | ||
if (length > MAX_LENGTH) { | ||
length = MAX_LENGTH; | ||
} else if (length < MIN_LENGTH) { | ||
length = MIN_LENGTH; | ||
} | ||
|
||
if (notation > MAX_NOTATION) { | ||
notation = MAX_NOTATION; | ||
} else if (notation < MIN_NOTATION) { | ||
notation = MIN_NOTATION; | ||
} | ||
|
||
SecureRandom random = new SecureRandom(); | ||
StringBuilder stringBuilder = new StringBuilder(length); | ||
|
||
for (int i = 0; i < length; i++) { | ||
String c = String.valueOf(CHARACTER_STRING.charAt(random.nextInt(notation))); | ||
|
||
if (caseSensitive) { | ||
c = random.nextBoolean() ? c.toUpperCase() : c; | ||
} | ||
|
||
stringBuilder.append(c); | ||
} | ||
|
||
return stringBuilder.toString(); | ||
} | ||
|
||
public String generateSequence(int length) { | ||
return generateSequence(length, MAX_NOTATION, true); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/org/teapot/backend/util/VerificationMailSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.teapot.backend.util; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Component; | ||
import org.teapot.backend.model.User; | ||
import org.teapot.backend.model.VerificationToken; | ||
import org.teapot.backend.repository.TeapotPropertyRepository; | ||
import org.teapot.backend.repository.VerificationTokenRepository; | ||
|
||
import java.util.Locale; | ||
|
||
|
||
@Component | ||
public class VerificationMailSender { | ||
|
||
@Autowired | ||
private TeapotPropertyRepository propertyRepository; | ||
|
||
@Autowired | ||
private VerificationTokenRepository tokenRepository; | ||
|
||
@Autowired | ||
private VerificationTokenGenerator generator; | ||
|
||
@Autowired | ||
private MessageSource messages; | ||
|
||
@Autowired | ||
private JavaMailSender mailSender; | ||
|
||
public void createTokenAndSend(User user, Locale locale) { | ||
new Thread(() -> { | ||
VerificationToken verificationToken = generator.generateToken(); | ||
verificationToken.setUser(user); | ||
tokenRepository.save(verificationToken); | ||
|
||
String confirmUrl = "https://" | ||
+ propertyRepository.findByName("site-uri").getValue() | ||
+ "/confirmRegistration?token=" | ||
+ verificationToken.getToken(); | ||
|
||
SimpleMailMessage mailMessage = new SimpleMailMessage(); | ||
mailMessage.setTo(user.getEmail()); | ||
mailMessage.setSubject(messages.getMessage( | ||
"mail.confirm.subject", null, locale)); | ||
mailMessage.setText(messages.getMessage( | ||
"mail.confirm.text", new String[]{confirmUrl}, locale)); | ||
mailSender.send(mailMessage); | ||
}).start(); | ||
} | ||
} |
Oops, something went wrong.