if (SecurityContextHolder.getContext().getAuthentication().getName().equals(username)) {
try {
User user = securityManager.getUserByLoginName(username);
if (user == null) {
//user is authed, but doesn't exist in the LIMS DB. Save that user!
User u = new UserImpl();
Object o = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (o instanceof UserDetails) {
UserDetails details = (UserDetails) o;
u.setLoginName(details.getUsername());
u.setFullName(details.getUsername());
u.setPassword(details.getPassword());
u.setActive(true);
if (details.getAuthorities().contains(new GrantedAuthorityImpl("ROLE_ADMIN"))) {
u.setAdmin(true);
}
if (details.getAuthorities().contains(new GrantedAuthorityImpl("ROLE_INTERNAL"))) {
u.setInternal(true);
u.setRoles(new String[]{"ROLE_INTERNAL"});
}
else if (details.getAuthorities().contains(new GrantedAuthorityImpl("ROLE_EXTERNAL"))) {
u.setExternal(true);
u.setRoles(new String[]{"ROLE_EXTERNAL"});
}
else {
log.warn("Unrecognised roles");
}
if (details instanceof InetOrgPerson) {
u.setFullName(((InetOrgPerson) details).getDisplayName());
u.setEmail(((InetOrgPerson) details).getMail());
}
securityManager.saveUser(u);
}
else {