KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > security > CustomUserRegistry


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.ui.core.security;
19
20 import java.util.Locale JavaDoc;
21 import java.util.TimeZone JavaDoc;
22
23 import javax.naming.NamingException JavaDoc;
24 import javax.naming.directory.Attribute JavaDoc;
25 import javax.naming.directory.Attributes JavaDoc;
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 /**
37  * @author Elias Torres (<a HREF="mailto:eliast@us.ibm.com">eliast@us.ibm.com</a>)
38  *
39  */

40 public class CustomUserRegistry {
41   
42   private static Log log = LogFactory.getLog(CustomUserRegistry.class);
43
44   private static String JavaDoc DEFAULT_NAME_LDAP_ATTRIBUTE = "cn";
45   private static String JavaDoc DEFAULT_EMAIL_LDAP_ATTRIBUTE = "mail";
46   private static String JavaDoc DEFAULT_LOCALE_LDAP_ATTRIBUTE = "locale";
47   private static String JavaDoc DEFAULT_TIMEZONE_LDAP_ATTRIBUTE = "timezone";
48   
49   private static String JavaDoc NAME_LDAP_PROPERTY = "users.sso.registry.ldap.attributes.name";
50   private static String JavaDoc EMAIL_LDAP_PROPERTY = "users.sso.registry.ldap.attributes.email";
51   private static String JavaDoc LOCALE_LDAP_PROPERTY = "users.sso.registry.ldap.attributes.locale";
52   private static String JavaDoc 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 JavaDoc 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 JavaDoc userName = userDetails.getUsername();
83     String JavaDoc 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 JavaDoc());
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 JavaDoc attributes = ldapDetails.getAttributes();
116       String JavaDoc name = getLdapAttribute(attributes, RollerConfig.getProperty(NAME_LDAP_PROPERTY, DEFAULT_NAME_LDAP_ATTRIBUTE));
117       String JavaDoc email = getLdapAttribute(attributes, RollerConfig.getProperty(EMAIL_LDAP_PROPERTY, DEFAULT_EMAIL_LDAP_ATTRIBUTE));
118       
119       ud.setFullName(name);
120       ud.setEmailAddress(email);
121       
122       String JavaDoc locale = getLdapAttribute(attributes, RollerConfig.getProperty(LOCALE_LDAP_PROPERTY, DEFAULT_LOCALE_LDAP_ATTRIBUTE));
123       String JavaDoc 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 JavaDoc getLdapAttribute(Attributes JavaDoc attributes, String JavaDoc name) {
137     if(attributes == null) {
138       return null;
139     }
140     
141     Attribute JavaDoc attribute = attributes.get(name);
142     
143     if(attribute == null) {
144       return null;
145     }
146     
147     Object JavaDoc oValue = null;
148     try {
149       oValue = attribute.get();
150     } catch (NamingException JavaDoc 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