return null;
}
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User ud = new User();
// setting default
ud.setId(null);
ud.setLocale(Locale.getDefault().toString());
ud.setTimeZone(TimeZone.getDefault().getID());
ud.setDateCreated(new java.util.Date());
String userName = null;
String password = null;
String fullName = null;
String email = null;
String screenName = null;
String locale = null;
String timezone = null;
boolean enabled = false;
if(authentication == null) {
// Try to get SSO data from HttpServletRequest
userName = getRequestAttribute(request, WebloggerConfig.getProperty(UID_LDAP_PROPERTY, DEFAULT_UID_LDAP_ATTRIBUTE));
screenName = getRequestAttribute(request, WebloggerConfig.getProperty(SNAME_LDAP_PROPERTY, DEFAULT_SNAME_LDAP_ATTRIBUTE));
fullName = getRequestAttribute(request, WebloggerConfig.getProperty(NAME_LDAP_PROPERTY, DEFAULT_NAME_LDAP_ATTRIBUTE));
email = getRequestAttribute(request, WebloggerConfig.getProperty(EMAIL_LDAP_PROPERTY, DEFAULT_EMAIL_LDAP_ATTRIBUTE));
locale = getRequestAttribute(request, WebloggerConfig.getProperty(LOCALE_LDAP_PROPERTY, DEFAULT_LOCALE_LDAP_ATTRIBUTE));
timezone = getRequestAttribute(request, WebloggerConfig.getProperty(TIMEZONE_LDAP_PROPERTY, DEFAULT_TIMEZONE_LDAP_ATTRIBUTE));
if (userName == null && fullName == null && screenName == null &&
email == null && locale == null && timezone == null) {
log.warn("No Authentication found in SecurityContextHolder and HttpServletRequest.");
return null;
} else {
enabled = true;
}
} else {
Object oPrincipal = authentication.getPrincipal();
if(oPrincipal == null) {
log.warn("Principal is null. Skipping auto-registration.");
return null;
}
if (!(oPrincipal instanceof UserDetails)) {
log.warn("Unsupported Principal type in Authentication. Skipping auto-registration.");
return null;
}
UserDetails userDetails = (UserDetails) oPrincipal;
userName = userDetails.getUsername();
password = userDetails.getPassword();
enabled = userDetails.isEnabled();
if(userDetails instanceof RollerUserDetails) {
RollerUserDetails rollerDetails = (RollerUserDetails) userDetails;
screenName = rollerDetails.getScreenName();
fullName = rollerDetails.getFullName();
email = rollerDetails.getEmailAddress();
locale = rollerDetails.getLocale();
timezone = rollerDetails.getTimeZone();
} else if(userDetails instanceof LdapUserDetails) {
LdapUserDetails ldapDetails = (LdapUserDetails) userDetails;
Attributes attributes = ldapDetails.getAttributes();
screenName = getLdapAttribute(attributes, WebloggerConfig.getProperty(SNAME_LDAP_PROPERTY, DEFAULT_SNAME_LDAP_ATTRIBUTE));
fullName = getLdapAttribute(attributes, WebloggerConfig.getProperty(NAME_LDAP_PROPERTY, DEFAULT_NAME_LDAP_ATTRIBUTE));
email = getLdapAttribute(attributes, WebloggerConfig.getProperty(EMAIL_LDAP_PROPERTY, DEFAULT_EMAIL_LDAP_ATTRIBUTE));
locale = getLdapAttribute(attributes, WebloggerConfig.getProperty(LOCALE_LDAP_PROPERTY, DEFAULT_LOCALE_LDAP_ATTRIBUTE));
timezone = getLdapAttribute(attributes, WebloggerConfig.getProperty(TIMEZONE_LDAP_PROPERTY, DEFAULT_TIMEZONE_LDAP_ATTRIBUTE));
}
}
boolean storePassword = WebloggerConfig.getBooleanProperty("users.sso.passwords.save");
if(!storePassword) {
password = WebloggerConfig.getProperty("users.sso.passwords.defaultValue","<unknown>");
}
ud.setPassword(password);
ud.setEnabled(enabled ? Boolean.TRUE : Boolean.FALSE);
ud.setUserName(userName);
ud.setFullName(fullName);
ud.setEmailAddress(email);
ud.setScreenName(screenName);
if (locale != null) {
ud.setLocale(locale);
}
if (timezone != null) {
ud.setTimeZone(timezone);
}
return ud;
}