Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6097e4

Browse files
Resend verification mail
1 parent 18bf803 commit e6097e4

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

‎src/main/java/com/naturalprogrammer/spring5tutorial/NpSpring5TutorialApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.data.web.config.EnableSpringDataWebSupport;
56

67
@SpringBootApplication
8+
@EnableSpringDataWebSupport
79
public class NpSpring5TutorialApplication {
810

911
public static void main(String[] args) {

‎src/main/java/com/naturalprogrammer/spring5tutorial/controllers/UserController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.naturalprogrammer.spring5tutorial.controllers;
22

3+
import javax.mail.MessagingException;
4+
35
import org.springframework.stereotype.Controller;
46
import org.springframework.web.bind.annotation.GetMapping;
57
import org.springframework.web.bind.annotation.PathVariable;
68
import org.springframework.web.bind.annotation.RequestMapping;
79
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
810

11+
import com.naturalprogrammer.spring5tutorial.domain.User;
912
import com.naturalprogrammer.spring5tutorial.services.UserService;
1013
import com.naturalprogrammer.spring5tutorial.utils.MyUtils;
1114

@@ -27,4 +30,13 @@ public String verify(@PathVariable String verificationCode,
2730
MyUtils.flash(redirectAttributes, "success", "verificationSuccessful");
2831
return "redirect:/";
2932
}
33+
34+
@GetMapping("/{userId}/resend-verification-mail")
35+
public String resendVerificationMail(@PathVariable("userId") User user,
36+
RedirectAttributes redirectAttributes) throws MessagingException {
37+
38+
userService.resendVerificationMail(user);
39+
MyUtils.flash(redirectAttributes, "success", "verificationMailResent");
40+
return "redirect:/";
41+
}
3042
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.naturalprogrammer.spring5tutorial.services;
22

3+
import javax.mail.MessagingException;
4+
35
import org.springframework.boot.context.event.ApplicationReadyEvent;
46

57
import com.naturalprogrammer.spring5tutorial.commands.UserCommand;
8+
import com.naturalprogrammer.spring5tutorial.domain.User;
69

710
public interface UserService {
811

912
void signup(UserCommand userCommand);
1013
void afterApplicationReady(ApplicationReadyEvent event);
1114
void verify(String verificationCode);
15+
void resendVerificationMail(User user) throws MessagingException;
1216
}

‎src/main/java/com/naturalprogrammer/spring5tutorial/services/UserServiceImpl.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.naturalprogrammer.spring5tutorial.services;
22

3+
import java.util.Optional;
34
import java.util.UUID;
45

56
import javax.annotation.PostConstruct;
@@ -128,4 +129,33 @@ public void verify(String verificationCode) {
128129
userRepository.save(user);
129130
MyUtils.afterCommit(() -> MyUtils.login(user));
130131
}
132+
133+
@Override
134+
public void resendVerificationMail(User user) throws MessagingException {
135+
136+
MyUtils.validate(user != null, "userNotFound");
137+
MyUtils.validate(isAdminOrSelfLoggedIn(user), "notPermitted");
138+
MyUtils.validate(user.getRoles().contains(Role.UNVERIFIED),
139+
"alreadyVerified");
140+
141+
sendVerificationMail(user);
142+
}
143+
144+
private boolean isAdminOrSelfLoggedIn(User user) {
145+
146+
Optional<User> currentUser = MyUtils.currentUser();
147+
148+
if (!currentUser.isPresent())
149+
return false;
150+
151+
User cUser = currentUser.get();
152+
153+
if (cUser.getRoles().contains(Role.ADMIN))
154+
return true;
155+
156+
if (cUser.getId().equals(user.getId()))
157+
return true;
158+
159+
return false;
160+
}
131161
}

‎src/main/resources/messages.properties

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ verifySubject: Please verify your email
77
verifyBody: Hi,<br/><br/>Your email at XYZ is unverified. Please click on\
88
the following link to get verified:<br/><br/>{0}
99

10-
pleaseVerify: Hey, please verify yourself!
10+
pleaseVerify: Hey, please verify yourself! \
11+
<a href="/users/{0}/resend-verification-mail">Click here</a> to get the verification mail again.
1112

1213
verificationSuccessful: Verification succeeded!
1314
alreadyVerified: You are already verified!
14-
wrongVerificationCode: The verification code doesn't match
15+
wrongVerificationCode: The verification code doesn't match
16+
17+
verificationMailResent: Verification mail resent!
18+
userNotFound: User not found
19+
notPermitted: Not permitted

‎src/main/webapp/WEB-INF/jsp/includes/header.jsp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
<sec:authorize access="hasRole('UNVERIFIED')">
108108
<div class="alert alert-warning alert-dismissible" role="alert">
109109
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
110-
<spring:message code="pleaseVerify" />
110+
<sec:authentication property="principal.id" var="currentUserId" />
111+
<spring:message code="pleaseVerify" arguments="${currentUserId}"/>
111112
</div>
112113
</sec:authorize>
113114

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /