/***********************************************
* Columbia University CS Department
* Network Security Lab
*
* Open and Survivable Embedded Systems (OASES)
*
* Structured Policy Command Language (SPCL)
*
*
* This software is provided as is without any
* warranty or guarantee of merchantibility
* or fitness for a particular purpose. This
* software is released under the terms of the
* GNU General Public License (GPL).
*
* Last updated: April 10, 2003
*
***********************************************/

package spcl.data;

import java.util.*;


/**
 * A container for rules about principals, groups,
 * and objects. The zone has the master list of
 * these, but the policy holds the rules related
 * to each. Keep this in mind when loading and
 * retiring policy (resolve conflicting rules and
 * make sure to delete a p,g,o as necessary)
 * <p>
 * We don't worry about this restriction right now
 * because we only allow one zone & policy to be
 * active at a time.
 * Eventually, the SPCL should become some kind of XML
 * configuration file so that it can be parsed in a
 * standard way.
*/
public class Policy{

	private String name;
	private Principal defaultPrincipal=null;
	private Hashtable principals = new Hashtable();
	private Hashtable groups = new Hashtable();
	private Hashtable objects = new Hashtable();

	public Policy( String name ){
		this.name = name;
	}

	public String getName(){
		return name;
	}

	public void setDefaultPrincipal( Principal def ){
		defaultPrincipal=def;
		principals.put( def.getName(), def );
	}

	public Principal getDefaultPrincipal(){
		return defaultPrincipal;
	}

	/**
	 * add a principal to the hashtable. Note that 
	 * this principal is not checked until the 
	 * resolution phase.
	*/
	public void addPrincipal( Principal p ){
		principals.put( p.getName(), p );
	}
	
	public Principal getPrincipal( String pname ){
	    return ((Principal)principals.get( pname ));
	}
	
	public void addGroup( Group g ){
		groups.put( g.getName(), g );
	}
	
	public void addObject( SystemObject o ){
		objects.put( o.getName(), o );
	}
	
	public SystemObject getObject( String soname ){
		return ((SystemObject)objects.get( soname ));
	}
	
	public boolean hasObject( String objectName ){
		return objects.containsKey( objectName );
	}

	public boolean hasPrincipal( String username ){
		return principals.containsKey( username );
	}
	
	public Enumeration getPrincipals(){
		return principals.elements();
	}

	public Enumeration getObjects(){
		return objects.elements();
	}

	public Enumeration getGroups(){
		return groups.elements();
	}

	public Vector getUserGroups( String username ){
		Group temp = null;
		Vector userGroups = new Vector();
		Enumeration elements = groups.elements();
		while( elements.hasMoreElements() ){
			temp = (Group)elements.nextElement();
			if( temp.isMember( username ) )
				userGroups.addElement( temp );
		}
		return userGroups;
	}


}
