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