Skip to content

Commit

Permalink
JC-2409 Error 500 when saving PM with invalid "To" field
Browse files Browse the repository at this point in the history
- Fixed hibernate lazy initialization exception by moving
userService.getCurrentUser() to service layer.
  • Loading branch information
evgeniycheban committed Dec 10, 2017
1 parent cdf589c commit 47d72d0
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ public interface PrivateMessageService extends EntityService<PrivateMessage> {
* @param userTo receiver of the message
* @param title the title of the message.
* @param body the body of the message.
* @param userFrom sender.
*/
void saveDraft(long id, JCUser userTo, String title, String body, JCUser userFrom);
void saveDraft(long id, JCUser userTo, String title, String body);

/**
* Get count of new messages for current user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ JCUser saveEditedUserNotifications(long editedUserId, UserNotificationsContainer
*/
void checkPermissionToCreateAndEditSimplePage(Long userId);

/**
* This method checks a permission of user to send private messages.
*
* @param userId an identified of user, for which we check permission.
*/
void checkPermissionToSendPrivateMessages(Long userId);

/**
* Searches for the common user, meaning that she might or might not be registered in JCommune, she can also be
* registered by some other JTalks component. This might be required to search through all the users of JTalks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ public Page<PrivateMessage> getDraftsForCurrentUser(String page) {
* {@inheritDoc}
*/
@Override
@PreAuthorize("hasPermission(#userFrom.id, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')")
public void saveDraft(long id, JCUser userTo, String title, String body, JCUser userFrom) {
public void saveDraft(long id, JCUser userTo, String title, String body) {
JCUser userFrom = userService.getCurrentUser();
userService.checkPermissionToSendPrivateMessages(userFrom.getId());
PrivateMessage pm;
if (this.getDao().isExist(id)) {
pm = this.getDao().get(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ public void checkPermissionToCreateAndEditSimplePage(Long userId) {
LOGGER.debug("Check permission to create or edit simple(static) pages - " + userId);
}

@Override
@PreAuthorize("hasPermission(#userId, 'USER', 'ProfilePermission.SEND_PRIVATE_MESSAGES')")
public void checkPermissionToSendPrivateMessages(Long userId) {
LOGGER.debug("Check permission to send private messages");
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ public void testGetDraftsForCurrentUser() {
}

@Test
public void testSaveDraft() throws NotFoundException {
public void testSaveDraft() {
JCUser recipient = new JCUser("name", "[email protected]", "pwd");

when(securityService.<User>createAclBuilder()).thenReturn(aclBuilder);
when(userService.getCurrentUser()).thenReturn(JC_USER);

pmService.saveDraft(PM_ID, recipient, "title", "body", JC_USER);
pmService.saveDraft(PM_ID, recipient, "title", "body");

verify(pmDao).saveOrUpdate(any(PrivateMessage.class));
verify(aclBuilder).grant(GeneralPermission.WRITE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -242,10 +242,10 @@ public ModelAndView editDraftPage(@PathVariable(PM_ID) Long id) throws NotFoundE
* @return redirect to "drafts" folder if saved successfully or show form with error message
*/
@RequestMapping(value = "/pm/save", method = {RequestMethod.POST, RequestMethod.GET})
public ModelAndView saveDraft(@Valid @ModelAttribute("privateMessageDto") PrivateMessageDraftDto pmDto, BindingResult result) {
public ModelAndView saveDraft(@Valid @ModelAttribute("privateMessageDto") PrivateMessageDraftDto pmDto,
BindingResult result) {
String targetView = "redirect:/drafts";
long pmDtoId = pmDto.getId();
JCUser userFrom = userService.getCurrentUser();
JCUser userTo = null;
if (pmDto.getRecipient() != null) {
try {
Expand All @@ -258,7 +258,7 @@ public ModelAndView saveDraft(@Valid @ModelAttribute("privateMessageDto") Privat
// The case when field "To:" filled incorrectly and fields "Title:" and "Body" are both empty .
if (pmDtoId != 0) { //means that we try to edit existing draft
try {
pmService.delete(Arrays.asList(pmDtoId));
pmService.delete(Collections.singletonList(pmDtoId));
} catch (NotFoundException e) {
// Catch block is empty because we don't need any additional logic in case if user removed
// draft in separate browser tab. We should just redirect him to list of drafts
Expand All @@ -269,7 +269,7 @@ public ModelAndView saveDraft(@Valid @ModelAttribute("privateMessageDto") Privat
if (result.hasFieldErrors()){
return new ModelAndView(PM_FORM).addObject(DTO,pmDto);
}
pmService.saveDraft(pmDtoId, userTo, pmDto.getTitle(), pmDto.getBody(), userFrom);
pmService.saveDraft(pmDtoId, userTo, pmDto.getTitle(), pmDto.getBody());
return new ModelAndView(targetView);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void saveDraftPost() throws Exception {
.andExpect(model().hasNoErrors())
.andExpect(status().isMovedTemporarily())
.andExpect(redirectedUrl("/drafts"));
verify(pmService).saveDraft(PM_ID, JC_USER, TITLE, BODY, JC_USER);
verify(pmService).saveDraft(PM_ID, JC_USER, TITLE, BODY);
}

@Test
Expand All @@ -347,7 +347,7 @@ public void testSaveDraftGet() throws Exception {
.andExpect(model().hasNoErrors())
.andExpect(status().isMovedTemporarily())
.andExpect(redirectedUrl("/drafts"));
verify(pmService).saveDraft(0, JC_USER, TITLE, BODY, JC_USER);
verify(pmService).saveDraft(0, JC_USER, TITLE, BODY);
}

@Test
Expand Down

0 comments on commit 47d72d0

Please sign in to comment.