mirror of
https://github.com/spring-projects/spring-security.git
synced 2026-04-21 16:30:27 +00:00
169 lines
4.7 KiB
Java
169 lines
4.7 KiB
Java
|
|
/*
|
||
|
|
* The Acegi Security System for Spring is published under the terms
|
||
|
|
* of the Apache Software License.
|
||
|
|
*
|
||
|
|
* Visit http://acegisecurity.sourceforge.net for further details.
|
||
|
|
*/
|
||
|
|
|
||
|
|
package sample.contact;
|
||
|
|
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.Iterator;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.Random;
|
||
|
|
import java.util.Vector;
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Backend business object that manages the contacts.
|
||
|
|
*
|
||
|
|
* <P>
|
||
|
|
* As a backend, it never faces the public callers. It is always accessed via
|
||
|
|
* the {@link ContactManagerFacade}.
|
||
|
|
* </p>
|
||
|
|
*
|
||
|
|
* <P>
|
||
|
|
* This facade approach is not really necessary in this application, and is
|
||
|
|
* done simply to demonstrate granting additional authorities via the
|
||
|
|
* <code>RunAsManager</code>.
|
||
|
|
* </p>
|
||
|
|
*
|
||
|
|
* @author Ben Alex
|
||
|
|
* @version $Id$
|
||
|
|
*/
|
||
|
|
public class ContactManagerBackend implements ContactManager {
|
||
|
|
//~ Instance fields ========================================================
|
||
|
|
|
||
|
|
private Map contacts;
|
||
|
|
|
||
|
|
//~ Constructors ===========================================================
|
||
|
|
|
||
|
|
public ContactManagerBackend() {
|
||
|
|
this.contacts = new HashMap();
|
||
|
|
save(new Contact(this.getNextId(), "John Smith", "john@somewhere.com",
|
||
|
|
"marissa"));
|
||
|
|
save(new Contact(this.getNextId(), "Michael Citizen",
|
||
|
|
"michael@xyz.com", "marissa"));
|
||
|
|
save(new Contact(this.getNextId(), "Joe Bloggs", "joe@demo.com",
|
||
|
|
"marissa"));
|
||
|
|
save(new Contact(this.getNextId(), "Karen Sutherland",
|
||
|
|
"karen@sutherland.com", "dianne"));
|
||
|
|
save(new Contact(this.getNextId(), "Mitchell Howard",
|
||
|
|
"mitchell@abcdef.com", "dianne"));
|
||
|
|
save(new Contact(this.getNextId(), "Rose Costas", "rose@xyz.com",
|
||
|
|
"scott"));
|
||
|
|
save(new Contact(this.getNextId(), "Amanda Smith", "amanda@abcdef.com",
|
||
|
|
"scott"));
|
||
|
|
}
|
||
|
|
|
||
|
|
//~ Methods ================================================================
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Security system expects ROLE_RUN_AS_SERVER
|
||
|
|
*
|
||
|
|
* @param owner DOCUMENT ME!
|
||
|
|
*
|
||
|
|
* @return DOCUMENT ME!
|
||
|
|
*/
|
||
|
|
public Contact[] getAllByOwner(String owner) {
|
||
|
|
List list = new Vector();
|
||
|
|
Iterator iter = this.contacts.keySet().iterator();
|
||
|
|
|
||
|
|
while (iter.hasNext()) {
|
||
|
|
Integer contactId = (Integer) iter.next();
|
||
|
|
Contact contact = (Contact) this.contacts.get(contactId);
|
||
|
|
|
||
|
|
if (contact.getOwner().equals(owner)) {
|
||
|
|
list.add(contact);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Contact[] resultType = {new Contact(new Integer(1), "holder", "holder",
|
||
|
|
"holder")};
|
||
|
|
|
||
|
|
if (list.size() == 0) {
|
||
|
|
return null;
|
||
|
|
} else {
|
||
|
|
return (Contact[]) list.toArray(resultType);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Security system expects ROLE_RUN_AS_SERVER
|
||
|
|
*
|
||
|
|
* @param id DOCUMENT ME!
|
||
|
|
*
|
||
|
|
* @return DOCUMENT ME!
|
||
|
|
*/
|
||
|
|
public Contact getById(Integer id) {
|
||
|
|
return (Contact) this.contacts.get(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Public method
|
||
|
|
*
|
||
|
|
* @return DOCUMENT ME!
|
||
|
|
*/
|
||
|
|
public Integer getNextId() {
|
||
|
|
int max = 0;
|
||
|
|
Iterator iter = this.contacts.keySet().iterator();
|
||
|
|
|
||
|
|
while (iter.hasNext()) {
|
||
|
|
Integer id = (Integer) iter.next();
|
||
|
|
|
||
|
|
if (id.intValue() > max) {
|
||
|
|
max = id.intValue();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return new Integer(max + 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This is a public method, meaning a client could call this method
|
||
|
|
* directly (ie not via a facade). If this was an issue, the public method
|
||
|
|
* on the facade should not be public but secure. Quite possibly an
|
||
|
|
* AnonymousAuthenticationToken and associated provider could be used on a
|
||
|
|
* secure method, thus allowing a RunAsManager to protect the backend.
|
||
|
|
*
|
||
|
|
* @return DOCUMENT ME!
|
||
|
|
*/
|
||
|
|
public Contact getRandomContact() {
|
||
|
|
Random rnd = new Random();
|
||
|
|
int getNumber = rnd.nextInt(this.contacts.size()) + 1;
|
||
|
|
Iterator iter = this.contacts.keySet().iterator();
|
||
|
|
int i = 0;
|
||
|
|
|
||
|
|
while (iter.hasNext()) {
|
||
|
|
i++;
|
||
|
|
|
||
|
|
Integer id = (Integer) iter.next();
|
||
|
|
|
||
|
|
if (i == getNumber) {
|
||
|
|
return (Contact) this.contacts.get(id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Security system expects ROLE_RUN_AS_SERVER
|
||
|
|
*
|
||
|
|
* @param contact DOCUMENT ME!
|
||
|
|
*/
|
||
|
|
public void delete(Contact contact) {
|
||
|
|
this.contacts.remove(contact.getId());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Security system expects ROLE_RUN_AS_SERVER
|
||
|
|
*
|
||
|
|
* @param contact DOCUMENT ME!
|
||
|
|
*/
|
||
|
|
public void save(Contact contact) {
|
||
|
|
this.contacts.put(contact.getId(), contact);
|
||
|
|
}
|
||
|
|
}
|