Search in sources :

Example 1 with Message

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());
}
Also used : CurrentUser(sample.security.CurrentUser) User(sample.data.User) Message(sample.data.Message) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Message

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) {
        }
    }));
}
Also used : Description(org.hamcrest.Description) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) BaseMatcher(org.hamcrest.BaseMatcher) Message(sample.data.Message) List(java.util.List) Test(org.junit.Test)

Example 3 with Message

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());
}
Also used : User(sample.data.User) Message(sample.data.Message)

Aggregations

Message (sample.data.Message)3 User (sample.data.User)2 List (java.util.List)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1 Test (org.junit.Test)1 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 CurrentUser (sample.security.CurrentUser)1