From f2454d531e9e99c406a513d92702858510fef4b9 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sun, 11 Dec 2016 13:56:26 +0700 Subject: i18n, using SecurityContextHolder for get currentUser (user always not null) --- .../com/juick/www/controllers/IndexController.java | 22 +++++++------- .../com/juick/www/controllers/LoginController.java | 34 ---------------------- .../com/juick/www/controllers/PMController.java | 16 ++++------ .../juick/www/controllers/SettingsController.java | 4 +-- .../com/juick/www/controllers/TagController.java | 4 +-- 5 files changed, 19 insertions(+), 61 deletions(-) delete mode 100644 juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java (limited to 'juick-spring-www/src/main/java/com/juick/www/controllers') diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java index c6de2fbf..c56763b1 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/IndexController.java @@ -12,12 +12,13 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import javax.inject.Inject; import java.io.IOException; import java.net.URLEncoder; -import java.security.Principal; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -39,9 +40,6 @@ public class IndexController { @RequestMapping(value = "/", method = RequestMethod.GET) public String indexPage( - Principal principal, - @CookieValue("ref") Optional ref, - @RequestHeader("Referer") Optional referer, @RequestParam("show") Optional paramShow, @RequestParam("tag") Optional paramTagStr, @RequestParam(value = "before") Optional paramBefore, @@ -49,16 +47,15 @@ public class IndexController { ModelMap model) throws IOException { if (paramTagStr.isPresent()) - return "redirect:/tag/" + URLEncoder.encode(paramTagStr.get(), "UTF-8"); + return "redirect:/tag/" + URLEncoder.encode(paramTagStr.get(), "UTF-8"); if (StringUtils.isNotEmpty(paramSearch) && paramSearch.length() > 64) paramSearch = ""; - String name = UserUtils.getUsername(principal, null); - User visitor = userService.getUserByName(name); + User visitor = UserUtils.getCurrentUser(); String title; - if (visitor.getUid() > 0) { + if (!visitor.isAnonym()) { title = "Популярные"; } else { title = "Микроблоги Juick: популярные записи"; @@ -121,4 +118,9 @@ public class IndexController { //model.addAttribute("isModerator", userService.getModerators().contains(visitor.getUid())); return "index"; } -} + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public String getLoginForm() { + return "views/login"; + } +} \ No newline at end of file diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java deleted file mode 100644 index 8827d948..00000000 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.juick.www.controllers; - -import com.juick.User; -import com.juick.service.UserService; -import com.juick.util.UserUtils; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -import javax.inject.Inject; -import java.security.Principal; - -/** - * Created by vitalyster on 09.12.2016. - */ -@Controller -public class LoginController { - @Inject - private UserService userService; - @Inject - private Environment env; - - @RequestMapping(value = "/login", method = RequestMethod.GET) - public String getLoginForm(Principal principal) { - String name = UserUtils.getUsername(principal, null); - User visitor = userService.getUserByName(name); - - if (visitor.getUid() > 0) - return "redirect:/login"; - - return "views/login"; - } -} diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java index 15a93684..41c6b33c 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java @@ -21,7 +21,6 @@ import org.springframework.web.bind.annotation.RequestParam; import rocks.xmpp.addr.Jid; import javax.inject.Inject; -import java.security.Principal; import java.util.List; /** @@ -41,9 +40,8 @@ public class PMController { WebApp webApp; @RequestMapping("/pm/inbox") - public String doGetInbox(Principal principal, ModelMap context) { - String name = UserUtils.getUsername(principal, null); - User visitor = userService.getUserByName(name); + public String doGetInbox(ModelMap context) { + User visitor = UserUtils.getCurrentUser(); String title = "PM: Inbox"; List msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText()))); @@ -56,12 +54,10 @@ public class PMController { @RequestMapping(value = "/pm/sent", method = RequestMethod.GET) public String doGetSent( - Principal principal, @RequestParam String uname, ModelMap context) { String title = "PM: Sent"; - String name = UserUtils.getUsername(principal, null); - User visitor = userService.getUserByName(name); + User visitor = UserUtils.getCurrentUser(); List msgs = pmQueriesService.getLastPMSent(visitor.getUid()); if (WebUtils.isNotUserName(uname)) { @@ -77,12 +73,10 @@ public class PMController { @RequestMapping(value = "/pm/sent", method = RequestMethod.POST) public String doPostPM( - Principal principal, @RequestParam String uname, @RequestParam String body, ModelMap context) { - String name = UserUtils.getUsername(principal, null); - User visitor = userService.getUserByName(name); + User visitor = UserUtils.getCurrentUser(); if (uname.startsWith("@")) { uname = uname.substring(1); } @@ -132,6 +126,6 @@ public class PMController { } return "redirect:/pm/sent"; } - throw new HttpBadRequestException(); + throw new HttpBadRequestException(); } } diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/SettingsController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/SettingsController.java index a038389f..ceccdbb7 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/SettingsController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/SettingsController.java @@ -36,13 +36,11 @@ public class SettingsController { @RequestMapping("settings") public String showSettings( - Principal principal, @RequestParam(required = false) String page, @RequestParam(required = false) String code, ModelMap context ) throws ServletException, IOException { - String name = UserUtils.getUsername(principal, null); - User visitor = userService.getUserByName(name); + User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { return "redirect:/login"; } diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java index 0961f683..f61928a7 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/TagController.java @@ -41,13 +41,11 @@ public class TagController { @RequestMapping("/tag/{tagName}") protected String doGet( - Principal principal, @PathVariable("tagName") String paramTagStr, @RequestParam(value = "before", required = false, defaultValue = "0") Integer paramBefore, @QueryString Optional queryString, ModelMap model) throws UnsupportedEncodingException { - String name = UserUtils.getUsername(principal, null); - User visitor = userService.getUserByName(name); + User visitor = UserUtils.getCurrentUser(); com.juick.Tag paramTag = tagService.getTag(paramTagStr, false); if (paramTag == null) { -- cgit v1.2.3