diff --git a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java index 43bc410c70..bcb9971fa5 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/User.java @@ -24,6 +24,8 @@ public class User { private Date tokenExpiration; + private boolean needCaptcha; + @OneToMany(mappedBy = "user") private List posts; @@ -71,6 +73,14 @@ public class User { this.tokenExpiration = tokenExpiration; } + public boolean isCaptchaNeeded() { + return needCaptcha; + } + + public void setNeedCaptcha(boolean needCaptcha) { + this.needCaptcha = needCaptcha; + } + public List getPosts() { return posts; } diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index ee53d8257a..51ca005752 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -71,8 +71,8 @@ public class RedditController { @RequestMapping("/post") public final String showSubmissionForm(final Model model) { - final String needsCaptchaResult = needsCaptcha(); - if (needsCaptchaResult.equalsIgnoreCase("true")) { + final boolean isCaptchaNeeded = getCurrentUser().isCaptchaNeeded(); + if (isCaptchaNeeded) { final String iden = getNewCaptcha(); model.addAttribute("iden", iden); } @@ -81,8 +81,8 @@ public class RedditController { @RequestMapping("/postSchedule") public final String showSchedulePostForm(final Model model) { - final String needsCaptchaResult = needsCaptcha(); - if (needsCaptchaResult.equalsIgnoreCase("true")) { + final boolean isCaptchaNeeded = getCurrentUser().isCaptchaNeeded(); + if (isCaptchaNeeded) { model.addAttribute("msg", "Sorry, You do not have enought karma"); return "submissionResponse"; } @@ -92,7 +92,7 @@ public class RedditController { @RequestMapping(value = "/schedule", method = RequestMethod.POST) public final String schedule(final Model model, @RequestParam final Map formParams) throws ParseException { logger.info("User scheduling Post with these parameters: " + formParams.entrySet()); - final User user = userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue()); + final User user = getCurrentUser(); final Post post = new Post(); post.setUser(user); post.setSent(false); @@ -116,7 +116,7 @@ public class RedditController { @RequestMapping("/posts") public final String getScheduledPosts(final Model model) { - final User user = userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue()); + final User user = getCurrentUser(); final List posts = postReopsitory.findByUser(user); model.addAttribute("posts", posts); return "postListView"; @@ -160,6 +160,10 @@ public class RedditController { // === private + private User getCurrentUser() { + return userReopsitory.findByAccessToken(redditRestTemplate.getAccessToken().getValue()); + } + private final MultiValueMap constructParams(final Map formParams) { final MultiValueMap param = new LinkedMultiValueMap(); param.add(RedditApiConstants.API_TYPE, "json"); @@ -211,9 +215,15 @@ public class RedditController { user.setAccessToken(token.getValue()); user.setRefreshToken(token.getRefreshToken().getValue()); user.setTokenExpiration(token.getExpiration()); - userReopsitory.save(user); } + final String needsCaptchaResult = needsCaptcha(); + if (needsCaptchaResult.equalsIgnoreCase("true")) { + user.setNeedCaptcha(true); + } else { + user.setNeedCaptcha(false); + } + userReopsitory.save(user); } }