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;
|
|
|
|
|
|
2004-11-15 13:04:54 +00:00
|
|
|
import net.sf.acegisecurity.acl.basic.AclObjectIdentity;
|
|
|
|
|
import net.sf.acegisecurity.acl.basic.BasicAclExtendedDao;
|
|
|
|
|
import net.sf.acegisecurity.acl.basic.NamedEntityObjectIdentity;
|
2004-11-15 03:25:39 +00:00
|
|
|
import net.sf.acegisecurity.acl.basic.SimpleAclEntry;
|
|
|
|
|
import net.sf.acegisecurity.context.ContextHolder;
|
2005-02-21 06:48:31 +00:00
|
|
|
import net.sf.acegisecurity.context.security.SecureContext;
|
2004-11-15 03:25:39 +00:00
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
|
|
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$
|
|
|
|
|
*/
|
2004-11-15 03:25:39 +00:00
|
|
|
public class ContactManagerBackend 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;
|
|
|
|
|
private int counter = 100;
|
2004-03-16 23:57:17 +00:00
|
|
|
|
|
|
|
|
//~ Methods ================================================================
|
|
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public List getAll() {
|
|
|
|
|
return contactDao.findAll();
|
|
|
|
|
}
|
2004-03-16 23:57:17 +00:00
|
|
|
|
2004-11-15 03:25:39 +00:00
|
|
|
public List getAllRecipients() {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-16 23:57:17 +00:00
|
|
|
public Contact getById(Integer 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() {
|
|
|
|
|
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);
|
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 {
|
|
|
|
|
if (contactDao == null) {
|
|
|
|
|
throw new IllegalArgumentException("contactDao required");
|
2004-03-16 23:57:17 +00:00
|
|
|
}
|
2004-11-15 13:04:54 +00:00
|
|
|
|
|
|
|
|
if (basicAclExtendedDao == null) {
|
|
|
|
|
throw new IllegalArgumentException("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
|
|
|
|
|
contact.setId(new Integer(counter++));
|
|
|
|
|
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));
|
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));
|
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);
|
2004-11-15 03:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void update(Contact contact) {
|
|
|
|
|
contactDao.update(contact);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected String getUsername() {
|
|
|
|
|
return ((SecureContext) ContextHolder.getContext()).getAuthentication()
|
|
|
|
|
.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
|
|
|
}
|