ContactManagerBackend.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.springframework.security.Authentication;
  17. import org.springframework.security.acls.AccessControlEntry;
  18. import org.springframework.security.acls.MutableAcl;
  19. import org.springframework.security.acls.MutableAclService;
  20. import org.springframework.security.acls.NotFoundException;
  21. import org.springframework.security.acls.Permission;
  22. import org.springframework.security.acls.domain.BasePermission;
  23. import org.springframework.security.acls.objectidentity.ObjectIdentity;
  24. import org.springframework.security.acls.objectidentity.ObjectIdentityImpl;
  25. import org.springframework.security.acls.sid.PrincipalSid;
  26. import org.springframework.security.acls.sid.Sid;
  27. import org.springframework.security.context.SecurityContextHolder;
  28. import org.springframework.security.userdetails.UserDetails;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.beans.factory.InitializingBean;
  31. import org.springframework.context.support.ApplicationObjectSupport;
  32. import org.springframework.util.Assert;
  33. import java.util.List;
  34. import java.util.Random;
  35. /**
  36. * Concrete implementation of {@link ContactManager}.
  37. *
  38. * @author Ben Alex
  39. * @version $Id$
  40. */
  41. @Transactional
  42. public class ContactManagerBackend extends ApplicationObjectSupport implements ContactManager, InitializingBean {
  43. //~ Instance fields ================================================================================================
  44. private ContactDao contactDao;
  45. private MutableAclService mutableAclService;
  46. private int counter = 1000;
  47. //~ Methods ========================================================================================================
  48. public void addPermission(Contact contact, Sid recipient, Permission permission) {
  49. MutableAcl acl;
  50. ObjectIdentity oid = new ObjectIdentityImpl(Contact.class, contact.getId());
  51. try {
  52. acl = (MutableAcl) mutableAclService.readAclById(oid);
  53. } catch (NotFoundException nfe) {
  54. acl = mutableAclService.createAcl(oid);
  55. }
  56. acl.insertAce(acl.getEntries().length, permission, recipient, true);
  57. mutableAclService.updateAcl(acl);
  58. if (logger.isDebugEnabled()) {
  59. logger.debug("Added permission " + permission + " for Sid " + recipient + " contact " + contact);
  60. }
  61. }
  62. public void afterPropertiesSet() throws Exception {
  63. Assert.notNull(contactDao, "contactDao required");
  64. Assert.notNull(mutableAclService, "mutableAclService required");
  65. }
  66. public void create(Contact contact) {
  67. // Create the Contact itself
  68. contact.setId(new Long(counter++));
  69. contactDao.create(contact);
  70. // Grant the current principal administrative permission to the contact
  71. addPermission(contact, new PrincipalSid(getUsername()), BasePermission.ADMINISTRATION);
  72. if (logger.isDebugEnabled()) {
  73. logger.debug("Created contact " + contact + " and granted admin permission to recipient " + getUsername());
  74. }
  75. }
  76. public void delete(Contact contact) {
  77. contactDao.delete(contact.getId());
  78. // Delete the ACL information as well
  79. ObjectIdentity oid = new ObjectIdentityImpl(Contact.class, contact.getId());
  80. mutableAclService.deleteAcl(oid, false);
  81. if (logger.isDebugEnabled()) {
  82. logger.debug("Deleted contact " + contact + " including ACL permissions");
  83. }
  84. }
  85. public void deletePermission(Contact contact, Sid recipient, Permission permission) {
  86. ObjectIdentity oid = new ObjectIdentityImpl(Contact.class, contact.getId());
  87. MutableAcl acl = (MutableAcl) mutableAclService.readAclById(oid);
  88. // Remove all permissions associated with this particular recipient (string equality to KISS)
  89. AccessControlEntry[] entries = acl.getEntries();
  90. for (int i = 0; i < entries.length; i++) {
  91. if (entries[i].getSid().equals(recipient) && entries[i].getPermission().equals(permission)) {
  92. acl.deleteAce(i);
  93. }
  94. }
  95. mutableAclService.updateAcl(acl);
  96. if (logger.isDebugEnabled()) {
  97. logger.debug("Deleted contact " + contact + " ACL permissions for recipient " + recipient);
  98. }
  99. }
  100. @Transactional(readOnly=true)
  101. public List getAll() {
  102. if (logger.isDebugEnabled()) {
  103. logger.debug("Returning all contacts");
  104. }
  105. return contactDao.findAll();
  106. }
  107. @Transactional(readOnly=true)
  108. public List getAllRecipients() {
  109. if (logger.isDebugEnabled()) {
  110. logger.debug("Returning all recipients");
  111. }
  112. List list = contactDao.findAllPrincipals();
  113. return list;
  114. }
  115. @Transactional(readOnly=true)
  116. public Contact getById(Long id) {
  117. if (logger.isDebugEnabled()) {
  118. logger.debug("Returning contact with id: " + id);
  119. }
  120. return contactDao.getById(id);
  121. }
  122. /**
  123. * This is a public method.
  124. */
  125. @Transactional(readOnly=true)
  126. public Contact getRandomContact() {
  127. if (logger.isDebugEnabled()) {
  128. logger.debug("Returning random contact");
  129. }
  130. Random rnd = new Random();
  131. List contacts = contactDao.findAll();
  132. int getNumber = rnd.nextInt(contacts.size());
  133. return (Contact) contacts.get(getNumber);
  134. }
  135. protected String getUsername() {
  136. Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  137. if (auth.getPrincipal() instanceof UserDetails) {
  138. return ((UserDetails) auth.getPrincipal()).getUsername();
  139. } else {
  140. return auth.getPrincipal().toString();
  141. }
  142. }
  143. public void setContactDao(ContactDao contactDao) {
  144. this.contactDao = contactDao;
  145. }
  146. public void setMutableAclService(MutableAclService mutableAclService) {
  147. this.mutableAclService = mutableAclService;
  148. }
  149. public void update(Contact contact) {
  150. contactDao.update(contact);
  151. if (logger.isDebugEnabled()) {
  152. logger.debug("Updated contact " + contact);
  153. }
  154. }
  155. }