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.authentication;
29  
30  import java.util.HashSet;
31  import java.util.Set;
32  import net.sf.jguard.core.authentication.credentials.JGuardCredential;
33  import net.sf.jguard.core.organization.Organization;
34  import net.sf.jguard.ext.authentication.manager.HibernateConverterUtils;
35  import net.sf.jguard.ext.organization.PersistedOrganization;
36  import net.sf.jguard.ext.util.SubjectUtils;
37  
38  /**
39  * POJO part of {@link javax.security.auth.PersistedSubject}.
40  * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
41  */
42  public class PersistedSubject {
43      
44      private Set principals;
45      private Set publicCredentials;
46      private Set privateCredentials;
47      private Long id;
48      private PersistedOrganization persistedOrganization;
49      private String login = null;
50      private boolean active = true;
51      public static final String LOGIN ="login";
52      public static final String ACTIVE ="active";
53      public final static String PERSISTENCE_ID ="persistenceId";
54      public PersistedSubject(){
55              
56      }
57      
58       public PersistedSubject(javax.security.auth.Subject subject,PersistedOrganization organization){
59          
60              String idToString = SubjectUtils.getCredentialValueAsString(subject, false, PERSISTENCE_ID);
61              if (idToString != null && !idToString.equals("")) {
62                  id = new Long(idToString);
63              }
64  
65              principals = HibernateConverterUtils.getPersistedPrincipals(subject.getPrincipals());
66              privateCredentials = subject.getPrivateCredentials(JGuardCredential.class);
67              privateCredentials.remove(new JGuardCredential(PERSISTENCE_ID, idToString));
68              
69              login = SubjectUtils.getCredentialValueAsString(subject, false,LOGIN);
70              active = Boolean.valueOf(SubjectUtils.getCredentialValueAsString(subject, false,ACTIVE)).booleanValue();
71              privateCredentials.remove(new JGuardCredential(LOGIN,getLogin()));
72              
73              publicCredentials = subject.getPublicCredentials(JGuardCredential.class);
74              persistedOrganization = organization;
75              
76      }
77       
78      public javax.security.auth.Subject toJavaxSecuritySubject(){
79          Set ppals = HibernateConverterUtils.getjavaSecurityPrincipals(principals);
80          if(id!=null && !id.toString().equals("0")){
81              //this credential is used to keep track of the database row in an Object not related with datéabase in its API
82              //so, the final user should not keep an eye on it 
83              JGuardCredential persistanceIdCredential = new JGuardCredential(PERSISTENCE_ID,id.toString());
84              privateCredentials.add(persistanceIdCredential);
85          }
86          Set clonedPrincipals = new HashSet(ppals);
87          clonedPrincipals.add(persistedOrganization.toOrganization());
88          HashSet privCredentials = new HashSet(privateCredentials);
89          privCredentials.add(new JGuardCredential(LOGIN,getLogin()));
90          privCredentials.add(new JGuardCredential(ACTIVE,Boolean.toString(active)));
91          javax.security.auth.Subject subject = new javax.security.auth.Subject(false,clonedPrincipals,publicCredentials,privCredentials);
92          return subject;
93      }
94  
95      Set getPrincipals() {
96          return principals;
97      }
98  
99      public void setPrincipals(Set principals) {
100         this.principals = principals;
101     }
102 
103     public Set getPublicCredentials() {
104         return publicCredentials;
105     }
106 
107     public void setPublicCredentials(Set publicCredentials) {
108         this.publicCredentials = publicCredentials;
109     }
110 
111     public Set getPrivateCredentials() {
112         return privateCredentials;
113     }
114 
115     public void setPrivateCredentials(Set privateCredentials) {
116         this.privateCredentials = privateCredentials;
117     }
118 
119     public Long getId() {
120         return id;
121     }
122 
123     
124     private void setId(Long id) {
125         this.id = id;
126     }
127 
128     public Organization getOrganization() {
129         return persistedOrganization;
130     }
131 
132     public void setOrganization(PersistedOrganization organization) {
133         this.persistedOrganization = organization;
134     }
135 
136     public int hashCode() {
137         int hash = 3;
138         hash = 79 * hash + (this.principals != null ? this.principals.hashCode() : 0);
139         hash = 79 * hash + (this.publicCredentials != null ? this.publicCredentials.hashCode() : 0);
140         hash = 79 * hash + (this.privateCredentials != null ? this.privateCredentials.hashCode() : 0);
141         hash = 79 * hash + (this.persistedOrganization != null ? this.persistedOrganization.hashCode() : 0);
142         hash = 79 * hash + (this.getLogin()!= null ? this.getLogin().hashCode() : 0);
143         return hash;
144     }
145     
146     public boolean equals(Object other) {
147        if (this == other){
148             return true;
149        }
150        if ( !(other instanceof PersistedSubject) ){
151             return false;
152        }
153         
154        final PersistedSubject psubject = (PersistedSubject)other;
155        if(principals.equals(psubject.getPrincipals())&&
156             privateCredentials.equals(psubject.getPrivateCredentials())&&
157             publicCredentials.equals(psubject.getPublicCredentials())&&
158             persistedOrganization.equals(psubject.getOrganization())){
159              return true;
160        }
161        return false;
162     }
163 
164     public String getLogin() {
165         return login;
166     }
167 
168     public void setLogin(String login) {
169         this.login = login;
170     }
171 
172     public boolean isActive() {
173         return active;
174     }
175 
176     public void setActive(boolean active) {
177         this.active = active;
178     }
179 
180 }