AddPermissionController.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package sample.contact;
  16. import org.acegisecurity.acls.Permission;
  17. import org.acegisecurity.acls.domain.BasePermission;
  18. import org.acegisecurity.acls.sid.PrincipalSid;
  19. import org.springframework.beans.factory.InitializingBean;
  20. import org.springframework.dao.DataAccessException;
  21. import org.springframework.util.Assert;
  22. import org.springframework.validation.BindException;
  23. import org.springframework.web.bind.RequestUtils;
  24. import org.springframework.web.servlet.ModelAndView;
  25. import org.springframework.web.servlet.mvc.SimpleFormController;
  26. import org.springframework.web.servlet.view.RedirectView;
  27. import java.util.HashMap;
  28. import java.util.Iterator;
  29. import java.util.LinkedHashMap;
  30. import java.util.Map;
  31. import javax.servlet.http.HttpServletRequest;
  32. import javax.servlet.http.HttpServletResponse;
  33. /**
  34. * Controller for adding an ACL permission.
  35. *
  36. * @author Ben Alex
  37. * @version $Id$
  38. */
  39. public class AddPermissionController extends SimpleFormController implements InitializingBean {
  40. //~ Instance fields ================================================================================================
  41. private ContactManager contactManager;
  42. //~ Methods ========================================================================================================
  43. public void afterPropertiesSet() throws Exception {
  44. Assert.notNull(contactManager, "A ContactManager implementation is required");
  45. }
  46. protected ModelAndView disallowDuplicateFormSubmission(HttpServletRequest request, HttpServletResponse response)
  47. throws Exception {
  48. BindException errors = new BindException(formBackingObject(request), getCommandName());
  49. errors.reject("err.duplicateFormSubmission", "Duplicate form submission. *");
  50. return showForm(request, response, errors);
  51. }
  52. protected Object formBackingObject(HttpServletRequest request)
  53. throws Exception {
  54. int contactId = RequestUtils.getRequiredIntParameter(request, "contactId");
  55. Contact contact = contactManager.getById(new Long(contactId));
  56. AddPermission addPermission = new AddPermission();
  57. addPermission.setContact(contact);
  58. return addPermission;
  59. }
  60. protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response)
  61. throws Exception {
  62. return disallowDuplicateFormSubmission(request, response);
  63. }
  64. private Map listPermissions(HttpServletRequest request) {
  65. Map map = new LinkedHashMap();
  66. map.put(new Integer(BasePermission.ADMINISTRATION.getMask()),
  67. getApplicationContext().getMessage("select.administer", null, "Administer", request.getLocale()));
  68. map.put(new Integer(BasePermission.READ.getMask()),
  69. getApplicationContext().getMessage("select.read", null, "Read", request.getLocale()));
  70. map.put(new Integer(BasePermission.DELETE.getMask()),
  71. getApplicationContext().getMessage("select.delete", null, "Delete", request.getLocale()));
  72. return map;
  73. }
  74. private Map listRecipients(HttpServletRequest request) {
  75. Map map = new LinkedHashMap();
  76. map.put("",
  77. getApplicationContext().getMessage("select.pleaseSelect", null, "-- please select --", request.getLocale()));
  78. Iterator recipientsIter = contactManager.getAllRecipients().iterator();
  79. while (recipientsIter.hasNext()) {
  80. String recipient = (String) recipientsIter.next();
  81. map.put(recipient, recipient);
  82. }
  83. return map;
  84. }
  85. protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
  86. BindException errors) throws Exception {
  87. AddPermission addPermission = (AddPermission) command;
  88. PrincipalSid sid = new PrincipalSid(addPermission.getRecipient());
  89. Permission permission = BasePermission.buildFromMask(addPermission.getPermission().intValue());
  90. try {
  91. contactManager.addPermission(addPermission.getContact(), sid, permission);
  92. } catch (DataAccessException existingPermission) {
  93. existingPermission.printStackTrace();
  94. errors.rejectValue("recipient", "err.recipientExistsForContact", "Addition failure.");
  95. return showForm(request, response, errors);
  96. }
  97. return new ModelAndView(new RedirectView(getSuccessView()));
  98. }
  99. protected Map referenceData(HttpServletRequest request)
  100. throws Exception {
  101. Map model = new HashMap();
  102. model.put("recipients", listRecipients(request));
  103. model.put("permissions", listPermissions(request));
  104. return model;
  105. }
  106. public void setContactManager(ContactManager contact) {
  107. this.contactManager = contact;
  108. }
  109. }