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.manager;
29  
30  import java.security.Principal;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.Set;
34  import net.sf.jguard.ext.authentication.PersistedPrincipal;
35  import net.sf.jguard.core.organization.Organization;
36  import net.sf.jguard.core.principals.RolePrincipal;
37  import net.sf.jguard.ext.organization.PersistedOrganization;
38  import org.hibernate.Session;
39  
40  /**
41  *
42  * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
43  */
44  public class HibernateConverterUtils {
45  
46       public static net.sf.jguard.ext.authentication.PersistedPrincipal getPersistedPrincipal(Principal ppal) {
47          
48          if (ppal instanceof RolePrincipal) {
49              RolePrincipal rp = (RolePrincipal) ppal;
50              Long id = rp.getId();
51              net.sf.jguard.ext.authentication.PersistedPrincipal principal = null;
52              if(id!=null){
53                  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
54                  principal = (PersistedPrincipal) session.get(PersistedPrincipal.class,id);
55                  return principal;
56              }else{
57                  principal = new net.sf.jguard.ext.authentication.PersistedPrincipal();
58                  principal.setClassName(ppal.getClass().getName());
59                  principal.setName(rp.getLocalName());
60                  principal.setApplicationName(rp.getApplicationName());
61              }
62              return principal;
63          }else{
64              return null;
65          }
66          
67      }
68       
69        public static Set getjavaSecurityPrincipals(Set principals){
70              Set ppals = new HashSet();
71              Iterator it = principals.iterator();
72              while(it.hasNext()){
73                  net.sf.jguard.ext.authentication.PersistedPrincipal principal = (net.sf.jguard.ext.authentication.PersistedPrincipal)it.next();
74                  String applicationName = principal.getApplicationName();
75                  String className = principal.getClassName();
76                  String name = principal.getName();
77                  
78                  if(RolePrincipal.class.getName().equals(className)){
79                      RolePrincipal role = new RolePrincipal();
80                      role.setApplicationName(applicationName);
81                      role.setLocalName(name);
82                      role.setId(principal.getId());
83                      ppals.add(role);
84                  }
85              }
86              return ppals;
87          }
88          
89          public static Set getPersistedPrincipals(Set ppals) {
90          Set s = new HashSet();
91          Iterator it = ppals.iterator();
92          while (it.hasNext()) {
93              java.security.Principal ppal = (java.security.Principal) it.next();
94              net.sf.jguard.ext.authentication.PersistedPrincipal principal = getPersistedPrincipal(ppal);
95              if(principal != null){
96                  s.add(principal);
97              }
98          }
99          return s;
100     }
101      
102      
103    public static Set getOrganizations(Set persistedOrganizations){
104     Set orgas = new HashSet();
105     Iterator it = persistedOrganizations.iterator();
106     while(it.hasNext()){
107         PersistedOrganization org = (PersistedOrganization)it.next();
108         Organization orga = org.toOrganization();
109         orgas.add(orga);
110     }
111     return orgas;
112    }
113 }