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 Attributes getAttributes() {
47          return _attributes;
48      }
49  
50      public Attributes getAttributes(Name name) throws NamingException {
51          return getAttributes(name.toString());
52      }
53  
54      public Attributes getAttributes(Name name, String[] ids)
55          throws NamingException {
56  
57          return getAttributes(name.toString(), ids);
58      }
59  
60      public Attributes getAttributes(String name) throws NamingException {
61          if (Validator.isNotNull(name)) {
62              throw new NameNotFoundException();
63          }
64  
65          return (Attributes)_attributes.clone();
66      }
67  
68      public Attributes getAttributes(String name, String[] ids)
69          throws NamingException {
70  
71          if (Validator.isNotNull(name)) {
72              throw new NameNotFoundException();
73          }
74  
75          Attributes attributes = new BasicAttributes(true);
76  
77          for (int i = 0; i < ids.length; i++) {
78              Attribute attr = _attributes.get(ids[i]);
79  
80              if (attr != null) {
81                  attributes.put(attr);
82              }
83          }
84  
85          return attributes;
86      }
87  
88      public User getUser() {
89          return _user;
90      }
91  
92      public void setUser(User user, long ldapServerId) throws Exception {
93          _user = user;
94  
95          Properties userMappings = LDAPSettingsUtil.getUserMappings(
96              ldapServerId, _user.getCompanyId());
97  
98          _attributes = new BasicAttributes(true);
99  
100         // Required attributes
101 
102         Attribute objectClass = new BasicAttribute("objectclass");
103 
104         String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
105 
106         String[] defaultObjectClasses = PrefsPropsUtil.getStringArray(
107             _user.getCompanyId(),
108             PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES + postfix,
109             StringPool.COMMA);
110 
111         for (int i = 0; i < defaultObjectClasses.length; i++) {
112             objectClass.add(defaultObjectClasses[i]);
113         }
114 
115         _attributes.put(objectClass);
116 
117         _attributes.put(
118             userMappings.getProperty("firstName"), _user.getFirstName());
119         _attributes.put(
120             userMappings.getProperty("lastName"), _user.getLastName());
121 
122         if (Validator.isNotNull(_user.getPasswordUnencrypted())) {
123             _attributes.put(
124                 userMappings.getProperty("password"),
125                 _user.getPasswordUnencrypted());
126         }
127 
128         if (Validator.isNotNull(_user.getEmailAddress())) {
129             _attributes.put(
130                 userMappings.getProperty("emailAddress"),
131                 _user.getEmailAddress());
132         }
133 
134         // Optional attributes
135 
136         String fullNameMapping = userMappings.getProperty("fullName");
137 
138         if (Validator.isNotNull(fullNameMapping)) {
139             _attributes.put(fullNameMapping, _user.getFullName());
140         }
141 
142         String jobTitleMapping = userMappings.getProperty("jobTitle");
143 
144         if (Validator.isNotNull(jobTitleMapping) &&
145             Validator.isNotNull(_user.getJobTitle())) {
146 
147             _attributes.put(jobTitleMapping, _user.getJobTitle());
148         }
149     }
150 
151     private Attributes _attributes;
152     private User _user;
153 
154 }