001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.security.ldap;
022:
023: import com.liferay.portal.kernel.util.Validator;
024: import com.liferay.portal.model.User;
025: import com.liferay.portal.util.PropsUtil;
026: import com.liferay.util.ldap.DummyDirContext;
027:
028: import java.util.Properties;
029:
030: import javax.naming.Name;
031: import javax.naming.NameNotFoundException;
032: import javax.naming.NamingException;
033: import javax.naming.directory.Attribute;
034: import javax.naming.directory.Attributes;
035: import javax.naming.directory.BasicAttribute;
036: import javax.naming.directory.BasicAttributes;
037:
038: /**
039: * <a href="LDAPUser.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Scott Lee
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class LDAPUser extends DummyDirContext {
046:
047: public LDAPUser() {
048: super ();
049: }
050:
051: public User getUser() {
052: return _user;
053: }
054:
055: public void setUser(User user) throws Exception {
056: _user = user;
057:
058: Properties userMappings = PortalLDAPUtil.getUserMappings(_user
059: .getCompanyId());
060:
061: _attrs = new BasicAttributes(true);
062:
063: Attribute objectClass = new BasicAttribute("objectclass");
064:
065: String[] defaultObjectClasses = PropsUtil
066: .getArray(PropsUtil.LDAP_USER_DEFAULT_OBJECT_CLASSES);
067:
068: for (int i = 0; i < defaultObjectClasses.length; i++) {
069: objectClass.add(defaultObjectClasses[i]);
070: }
071:
072: _attrs.put(objectClass);
073:
074: _attrs.put(userMappings.getProperty("firstName"), _user
075: .getFirstName());
076: _attrs.put(userMappings.getProperty("lastName"), _user
077: .getLastName());
078:
079: if (Validator.isNotNull(user.getPasswordUnencrypted())) {
080: _attrs.put(userMappings.getProperty("password"), _user
081: .getPasswordUnencrypted());
082: }
083:
084: _attrs.put(userMappings.getProperty("emailAddress"), _user
085: .getEmailAddress());
086: }
087:
088: public Attributes getAttributes() {
089: return _attrs;
090: }
091:
092: public Attributes getAttributes(String name) throws NamingException {
093: if (Validator.isNotNull(name)) {
094: throw new NameNotFoundException();
095: }
096:
097: return (Attributes) _attrs.clone();
098: }
099:
100: public Attributes getAttributes(Name name) throws NamingException {
101: return getAttributes(name.toString());
102: }
103:
104: public Attributes getAttributes(String name, String[] ids)
105: throws NamingException {
106:
107: if (Validator.isNotNull(name)) {
108: throw new NameNotFoundException();
109: }
110:
111: Attributes attrs = new BasicAttributes(true);
112:
113: for (int i = 0; i < ids.length; i++) {
114: Attribute attr = _attrs.get(ids[i]);
115:
116: if (attr != null) {
117: attrs.put(attr);
118: }
119: }
120:
121: return attrs;
122: }
123:
124: public Attributes getAttributes(Name name, String[] ids)
125: throws NamingException {
126:
127: return getAttributes(name.toString(), ids);
128: }
129:
130: private User _user;
131: private Attributes _attrs;
132:
133: }
|