001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package sample.contact;
016:
017: import org.acegisecurity.Authentication;
018:
019: import org.acegisecurity.acls.AccessControlEntry;
020: import org.acegisecurity.acls.MutableAcl;
021: import org.acegisecurity.acls.MutableAclService;
022: import org.acegisecurity.acls.NotFoundException;
023: import org.acegisecurity.acls.Permission;
024: import org.acegisecurity.acls.domain.BasePermission;
025: import org.acegisecurity.acls.objectidentity.ObjectIdentity;
026: import org.acegisecurity.acls.objectidentity.ObjectIdentityImpl;
027: import org.acegisecurity.acls.sid.PrincipalSid;
028: import org.acegisecurity.acls.sid.Sid;
029:
030: import org.acegisecurity.context.SecurityContextHolder;
031:
032: import org.acegisecurity.userdetails.UserDetails;
033:
034: import org.springframework.beans.factory.InitializingBean;
035:
036: import org.springframework.context.support.ApplicationObjectSupport;
037:
038: import org.springframework.util.Assert;
039:
040: import java.util.List;
041: import java.util.Random;
042:
043: /**
044: * Concrete implementation of {@link ContactManager}.
045: *
046: * @author Ben Alex
047: * @version $Id: ContactManagerBackend.java 1754 2006-11-17 02:01:21Z benalex $
048: */
049: public class ContactManagerBackend extends ApplicationObjectSupport
050: implements ContactManager, InitializingBean {
051: //~ Instance fields ================================================================================================
052:
053: private ContactDao contactDao;
054: private MutableAclService mutableAclService;
055: private int counter = 1000;
056:
057: //~ Methods ========================================================================================================
058:
059: public void addPermission(Contact contact, Sid recipient,
060: Permission permission) {
061: MutableAcl acl;
062: ObjectIdentity oid = new ObjectIdentityImpl(Contact.class,
063: contact.getId());
064:
065: try {
066: acl = (MutableAcl) mutableAclService.readAclById(oid);
067: } catch (NotFoundException nfe) {
068: acl = mutableAclService.createAcl(oid);
069: }
070:
071: acl.insertAce(null, permission, recipient, true);
072: mutableAclService.updateAcl(acl);
073:
074: if (logger.isDebugEnabled()) {
075: logger.debug("Added permission " + permission + " for Sid "
076: + recipient + " contact " + contact);
077: }
078: }
079:
080: public void afterPropertiesSet() throws Exception {
081: Assert.notNull(contactDao, "contactDao required");
082: Assert.notNull(mutableAclService, "mutableAclService required");
083: }
084:
085: public void create(Contact contact) {
086: // Create the Contact itself
087: contact.setId(new Long(counter++));
088: contactDao.create(contact);
089:
090: // Grant the current principal administrative permission to the contact
091: addPermission(contact, new PrincipalSid(getUsername()),
092: BasePermission.ADMINISTRATION);
093:
094: if (logger.isDebugEnabled()) {
095: logger.debug("Created contact " + contact
096: + " and granted admin permission to recipient "
097: + getUsername());
098: }
099: }
100:
101: public void delete(Contact contact) {
102: contactDao.delete(contact.getId());
103:
104: // Delete the ACL information as well
105: ObjectIdentity oid = new ObjectIdentityImpl(Contact.class,
106: contact.getId());
107: mutableAclService.deleteAcl(oid, false);
108:
109: if (logger.isDebugEnabled()) {
110: logger.debug("Deleted contact " + contact
111: + " including ACL permissions");
112: }
113: }
114:
115: public void deletePermission(Contact contact, Sid recipient,
116: Permission permission) {
117: ObjectIdentity oid = new ObjectIdentityImpl(Contact.class,
118: contact.getId());
119: MutableAcl acl = (MutableAcl) mutableAclService
120: .readAclById(oid);
121:
122: // Remove all permissions associated with this particular recipient (string equality to KISS)
123: AccessControlEntry[] entries = acl.getEntries();
124:
125: for (int i = 0; i < entries.length; i++) {
126: if (entries[i].getSid().equals(recipient)
127: && entries[i].getPermission().equals(permission)) {
128: acl.deleteAce(entries[i].getId());
129: }
130: }
131:
132: mutableAclService.updateAcl(acl);
133:
134: if (logger.isDebugEnabled()) {
135: logger.debug("Deleted contact " + contact
136: + " ACL permissions for recipient " + recipient);
137: }
138: }
139:
140: public List getAll() {
141: if (logger.isDebugEnabled()) {
142: logger.debug("Returning all contacts");
143: }
144:
145: return contactDao.findAll();
146: }
147:
148: public List getAllRecipients() {
149: if (logger.isDebugEnabled()) {
150: logger.debug("Returning all recipients");
151: }
152:
153: List list = contactDao.findAllPrincipals();
154:
155: return list;
156: }
157:
158: public Contact getById(Long id) {
159: if (logger.isDebugEnabled()) {
160: logger.debug("Returning contact with id: " + id);
161: }
162:
163: return contactDao.getById(id);
164: }
165:
166: /**
167: * This is a public method.
168: *
169: * @return DOCUMENT ME!
170: */
171: public Contact getRandomContact() {
172: if (logger.isDebugEnabled()) {
173: logger.debug("Returning random contact");
174: }
175:
176: Random rnd = new Random();
177: List contacts = contactDao.findAll();
178: int getNumber = rnd.nextInt(contacts.size());
179:
180: return (Contact) contacts.get(getNumber);
181: }
182:
183: protected String getUsername() {
184: Authentication auth = SecurityContextHolder.getContext()
185: .getAuthentication();
186:
187: if (auth.getPrincipal() instanceof UserDetails) {
188: return ((UserDetails) auth.getPrincipal()).getUsername();
189: } else {
190: return auth.getPrincipal().toString();
191: }
192: }
193:
194: public void setContactDao(ContactDao contactDao) {
195: this .contactDao = contactDao;
196: }
197:
198: public void setMutableAclService(MutableAclService mutableAclService) {
199: this .mutableAclService = mutableAclService;
200: }
201:
202: public void update(Contact contact) {
203: contactDao.update(contact);
204:
205: if (logger.isDebugEnabled()) {
206: logger.debug("Updated contact " + contact);
207: }
208: }
209: }
|