1 18 package org.apache.roller.ui.core.security; 19 20 import java.util.Locale ; 21 import java.util.TimeZone ; 22 23 import javax.naming.NamingException ; 24 import javax.naming.directory.Attribute ; 25 import javax.naming.directory.Attributes ; 26 27 import org.acegisecurity.Authentication; 28 import org.acegisecurity.context.SecurityContextHolder; 29 import org.acegisecurity.userdetails.UserDetails; 30 import org.acegisecurity.userdetails.ldap.LdapUserDetails; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.roller.config.RollerConfig; 34 import org.apache.roller.pojos.UserData; 35 36 40 public class CustomUserRegistry { 41 42 private static Log log = LogFactory.getLog(CustomUserRegistry.class); 43 44 private static String DEFAULT_NAME_LDAP_ATTRIBUTE = "cn"; 45 private static String DEFAULT_EMAIL_LDAP_ATTRIBUTE = "mail"; 46 private static String DEFAULT_LOCALE_LDAP_ATTRIBUTE = "locale"; 47 private static String DEFAULT_TIMEZONE_LDAP_ATTRIBUTE = "timezone"; 48 49 private static String NAME_LDAP_PROPERTY = "users.sso.registry.ldap.attributes.name"; 50 private static String EMAIL_LDAP_PROPERTY = "users.sso.registry.ldap.attributes.email"; 51 private static String LOCALE_LDAP_PROPERTY = "users.sso.registry.ldap.attributes.locale"; 52 private static String TIMEZONE_LDAP_PROPERTY = "users.sso.registry.ldap.attributes.timezone"; 53 54 public static UserData getUserDetailsFromAuthentication() { 55 boolean usingSSO = RollerConfig.getBooleanProperty("users.sso.enabled"); 56 if(!usingSSO) { 57 log.info("SSO is not enabled. Skipping CustomUserRegistry functionality."); 58 return null; 59 } 60 61 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 62 63 if(authentication == null) { 64 log.warn("No Authentication found in SecurityContextHolder."); 65 return null; 66 } 67 68 Object oPrincipal = authentication.getPrincipal(); 69 70 if(oPrincipal == null) { 71 log.warn("Principal is null. Skipping auto-registration."); 72 return null; 73 } 74 75 if (!(oPrincipal instanceof UserDetails)) { 76 log.warn("Unsupported Principal type in Authentication. Skipping auto-registration."); 77 return null; 78 } 79 80 UserDetails userDetails = (UserDetails) oPrincipal; 81 82 String userName = userDetails.getUsername(); 83 String password = userDetails.getPassword(); 84 boolean enabled = userDetails.isEnabled(); 85 86 UserData ud = new UserData(); 87 ud.setId(null); 88 ud.setUserName(userName); 89 90 boolean storePassword = RollerConfig.getBooleanProperty("users.sso.passwords.save"); 91 if(!storePassword) { 92 password = RollerConfig.getProperty("users.sso.passwords.defaultValue","<unknown>"); 93 } 94 ud.setPassword(password); 95 ud.setEnabled(enabled ? Boolean.TRUE : Boolean.FALSE); 96 ud.setLocale(Locale.getDefault().toString()); 97 ud.setTimeZone(TimeZone.getDefault().getID()); 98 ud.setDateCreated(new java.util.Date ()); 99 100 if(userDetails instanceof RollerUserDetails) { 101 RollerUserDetails rollerDetails = (RollerUserDetails) userDetails; 102 103 ud.setFullName(rollerDetails.getFullName()); 104 ud.setEmailAddress(rollerDetails.getFullName()); 105 if(rollerDetails.getTimeZone() != null) { 106 ud.setTimeZone(rollerDetails.getTimeZone()); 107 } 108 109 if(rollerDetails.getLocale() != null) { 110 ud.setLocale(rollerDetails.getLocale()); 111 } 112 113 } else if(userDetails instanceof LdapUserDetails) { 114 LdapUserDetails ldapDetails = (LdapUserDetails) userDetails; 115 Attributes attributes = ldapDetails.getAttributes(); 116 String name = getLdapAttribute(attributes, RollerConfig.getProperty(NAME_LDAP_PROPERTY, DEFAULT_NAME_LDAP_ATTRIBUTE)); 117 String email = getLdapAttribute(attributes, RollerConfig.getProperty(EMAIL_LDAP_PROPERTY, DEFAULT_EMAIL_LDAP_ATTRIBUTE)); 118 119 ud.setFullName(name); 120 ud.setEmailAddress(email); 121 122 String locale = getLdapAttribute(attributes, RollerConfig.getProperty(LOCALE_LDAP_PROPERTY, DEFAULT_LOCALE_LDAP_ATTRIBUTE)); 123 String timezone = getLdapAttribute(attributes, RollerConfig.getProperty(TIMEZONE_LDAP_PROPERTY, DEFAULT_TIMEZONE_LDAP_ATTRIBUTE)); 124 125 if(locale != null) { 126 ud.setLocale(locale); 127 } 128 if(timezone != null) { 129 ud.setTimeZone(timezone); 130 } 131 } 132 133 return ud; 134 } 135 136 private static String getLdapAttribute(Attributes attributes, String name) { 137 if(attributes == null) { 138 return null; 139 } 140 141 Attribute attribute = attributes.get(name); 142 143 if(attribute == null) { 144 return null; 145 } 146 147 Object oValue = null; 148 try { 149 oValue = attribute.get(); 150 } catch (NamingException e) { 151 return null; 152 } 153 154 if(oValue == null) { 155 return null; 156 } 157 158 return oValue.toString(); 159 } 160 161 } 162 | Popular Tags |