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.ext.provisioning;
29  
30  import java.util.HashSet;
31  import java.util.Iterator;
32  import java.util.Set;
33  import net.sf.jguard.core.authentication.credentials.JGuardCredential;
34  import net.sf.jguard.core.provisioning.SubjectTemplate;
35  import net.sf.jguard.ext.authentication.manager.HibernateConverterUtils;
36  
37  /**
38  *
39  * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
40  */
41  public class PersistedSubjectTemplate {
42      private Long id;
43      private Set principals;
44      
45      private Set subjectTemplateCredentials;
46              
47      public PersistedSubjectTemplate(){
48          subjectTemplateCredentials = new HashSet();
49          principals= new HashSet();
50      }
51      public PersistedSubjectTemplate(SubjectTemplate subjectTemplate){
52          super();
53          id = subjectTemplate.getId();
54          subjectTemplateCredentials = new HashSet();
55          addCredentialSet(subjectTemplate.getPrivateRequiredCredentials(), false, true);
56          addCredentialSet(subjectTemplate.getPublicRequiredCredentials(), true, true);
57          addCredentialSet(subjectTemplate.getPublicOptionalCredentials(), true, false);
58          addCredentialSet(subjectTemplate.getPrivateOptionalCredentials(), false, false);
59          
60          principals=HibernateConverterUtils.getPersistedPrincipals(subjectTemplate.getPrincipals());
61      }
62  
63      public Long getId() {
64          return id;
65      }
66  
67      public void setId(Long id) {
68          this.id = id;
69      }
70  
71      public Set getPrincipals() {
72          return principals;
73      }
74  
75      public void setPrincipals(Set principals) {
76          this.principals = principals;
77      }
78  
79      public Set getPublicOptionalCredentials() {
80          return getSubjectTemplateCredentials(true, false);
81      }
82      
83      public Set getPublicRequiredCredentials() {
84    	return getSubjectTemplateCredentials(true, true);
85      }
86      public Set getPrivateRequiredCredentials() {
87          return getSubjectTemplateCredentials(false, true);
88      }
89      public Set getPrivateOptionalCredentials() {
90          return getSubjectTemplateCredentials(false, false);
91      }
92      public Set getSubjectTemplateCredentials() {
93          return subjectTemplateCredentials;
94      }
95              
96      private Set getSubjectTemplateCredentials(boolean publicVisibility, boolean required){
97          Set credentials = new HashSet();
98           Iterator itStc = getSubjectTemplateCredentials().iterator();
99           while(itStc.hasNext()){
100              SubjectTemplateCredential stc = (SubjectTemplateCredential)itStc.next();
101              if(stc.isPublicVisibility()==publicVisibility && stc.isRequired()==required){
102                  credentials.add(stc.getCredential());
103              }
104          }
105          return credentials;
106     }
107     public void setSubjectTemplateCredentials(Set subjectTemplateCredentials) {
108         this.subjectTemplateCredentials = subjectTemplateCredentials;
109     }
110     public void setPublicOptionalCredentials(Set publicOptionalCredentials) {
111   		addCredentialSet(publicOptionalCredentials,true,false);
112     }
113     public void setPublicRequiredCredentials(Set publicOptionalCredentials) {
114   		addCredentialSet(publicOptionalCredentials,true,true);
115     }
116     public void setPrivateRequiredCredentials(Set publicOptionalCredentials) {
117   		addCredentialSet(publicOptionalCredentials,false,true);
118     }
119     
120     public void setPrivateOptionalCredentials(Set publicOptionalCredentials) {
121   		addCredentialSet(publicOptionalCredentials,false,false);
122     }
123    private void addCredentialSet(Set creds,boolean publicVisibility,boolean required){
124 
125          //remove similar credentials
126          Iterator itStc = getSubjectTemplateCredentials().iterator();
127          while(itStc.hasNext()){
128              SubjectTemplateCredential stc = (SubjectTemplateCredential)itStc.next();
129              if(stc.isPublicVisibility()==publicVisibility && stc.isRequired()==required){
130                  itStc.remove();
131              }
132          }
133 
134           Iterator it = creds.iterator();
135           while(it.hasNext()){
136               Object next = it.next();
137               if(next instanceof JGuardCredential){
138                   JGuardCredential cred = (JGuardCredential)next;
139                   SubjectTemplateCredential stc = new SubjectTemplateCredential(this, cred, publicVisibility, required);
140                   getSubjectTemplateCredentials().add(stc);
141               }
142           }
143     }
144    
145    public SubjectTemplate toSubjectTemplate(){
146        SubjectTemplate st = new SubjectTemplate();
147        st.setId(id);
148        st.setPrincipals(HibernateConverterUtils.getjavaSecurityPrincipals(st.getPrincipals()));
149        st.setPrivateOptionalCredentials(getPrivateOptionalCredentials());
150        st.setPrivateRequiredCredentials(getPrivateRequiredCredentials());
151        st.setPublicOptionalCredentials(getPublicOptionalCredentials());
152        st.setPublicRequiredCredentials(getPublicRequiredCredentials());
153        return st;
154    }
155 }