spring-security/samples/contacts/src/main/java/sample/contact/ContactManagerBackend.java

144 lines
4.4 KiB
Java
Raw Normal View History

/* 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;
import net.sf.acegisecurity.acl.basic.AclObjectIdentity;
import net.sf.acegisecurity.acl.basic.BasicAclExtendedDao;
import net.sf.acegisecurity.acl.basic.NamedEntityObjectIdentity;
import net.sf.acegisecurity.acl.basic.SimpleAclEntry;
import net.sf.acegisecurity.context.ContextHolder;
import net.sf.acegisecurity.context.security.SecureContext;
import org.springframework.beans.factory.InitializingBean;
2004-03-16 23:57:17 +00:00
import java.util.List;
import java.util.Random;
/**
* Concrete implementation of {@link ContactManager}.
2004-03-16 23:57:17 +00:00
*
* @author Ben Alex
* @version $Id$
*/
public class ContactManagerBackend implements ContactManager, InitializingBean {
2004-03-16 23:57:17 +00:00
//~ Instance fields ========================================================
private BasicAclExtendedDao basicAclExtendedDao;
private ContactDao contactDao;
private int counter = 100;
2004-03-16 23:57:17 +00:00
//~ Methods ================================================================
public List getAll() {
return contactDao.findAll();
}
2004-03-16 23:57:17 +00:00
public List getAllRecipients() {
List list = contactDao.findAllPrincipals();
list.addAll(contactDao.findAllRoles());
2004-03-16 23:57:17 +00:00
return list;
2004-03-16 23:57:17 +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) {
return contactDao.getById(id);
2004-03-16 23:57:17 +00:00
}
public void setContactDao(ContactDao contactDao) {
this.contactDao = contactDao;
}
2004-03-16 23:57:17 +00:00
public ContactDao getContactDao() {
return contactDao;
2004-03-16 23:57:17 +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();
List contacts = contactDao.findAll();
int getNumber = rnd.nextInt(contacts.size());
2004-03-16 23:57:17 +00:00
return (Contact) contacts.get(getNumber);
}
2004-03-16 23:57:17 +00:00
public void addPermission(Contact contact, String recipient,
Integer permission) {
SimpleAclEntry simpleAclEntry = new SimpleAclEntry();
simpleAclEntry.setAclObjectIdentity(makeObjectIdentity(contact));
simpleAclEntry.setMask(permission.intValue());
simpleAclEntry.setRecipient(recipient);
basicAclExtendedDao.create(simpleAclEntry);
}
2004-03-16 23:57:17 +00:00
public void afterPropertiesSet() throws Exception {
if (contactDao == null) {
throw new IllegalArgumentException("contactDao required");
2004-03-16 23:57:17 +00:00
}
if (basicAclExtendedDao == null) {
throw new IllegalArgumentException("basicAclExtendedDao required");
}
}
2004-03-16 23:57:17 +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
addPermission(contact, getUsername(),
new Integer(SimpleAclEntry.ADMINISTRATION));
2004-03-16 23:57:17 +00:00
}
public void delete(Contact contact) {
contactDao.delete(contact.getId());
// Delete the ACL information as well
basicAclExtendedDao.delete(makeObjectIdentity(contact));
2004-03-16 23:57:17 +00:00
}
public void deletePermission(Contact contact, String recipient) {
basicAclExtendedDao.delete(makeObjectIdentity(contact), recipient);
}
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
}
private AclObjectIdentity makeObjectIdentity(Contact contact) {
return new NamedEntityObjectIdentity(contact.getClass().getName(),
contact.getId().toString());
}
2004-03-16 23:57:17 +00:00
}