1 16 package com.blandware.atleap.webapp.acegi; 17 18 import java.util.Iterator ; 19 import java.util.List ; 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 46 public class UserManagerDaoImpl implements UserDetailsService { 47 48 protected UserManager userManager = null; 49 protected UserCache userCache = null; 50 51 54 public UserManagerDaoImpl() { 55 } 56 57 62 public void setUserManager(UserManager userManager) { 63 this.userManager = userManager; 64 } 65 66 71 public void setUserCache(UserCache userCache) { 72 this.userCache = userCache; 73 } 74 75 92 public UserDetails loadUserByUsername(String 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 roles = user.getRoles(); 100 101 if (roles.size() == 0) { 102 throw new UsernameNotFoundException("User has no GrantedAuthority"); 103 } 104 105 Iterator 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 122 public void updateUser(String 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 136 public void updateUser(User user) { 137 SecurityContext securityContext = SecurityContextHolder.getContext(); 138 139 if (securityContext != null && user != null) { 140 String username = user.getName(); 141 List roles = user.getRoles(); 142 143 if (roles.size() == 0) { 144 throw new UsernameNotFoundException("User has no GrantedAuthority"); 145 } 146 147 Iterator 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 |