IndexController.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package sample.contact;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.access.expression.PermissionEvaluator;
  7. import org.springframework.security.acls.Permission;
  8. import org.springframework.security.acls.domain.BasePermission;
  9. import org.springframework.security.acls.expression.AclPermissionEvaluator;
  10. import org.springframework.security.core.Authentication;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.servlet.ModelAndView;
  16. /**
  17. * Controller which handles simple, single request use cases such as index pages and contact deletion.
  18. *
  19. * @author Luke Taylor
  20. * @version $Id$
  21. * @since 3.0
  22. */
  23. @Controller
  24. public class IndexController {
  25. private final static Permission[] HAS_DELETE = new Permission[] {BasePermission.DELETE, BasePermission.ADMINISTRATION};
  26. private final static Permission[] HAS_ADMIN = new Permission[] {BasePermission.ADMINISTRATION};
  27. //~ Instance fields ================================================================================================
  28. @Autowired
  29. private ContactManager contactManager;
  30. @Autowired
  31. private PermissionEvaluator permissionEvaluator;
  32. //~ Methods ========================================================================================================
  33. /**
  34. * The public index page, used for unauthenticated users.
  35. */
  36. @RequestMapping(value="/hello.htm", method=RequestMethod.GET)
  37. public ModelAndView displayPublicIndex() {
  38. Contact rnd = contactManager.getRandomContact();
  39. return new ModelAndView("hello", "contact", rnd);
  40. }
  41. /**
  42. * The index page for an authenticated user.
  43. * <p>
  44. * This controller displays a list of all the contacts for which the current user has read or admin permissions.
  45. * It makes a call to {@link ContactManager#getAll()} which automatically filters the returned list using Spring
  46. * Security's ACL mechanism (see the expression annotations on this interface for the details).
  47. * <p>
  48. * In addition to rendering the list of contacts, the view will also include a "Del" or "Admin" link beside the
  49. * contact, depending on whether the user has the corresponding permissions (admin permission is assumed to imply
  50. * delete here). This information is stored in the model using the injected {@link PermissionEvaluator} instance.
  51. * The implementation should be an instance of {@link AclPermissionEvaluator} or one which is compatible with Spring
  52. * Security's ACL module.
  53. */
  54. @RequestMapping(value="/secure/index.htm", method=RequestMethod.GET)
  55. public ModelAndView displayUserContacts() {
  56. List<Contact> myContactsList = contactManager.getAll();
  57. Map<Contact, Boolean> hasDelete = new HashMap<Contact, Boolean>(myContactsList.size());
  58. Map<Contact, Boolean> hasAdmin = new HashMap<Contact, Boolean>(myContactsList.size());
  59. Authentication user = SecurityContextHolder.getContext().getAuthentication();
  60. for (Contact contact : myContactsList) {
  61. hasDelete.put(contact, Boolean.valueOf(permissionEvaluator.hasPermission(user, contact, HAS_DELETE)));
  62. hasAdmin.put(contact, Boolean.valueOf(permissionEvaluator.hasPermission(user, contact, HAS_ADMIN)));
  63. }
  64. Map<String, Object> model = new HashMap<String, Object>();
  65. model.put("contacts", myContactsList);
  66. model.put("hasDeletePermission", hasDelete);
  67. model.put("hasAdminPermission", hasAdmin);
  68. return new ModelAndView("index", "model", model);
  69. }
  70. }