/** * Sends a password recovery message for the user. * This method does not generate new password, just sends a message. * * @param user a person we will send a mail * @param newPassword new user password to be placed in an email * @throws MailingFailedException when mailing failed */ public void sendPasswordRecoveryMail(JCUser user, String newPassword) throws MailingFailedException { String urlSuffix = "/login"; String url = this.getDeploymentRootUrl() + urlSuffix; String name = user.getUsername(); Locale locale = user.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, name); model.put("newPassword", newPassword); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); this.sendEmail(user.getEmail(), messageSource.getMessage("passwordRecovery.subject", new Object[]{}, locale), model, "passwordRecovery.vm"); LOGGER.info("Password recovery email sent for {}", name); }
/** * Send email notification to user when he was mentioned in forum. * Email notification will be sent only when notifications are enabled * in forum, otherwise nothing will happen. * * @param recipient mentioned user who will receive notification * @param postId id of post where user was mentioned */ public void sendUserMentionedNotification(JCUser recipient, long postId) { String urlSuffix = "/posts/" + postId; String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, recipient.getUsername()); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); try { this.sendEmail(recipient.getEmail(), messageSource.getMessage("userMentioning.subject", new Object[]{}, locale), model, "userMentioning.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent activation mail for user: " + recipient.getUsername()); } }
/** * Handle user's successful authentication. * Updates last login time for authenticated user. * * @param request http request * @param response http response * @param authentication user's authentication * @throws ServletException forwarded from handler chain * @throws IOException forwarded from handler chain */ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { JCUser user = (JCUser) authentication.getPrincipal(); userService.updateLastLoginTime(user); logger.info("JCUser logged in: " + user.getUsername()); //apply language settings assuming CookieLocaleResolver usage String languageCode = user.getLanguage().getLanguageCode(); LocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setLocale(request, response, user.getLanguage().getLocale()); Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, languageCode); cookie.setPath("/"); response.addCookie(cookie); super.onAuthenticationSuccess(request, response, authentication); } }
/** * Sends notification to user about received private message. * * @param recipient a person to be notified about received private message by email * @param pm private message itself */ public void sendReceivedPrivateMessageNotification(JCUser recipient, PrivateMessage pm) { try { String urlSuffix = "/pm/inbox/" + pm.getId(); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put("recipient", recipient); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); this.sendEmail(recipient.getEmail(), messageSource.getMessage("receivedPrivateMessageNotification.subject", new Object[]{}, locale), model, "receivedPrivateMessageNotification.vm"); } catch (MailingFailedException e) { LOGGER.error(String.format(LOG_TEMPLATE, "Private message", pm.getId(), recipient.getUsername())); } }
/** * 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; }
/** * Sends email with a hyperlink to activate user account. * * @param recipient user to send activation mail to */ public void sendAccountActivationMail(JCUser recipient) { try { String urlSuffix = "/user/activate/" + recipient.getUuid(); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, recipient.getUsername()); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); this.sendEmail(recipient.getEmail(), messageSource.getMessage("accountActivation.subject", new Object[]{}, locale), model, "accountActivation.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent activation mail for user: " + recipient.getUsername()); } }
/** * Sends update notification to user specified if * {@link SubscriptionAwareEntity} was updated, e.g. when some new * information were added to the subscribed entity. This method won't check if user * is subscribed to the particular notification or not. * * @param recipient a person to be notified about updates by email * @param entity changed subscribed entity. */ public void sendUpdatesOnSubscription(JCUser recipient, SubscriptionAwareEntity entity) { try { String urlSuffix = entity.getUrlSuffix(); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(LINK, url); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + entity.getUnsubscribeLinkForSubscribersOf(entity.getClass())); sendEmailOnForumUpdates(recipient, model, locale, (Entity) entity, "subscriptionNotification.subject", "subscriptionNotification.vm"); } catch (MailingFailedException e) { LOGGER.error(String.format(LOG_TEMPLATE, entity.getClass().getCanonicalName(), ((Entity) entity).getId(), recipient.getUsername())); } }
/** * Set mail about removing topic. * * @param recipient Recipient for which send notification * @param topic Current topic * @param curUser User that removed the topic */ public void sendRemovingTopicMail(JCUser recipient, Topic topic, String curUser) { Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(USER, recipient); model.put(RECIPIENT_LOCALE, locale); model.put(CUR_USER, curUser); //Topic not exist more and user not subscribed to branch, so simply redirect to branch model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + "/branches/" + topic.getBranch().getId()); model.put(TOPIC, topic); try { String subjectTemplate = REMOVE_TOPIC_SUBJECT_TEMPLATE; String messageBodyTemplate = REMOVE_TOPIC_MESSAGE_BODY_TEMPLATE; if (topic.isCodeReview()) { subjectTemplate = REMOVE_CODE_REVIEW_SUBJECT_TEMPLATE; messageBodyTemplate = REMOVE_CODE_REVIEW_MESSAGE_BODY_TEMPLATE; } String subject = messageSource.getMessage(subjectTemplate, new Object[]{}, locale); this.sendEmail(recipient.getEmail(), subject, model, messageBodyTemplate); } catch (MailingFailedException e) { LOGGER.error("Failed to sent mail about removing topic or code review for user: " + recipient.getUsername()); } }
/** * Sends email to topic starter that his or her topic was moved * * @param recipient user to send notification * @param topic relocated topic * @param curUser User that moved topic */ public <T extends SubscriptionAwareEntity> void sendTopicMovedMail( JCUser recipient, Topic topic, String curUser, Class<T> subsсriptionTargetClass) { String urlSuffix = getTopicUrlSuffix(topic); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = recipient.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(NAME, recipient.getUsername()); model.put(CUR_USER, curUser); model.put(LINK, url); model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + topic.getUnsubscribeLinkForSubscribersOf(subsсriptionTargetClass)); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); model.put(RECIPIENT_LOCALE, locale); try { this.sendEmail(recipient.getEmail(), messageSource.getMessage("moveTopic.subject", new Object[]{}, locale), model, "moveTopic.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent activation mail for user: " + recipient.getUsername()); } }
localeResolver.setLocale(request, response, userService.getCurrentUser().getLanguage().getLocale()); return new ModelAndView("redirect:" + referer); } else {
/** * Send email about new topic in the subscribed branch. * * @param subscriber recipient * @param topic newly created topic */ void sendTopicCreationMail(JCUser subscriber, Topic topic) { try { String urlSuffix = getTopicUrlSuffix(topic); String url = this.getDeploymentRootUrl() + urlSuffix; Locale locale = subscriber.getLanguage().getLocale(); Map<String, Object> model = new HashMap<>(); model.put(LINK, url); model.put(LINK_UNSUBSCRIBE, this.getDeploymentRootUrl() + topic.getBranch().getUnsubscribeLinkForSubscribersOf(Branch.class)); model.put(LINK_LABEL, getDeploymentRootUrlWithoutPort() + urlSuffix); sendEmailOnForumUpdates(subscriber, model, locale, topic.getBranch(), "subscriptionNotification.subject", "branchSubscriptionNotification.vm"); } catch (MailingFailedException e) { LOGGER.error("Failed to sent mail about creation topic for user: " + subscriber.getUsername()); } }
/** * {@inheritDoc} */ @Override public CreateTopicBtnDto getCreateTopicBtnDto(long branchId) { Locale locale = ReadOnlySecurityService.getInstance().getCurrentUser().getLanguage().getLocale(); return new CreateTopicBtnDto("new-question-btn", translateLabel("label.addQuestion", locale), translateLabel("label.addQuestion.tip", locale), CONTEXT + "/new?branchId=" + branchId, order); }
/** * Translate labels for specified plugin into language which selected by current user as forum language. * * @param properties list of properties to translate * @param plugin plugin with have specified properties * * @return map of properties translation depending from name */ private Map<String, String> translatePropertiesLabels(List<PluginProperty> properties, Plugin plugin) { Map<String, String> labels = new HashMap<>(); Locale locale = userService.getCurrentUser().getLanguage().getLocale(); for (PluginProperty property : properties) { String translation = plugin.translateLabel(property.getName(), locale); labels.put(property.getName(), translation); if (property.getHint() != null) { String hintTranslation = plugin.translateLabel(property.getHint(), locale); labels.put(property.getHint(), hintTranslation); } } return labels; }
localeResolver.setLocale(request, response, userService.getCurrentUser().getLanguage().getLocale()); return new JsonResponse(JsonResponseStatus.SUCCESS); } else if(authenticationStatus.equals(AuthenticationStatus.NOT_ENABLED)){
copy.setLanguage(user.getLanguage()); copy.setPageSize(user.getPageSize()); copy.setLocation(user.getLocation());