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  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          _attributes.put(
90              userMappings.getProperty("emailAddress"), _user.getEmailAddress());
91  
92          // Optional attributes
93  
94          String fullNameMapping = userMappings.getProperty("fullName");
95  
96          if (Validator.isNotNull(fullNameMapping)) {
97              _attributes.put(fullNameMapping, _user.getFullName());
98          }
99  
100         String jobTitleMapping = userMappings.getProperty("jobTitle");
101 
102         if (Validator.isNotNull(jobTitleMapping) &&
103             Validator.isNotNull(_user.getJobTitle())) {
104 
105             _attributes.put(jobTitleMapping, _user.getJobTitle());
106         }
107     }
108 
109     public Attributes getAttributes() {
110         return _attributes;
111     }
112 
113     public Attributes getAttributes(String name) throws NamingException {
114         if (Validator.isNotNull(name)) {
115             throw new NameNotFoundException();
116         }
117 
118         return (Attributes)_attributes.clone();
119     }
120 
121     public Attributes getAttributes(Name name) throws NamingException {
122         return getAttributes(name.toString());
123     }
124 
125     public Attributes getAttributes(String name, String[] ids)
126         throws NamingException {
127 
128         if (Validator.isNotNull(name)) {
129             throw new NameNotFoundException();
130         }
131 
132         Attributes attributes = new BasicAttributes(true);
133 
134         for (int i = 0; i < ids.length; i++) {
135             Attribute attr = _attributes.get(ids[i]);
136 
137             if (attr != null) {
138                 attributes.put(attr);
139             }
140         }
141 
142         return attributes;
143     }
144 
145     public Attributes getAttributes(Name name, String[] ids)
146         throws NamingException {
147 
148         return getAttributes(name.toString(), ids);
149     }
150 
151     private User _user;
152     private Attributes _attributes;
153 
154 }