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 +-- .../src/main/resources/messages.properties | 17 ++++++++++- .../src/main/resources/messages_en.properties | 17 ++++++++++- .../src/main/webapp/WEB-INF/templates/index.html | 1 - .../WEB-INF/templates/layout/mainLayout.html | 1 - .../main/webapp/WEB-INF/templates/views/help.html | 3 +- .../WEB-INF/templates/views/partial/footer.html | 12 ++++---- .../templates/views/partial/navigation.html | 31 ++++++++++---------- 12 files changed, 73 insertions(+), 89 deletions(-) delete mode 100644 juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java (limited to 'juick-spring-www') 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) { diff --git a/juick-spring-www/src/main/resources/messages.properties b/juick-spring-www/src/main/resources/messages.properties index 29715d5d..a7034c5f 100644 --- a/juick-spring-www/src/main/resources/messages.properties +++ b/juick-spring-www/src/main/resources/messages.properties @@ -6,5 +6,20 @@ link.contacts=Контакты link.help=Помощь link.adv=Реклама +link.popular=Популярные +link.allMessages=Все сообщения +link.withPhotos=Фотографии +link.my=Моя лента +link.privateMessages=Приватные +link.discuss=Обсуждения +link.recommended=Рекомендации +link.postMessage=Написать сообщение +link.logout=Выйти + label.sponsor=Спонсор -label.sponsors=Спонсоры \ No newline at end of file +label.sponsors=Спонсоры +label.search=Поиск + +message.loginForSending=Чтобы добавлять сообщения и комментарии, представьтесь + +title.help=Справка \ No newline at end of file diff --git a/juick-spring-www/src/main/resources/messages_en.properties b/juick-spring-www/src/main/resources/messages_en.properties index 9efa2cbb..02c83e12 100644 --- a/juick-spring-www/src/main/resources/messages_en.properties +++ b/juick-spring-www/src/main/resources/messages_en.properties @@ -6,5 +6,20 @@ link.contacts=Contacts link.help=Help link.adv=Advertisement +link.popular=Popular +link.allMessages=All messages +link.withPhotos=Photos +link.my=My messages +link.privateMessages=My private messages +link.discuss=Discuss +link.recommended=Recommended +link.postMessage=Post +link.logout=Logout + label.sponsor=Sponsor -label.sponsors=Sponsors \ No newline at end of file +label.sponsors=Sponsors +label.search=Search + +message.loginForSending=Login for post messages and comments + +title.help=Help \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html index 5687985d..1695cd02 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html @@ -10,6 +10,5 @@

Главная страница !

- \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html index 33995d65..06805dc6 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html @@ -11,7 +11,6 @@ Juick.com -