ContactManagerBackend.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright 2004, 2005 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.Authentication;
  17. import net.sf.acegisecurity.UserDetails;
  18. import net.sf.acegisecurity.acl.basic.AclObjectIdentity;
  19. import net.sf.acegisecurity.acl.basic.BasicAclExtendedDao;
  20. import net.sf.acegisecurity.acl.basic.NamedEntityObjectIdentity;
  21. import net.sf.acegisecurity.acl.basic.SimpleAclEntry;
  22. import net.sf.acegisecurity.context.security.SecureContextUtils;
  23. import org.springframework.beans.factory.InitializingBean;
  24. import org.springframework.context.support.ApplicationObjectSupport;
  25. import java.util.List;
  26. import java.util.Random;
  27. /**
  28. * Concrete implementation of {@link ContactManager}.
  29. *
  30. * @author Ben Alex
  31. * @version $Id$
  32. */
  33. public class ContactManagerBackend extends ApplicationObjectSupport
  34. implements ContactManager, InitializingBean {
  35. //~ Instance fields ========================================================
  36. private BasicAclExtendedDao basicAclExtendedDao;
  37. private ContactDao contactDao;
  38. private int counter = 100;
  39. //~ Methods ================================================================
  40. public List getAll() {
  41. if (logger.isDebugEnabled()) {
  42. logger.debug("Returning all contacts");
  43. }
  44. return contactDao.findAll();
  45. }
  46. public List getAllRecipients() {
  47. if (logger.isDebugEnabled()) {
  48. logger.debug("Returning all recipients");
  49. }
  50. List list = contactDao.findAllPrincipals();
  51. list.addAll(contactDao.findAllRoles());
  52. return list;
  53. }
  54. public void setBasicAclExtendedDao(BasicAclExtendedDao basicAclExtendedDao) {
  55. this.basicAclExtendedDao = basicAclExtendedDao;
  56. }
  57. public BasicAclExtendedDao getBasicAclExtendedDao() {
  58. return basicAclExtendedDao;
  59. }
  60. public Contact getById(Integer id) {
  61. if (logger.isDebugEnabled()) {
  62. logger.debug("Returning contact with id: " + id);
  63. }
  64. return contactDao.getById(id);
  65. }
  66. public void setContactDao(ContactDao contactDao) {
  67. this.contactDao = contactDao;
  68. }
  69. public ContactDao getContactDao() {
  70. return contactDao;
  71. }
  72. /**
  73. * This is a public method.
  74. *
  75. * @return DOCUMENT ME!
  76. */
  77. public Contact getRandomContact() {
  78. if (logger.isDebugEnabled()) {
  79. logger.debug("Returning random contact");
  80. }
  81. Random rnd = new Random();
  82. List contacts = contactDao.findAll();
  83. int getNumber = rnd.nextInt(contacts.size());
  84. return (Contact) contacts.get(getNumber);
  85. }
  86. public void addPermission(Contact contact, String recipient,
  87. Integer permission) {
  88. SimpleAclEntry simpleAclEntry = new SimpleAclEntry();
  89. simpleAclEntry.setAclObjectIdentity(makeObjectIdentity(contact));
  90. simpleAclEntry.setMask(permission.intValue());
  91. simpleAclEntry.setRecipient(recipient);
  92. basicAclExtendedDao.create(simpleAclEntry);
  93. if (logger.isDebugEnabled()) {
  94. logger.debug("Added permission " + permission + " for recipient "
  95. + recipient + " contact " + contact);
  96. }
  97. }
  98. public void afterPropertiesSet() throws Exception {
  99. if (contactDao == null) {
  100. throw new IllegalArgumentException("contactDao required");
  101. }
  102. if (basicAclExtendedDao == null) {
  103. throw new IllegalArgumentException("basicAclExtendedDao required");
  104. }
  105. }
  106. public void create(Contact contact) {
  107. // Create the Contact itself
  108. contact.setId(new Integer(counter++));
  109. contactDao.create(contact);
  110. // Grant the current principal access to the contact
  111. addPermission(contact, getUsername(),
  112. new Integer(SimpleAclEntry.ADMINISTRATION));
  113. if (logger.isDebugEnabled()) {
  114. logger.debug("Created contact " + contact
  115. + " and granted admin permission to recipient " + getUsername());
  116. }
  117. }
  118. public void delete(Contact contact) {
  119. contactDao.delete(contact.getId());
  120. // Delete the ACL information as well
  121. basicAclExtendedDao.delete(makeObjectIdentity(contact));
  122. if (logger.isDebugEnabled()) {
  123. logger.debug("Deleted contact " + contact
  124. + " including ACL permissions");
  125. }
  126. }
  127. public void deletePermission(Contact contact, String recipient) {
  128. basicAclExtendedDao.delete(makeObjectIdentity(contact), recipient);
  129. if (logger.isDebugEnabled()) {
  130. logger.debug("Deleted contact " + contact
  131. + " ACL permissions for recipient " + recipient);
  132. }
  133. }
  134. public void update(Contact contact) {
  135. contactDao.update(contact);
  136. if (logger.isDebugEnabled()) {
  137. logger.debug("Updated contact " + contact);
  138. }
  139. }
  140. protected String getUsername() {
  141. Authentication auth = SecureContextUtils.getSecureContext()
  142. .getAuthentication();
  143. if (auth.getPrincipal() instanceof UserDetails) {
  144. return ((UserDetails) auth.getPrincipal()).getUsername();
  145. } else {
  146. return auth.getPrincipal().toString();
  147. }
  148. }
  149. private AclObjectIdentity makeObjectIdentity(Contact contact) {
  150. return new NamedEntityObjectIdentity(contact.getClass().getName(),
  151. contact.getId().toString());
  152. }
  153. }