/** * @param value filed value to be validated * @param context validator context, not used here * @return true if user specified is not the current one logged in */ @Override public boolean isValid(String value, ConstraintValidatorContext context) { JCUser user = service.getCurrentUser(); return user.isAnonymous() || !user.getUsername().equalsIgnoreCase(value); } }
/** * Makes URL to mark topic page as read. * For anonymous user returns empty optional. * * @param user current user * @param page page to mark as read * @return Optional url string */ public Optional<String> getMarkAsReadUrl(JCUser user, String page) { if (user.isAnonymous()) { return Optional.absent(); } return Optional.of("{topicId}/page/{pageNum}/markread?userId={userId}&lastModified={lastModified}" .replace("{topicId}", String.valueOf(getId())) .replace("{pageNum}", page) .replace("{userId}", String.valueOf(user.getId())) .replace("{lastModified}", String.valueOf(getLastModificationPostDate().getMillis())) ); } }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#branch.id, 'BRANCH', 'BranchPermission.VIEW_TOPICS')") public void markAllTopicsAsRead(Branch branch) { JCUser user = userService.getCurrentUser(); if (!user.isAnonymous()) { // would be logical to remove per-topic Last Read Post records from DB, // but it's not worth it since most people will anyway press on global Mark All As Read // at some point and this will clean the records for user. Ergo, it's not expected // that the DB will be overwhelmed with per-topic Last Read Post records. branchReadedMarkerDao.markBranchAsRead(user, branch); } }
/** * Shows login page. Also checks if user is already logged in. * If so he is redirected to referer page. * * @param request Current servlet request * @return login view name or redirect to main page */ @RequestMapping(value = "/login", method = RequestMethod.GET) public ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response) { JCUser currentUser = userService.getCurrentUser(); String referer = getReferer(request, response); if (currentUser.isAnonymous()) { ModelAndView mav = new ModelAndView(LOGIN); mav.addObject(REFERER_ATTR, referer); LoginUserDto loginUserDto = new LoginUserDto(); mav.addObject(LOGIN_DTO, loginUserDto); return mav; } else { return new ModelAndView("redirect:" + referer); } }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#topic.branch.id, 'BRANCH', 'BranchPermission.VIEW_TOPICS')") public void markTopicAsRead(Topic topic) { JCUser current = userService.getCurrentUser(); if (!current.isAnonymous()) { // topics are always unread for anonymous users saveLastReadPost(current, topic, topic.getLastPost()); } }
/** * Render registration page with bind objects to form. * Also checks if user is already logged in. * If so he is redirected to main page. * * @param request Servlet request. * @param locale To set currently selected language as user's default * @return {@code ModelAndView} with "registration" view, any additional html from registration plugins and * {@link org.jtalks.jcommune.model.dto.RegisterUserDto} with name "newUser */ @RequestMapping(value = "/user/new", method = RequestMethod.GET) public ModelAndView registrationPage(HttpServletRequest request, Locale locale) { JCUser currentUser = userService.getCurrentUser(); if (currentUser.isAnonymous()) { Map<String, String> registrationPlugins = getRegistrationPluginsHtml(request, locale); return new ModelAndView(REGISTRATION) .addObject("newUser", new RegisterUserDto()) .addObject("registrationPlugins", registrationPlugins); } else { return new ModelAndView("redirect:" + MAIN_PAGE_REFERER); } }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#topic.branch.id, 'BRANCH', 'BranchPermission.VIEW_TOPICS')") public void markTopicPageAsRead(Topic topic, int pageNum) { JCUser current = userService.getCurrentUser(); // topics are always unread for anonymous users if (!current.isAnonymous()) { Post lastPostOnPage = this.calculatePostOnPage(current, topic, pageNum); saveLastReadPost(current, topic, lastPostOnPage); } }
/** * {@inheritDoc} */ @Override public List<Long> getForbiddenBranchesIds(JCUser user) { Query query = session().getNamedQuery("getForbiddenBranchesIds"); if (user.isAnonymous()) { query.setString("sid", user.getClass().getSimpleName()); } else { query.setParameterList("sid", getGroupIds(user)); } return query.list(); }
/** * {@inheritDoc} */ @Override public List<Long> getAvailableBranchIds(JCUser user, List<Branch> branches) { if (branches.isEmpty()) { return Collections.emptyList(); } List<Long> branchIds = getEntityIdsLongs(new ArrayList<Entity>(branches)); if (!user.isAnonymous()) { return visibleBranchesForLoggedIn(user, branchIds); } Query query = session().getNamedQuery("getAvailableBranchesForAnonymousUser"); query.setParameterList("branchIds", branchIds); return query.list(); }
/** * {@inheritDoc} */ @Override public Page<Topic> getTopicsUpdatedSince(DateTime timeStamp, PageRequest pageRequest, JCUser user) { if (!user.isAnonymous()) { return getRecentTopicsByGroupIds(getGroupIds(user), timeStamp, pageRequest); } return getRecentTopicsForAnonymousUser(timeStamp, pageRequest); }
/** * {@inheritDoc} */ @Override public Page<Topic> getUnansweredTopics(PageRequest pageRequest, JCUser user) { if (!user.isAnonymous()) { return getUnansweredTopicsByGroupIds(getGroupIds(user), pageRequest); } return getUnansweredTopicsForAnonymousUser(pageRequest); }
@RequestMapping(value = "**/language", method = RequestMethod.GET) public String saveUserLanguage(@RequestParam(value = "lang", defaultValue = "en") String lang, HttpServletResponse response, HttpServletRequest request) throws ServletException { final JCUser jcuser = userService.getCurrentUser(); final Language languageFromRequest = Language.byLocale(new Locale(lang)); if (!jcuser.isAnonymous()) { retryTemplate.execute(new RetryCallback<Void, RuntimeException>() { @Override public Void doWithRetry(RetryContext context) throws RuntimeException { userService.changeLanguage(jcuser, languageFromRequest); return null; } }); } LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); localeResolver.setLocale(request, response, languageFromRequest.getLocale()); return "redirect:" + request.getHeader("Referer"); }
/** * Resolves user locale. If user is logged in locale will be read from database. If user is anonymous locale will be * resolved form request and cookies using standard {@link CookieLocaleResolver} mechanism * * @param request HttpServletRequest * @return user locale */ @Override public Locale resolveLocale(HttpServletRequest request) { Locale locale = (Locale)request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME); if (locale != null) { return locale; } JCUser currentUser = userReader.getCurrentUser(); if (currentUser.isAnonymous()) { locale = super.resolveLocale(request); } else { locale = currentUser.getLanguage().getLocale(); } request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale); return locale; }
/** * Gets copy of user currently logged in. This is done so that Plugins won't be able to modify objects and * break core forum. * * @return the copy of user currently logged in or {@link AnonymousUser} if user is anonymous * @see AnonymousUser */ @Override public JCUser getCurrentUser() { JCUser currentUser = userReader.getCurrentUser(); if (currentUser.isAnonymous()) { return new AnonymousUser(); } else { return JCUser.copyUser(currentUser); } } }
if (user.isAnonymous()) { queryForbidden.setString("sid", user.getClass().getSimpleName()); queryAllowed.setString("sid", user.getClass().getSimpleName());
/** * {@inheritDoc} */ @Override public List<Topic> fillLastReadPostForTopics(List<Topic> topics) { JCUser currentUser = userService.getCurrentUser(); if (!currentUser.isAnonymous()) { List<Topic> notModifiedTopics = extractNotModifiedTopicsSinceForumMarkedAsRead( currentUser, topics); for (Topic notModifiedTopic : notModifiedTopics) { Post lastPost = notModifiedTopic.getLastPost(); notModifiedTopic.setLastReadPostDate(lastPost.getCreationDate()); } // @SuppressWarnings("unchecked") List<Topic> modifiedTopics = ListUtils.removeAll(topics, notModifiedTopics); fillLastReadPostsForModifiedTopics(modifiedTopics, currentUser); } return topics; }