View Javadoc

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