View Javadoc

1   /*
2    * GridUser.java
3    *
4    * Created on August 31, 2004, 4:00 PM
5    */
6   
7   package gov.bnl.gums;
8   
9   import gov.bnl.gums.userGroup.UserGroup;
10  
11  import java.lang.ref.SoftReference;
12  
13  import org.apache.log4j.Logger;
14  import javax.persistence.*;
15  
16  /** 
17   * Represent a GRID Identity in GUMS, which is a certificate with its DN and FQAN.
18   *
19   * @author  Gabriele Carcassi, Jay Packard
20   */
21  @Entity
22  public class GridUser {
23      static protected Logger log = Logger.getLogger(GridUser.class);
24  
25      protected FQAN fqanObj;
26      
27      // persistent fields
28  	protected long id;
29  	protected String dn;
30  	protected String email;
31  	protected SoftReference<UserGroup> userGroupRef = null;
32      
33      /**
34       * Creates a GRID credentail with no DN and FQAN.
35       */
36      public GridUser() {
37      }
38      
39      /**
40       * Creates a GRID credential with DN.
41       */
42      public GridUser(String userDN) {
43      	this(userDN, null, null, false);
44      }
45      
46      /**
47       * Creates a GRID credential with DN and FQAN.
48       */
49      public GridUser(String userDN, String fqan) {
50          this(userDN, fqan, null, true);
51      }
52      
53      /**
54       * Creates a GRID credential with DN and FQAN.
55       */
56      public GridUser(String userDN, String fqan, boolean enforceFqanWellFormedness) {
57          this(userDN, fqan, null, enforceFqanWellFormedness);
58      }
59   
60      /**
61       * Creates a new object representing a Grid credential.
62       * 
63       * @param userDN the DN of the user certificate (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
64       * @param fqan The Fully Qualified Attribute name (i.e. "/atlas/production/Role=Leader")
65       * @param email
66       */
67      public GridUser(String userDN, String fqan, String email) {
68      	this(userDN, fqan, email, true);
69      }
70      
71      /**
72       * Creates a new object representing a Grid credential.
73       * 
74       * @param userDN the DN of the user certificate (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
75       * @param fqan The Fully Qualified Attribute name (i.e. "/atlas/production/Role=Leader")
76       * @param email
77       */
78      public GridUser(String userDN, String fqan, String email, boolean enforceFqanWellFormedness) {
79          setDn(userDN);
80          if (fqan!=null && fqan.length()>0)
81          	setFqanObj(new FQAN(fqan, enforceFqanWellFormedness));
82         	setEmail(email);
83      }
84  
85    /**
86       * @param user
87       * @return true if user DN element matches
88       */
89      public int compareDn(GridUser user) {
90  		if (this.dn == null || user.dn == null)
91  			return (this.dn==user.dn ? 0 : (user.dn==null ? -1: 1));
92      	return this.compareDn( user.getDn() );
93      }
94      
95      /**
96       * @param userDn
97       * @return true if user DN element matches
98       */
99      public int compareDn(String userDn) {
100 		if (this.dn == null || userDn == null)
101 			return (this.dn==userDn ? 0 : (userDn==null ? -1: 1));
102     	return this.dn.compareTo( userDn );//compareToIgnoreCase( userDn );
103     }
104     
105     /**
106      * A GridUser will be equal only to another GridUser with the same DN and FQAN.
107      * 
108      * @param obj another object
109      * @return true if the object was a GridUser with equivalent credentials
110      */
111     public boolean equals(Object obj) {
112     	if (obj==null)
113     		return false;
114         GridUser user = (GridUser) obj;
115         if ((user.getDn() == null) ? dn != null : (user.compareDn(dn)!=0)) {
116             if (log.isTraceEnabled()) {
117                 log.trace(this + " !equals " + obj + " for different DN");
118             }
119             return false;
120         }
121         if ((user.fqanObj == null) ? fqanObj != null : (!user.fqanObj.equals(fqanObj))) {
122             if (log.isTraceEnabled()) {
123                 log.trace(this + " !equals " + obj + " for different FQAN");
124             }
125             return false;
126         }
127         if (log.isTraceEnabled()) {
128             log.trace(this + " equals " + obj);
129         }
130         return true;
131     }
132     
133     /**
134      * Retrieve the certificate DN of the user.
135      * 
136      * @return The certificate DN (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
137      */
138     public String getDn() {
139         return this.dn;
140     }
141     
142     /**
143      * Retrieve the email of the user.
144      * 
145      * @return The email
146      */
147     public String getEmail() {
148         return this.email;
149     }
150 
151     /**
152      * Retrieve the string representation of the VOMS Fully Qualified Attribute name.
153      * 
154      * @return The string representation of the VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
155      */
156     public String getFqan() {
157         return fqanObj==null ? null : this.fqanObj.toString();
158     }
159     
160     /**
161      * Retrieve VOMS Fully Qualified Attribute name.
162      * 
163      * @return The VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
164      */
165     @Transient
166     public FQAN getFqanObj() {
167         return this.fqanObj;
168     }
169     
170 	@ManyToOne
171     @JoinColumn(name="userGroup")
172     public UserGroup getUserGroup() {
173 		return userGroupRef == null ? null : userGroupRef.get();
174 	}
175     
176     /**
177      * Changed to reflect the change in equals, as in Object contract.
178      * 
179      * @return A hash created from the DN and FQAN.
180      */
181     public int hashCode() {
182         if (dn != null)
183             return dn.hashCode();
184         if (fqanObj != null)
185             return fqanObj.getFqan().hashCode();
186         return 0;
187     }
188     
189     /**
190      * Initialize from string array.
191      * 
192      * @return A hash created from the DN and FQAN.
193      */
194     public void initialize(String[] elements) {
195     	if (elements.length >= 1)
196     		dn = elements[0].trim();
197     	if (elements.length >= 2)
198     		fqanObj = new FQAN(elements[1].trim());
199     }
200     
201     /**
202      * Changes the certificate DN for the Grid credential.
203      * 
204      * @param certificateDN A GRID certificate DN (i.e. "/DC=org/DC=doegrids/OU=People/CN=Gabriele Carcassi")
205      */
206     public void setDn(String certificateDN) {
207         this.dn = certificateDN;//removeSpaces(certificateDN);
208     }
209  
210     /**
211      * Changes the email.
212      * 
213      * @param email
214      */
215     public void setEmail(String email) {
216         this.email = email;
217     }
218     
219     /**
220      * Sets the VOMS Fully Qualified Attribute name for the credential.
221      * 
222      * @param voFQAN The VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
223      */
224     public void setFqan(String fqan) {
225     	if (fqan == null)
226     		return;
227         this.fqanObj = new FQAN(fqan);
228     }
229     
230     /**
231      * Sets the VOMS Fully Qualified Attribute name for the credential.
232      * 
233      * @param voFQAN The VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
234      */
235     public void setFqanObj(FQAN voFQAN) {
236         this.fqanObj = voFQAN;
237     }
238     
239 	public void setUserGroup(UserGroup userGroup) {
240 		this.userGroupRef = new SoftReference<UserGroup>(userGroup);
241 	}
242 
243 	/**
244      * Returns a legible String representation for the credentail.
245      * 
246      * @return String reprentation of the credential (i.e. "GridID[/DC=org/DC=doegrids/OU=People/CN=Gabriele Carcassi]")
247      */
248     public String toString() {
249         if (fqanObj == null) {
250             //return "GridID[" + dn + "]";
251             return dn;
252         }
253 //        return "GridID[" + dn + ", " + fqanObj + "]";
254         return dn + "," + fqanObj;
255     }
256 
257 	@Id
258 	@GeneratedValue(strategy = GenerationType.AUTO)
259 	@SuppressWarnings("unused")
260     private long getId() {
261 		return id;
262 	}
263 
264 	@SuppressWarnings("unused")
265 	private void setId(long id) {
266 		this.id = id;
267 	}
268     
269     /**
270      * Trim and remove two or more consecutive strings
271      * 
272      * @param str
273      * @return new string
274      */
275     /*private String removeSpaces(String str) {
276     	if (str!=null) {
277     		str = str.trim();
278 	    	String tempStr;
279 	    	while ( !(tempStr=str.replaceAll("\\s\\s", " ")).equals(str) )
280 	    		str = tempStr;	   	    	
281     	}
282    		return str;
283     }*/
284     
285 }