ContactManagerFacade.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright 2004 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 net.sf.acegisecurity.AccessDeniedException;
  17. import net.sf.acegisecurity.Authentication;
  18. import net.sf.acegisecurity.context.ContextHolder;
  19. import net.sf.acegisecurity.context.SecureContext;
  20. import net.sf.acegisecurity.providers.dao.User;
  21. import org.springframework.beans.factory.InitializingBean;
  22. /**
  23. * This is the public facade to the application's main business object.
  24. *
  25. * <p>
  26. * Used to demonstrate security configuration in a multi-tier application. Most
  27. * methods of this class are secured via standard security definitions in the
  28. * bean context. There is one method that supplements these security checks.
  29. * All methods delegate to a "backend" object. The "backend" object relies on
  30. * the facade's <code>RunAsManager</code> assigning an additional
  31. * <code>GrantedAuthority</code> that is required to call its methods.
  32. * </p>
  33. *
  34. * @author Ben Alex
  35. * @version $Id$
  36. */
  37. public class ContactManagerFacade implements ContactManager, InitializingBean {
  38. //~ Instance fields ========================================================
  39. private ContactManager backend;
  40. //~ Methods ================================================================
  41. /**
  42. * Security system will ensure the owner parameter equals the currently
  43. * logged in user.
  44. *
  45. * @param owner DOCUMENT ME!
  46. *
  47. * @return DOCUMENT ME!
  48. */
  49. public Contact[] getAllByOwner(String owner) {
  50. return backend.getAllByOwner(owner);
  51. }
  52. public void setBackend(ContactManager backend) {
  53. this.backend = backend;
  54. }
  55. public ContactManager getBackend() {
  56. return backend;
  57. }
  58. /**
  59. * Security system will ensure logged in user has ROLE_TELLER.
  60. *
  61. * <p>
  62. * Security system cannot ensure that only the owner can get the contact,
  63. * as doing so would require it to specifically open the contact. Whilst
  64. * possible, this would be expensive as the operation would be performed
  65. * both by the security system as well as the implementation. Instead the
  66. * facade will confirm the contact.getOwner() matches what is on the
  67. * ContextHolder.
  68. * </p>
  69. *
  70. * @param id DOCUMENT ME!
  71. *
  72. * @return DOCUMENT ME!
  73. *
  74. * @throws AccessDeniedException DOCUMENT ME!
  75. */
  76. public Contact getById(Integer id) {
  77. Contact result = backend.getById(id);
  78. Authentication auth = ((SecureContext) ContextHolder.getContext())
  79. .getAuthentication();
  80. String username = auth.getPrincipal().toString();
  81. if (auth.getPrincipal() instanceof User) {
  82. username = ((User) auth.getPrincipal()).getUsername();
  83. }
  84. if (username.equals(result.getOwner())) {
  85. return result;
  86. } else {
  87. throw new AccessDeniedException(
  88. "The requested id is not owned by the currently logged in user");
  89. }
  90. }
  91. /**
  92. * Public method.
  93. *
  94. * @return DOCUMENT ME!
  95. */
  96. public Integer getNextId() {
  97. return backend.getNextId();
  98. }
  99. /**
  100. * Public method.
  101. *
  102. * @return DOCUMENT ME!
  103. */
  104. public Contact getRandomContact() {
  105. return backend.getRandomContact();
  106. }
  107. public void afterPropertiesSet() throws Exception {
  108. if (backend == null) {
  109. throw new IllegalArgumentException(
  110. "A backend ContactManager implementation is required");
  111. }
  112. }
  113. /**
  114. * Security system will ensure logged in user has ROLE_SUPERVISOR.
  115. *
  116. * @param contact DOCUMENT ME!
  117. */
  118. public void delete(Contact contact) {
  119. backend.delete(contact);
  120. }
  121. /**
  122. * Security system will ensure the owner specified via contact.getOwner()
  123. * equals the currently logged in user.
  124. *
  125. * @param contact DOCUMENT ME!
  126. */
  127. public void save(Contact contact) {
  128. backend.save(contact);
  129. }
  130. }