1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.security.ldap;
16  
17  import com.liferay.portal.kernel.util.PropsKeys;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.util.PrefsPropsUtil;
22  import com.liferay.util.ldap.DummyDirContext;
23  
24  import java.util.Properties;
25  
26  import javax.naming.Name;
27  import javax.naming.NameNotFoundException;
28  import javax.naming.NamingException;
29  import javax.naming.directory.Attribute;
30  import javax.naming.directory.Attributes;
31  import javax.naming.directory.BasicAttribute;
32  import javax.naming.directory.BasicAttributes;
33  
34  /**
35   * <a href="LDAPUser.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Scott Lee
38   * @author Brian Wing Shun Chan
39   */
40  public class LDAPUser extends DummyDirContext {
41  
42      public LDAPUser() {
43          super();
44      }
45  
46      public User getUser() {
47          return _user;
48      }
49  
50      public void setUser(User user) throws Exception {
51          _user = user;
52  
53          Properties userMappings = PortalLDAPUtil.getUserMappings(
54              _user.getCompanyId());
55  
56          _attributes = new BasicAttributes(true);
57  
58          // Required attributes
59  
60          Attribute objectClass = new BasicAttribute("objectclass");
61  
62          String[] defaultObjectClasses = PrefsPropsUtil.getStringArray(
63              _user.getCompanyId(), PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
64              StringPool.COMMA);
65  
66          for (int i = 0; i < defaultObjectClasses.length; i++) {
67              objectClass.add(defaultObjectClasses[i]);
68          }
69  
70          _attributes.put(objectClass);
71  
72          _attributes.put(
73              userMappings.getProperty("firstName"), _user.getFirstName());
74          _attributes.put(
75              userMappings.getProperty("lastName"), _user.getLastName());
76  
77          if (Validator.isNotNull(_user.getPasswordUnencrypted())) {
78              _attributes.put(
79                  userMappings.getProperty("password"),
80                  _user.getPasswordUnencrypted());
81          }
82  
83          _attributes.put(
84              userMappings.getProperty("emailAddress"), _user.getEmailAddress());
85  
86          // Optional attributes
87  
88          String fullNameMapping = userMappings.getProperty("fullName");
89  
90          if (Validator.isNotNull(fullNameMapping)) {
91              _attributes.put(fullNameMapping, _user.getFullName());
92          }
93  
94          String jobTitleMapping = userMappings.getProperty("jobTitle");
95  
96          if (Validator.isNotNull(jobTitleMapping) &&
97              Validator.isNotNull(_user.getJobTitle())) {
98  
99              _attributes.put(jobTitleMapping, _user.getJobTitle());
100         }
101     }
102 
103     public Attributes getAttributes() {
104         return _attributes;
105     }
106 
107     public Attributes getAttributes(String name) throws NamingException {
108         if (Validator.isNotNull(name)) {
109             throw new NameNotFoundException();
110         }
111 
112         return (Attributes)_attributes.clone();
113     }
114 
115     public Attributes getAttributes(Name name) throws NamingException {
116         return getAttributes(name.toString());
117     }
118 
119     public Attributes getAttributes(String name, String[] ids)
120         throws NamingException {
121 
122         if (Validator.isNotNull(name)) {
123             throw new NameNotFoundException();
124         }
125 
126         Attributes attributes = new BasicAttributes(true);
127 
128         for (int i = 0; i < ids.length; i++) {
129             Attribute attr = _attributes.get(ids[i]);
130 
131             if (attr != null) {
132                 attributes.put(attr);
133             }
134         }
135 
136         return attributes;
137     }
138 
139     public Attributes getAttributes(Name name, String[] ids)
140         throws NamingException {
141 
142         return getAttributes(name.toString(), ids);
143     }
144 
145     private User _user;
146     private Attributes _attributes;
147 
148 }