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