use of sample.data.Message in project gs-spring-security-3.2 by rwinch.
the class MessageController method create.
@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(@Valid MessageForm messageForm, BindingResult result, RedirectAttributes redirect) {
User to = userRepository.findByEmail(messageForm.getToEmail());
if (to == null) {
result.rejectValue("toEmail", "toEmail", "User not found");
}
if (result.hasErrors()) {
return new ModelAndView("messages/compose");
}
Message message = new Message();
message.setSummary(messageForm.getSummary());
message.setText(messageForm.getText());
message.setTo(to);
message = messageRepository.save(message);
redirect.addFlashAttribute("globalMessage", "Message added successfully");
return new ModelAndView("redirect:/{message.id}", "message.id", message.getId());
}
use of sample.data.Message in project gs-spring-security-3.2 by rwinch.
the class SecurityTests method inboxShowsOnlyRobsMessages.
@Test
public void inboxShowsOnlyRobsMessages() throws Exception {
RequestBuilder request = get("/").with(user(rob).roles("USER"));
mvc.perform(request).andExpect(model().attribute("messages", new BaseMatcher<List<Message>>() {
@Override
public boolean matches(Object other) {
@SuppressWarnings("unchecked") List<Message> messages = (List<Message>) other;
return messages.size() == 1 && messages.get(0).getId() == 100;
}
@Override
public void describeTo(Description d) {
}
}));
}
use of sample.data.Message in project gs-spring-security-3.2 by rwinch.
the class MessagePermissionEvaluator method hasPermission.
/* (non-Javadoc)
* @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
*/
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
if (authentication == null) {
return false;
}
Message message = (Message) targetDomainObject;
if (message == null) {
return true;
}
User currentUser = (User) authentication.getPrincipal();
return currentUser.getId().equals(message.getTo().getId());
}
Aggregations