1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.security.ldap;
21  
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.util.PropsKeys;
25  import com.liferay.portal.util.PropsUtil;
26  import com.liferay.util.ldap.DummyDirContext;
27  
28  import java.util.Properties;
29  
30  import javax.naming.Name;
31  import javax.naming.NameNotFoundException;
32  import javax.naming.NamingException;
33  import javax.naming.directory.Attribute;
34  import javax.naming.directory.Attributes;
35  import javax.naming.directory.BasicAttribute;
36  import javax.naming.directory.BasicAttributes;
37  
38  /**
39   * <a href="LDAPUser.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Scott Lee
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class LDAPUser extends DummyDirContext {
46  
47      public LDAPUser() {
48          super();
49      }
50  
51      public User getUser() {
52          return _user;
53      }
54  
55      public void setUser(User user) throws Exception {
56          _user = user;
57  
58          Properties userMappings = PortalLDAPUtil.getUserMappings(
59              _user.getCompanyId());
60  
61          _attrs = new BasicAttributes(true);
62  
63          // Required attributes
64  
65          Attribute objectClass = new BasicAttribute("objectclass");
66  
67          String[] defaultObjectClasses = PropsUtil.getArray(
68              PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES);
69  
70          for (int i = 0; i < defaultObjectClasses.length; i++) {
71              objectClass.add(defaultObjectClasses[i]);
72          }
73  
74          _attrs.put(objectClass);
75  
76          _attrs.put(userMappings.getProperty("firstName"), _user.getFirstName());
77          _attrs.put(userMappings.getProperty("lastName"), _user.getLastName());
78  
79          if (Validator.isNotNull(user.getPasswordUnencrypted())) {
80              _attrs.put(
81                  userMappings.getProperty("password"),
82                  _user.getPasswordUnencrypted());
83          }
84  
85          _attrs.put(
86              userMappings.getProperty("emailAddress"), _user.getEmailAddress());
87  
88          // Optional attributes
89  
90          String fullNameMapping = userMappings.getProperty("fullName");
91  
92          if (Validator.isNotNull(fullNameMapping)) {
93              _attrs.put(fullNameMapping, _user.getFullName());
94          }
95  
96          String jobTitleMapping = userMappings.getProperty("jobTitle");
97  
98          if (Validator.isNotNull(jobTitleMapping)) {
99              _attrs.put(jobTitleMapping, _user.getContact().getJobTitle());
100         }
101     }
102 
103     public Attributes getAttributes() {
104         return _attrs;
105     }
106 
107     public Attributes getAttributes(String name) throws NamingException {
108         if (Validator.isNotNull(name)) {
109             throw new NameNotFoundException();
110         }
111 
112         return (Attributes)_attrs.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 attrs = new BasicAttributes(true);
127 
128         for (int i = 0; i < ids.length; i++) {
129             Attribute attr = _attrs.get(ids[i]);
130 
131             if (attr != null) {
132                 attrs.put(attr);
133             }
134         }
135 
136         return attrs;
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 _attrs;
147 
148 }