1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.user.model.dao;
17
18 import java.util.Iterator;
19 import java.util.Set;
20
21 import net.sf.acegisecurity.GrantedAuthority;
22 import net.sf.acegisecurity.GrantedAuthorityImpl;
23 import net.sf.acegisecurity.providers.dao.AuthenticationDao;
24 import net.sf.acegisecurity.providers.dao.UsernameNotFoundException;
25 import net.sf.oness.common.model.dao.hibernate.HibernateDaoSupport;
26 import net.sf.oness.user.model.bo.Authority;
27 import net.sf.oness.user.model.bo.User;
28
29 import org.springframework.dao.DataAccessException;
30 import org.springframework.orm.hibernate.HibernateObjectRetrievalFailureException;
31
32 /***
33 * Retrieves user details from database.
34 *
35 * @author Carlos Sanchez
36 * @version $Revision: 1.3 $
37 */
38 public class UserHibernateDao
39 extends HibernateDaoSupport
40 implements AuthenticationDao, UserDao
41 {
42
43 public User create( User user )
44 {
45 getHibernateTemplate().save( user );
46 return user;
47 }
48
49 public Authority create( Authority authority )
50 {
51 getHibernateTemplate().save( authority );
52 return authority;
53 }
54
55 public net.sf.acegisecurity.providers.dao.User loadUserByUsername( String name ) throws UsernameNotFoundException,
56 DataAccessException
57 {
58
59 User user;
60 try
61 {
62 user = (User) getHibernateTemplate().load( User.class, name );
63
64
65
66
67
68
69
70
71
72 }
73 catch ( HibernateObjectRetrievalFailureException e )
74 {
75 throw new UsernameNotFoundException( "User not found" );
76 }
77
78 Set auths = user.getAuthorities();
79 if ( auths.size() == 0 )
80 {
81 throw new UsernameNotFoundException( "User has no GrantedAuthority" );
82 }
83
84 GrantedAuthority[] arrayAuths = new GrantedAuthority[auths.size()];
85 Iterator iter = auths.iterator();
86 int i = 0;
87 while ( iter.hasNext() )
88 {
89 Authority element = (Authority) iter.next();
90 arrayAuths[i++] = new GrantedAuthorityImpl( element.getName() );
91 }
92
93 return new net.sf.acegisecurity.providers.dao.User( user.getName(), user.getPassword(), user.isEnabled()
94 .booleanValue(), arrayAuths );
95 }
96 }