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