2005-02-21 06:48:31 +00:00
|
|
|
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
2004-03-16 23:57:17 +00:00
|
|
|
*
|
2004-03-23 04:44:48 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
2004-03-16 23:57:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package sample.contact;
|
|
|
|
|
|
2005-11-17 00:56:49 +00:00
|
|
|
import org.acegisecurity.Authentication;
|
|
|
|
|
import org.acegisecurity.acl.basic.AclObjectIdentity;
|
|
|
|
|
import org.acegisecurity.acl.basic.BasicAclExtendedDao;
|
|
|
|
|
import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
|
|
|
|
|
import org.acegisecurity.acl.basic.SimpleAclEntry;
|
|
|
|
|
import org.acegisecurity.context.SecurityContextHolder;
|
2005-11-29 13:10:15 +00:00
|
|
|
import org.acegisecurity.userdetails.UserDetails;
|
2004-11-15 03:25:39 +00:00
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
|
|
2005-03-22 11:17:22 +00:00
|
|
|
import org.springframework.context.support.ApplicationObjectSupport;
|
2005-05-07 09:11:37 +00:00
|
|
|
|
2005-04-15 01:21:41 +00:00
|
|
|
import org.springframework.util.Assert;
|
2005-03-22 11:17:22 +00:00
|
|
|
|
2004-03-16 23:57:17 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-11-15 03:25:39 +00:00
|
|
|
* Concrete implementation of {@link ContactManager}.
|
2004-03-16 23:57:17 +00:00
|
|
|
*
|
|
|
|
|
* @author Ben Alex
|
|
|
|
|
* @version $Id$
|
|
|
|
|
*/
|
2005-03-22 11:17:22 +00:00
|
|
|
public class ContactManagerBackend extends ApplicationObjectSupport
|
|
|
|
|
implements ContactManager, InitializingBean {
|
2004-03-16 23:57:17 +00:00
|
|
|
//~ Instance fields ========================================================
|
|
|
|
|
|
2004-11-15 13:04:54 +00:00
|
|
|
private BasicAclExtendedDao basicAclExtendedDao;
|
2004-11-15 03:25:39 +00:00
|
|
|
private ContactDao contactDao;
|
2005-11-04 04:15:57 +00:00
|
|
|
private int counter = 1000;
|
2004-03-16 23:57:17 +00:00
|
|
|
|
|
|
|
|
//~ Methods ================================================================
|
|
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public List getAll() {
|
2005-03-22 11:17:22 +00:00
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Returning all contacts");
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
return contactDao.findAll();
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public List getAllRecipients() {
|
2005-03-22 11:17:22 +00:00
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Returning all recipients");
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
List list = contactDao.findAllPrincipals();
|
|
|
|
|
list.addAll(contactDao.findAllRoles());
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
return list;
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-15 13:04:54 +00:00
|
|
|
public void setBasicAclExtendedDao(BasicAclExtendedDao basicAclExtendedDao) {
|
|
|
|
|
this.basicAclExtendedDao = basicAclExtendedDao;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BasicAclExtendedDao getBasicAclExtendedDao() {
|
|
|
|
|
return basicAclExtendedDao;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-04 04:15:57 +00:00
|
|
|
public Contact getById(Long id) {
|
2005-03-22 11:17:22 +00:00
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Returning contact with id: " + id);
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
return contactDao.getById(id);
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public void setContactDao(ContactDao contactDao) {
|
|
|
|
|
this.contactDao = contactDao;
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public ContactDao getContactDao() {
|
|
|
|
|
return contactDao;
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2004-11-15 03:25:39 +00:00
|
|
|
* This is a public method.
|
2004-03-16 23:57:17 +00:00
|
|
|
*
|
|
|
|
|
* @return DOCUMENT ME!
|
|
|
|
|
*/
|
|
|
|
|
public Contact getRandomContact() {
|
2005-03-22 11:17:22 +00:00
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Returning random contact");
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-16 23:57:17 +00:00
|
|
|
Random rnd = new Random();
|
2004-11-15 03:25:39 +00:00
|
|
|
List contacts = contactDao.findAll();
|
|
|
|
|
int getNumber = rnd.nextInt(contacts.size());
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
return (Contact) contacts.get(getNumber);
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public void addPermission(Contact contact, String recipient,
|
|
|
|
|
Integer permission) {
|
2004-11-15 13:04:54 +00:00
|
|
|
SimpleAclEntry simpleAclEntry = new SimpleAclEntry();
|
|
|
|
|
simpleAclEntry.setAclObjectIdentity(makeObjectIdentity(contact));
|
|
|
|
|
simpleAclEntry.setMask(permission.intValue());
|
|
|
|
|
simpleAclEntry.setRecipient(recipient);
|
|
|
|
|
basicAclExtendedDao.create(simpleAclEntry);
|
2005-03-22 11:17:22 +00:00
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Added permission " + permission + " for recipient "
|
|
|
|
|
+ recipient + " contact " + contact);
|
|
|
|
|
}
|
2004-11-15 03:25:39 +00:00
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public void afterPropertiesSet() throws Exception {
|
2005-04-15 01:21:41 +00:00
|
|
|
Assert.notNull(contactDao, "contactDao required");
|
|
|
|
|
Assert.notNull(basicAclExtendedDao, "basicAclExtendedDao required");
|
2004-11-15 03:25:39 +00:00
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public void create(Contact contact) {
|
|
|
|
|
// Create the Contact itself
|
2005-11-04 04:15:57 +00:00
|
|
|
contact.setId(new Long(counter++));
|
2004-11-15 03:25:39 +00:00
|
|
|
contactDao.create(contact);
|
|
|
|
|
|
|
|
|
|
// Grant the current principal access to the contact
|
2004-11-15 13:04:54 +00:00
|
|
|
addPermission(contact, getUsername(),
|
|
|
|
|
new Integer(SimpleAclEntry.ADMINISTRATION));
|
2005-03-22 11:17:22 +00:00
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Created contact " + contact
|
|
|
|
|
+ " and granted admin permission to recipient " + getUsername());
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delete(Contact contact) {
|
2004-11-15 03:25:39 +00:00
|
|
|
contactDao.delete(contact.getId());
|
2004-11-15 13:04:54 +00:00
|
|
|
|
|
|
|
|
// Delete the ACL information as well
|
|
|
|
|
basicAclExtendedDao.delete(makeObjectIdentity(contact));
|
2005-03-22 11:17:22 +00:00
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Deleted contact " + contact
|
|
|
|
|
+ " including ACL permissions");
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public void deletePermission(Contact contact, String recipient) {
|
2004-11-15 13:04:54 +00:00
|
|
|
basicAclExtendedDao.delete(makeObjectIdentity(contact), recipient);
|
2005-03-22 11:17:22 +00:00
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Deleted contact " + contact
|
|
|
|
|
+ " ACL permissions for recipient " + recipient);
|
|
|
|
|
}
|
2004-11-15 03:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void update(Contact contact) {
|
|
|
|
|
contactDao.update(contact);
|
2005-03-22 11:17:22 +00:00
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("Updated contact " + contact);
|
|
|
|
|
}
|
2004-11-15 03:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected String getUsername() {
|
2005-05-08 23:42:14 +00:00
|
|
|
Authentication auth = SecurityContextHolder.getContext()
|
|
|
|
|
.getAuthentication();
|
2005-03-22 11:17:22 +00:00
|
|
|
|
|
|
|
|
if (auth.getPrincipal() instanceof UserDetails) {
|
|
|
|
|
return ((UserDetails) auth.getPrincipal()).getUsername();
|
|
|
|
|
} else {
|
|
|
|
|
return auth.getPrincipal().toString();
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|
2004-11-15 13:04:54 +00:00
|
|
|
|
|
|
|
|
private AclObjectIdentity makeObjectIdentity(Contact contact) {
|
|
|
|
|
return new NamedEntityObjectIdentity(contact.getClass().getName(),
|
|
|
|
|
contact.getId().toString());
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|