KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > acegi > UserManagerDaoImpl


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.acegi;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.acegisecurity.Authentication;
22 import org.acegisecurity.GrantedAuthority;
23 import org.acegisecurity.GrantedAuthorityImpl;
24 import org.acegisecurity.context.SecurityContext;
25 import org.acegisecurity.context.SecurityContextHolder;
26 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
27 import org.acegisecurity.providers.dao.DaoAuthenticationProvider;
28 import org.acegisecurity.providers.dao.UserCache;
29 import org.acegisecurity.userdetails.UserDetails;
30 import org.acegisecurity.userdetails.UserDetailsService;
31 import org.acegisecurity.userdetails.UsernameNotFoundException;
32 import org.springframework.dao.DataAccessException;
33
34 import com.blandware.atleap.model.core.Role;
35 import com.blandware.atleap.model.core.User;
36 import com.blandware.atleap.service.core.UserManager;
37
38 /**
39  * <p>This class implements authentication via our UserManager</p>
40  * <p/>
41  * <p><a HREF="UserManagerDaoImpl.java.htm"><i>View Source</i></a></p>
42  *
43  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
44  * @version $Revision: 1.4 $ $Date: 2006/03/16 11:09:40 $
45  */

46 public class UserManagerDaoImpl implements UserDetailsService {
47
48     protected UserManager userManager = null;
49     protected UserCache userCache = null;
50
51     /**
52      * Creates new instance of UserManagerDaoImpl
53      */

54     public UserManagerDaoImpl() {
55     }
56
57     /**
58      * Set our UserManager to perform operations with user
59      *
60      * @param userManager user manager to set
61      */

62     public void setUserManager(UserManager userManager) {
63         this.userManager = userManager;
64     }
65
66     /**
67      * Set UserCache
68      *
69      * @param userCache user cache to set
70      */

71     public void setUserCache(UserCache userCache) {
72         this.userCache = userCache;
73     }
74     
75     /**
76      * Locates the user based on the username. In the actual implementation,
77      * the search may possibly be case insensitive, or case insensitive
78      * depending on how the implementaion instance is configured. In this
79      * case, the <code>UserDetails</code> object that comes back may have a
80      * username that is of a different case than what was actually requested..
81      *
82      * @param username the username presented to the {@link
83      * DaoAuthenticationProvider}
84      * @return a fully populated user record (never <code>null</code>)
85      * @throws org.acegisecurity.providers.dao.UsernameNotFoundException
86      * if the user could not be found or the
87      * user has no GrantedAuthority
88      * @throws org.springframework.dao.DataAccessException
89      * if user could not be found for a
90      * repository-specific reason
91      */

92     public UserDetails loadUserByUsername(String JavaDoc username) throws UsernameNotFoundException, DataAccessException {
93         User user = userManager.retrieveUser(username);
94
95         if (user == null) {
96             throw new UsernameNotFoundException("User with name " + username + " not found");
97         }
98
99         List JavaDoc roles = user.getRoles();
100
101         if (roles.size() == 0) {
102             throw new UsernameNotFoundException("User has no GrantedAuthority");
103         }
104
105         Iterator JavaDoc it = roles.iterator();
106         GrantedAuthority[] authorities = new GrantedAuthority[roles.size()];
107         int i = 0;
108         while (it.hasNext()) {
109             Role role = (Role) it.next();
110             authorities[i++] = new GrantedAuthorityImpl(role.getName());
111         }
112
113         return new org.acegisecurity.userdetails.User(username, user.getPassword(), user.getEnabled().booleanValue(), true,
114                 true, true, authorities);
115
116     }
117     
118     /**
119      * Update user details (password and roles) in Acegi
120      * @param username username
121      */

122     public void updateUser(String JavaDoc username) {
123         User user = userManager.retrieveUser(username);
124
125         if (user == null) {
126             throw new UsernameNotFoundException("User with name " + username + " not found");
127         }
128         
129         updateUser(user);
130     }
131     
132     /**
133      * Update user details (password and roles) in Acegi
134      * @param user user
135      */

136     public void updateUser(User user) {
137         SecurityContext securityContext = SecurityContextHolder.getContext();
138         
139         if (securityContext != null && user != null) {
140             String JavaDoc username = user.getName();
141             List JavaDoc roles = user.getRoles();
142
143             if (roles.size() == 0) {
144                 throw new UsernameNotFoundException("User has no GrantedAuthority");
145             }
146
147             Iterator JavaDoc it = roles.iterator();
148             GrantedAuthority[] authorities = new GrantedAuthority[roles.size()];
149             int i = 0;
150             while (it.hasNext()) {
151                 Role role = (Role) it.next();
152                 authorities[i++] = new GrantedAuthorityImpl(role.getName());
153             }
154             
155             if (userCache != null)
156                 userCache.removeUserFromCache(username);
157             
158             UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(username, user.getPassword(), authorities);
159             Authentication oldAuthentication = securityContext.getAuthentication();
160             newAuthentication.setDetails(oldAuthentication.getDetails());
161             securityContext.setAuthentication(newAuthentication);
162         }
163     }
164     
165     
166 }
167
Popular Tags