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  
29  package net.sf.jguard.core.provisioning;
30  
31  import java.util.HashSet;
32  import java.util.Set;
33  import net.sf.jguard.core.authentication.credentials.JGuardCredential;
34  import net.sf.jguard.core.organization.Organization;
35  import net.sf.jguard.core.principals.PrincipalUtils;
36  
37  /**
38   * a candidate to be an {@link Organization}.
39   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
40   */
41  public class OrganizationTemplate extends EntityTemplate{
42     
43      private Set credentials;
44      
45      private SubjectTemplate subjectTemplate;
46      private Long id;
47      public static final String ORGANIZATION_TEMPLATE = "organizationTemplate";
48      private Set principals;
49      
50          public OrganizationTemplate(){
51              super();
52              credentials = new HashSet();
53              subjectTemplate = new SubjectTemplate();
54              principals = new HashSet();	
55          }
56  
57          public OrganizationTemplate(Organization organization){
58              super();
59              credentials = new HashSet(organization.getCredentials());
60              principals = organization.getPrincipals();
61              subjectTemplate = organization.getSubjectTemplate();
62              principals = new HashSet();	
63          }
64          
65   
66      
67          /**
68  	 * build an Organization from a validated OrganizationTemplate.
69  	 * @param orga organization to convert to an Organization instace
70  	 * @return Organization built
71  	 */
72  	public Organization buildOrganization(OrganizationTemplate orga){
73  
74  		Set principalsForRegisteredUsers = new HashSet();
75  		principalsForRegisteredUsers.addAll((getPrincipals()));
76                  Set allCredentials =  new HashSet(orga.getCredentials());
77                  Set creds = allCredentials;
78                  Set principalsForOrganization = new HashSet();
79                  //we add the principals from our organizationTemplate
80  		principalsForOrganization.addAll((getPrincipals()));
81                  Organization organization = new Organization();
82                  organization.setCredentials(creds);
83  		organization.setPrincipals(principalsForOrganization);
84                  organization.setSubjectTemplate(orga.getSubjectTemplate());
85  		return organization;
86  	}
87  
88  	/**
89  	 * build a Subject from a validated SubjectTemplate.
90  	 * @return subject built
91  	 */
92  	public Organization toOrganization(){
93  		return buildOrganization(this);
94  	}
95  
96    
97  
98      /**
99       * @param organizationCandidate
100      */
101     public void validateTemplate(OrganizationTemplate organizationCandidate) {
102         if(organizationCandidate==null){
103             throw new IllegalArgumentException(" organizationTemplate is null ");
104         }
105         Set requiredCredentialsFromCandidate = organizationCandidate.getCredentials();
106         if(requiredCredentialsFromCandidate==null){
107             requiredCredentialsFromCandidate = new HashSet();
108             organizationCandidate.setCredentials(requiredCredentialsFromCandidate);
109         }
110         EntityTemplate.filterCredentialSet(this.credentials,requiredCredentialsFromCandidate);
111         
112     }
113 
114     public SubjectTemplate getSubjectTemplate() {
115         return subjectTemplate;
116     }
117 
118     public void setSubjectTemplate(SubjectTemplate subjectTemplate) {
119         this.subjectTemplate = subjectTemplate;
120     }
121 
122     public Set getCredentials() {
123         return credentials;
124     }
125 
126     public void setCredentials(Set credentials) {
127         this.credentials = credentials;
128     }
129 
130     public Object clone() throws CloneNotSupportedException {
131         OrganizationTemplate clone = new OrganizationTemplate();
132         clone.setSubjectTemplate((SubjectTemplate)subjectTemplate.clone());
133         clone.setPrincipals(PrincipalUtils.clonePrincipalsSet(principals));
134         clone.setCredentials(JGuardCredential.cloneCredentialsSet(credentials));
135         return clone;
136     }
137 
138     public Long getId() {
139         return id;
140     }
141 
142     public void setId(Long id) {
143         this.id = id;
144     }
145 	public Set getPrincipals() {
146 		return principals;
147 	}
148 	/**
149 	 * defined the principals automatically granted to the registered user.
150 	 * @param principals
151 	 */
152 	public void setPrincipals(Set principals) {
153 		this.principals = principals;
154 	}
155 
156 	
157 }