View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26  
27  */
28  package net.sf.jguard.core.principals;
29  
30  import java.security.Permission;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.Set;
34  
35  import net.sf.jguard.core.authorization.permissions.JGPermissionCollection;
36  import net.sf.jguard.core.organization.Organization;
37  
38  
39  /**
40   * This Principal represents the notion of a role.
41   * it is the link between Authentication and Authorization parts.
42   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
43   * @author <a href="mailto:vinipitta@users.sourceforge.net">Vinícius Pitta Lima de Araujo</a>
44   * @author <a href="mailto:tandilero@users.sourceforge.net">Maximiliano Batelli</a>
45   */
46  public class RolePrincipal implements BasePrincipal{
47  
48  
49      /**
50       * serial version id.
51       */
52      private static final long serialVersionUID = 3761412993065431095L;
53  
54      private String localName="";
55  
56      //indicate to which application this role apply
57      private String applicationName = DEFAULT_APPLICATION_NAME;
58      //permissions bound to a domain owned by this Principal
59      private Set permissionsFromDomains;
60      //all the permissions (bound or not bound to a domain owned by this Principal)
61      private Set permissions;
62      //permisisons not bound to a Domain owned by this Principal.
63      private Set orphanedPermissions;
64      //domains owned to this principal
65      private Set domains;
66      private boolean active = true;
67      private String definition = "true";
68      private Organization organization = null;
69      private Long id;
70      
71      /**
72       * All principals that this role inherites from. This property is use to implement the General Hierarchy
73       * proposed by the NIST.
74       */
75      private Set descendants;
76  
77      /**
78       * empty constructor.
79       */
80      public RolePrincipal(){
81          permissions = new HashSet();
82          domains = new HashSet();
83          orphanedPermissions = new HashSet();
84          permissionsFromDomains = new HashSet();
85          descendants = new HashSet();
86         
87      }
88  
89      /**
90       * constructor.
91       * @param name
92       */
93      public RolePrincipal(String name){
94          permissions = new HashSet();
95          this.setName(name);
96          domains = new HashSet();
97          orphanedPermissions = new HashSet();
98          permissionsFromDomains = new HashSet();
99          descendants = new HashSet();
100        
101     }
102 
103 
104     /**
105      * constructor.
106      * @param localName
107      * @param applicationName
108      */
109     public RolePrincipal(String localName,String  applicationName){
110         permissions = new HashSet();
111         this.localName = localName;
112         this.applicationName = applicationName;
113         domains = new HashSet();
114         orphanedPermissions = new HashSet();
115         permissionsFromDomains = new HashSet();
116         descendants = new HashSet();
117         
118     }
119     
120     /**
121      * constructor.
122      * @param localName
123      * @param applicationName
124      */
125     public RolePrincipal(String localName,String  applicationName,Organization organizationOwner){
126         permissions = new HashSet();
127         this.localName = localName;
128         this.applicationName = applicationName;
129         domains = new HashSet();
130         orphanedPermissions = new HashSet();
131         permissionsFromDomains = new HashSet();
132         descendants = new HashSet();
133         organization = organizationOwner;
134     }
135     
136     /**
137      * override the java.lang.Object 's <i>clone</i> method.
138      * @return new JGuardPricipal with the same internal references (permissions and Domains).
139      */
140     public Object clone(){
141        RolePrincipal clone  = new RolePrincipal(this.localName,this.applicationName);
142        clone.setDomains(new HashSet(this.domains));
143        clone.setPermissions(new HashSet(this.permissions));
144        clone.setDescendants(new HashSet(this.descendants));
145         return clone;
146 
147     }
148 	/**
149          * return  the unique name formed by <i>localname#applicationName.</i>
150 	 * @see java.security.Principal#getName()
151 	 */
152 	public String getName() {
153 		return getName(localName, applicationName);
154 	}
155 	
156 	
157 	/**
158 	 * @see java.security.Principal#getName()
159 	 */
160 	public static String getName(String localName) {
161 		return getName(localName, "*");
162 	}
163 	/**
164 	 * @see java.security.Principal#getName()
165 	 */
166 	public static String getName(String localName,String applicationName) {
167 		StringBuffer sb = new StringBuffer(20);
168 		sb.append(applicationName);
169 		sb.append("#");
170 		sb.append(localName);
171 		return sb.toString();
172 	}
173 
174     /**
175      * compare an object to this RolePrincipal.
176      * override the Object's <i>equals</i> method.
177      * @param another object to compare
178      * @return true if another is equals to this RolePrincipal
179      */
180     public boolean equals(Object another){
181 
182         if(another instanceof RolePrincipal){
183             RolePrincipal principal = (RolePrincipal)another;
184             if(principal.getName().equals(this.getName())){
185                 return true;
186             }
187         }
188 
189         return false;
190     }
191 
192     /**
193      * override the Object's <i>toString</i> method.
194      * return String representation
195      */
196     public String toString(){
197         StringBuffer sb = new StringBuffer();
198         sb.append(" principal class name =");
199         sb.append(getClass().getName());
200         sb.append("\n");
201         sb.append(" principal localName =");
202         sb.append(localName);
203         sb.append("\n");
204         sb.append(" principal application name =");
205         sb.append(applicationName);
206         sb.append("\n");
207         sb.append(" organization owner =");
208         sb.append(getOrganization());
209         sb.append("\n");
210         sb.append(" principal domains =");
211         sb.append(domains);
212         sb.append("\n");
213         sb.append(" principal permissions =");
214         sb.append(permissions);
215         sb.append("\n");
216         sb.append(" principal descendants =");
217         sb.append(descendants);
218         sb.append("\n");
219         return sb.toString();
220     }
221 
222     /**
223      * override Object's <i>hashcode</i> method.
224      */
225     public int hashCode(){
226         int i =0;
227          i+=localName.hashCode()+applicationName.hashCode();
228          if(organization!=null){
229              i+=organization.hashCode();
230          }
231          return i;
232     }
233 	/**
234 	 * @param string
235 	 */
236 	public void setName(String string) {
237 		String[] tokens = string.split("#");
238 		if(tokens.length==1){
239 			applicationName = "*";
240 			localName = tokens[0];
241 		}else if(tokens.length==2){
242 			applicationName = tokens[0];
243 			localName = tokens[1];
244 			
245 		}else{
246 			throw new IllegalArgumentException(" name is composed of applicationName#localName");
247 		}
248 		
249 		
250 		
251 		
252 	}
253 
254 	/**
255 	 * return all permissions owned by this Principal plus
256 	 * permissions inherited from descendants.
257 	 * @return permissions
258 	 */
259 	public Set getAllPermissions() {
260 		//all permissions owned by this principal
261 		Set allPermissions = new HashSet();
262         allPermissions.addAll(permissions);
263 
264         //get inherited permissions
265         for (Iterator it = descendants.iterator(); it.hasNext(); ) {
266             allPermissions.addAll(((RolePrincipal) it.next()).getAllPermissions());
267         }
268 
269 		return allPermissions;
270 	}
271 
272     /**
273      * return the permissions bounded to a domain plus orphanedPermisions.
274      * @return
275      */
276     public Set getPermissions(){
277         return permissions;
278     }
279 
280     /**
281      * return the permissions not bound to a domain owned by this RolePrincipal.
282      * @return
283      */
284     public Set getOrphanedPermissions(){
285         return orphanedPermissions;
286     }
287 
288 	/**
289 	 * @param perms
290 	 */
291 	public void setPermissions(Set perms) {
292         Iterator itPermissionsSet = perms.iterator();
293         while(itPermissionsSet.hasNext()){
294           Permission perm = (Permission)itPermissionsSet.next();
295           addPermission(perm);
296         }
297 	}
298     /**
299      * add a permission to the RolePrincipal.
300      * @param permission
301      */
302     public void addPermission(Permission permission){
303         permissions.add(permission);
304         Iterator itDomains = domains.iterator();
305         boolean orphan = true;
306         while(itDomains.hasNext()){
307         	JGPermissionCollection temp = (JGPermissionCollection)itDomains.next();
308         	if(temp.containsPermission(permission)){
309         		orphan = false;
310         		break;
311         	}
312         }
313         if(orphan == true){
314             orphanedPermissions.add(permission);
315         }else{
316             permissionsFromDomains.add(permission);
317         }
318     }
319 
320     /**
321      * add an Domain to the RolePrincipal.
322      * @param domain
323      */
324     public void addDomain(JGPermissionCollection domain){
325         domains.add(domain);
326         Set permissionsDomain = domain.getPermissions();
327         //we remove from orphanedPermissions the permissions bound to this Domain
328         orphanedPermissions.removeAll(permissionsDomain);
329         //we add the permissions owned by domain to permissionsFromDomains and permissions
330         permissionsFromDomains.addAll(permissionsDomain);
331         permissions.addAll(permissionsDomain);
332     }
333 
334 
335 	/**
336 	 * @return application name
337 	 */
338 	public String getApplicationName() {
339 		return applicationName;
340 	}
341 
342 	/**
343      * define application name.
344 	 * @param string
345 	 */
346 	public void setApplicationName(String string) {
347 		applicationName = string;
348 	}
349 
350     /**
351      * @return Returns the domains.
352      */
353     public Set getDomains() {
354         return domains;
355     }
356 
357 
358     /**
359      * @return Returns the permissionsFromDomains.
360      */
361     public Set getPermissionsFromDomains() {
362         return permissionsFromDomains;
363     }
364 
365     /**
366      * @param doms The domains to set.
367      */
368     public void setDomains(Set doms) {
369         Iterator it = doms.iterator();
370         while(it.hasNext()){
371             JGPermissionCollection dom = (JGPermissionCollection)it.next();
372             this.addDomain(dom);
373         }
374 
375     }
376 
377 
378 
379     /**
380      * method used to compare two objects.
381      * this method is used in Collection to <strong>order</strong> items, and MUST be
382      * consistent with the <i>equals</i> method (eache method should return 0/true in the same cases).
383      * @param o object to compare
384      * @see java.lang.Comparable#compareTo(java.lang.Object)
385      * @return 0 if both objects are equals,
386      * &lt;0 if 0 is lesser than the RolePrincipal,
387      * &gt;0 if 0 is greater than the RolePrincipal
388      * @see java.lang.Comparable#compareTo(java.lang.Object)
389      */
390     public int compareTo(Object o) {
391         RolePrincipal principal = (RolePrincipal)o;
392         if (this.equals(o)){
393             return 0;
394         }
395 
396         return this.getName().compareTo(principal.getName());
397     }
398 
399     public void removeDomain(JGPermissionCollection domain){
400         permissionsFromDomains.removeAll(domain.getPermissions());
401         permissions.removeAll(domain.getPermissions());
402         domains.remove(domain);
403 
404     }
405 
406 	public Set getDescendants() {
407 		return descendants;
408 	}
409 
410 	public void setDescendants(Set descendants) {
411 		this.descendants = descendants;
412 	}
413 
414 	public boolean isActive() {
415 		return active;
416 	}
417 
418 	public void setActive(boolean active) {
419 		this.active = active;
420 	}
421 
422 	/**
423 	 * @return Returns definition.
424 	 */
425 	public String getDefinition() {
426 		return definition;
427 	}
428 
429 	/**
430 	 * @param definition The definition to set.
431 	 */
432 	public void setDefinition(String definition) {
433 		this.definition = definition;
434 	}
435 
436 	public String getLocalName() {
437 		return localName;
438 	}
439 
440 	public void setLocalName(String localName) {
441 		this.localName = localName;
442 	}
443 
444     public Organization getOrganization() {
445         return organization;
446     }
447 
448     public void setOrganization(Organization organization) {
449         this.organization = organization;
450     }
451 
452     public Long getId() {
453         return id;
454     }
455 
456     public void setId(Long id) {
457         this.id = id;
458     }
459 
460    
461 }