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.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.util.PrefsPropsUtil;
30  import com.liferay.util.ldap.DummyDirContext;
31  
32  import java.util.Properties;
33  
34  import javax.naming.Name;
35  import javax.naming.NameNotFoundException;
36  import javax.naming.NamingException;
37  import javax.naming.directory.Attribute;
38  import javax.naming.directory.Attributes;
39  import javax.naming.directory.BasicAttribute;
40  import javax.naming.directory.BasicAttributes;
41  
42  /**
43   * <a href="LDAPUser.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Scott Lee
46   * @author Brian Wing Shun Chan
47   */
48  public class LDAPUser extends DummyDirContext {
49  
50      public LDAPUser() {
51          super();
52      }
53  
54      public User getUser() {
55          return _user;
56      }
57  
58      public void setUser(User user, long ldapServerId) throws Exception {
59          String postfix = PortalLDAPUtil.getPropertyPostfix(ldapServerId);
60  
61          _user = user;
62  
63          Properties userMappings = PortalLDAPUtil.getUserMappings(
64              ldapServerId, _user.getCompanyId());
65  
66          _attributes = new BasicAttributes(true);
67  
68          // Required attributes
69  
70          Attribute objectClass = new BasicAttribute("objectclass");
71  
72          String[] defaultObjectClasses = PrefsPropsUtil.getStringArray(
73              _user.getCompanyId(),
74              PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES + postfix,
75              StringPool.COMMA);
76  
77          for (int i = 0; i < defaultObjectClasses.length; i++) {
78              objectClass.add(defaultObjectClasses[i]);
79          }
80  
81          _attributes.put(objectClass);
82  
83          _attributes.put(
84              userMappings.getProperty("firstName"), _user.getFirstName());
85          _attributes.put(
86              userMappings.getProperty("lastName"), _user.getLastName());
87  
88          if (Validator.isNotNull(_user.getPasswordUnencrypted())) {
89              _attributes.put(
90                  userMappings.getProperty("password"),
91                  _user.getPasswordUnencrypted());
92          }
93  
94          if (Validator.isNotNull(_user.getEmailAddress())) {
95              _attributes.put(
96                  userMappings.getProperty("emailAddress"),
97                  _user.getEmailAddress());
98          }
99  
100         // Optional attributes
101 
102         String fullNameMapping = userMappings.getProperty("fullName");
103 
104         if (Validator.isNotNull(fullNameMapping)) {
105             _attributes.put(fullNameMapping, _user.getFullName());
106         }
107 
108         String jobTitleMapping = userMappings.getProperty("jobTitle");
109 
110         if (Validator.isNotNull(jobTitleMapping) &&
111             Validator.isNotNull(_user.getJobTitle())) {
112 
113             _attributes.put(jobTitleMapping, _user.getJobTitle());
114         }
115     }
116 
117     public Attributes getAttributes() {
118         return _attributes;
119     }
120 
121     public Attributes getAttributes(String name) throws NamingException {
122         if (Validator.isNotNull(name)) {
123             throw new NameNotFoundException();
124         }
125 
126         return (Attributes)_attributes.clone();
127     }
128 
129     public Attributes getAttributes(Name name) throws NamingException {
130         return getAttributes(name.toString());
131     }
132 
133     public Attributes getAttributes(String name, String[] ids)
134         throws NamingException {
135 
136         if (Validator.isNotNull(name)) {
137             throw new NameNotFoundException();
138         }
139 
140         Attributes attributes = new BasicAttributes(true);
141 
142         for (int i = 0; i < ids.length; i++) {
143             Attribute attr = _attributes.get(ids[i]);
144 
145             if (attr != null) {
146                 attributes.put(attr);
147             }
148         }
149 
150         return attributes;
151     }
152 
153     public Attributes getAttributes(Name name, String[] ids)
154         throws NamingException {
155 
156         return getAttributes(name.toString(), ids);
157     }
158 
159     private User _user;
160     private Attributes _attributes;
161 
162 }