KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > principals > UserPrincipal


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 import java.security.Principal JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 import javax.security.auth.Subject JavaDoc;
39
40 import net.sf.jguard.core.authentication.credentials.JGuardCredential;
41
42 /**
43  * UserPrincipal is used to resolve ABAC permissions.
44  * @author <a HREF="mailto:vberetti@users.sourceforge.net">Vincent Beretti</a>
45  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
46  */

47 public class UserPrincipal implements BasePrincipal{
48
49     
50     private static final String JavaDoc NO_NAME_FOUND = "NO NAME FOUND";
51     private static final long serialVersionUID = 9075426017744650798L;
52     private Logger JavaDoc logger = Logger.getLogger(UserPrincipal.class.getName());
53     private String JavaDoc name = "NO NAME DEFINED";
54     private Subject JavaDoc subject;
55
56     public UserPrincipal(Subject JavaDoc subject){
57         this.subject = subject;
58     }
59
60     public void setName(String JavaDoc name) {
61         this.name = name;
62         
63     }
64
65     public Map JavaDoc getPrivateCredentials() {
66         
67         Set JavaDoc privateCredentials = subject.getPrivateCredentials();
68         Map JavaDoc pCredentials = transformCredentialSetIntoMap(privateCredentials);
69         return pCredentials;
70     }
71
72     private Map JavaDoc transformCredentialSetIntoMap(Set JavaDoc credentials) {
73         Map JavaDoc pCredentials = new HashMap JavaDoc();
74         Iterator JavaDoc privateCIterator = credentials.iterator();
75         while(privateCIterator.hasNext()){
76             Object JavaDoc credential = privateCIterator.next();
77             if(credential instanceof JGuardCredential){
78                 JGuardCredential jcred = (JGuardCredential)credential;
79                 if(!pCredentials.containsKey(jcred.getId())){
80                     Collection JavaDoc values = new HashSet JavaDoc();
81                     values.add(jcred.getValue());
82                     pCredentials.put(jcred.getId(),values);
83                 }else{
84                     Collection JavaDoc valuesStored = (Collection JavaDoc) pCredentials.get(jcred.getId());
85                     valuesStored.add(jcred.getValue());
86                 }
87                 
88             }
89         }
90         return pCredentials;
91     }
92
93     public Map JavaDoc getPublicCredentials() {
94         Set JavaDoc publicCredentials = subject.getPublicCredentials();
95         Map JavaDoc pCredentials = transformCredentialSetIntoMap(publicCredentials);
96         return pCredentials;
97     }
98
99     /**
100      * @return the value of a credential present in the public credentials ' set
101      * of the Subject, if its 'id' is <i>"name"</i>.
102      * @see net.sf.jguard.core.authentication.credentials.JGuardCredential
103      */

104     public String JavaDoc getName() {
105         //we cannot add a more significatn method to avoid infinite loop
106
return NO_NAME_FOUND;
107     }
108     
109     /**
110      * compare two SubjectAsPrincipal objects(compare their Subject).
111      * @param subjAsPrincipal
112      * @return true if the contained Subject is equals to the one contained
113      * in the SubjectAsPrincipal instance as parameter;otherwise, false.
114      */

115     public boolean equals (Object JavaDoc object){
116         UserPrincipal userPrincipal;
117         if (object instanceof UserPrincipal) {
118             userPrincipal = (UserPrincipal) object;
119             if(getPrincipals().equals(userPrincipal.getPrincipals())){
120                 return true;
121             }
122             // we cannot include credentials in this method to avoid class circularity error
123
}
124             return false;
125         
126     }
127     
128     /**
129      * return principals present in the subject except userPrincipals
130      * to avoid infinite loop if we look into principals recursively.
131      * @return
132      */

133     protected Map JavaDoc getPrincipals(){
134         
135         //we filter userprincipal
136
Set JavaDoc principals = subject.getPrincipals();
137         Set JavaDoc filteredSet = new HashSet JavaDoc();
138         Iterator JavaDoc it = principals.iterator();
139         while(it.hasNext()){
140             Principal JavaDoc principal = (Principal JavaDoc)it.next();
141             if (!(principal instanceof UserPrincipal)) {
142                 filteredSet.add(principal);
143             }
144         }
145         
146         //we transform set into map for jexl
147
Map JavaDoc ppals = new HashMap JavaDoc();
148
149         Iterator JavaDoc itFiletedPrincipals = filteredSet.iterator();
150         while (itFiletedPrincipals.hasNext()){
151             Principal JavaDoc principal = (Principal JavaDoc)itFiletedPrincipals.next();
152             ppals.put(principal.getName(), principal);
153         }
154
155         return ppals;
156     }
157     
158     
159     /**
160      * return {@link RolePrincipal} present in subject.
161      * @return
162      */

163     public Map JavaDoc getRoles(){
164         return getSpecificPrincipals(RolePrincipal.class);
165     }
166     
167     /**
168      *
169      * return {@link GroupPrincipal} present in subject.
170      * @return
171      */

172     public Map JavaDoc getGroups(){
173         return getSpecificPrincipals(GroupPrincipal.class);
174     }
175     
176     private Map JavaDoc getSpecificPrincipals(Class JavaDoc principalSubclass){
177         Set JavaDoc principals = subject.getPrincipals(principalSubclass);
178         
179         //we transform set into map for jexl
180
Map JavaDoc ppals = new HashMap JavaDoc();
181
182         Iterator JavaDoc itPrincipals = principals.iterator();
183         while (itPrincipals.hasNext()){
184             Principal JavaDoc principal = (Principal JavaDoc)itPrincipals.next();
185             ppals.put(principal.getName(), principal);
186         }
187         return ppals;
188     }
189
190     public int compareTo(Object JavaDoc o) {
191         UserPrincipal principal = (UserPrincipal)o;
192         if (this.equals(o)){
193             return 0;
194         }
195
196         return this.getName().compareTo(principal.getName());
197
198     }
199     
200     public String JavaDoc toString(){
201         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
202         sb.append("UserPrincipal ");
203         sb.append(name);
204         sb.append(this.hashcode());
205         return sb.toString();
206     }
207     
208     public int hashcode(){
209         return getRoles().hashCode()
210         +getPublicCredentials().hashCode()
211         +getPrivateCredentials().hashCode()+45;
212     }
213 }
214
215
216
217
Popular Tags