View Javadoc

1   /*
2    * UserGroup.java
3    *
4    * Created on May 24, 2004, 2:37 PM
5    */
6   
7   package gov.bnl.gums.userGroup;
8   
9   import gov.bnl.gums.GridUser;
10  import gov.bnl.gums.configuration.Configuration;
11  
12  import java.lang.ref.SoftReference;
13  import java.util.*;
14  
15  /** 
16   * An interface that defines a group of people, which GUMS will associate to a 
17   * mapping policy. An implementation could take/manage a list of users in
18   * any way it wanted, or it could combine different groups.
19   *
20   * @author Gabriele Carcassi, Jay Packard
21   */
22  public abstract class UserGroup {
23      /**
24       * @return user friendly string representation of the property type called statically 
25       */
26      static public String getTypeStatic() {
27  		return "abstract";
28  	}
29  	
30  	private String name = "";
31  	private String description = "";
32  	private SoftReference configurationRef = null;
33  	protected String[] accessTypes = {"write", "read all", "read self"};
34  	protected int accessIndex = 2;
35  	
36  	/**
37  	 * Create a new user group.  This empty constructor is needed by the XML Digestor.
38  	 */
39  	public UserGroup() {
40  	}
41  	
42  	/**
43  	 * Create a new user group with a configuration.
44  	 * 
45  	 * @param configuration
46  	 * @param name
47  	 */
48  	public UserGroup(Configuration configuration) {
49  		this.configurationRef = new SoftReference(configuration);
50  	}
51  
52  	/**
53  	 * Create a new user group with a configuration and a name.
54  	 * 
55  	 * @param configuration
56  	 * @param name
57  	 */
58  	public UserGroup(Configuration configuration, String name) {
59  		this.configurationRef = new SoftReference(configuration);
60  		this.name = name;
61  	}
62  	
63  	/**
64  	 * Create a clone of itself
65  	 * 
66  	 * @param configuration
67  	 * @return
68  	 */
69  	public abstract UserGroup clone(Configuration configuration);
70  
71  	/**
72  	 * Getter for property access, that determines what a member of this
73  	 * user group has access to in GUMS.
74  	 * 
75  	 * @return access as string
76  	 */
77  	public String getAccess() {
78      	return accessTypes[accessIndex];
79      }
80  
81  	/**
82  	 * Getter for property configuration.
83  	 * 
84  	 * @return Configuration object
85  	 */
86  	public Configuration getConfiguration() {
87  		if (configurationRef==null)
88  			return null;
89  		return (Configuration)configurationRef.get();
90  	}
91  	
92  	/**
93  	 * Getter for property description.
94  	 * 
95  	 * @return Description as string
96  	 */
97  	public String getDescription() {
98  		return description;
99  	}
100 	
101 	/**
102      * Returns the list of user identities that are part of the group.
103      * <p>
104      * Some UserGroups, however, could be defined by a rule that doesn't
105      * allow listing. For example, a group could be 'all the users
106      * with a DOEGrids certificate'. Though one could argue whether or
107      * not is a good idea to have such a group, one can implement one
108      * and throw an UnsupportedOperationException. This will make it
109      * impossible for GUMS to create a grid-mapfile, but would still
110      * allow direct user to account mapping through a call-out.
111      * @return a List of GridUser objects representing the user certificate DN.
112      */
113     public abstract List getMemberList();
114 
115 	/**
116 	 * Getter for property name.
117 	 * 
118 	 * @return name as string
119 	 */
120 	public String getName() {
121 		return name;
122 	}
123 	
124 	/**
125 	 * Getter for property type.
126 	 * 
127 	 * @return type as string
128 	 */
129     public String getType() {
130 		return "abstract";
131 	}
132     
133     /**
134      * @return true if this group allows at least read all access
135      */
136     public boolean hasReadAllAccess() {
137     	return (accessIndex<=1);
138     }
139     
140     /**
141      * @return true if this group allows at least read self access
142      */
143     public boolean hasReadSelfAccess() {
144     	return (accessIndex<=2);
145     }
146     
147     /**
148      * @return true if this group allows write access (admin privileges)
149      */
150     public boolean hasWriteAccess() {
151     	return (accessIndex==0);
152     }
153 	
154     /**
155      * Determines whether the given user identity is part of the group.
156      * @param userDN the certificate DN.
157      * @return true if it's in the group
158      */
159     public abstract boolean isInGroup(GridUser user);
160     
161     /**
162      * Setter for property access
163      * 
164      * @param access
165      */
166     public void setAccess(String access) {
167     	for(int i=0; i<accessTypes.length; i++) {
168     		if ( accessTypes[i].equalsIgnoreCase(access) ) {
169     			accessIndex = i;
170     			return;
171     		}
172     	}
173     	throw new RuntimeException("Invalid access type: "+access);
174     }
175     
176     /**
177      * Setter for property configuration.
178      * 
179      * @param configuration
180      */
181     public void setConfiguration(Configuration configuration) {
182 		this.configurationRef = new SoftReference(configuration);
183 	}
184     
185     /**
186      * Setter for property description.
187      * 
188      * @param description
189      */
190     public void setDescription(String description) {
191     	this.description = description;
192     }
193     
194     /**
195      * Setter for property name.
196      * 
197      * @param name
198      */
199     public void setName(String name) {
200 		this.name = name;
201 	}
202     
203     /**
204      * Get string representation of this object for displaying in the 
205      * diagnostic summary web page
206      * 
207      * @param bgColor back ground color
208      * @return
209      */
210     public abstract String toString(String bgColor);
211 
212 	/**
213 	 * Create a clone of itself
214 	 * 
215 	 * @param configuration
216 	 * @return
217 	 */
218     public abstract String toXML();
219     
220     /** 
221      * Updates the local list of the users from the source of the group.
222      * <p>
223      * Most user groups will get the information from a separate database
224      * accessible via WAN. For that reason, the user group will maintain a
225      * local cache with the list of members, which can be updated through
226      * this method.
227      */
228     public abstract void updateMembers();
229 }