1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.security.ldap;
24  
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.util.PropsUtil;
28  import com.liferay.util.ldap.DummyDirContext;
29  
30  import java.util.Properties;
31  
32  import javax.naming.Name;
33  import javax.naming.NameNotFoundException;
34  import javax.naming.NamingException;
35  import javax.naming.directory.Attribute;
36  import javax.naming.directory.Attributes;
37  import javax.naming.directory.BasicAttribute;
38  import javax.naming.directory.BasicAttributes;
39  
40  /**
41   * <a href="LDAPUser.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Scott Lee
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LDAPUser extends DummyDirContext {
48  
49      public LDAPUser() {
50          super();
51      }
52  
53      public User getUser() {
54          return _user;
55      }
56  
57      public void setUser(User user) throws Exception {
58          _user = user;
59  
60          Properties userMappings = PortalLDAPUtil.getUserMappings(
61              _user.getCompanyId());
62  
63          _attrs = new BasicAttributes(true);
64  
65          Attribute objectClass = new BasicAttribute("objectclass");
66  
67          String[] defaultObjectClasses = PropsUtil.getArray(
68              PropsUtil.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  
89      public Attributes getAttributes() {
90          return _attrs;
91      }
92  
93      public Attributes getAttributes(String name) throws NamingException {
94          if (Validator.isNotNull(name)) {
95              throw new NameNotFoundException();
96          }
97  
98          return (Attributes)_attrs.clone();
99      }
100 
101     public Attributes getAttributes(Name name) throws NamingException {
102         return getAttributes(name.toString());
103     }
104 
105     public Attributes getAttributes(String name, String[] ids)
106         throws NamingException {
107 
108         if (Validator.isNotNull(name)) {
109             throw new NameNotFoundException();
110         }
111 
112         Attributes attrs = new BasicAttributes(true);
113 
114         for (int i = 0; i < ids.length; i++) {
115             Attribute attr = _attrs.get(ids[i]);
116 
117             if (attr != null) {
118                 attrs.put(attr);
119             }
120         }
121 
122         return attrs;
123     }
124 
125     public Attributes getAttributes(Name name, String[] ids)
126         throws NamingException {
127 
128         return getAttributes(name.toString(), ids);
129     }
130 
131     private User _user;
132     private Attributes _attrs;
133 
134 }