/** * Quotes text given as a valid BB-coded quote. Such a quotes are rendered automatically in posts or forum messages. * On UI you can find Quote button near each post. * * @param source text to quote, not null * @param author text author, not null * @return well formed BB qoute */ public String quote(String source, JCUser author) { Validate.notNull(source, "Source cannot be null"); Validate.notNull(author, "Author cannot be null"); return String.format(QUOTE_PATEERN, author.getUsername(), source); }
/** * Get username by user's id. * * @param userId user's id * @return an username */ private String getUsername(long userId) { try { JCUser user = userService.get(userId); return user.getUsername(); } catch (NotFoundException e) { return null; } } }
/** * @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); } }
/** * Constructor which fills dto fields from user. * * @param user copying source */ public EditUserProfileDto(JCUser user) { this.userId = user.getId(); this.username = user.getUsername(); this.userProfileVersion = user.getVersion(); }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public JCUser getByUsername(String username) { List<JCUser> users = session().createCriteria(JCUser.class) .add(Restrictions.eq("username", username).ignoreCase()).list(); if (users.size() == 1) { return users.get(0); } else { //see JavaDocs to get understanding on when this situation can happen for (JCUser user : users) { if (user.getUsername().equals(username)) { return user; } } return null; } }
/** * Get link to user's profile. * * @param username user's name * @return null when user doesn't exist, otherwise link to user's profile */ private String getLinkToUserProfile(String username, UserDao userDao) { String userPofileLink = null; JCUser user = userDao.getByUsername(username); if (user != null && user.getUsername().equals(username)) { userPofileLink = getApplicationNameAsContextPath() + "/users/" + user.getId(); LOGGER.trace("{} has the following url of profile - {}", username, userPofileLink); } else { LOGGER.trace("Mentioned user wasn't find: {}", username); } return userPofileLink; }
/** * Render the page with a form for creation new Private Message with empty {@link PrivateMessageDto} bound. * * @return {@code ModelAndView} with the form */ @RequestMapping(value = "/pm/new", method = RequestMethod.GET) public ModelAndView newPmPage(@RequestParam(value = "recipientId", required = false) Long recipientId) throws NotFoundException { Long senderId = userService.getCurrentUser().getId(); pmService.checkPermissionsToSend(senderId); PrivateMessageDto pmDto = new PrivateMessageDto(); if (recipientId != null) { String name = userService.get(recipientId).getUsername(); pmDto.setRecipient(name); } return new ModelAndView(PM_FORM) .addObject(DTO, pmDto); }
/** * Create the reply private message dto from {@link PrivateMessage} * @param pm private message for conversion in to reply * @return dto for reply */ public static PrivateMessageDto getReplyDtoFor(PrivateMessage pm) { PrivateMessageDto dto = new PrivateMessageDto(); dto.setRecipient(pm.getUserFrom().getUsername()); dto.setTitle(pm.prepareTitleForReply()); return dto; } }
/** * Creates CodeReviewCommentDto from {@link PostComment} * * @param comment to create dto * @throws java.lang.NumberFormatException if comment have no line_number attribute * or if this attribute have invalid value */ public CodeReviewCommentDto(PostComment comment) { this.id = comment.getId(); this.lineNumber = Integer.parseInt(comment.getAttributes().get(LINE_NUMBER_PROPERTY_NAME)); this.body = comment.getBody(); this.authorId = comment.getAuthor().getId(); this.authorUsername = comment.getAuthor().getUsername(); if (comment.getUserChanged() != null) { this.editorId = comment.getUserChanged().getId(); this.editorUsername = comment.getUserChanged().getUsername(); } this.modificationDate = comment.getModificationDate(); }
/** * {@inheritDoc} */ @Override public JCUser saveEditedUserSecurity(long editedUserId, UserSecurityContainer userSecurityInfo) throws NotFoundException { JCUser editedUser = this.get(editedUserId); String newPassword = userSecurityInfo.getNewPassword(); if (newPassword != null) { String encryptedPassword = encryptionService.encryptPassword(newPassword); editedUser.setPassword(encryptedPassword); } this.getDao().saveOrUpdate(editedUser); LOGGER.info("Updated user security settings. Username: {}", editedUser.getUsername()); return editedUser; }
@Override @PreAuthorize("hasPermission(#topic.branch.id, 'BRANCH', 'BranchPermission.CREATE_POSTS')") public PostDraft saveOrUpdateDraft(Topic topic, String content) { JCUser currentUser = userService.getCurrentUser(); PostDraft draft = topic.getDraftForUser(currentUser); if (draft == null) { draft = new PostDraft(content, currentUser); topic.addDraft(draft); } else { draft.setContent(content); draft.updateLastSavedTime(); } topicDao.saveOrUpdate(topic); logger.debug("Draft saved in topic. Topic id={}, Post id={}, Post author={}", new Object[]{topic.getId(), draft.getId(), currentUser.getUsername()}); return draft; }
/** * {@inheritDoc} */ @Override public void restorePassword(String email) throws MailingFailedException { JCUser user = this.getDao().getByEmail(email); String randomPassword = RandomStringUtils.randomAlphanumeric(6); // first - mail attempt, then - database changes mailService.sendPasswordRecoveryMail(user, randomPassword); String encryptedRandomPassword = encryptionService.encryptPassword(randomPassword); user.setPassword(encryptedRandomPassword); this.getDao().saveOrUpdate(user); LOGGER.info("New random password was set for user {}", user.getUsername()); }
/** * Send notification to subscribers about removing topic or code review. * * @param topic Current topic */ public void sendNotificationAboutRemovingTopic(Topic topic) { subscribedEntityChanged(topic.getBranch()); Collection<JCUser> subscribers = subscriptionService.getAllowedSubscribers(topic); Collection<JCUser> branchSubscribers = subscriptionService.getAllowedSubscribers(topic.getBranch()); subscribers.removeAll(branchSubscribers); String curUser = userService.getCurrentUser().getUsername(); this.filterSubscribers(subscribers, topic); for (JCUser subscriber : subscribers) { mailService.sendRemovingTopicMail(subscriber, topic, curUser); } }
/** * {@inheritDoc} */ @Override public JCUser saveEditedUserNotifications(long editedUserId, UserNotificationsContainer userNotificationsInfo) throws NotFoundException { JCUser editedUser = this.get(editedUserId); editedUser.setMentioningNotificationsEnabled(userNotificationsInfo.isMentioningNotificationsEnabled()); editedUser.setSendPmNotification(userNotificationsInfo.isSendPmNotification()); editedUser.setAutosubscribe(userNotificationsInfo.isAutosubscribe()); this.getDao().saveOrUpdate(editedUser); LOGGER.info("Updated user notification settings. Username: {}", editedUser.getUsername()); return editedUser; }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#id, 'PRIVATE_MESSAGE', 'GeneralPermission.READ')") public PrivateMessage get(Long id) throws NotFoundException { PrivateMessage pm = super.get(id); if (!hasCurrentUserAccessToPM(pm)) { throw new NotFoundException(String.format("current user has no right to read pm %s with id %d", userService.getCurrentUser(), id)); } if (this.ifMessageShouldBeMarkedAsRead(pm)) { pm.setRead(true); this.getDao().saveOrUpdate(pm); userDataCache.decrementNewMessageCountFor(pm.getUserTo().getUsername()); } return pm; }
public CommentDto(PostComment postComment, JodaDateTimeTool dateTimeTool) { this.id = postComment.getId(); this.authorUsername = postComment.getAuthor().getUsername(); this.body = postComment.getBody(); this.formattedCreationDate = dateTimeTool.format(postComment.getCreationDate()); this.authorId = postComment.getAuthor().getId(); }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')") public PrivateMessage sendMessage(String title, String body, JCUser recipient, JCUser userFrom) { PrivateMessage pm = new PrivateMessage(recipient, userFrom, title, body); pm.setRead(false); pm.setStatus(PrivateMessageStatus.SENT); this.getDao().saveOrUpdate(pm); userDataCache.incrementNewMessageCountFor(recipient.getUsername()); securityService.createAclBuilder().grant(GeneralPermission.READ).to(recipient).on(pm).flush(); securityService.createAclBuilder().grant(GeneralPermission.READ).to(userFrom).on(pm).flush(); if (isSendNotificationMessage(recipient)) { mailService.sendReceivedPrivateMessageNotification(recipient, pm); } logger.debug("Private message to user {} was sent. Message id={}", recipient.getUsername(), pm.getId()); return pm; }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.CREATE_POSTS') " + "and #post.userCreated.username != principal.username") public Post vote(Post post, PostVote vote) { JCUser currentUser = userService.getCurrentUser(); if (!post.canBeVotedBy(currentUser, vote.isVotedUp())) { logger.info("User [{}] tries to vote for post with id={} in same direction more than one time", currentUser.getUsername(), post.getId()); throw new AccessDeniedException("User can't vote in same direction more than one time"); } vote.setUser(currentUser); int ratingChanges = post.calculateRatingChanges(vote); post.putVote(vote); getDao().saveOrUpdate(post); getDao().changeRating(post.getId(), ratingChanges); return post; }
/** * Create the full private message dto from {@link PrivateMessage} * * @param pm private message for conversion * @return dto for full private message */ public static PrivateMessageDto getFullPmDtoFor(PrivateMessage pm) { PrivateMessageDto dto = new PrivateMessageDto(); dto.setBody(pm.getBody()); dto.setTitle(pm.getTitle()); if (pm.getUserTo() != null) { dto.setRecipient(pm.getUserTo().getUsername()); } dto.setId(pm.getId()); return dto; }
/** * Creates feed item with information about the post * * @param post post to add to the feed * @param componentUrl base url of the forum component * @return item for the RSS feed */ private Item createFeedItem(Post post, String componentUrl) { Item item = new Item(); Content content = new Content(); content.setType(Content.TEXT); content.setValue(post.getPostContent()); item.setContent(content); item.setTitle(post.getTopic().getTitle()); item.setAuthor(post.getUserCreated().getUsername()); item.setLink(componentUrl + "/posts/" + post.getId()); item.setPubDate(post.getCreationDate().toDate()); return item; }