Skip to content

Commit 07af9a9

Browse files
committed
modify need captcha
1 parent fee0f90 commit 07af9a9

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class User {
2424

2525
private Date tokenExpiration;
2626

27+
private boolean needCaptcha;
28+
2729
@OneToMany(mappedBy = "user")
2830
private List<Post> posts;
2931

@@ -71,6 +73,14 @@ public void setTokenExpiration(Date tokenExpiration) {
7173
this.tokenExpiration = tokenExpiration;
7274
}
7375

76+
public boolean isCaptchaNeeded() {
77+
return needCaptcha;
78+
}
79+
80+
public void setNeedCaptcha(boolean needCaptcha) {
81+
this.needCaptcha = needCaptcha;
82+
}
83+
7484
public List<Post> getPosts() {
7585
return posts;
7686
}

spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public final String submit(final Model model, @RequestParam final Map<String, St
7171

7272
@RequestMapping("/post")
7373
public final String showSubmissionForm(final Model model) {
74-
final String needsCaptchaResult = needsCaptcha();
75-
if (needsCaptchaResult.equalsIgnoreCase("true")) {
74+
final boolean isCaptchaNeeded = getCurrentUser().isCaptchaNeeded();
75+
if (isCaptchaNeeded) {
7676
final String iden = getNewCaptcha();
7777
model.addAttribute("iden", iden);
7878
}
@@ -81,8 +81,8 @@ public final String showSubmissionForm(final Model model) {
8181

8282
@RequestMapping("/postSchedule")
8383
public final String showSchedulePostForm(final Model model) {
84-
final String needsCaptchaResult = needsCaptcha();
85-
if (needsCaptchaResult.equalsIgnoreCase("true")) {
84+
final boolean isCaptchaNeeded = getCurrentUser().isCaptchaNeeded();
85+
if (isCaptchaNeeded) {
8686
model.addAttribute("msg", "Sorry, You do not have enought karma");
8787
return "submissionResponse";
8888
}
@@ -92,7 +92,7 @@ public final String showSchedulePostForm(final Model model) {
9292
@RequestMapping(value = "/schedule", method = RequestMethod.POST)
9393
public final String schedule(final Model model, @RequestParam final Map<String, String> formParams) throws ParseException {
9494
logger.info("User scheduling Post with these parameters: " + formParams.entrySet());
95-
final User user = userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue());
95+
final User user = getCurrentUser();
9696
final Post post = new Post();
9797
post.setUser(user);
9898
post.setSent(false);
@@ -116,7 +116,7 @@ public final String schedule(final Model model, @RequestParam final Map<String,
116116

117117
@RequestMapping("/posts")
118118
public final String getScheduledPosts(final Model model) {
119-
final User user = userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue());
119+
final User user = getCurrentUser();
120120
final List<Post> posts = postReopsitory.findByUser(user);
121121
model.addAttribute("posts", posts);
122122
return "postListView";
@@ -160,6 +160,10 @@ public String updatePost(Model model, @PathVariable("id") final Long id, @Reques
160160

161161
// === private
162162

163+
private User getCurrentUser() {
164+
return userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue());
165+
}
166+
163167
private final MultiValueMap<String, String> constructParams(final Map<String, String> formParams) {
164168
final MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
165169
param.add(RedditApiConstants.API_TYPE, "json");
@@ -211,9 +215,15 @@ private final void addUser(final String name, final OAuth2AccessToken token) {
211215
user.setAccessToken(token.getValue());
212216
user.setRefreshToken(token.getRefreshToken().getValue());
213217
user.setTokenExpiration(token.getExpiration());
214-
userReopsitory.save(user);
215218
}
216219

220+
final String needsCaptchaResult = needsCaptcha();
221+
if (needsCaptchaResult.equalsIgnoreCase("true")) {
222+
user.setNeedCaptcha(true);
223+
} else {
224+
user.setNeedCaptcha(false);
225+
}
226+
userReopsitory.save(user);
217227
}
218228

219229
}

0 commit comments

Comments
 (0)