-
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.
only allow verified users to use the anime entry routes Signed-off-by: Sayan Biswas <me@sayanbiswas.in>
- Loading branch information
Showing
11 changed files
with
232 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.barta.myanimelounge; | ||
|
||
import com.barta.myanimelounge.interceptors.IsVerifiedInterceptor; | ||
import com.barta.myanimelounge.repository.UserRepository; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@EnableWebMvc | ||
public class AppConfig implements WebMvcConfigurer { | ||
private final UserRepository userRepository; | ||
|
||
public AppConfig(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(new IsVerifiedInterceptor(userRepository)) | ||
.excludePathPatterns("/auth/**", "/api/test/**"); | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/barta/myanimelounge/interceptors/IsVerifiedInterceptor.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,41 @@ | ||
package com.barta.myanimelounge.interceptors; | ||
|
||
import com.barta.myanimelounge.exceptions.EntryNotFoundException; | ||
import com.barta.myanimelounge.models.User; | ||
import com.barta.myanimelounge.repository.UserRepository; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@Component | ||
public class IsVerifiedInterceptor implements HandlerInterceptor { | ||
private final UserRepository userRepository; | ||
|
||
public IsVerifiedInterceptor(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (authentication != null) { | ||
User user = userRepository.findByUsername(authentication.getName()) | ||
.orElseThrow(EntryNotFoundException::new); | ||
System.out.println(user); | ||
if (!user.isEmailVerified()) { | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.getWriter() | ||
.write("{\"message\": \"Email verification is required to call this controller\"}"); | ||
response.setStatus(HttpStatus.FORBIDDEN.value()); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/com/barta/myanimelounge/models/ConfirmationToken.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,78 @@ | ||
package com.barta.myanimelounge.models; | ||
|
||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.OneToOne; | ||
import javax.persistence.Table; | ||
import javax.persistence.Temporal; | ||
import javax.persistence.TemporalType; | ||
|
||
@Entity | ||
@Table(name="confirmationToken") | ||
public class ConfirmationToken { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
@Column(name="token_id") | ||
private Long tokenId; | ||
|
||
@Column(name="confirmation_token") | ||
private String confirmationToken; | ||
|
||
@Temporal(TemporalType.TIMESTAMP) | ||
private Date createdDate; | ||
|
||
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER) | ||
@JoinColumn(nullable = false, name = "user_id") | ||
private User user; | ||
|
||
public ConfirmationToken() {} | ||
|
||
public ConfirmationToken(User user) { | ||
this.user = user; | ||
createdDate = new Date(); | ||
confirmationToken = UUID.randomUUID().toString(); | ||
} | ||
|
||
public Long getTokenId() { | ||
return tokenId; | ||
} | ||
|
||
public void setTokenId(Long tokenId) { | ||
this.tokenId = tokenId; | ||
} | ||
|
||
public String getConfirmationToken() { | ||
return confirmationToken; | ||
} | ||
|
||
public void setConfirmationToken(String confirmationToken) { | ||
this.confirmationToken = confirmationToken; | ||
} | ||
|
||
public Date getCreatedDate() { | ||
return createdDate; | ||
} | ||
|
||
public void setCreatedDate(Date createdDate) { | ||
this.createdDate = createdDate; | ||
} | ||
|
||
public User getUserEntity() { | ||
return user; | ||
} | ||
|
||
public void setUserEntity(User user) { | ||
this.user = user; | ||
} | ||
|
||
|
||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/barta/myanimelounge/repository/ConfirmationTokenRepository.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,10 @@ | ||
package com.barta.myanimelounge.repository; | ||
|
||
import com.barta.myanimelounge.models.ConfirmationToken; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository("confirmationTokenRepository") | ||
public interface ConfirmationTokenRepository extends JpaRepository<ConfirmationToken, Long> { | ||
ConfirmationToken findByConfirmationToken(String confirmationToken); | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/barta/myanimelounge/services/EmailService.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,24 @@ | ||
package com.barta.myanimelounge.services; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service("emailService") | ||
public class EmailService { | ||
|
||
private final JavaMailSender javaMailSender; | ||
|
||
@Autowired | ||
public EmailService(JavaMailSender javaMailSender) { | ||
this.javaMailSender = javaMailSender; | ||
} | ||
|
||
@Async | ||
public void sendEmail(SimpleMailMessage email) { | ||
javaMailSender.send(email); | ||
} | ||
|
||
} |