1
14
15 package com.liferay.portal.security.ldap;
16
17 import com.liferay.portal.UserEmailAddressException;
18 import com.liferay.portal.UserScreenNameException;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
22 import com.liferay.portal.kernel.util.LocaleUtil;
23 import com.liferay.portal.kernel.util.PropsKeys;
24 import com.liferay.portal.kernel.util.StringPool;
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.model.Contact;
27 import com.liferay.portal.model.ContactConstants;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.model.UserGroupRole;
30 import com.liferay.portal.model.impl.ContactImpl;
31 import com.liferay.portal.model.impl.UserImpl;
32 import com.liferay.portal.service.ServiceContext;
33 import com.liferay.portal.util.PrefsPropsUtil;
34 import com.liferay.util.ldap.LDAPUtil;
35
36 import java.util.Calendar;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.Map;
41 import java.util.Properties;
42
43 import javax.naming.NamingException;
44 import javax.naming.directory.Attributes;
45
46
52 public class BaseLDAPToPortalConverter implements LDAPToPortalConverter {
53
54 public LDAPGroup importLDAPGroup(
55 long companyId, Attributes attributes, Properties groupMappings)
56 throws Exception {
57
58 String groupName = LDAPUtil.getAttributeValue(
59 attributes, groupMappings, GroupConverterKeys.GROUP_NAME).
60 toLowerCase();
61 String description = LDAPUtil.getAttributeValue(
62 attributes, groupMappings, GroupConverterKeys.DESCRIPTION);
63
64 LDAPGroup ldapGroup = new LDAPGroup();
65
66 ldapGroup.setCompanyId(companyId);
67 ldapGroup.setDescription(description);
68 ldapGroup.setGroupName(groupName);
69
70 return ldapGroup;
71 }
72
73 public LDAPUser importLDAPUser(
74 long companyId, Attributes attributes, Properties userMappings,
75 Properties userExpandoMappings, Properties contactMappings,
76 Properties contactExpandoMappings, String password)
77 throws Exception {
78
79 boolean autoPassword = false;
80 boolean updatePassword = true;
81
82 if (password.equals(StringPool.BLANK)) {
83 autoPassword = true;
84 updatePassword = false;
85 }
86
87 long creatorUserId = 0;
88 boolean passwordReset = false;
89 boolean autoScreenName = false;
90
91 String screenName = LDAPUtil.getAttributeValue(
92 attributes, userMappings, UserConverterKeys.SCREEN_NAME).
93 toLowerCase();
94 String emailAddress = LDAPUtil.getAttributeValue(
95 attributes, userMappings, UserConverterKeys.EMAIL_ADDRESS);
96 String openId = StringPool.BLANK;
97 Locale locale = LocaleUtil.getDefault();
98 String firstName = LDAPUtil.getAttributeValue(
99 attributes, userMappings, UserConverterKeys.FIRST_NAME);
100 String middleName = LDAPUtil.getAttributeValue(
101 attributes, userMappings, UserConverterKeys.MIDDLE_NAME);
102 String lastName = LDAPUtil.getAttributeValue(
103 attributes, userMappings, UserConverterKeys.LAST_NAME);
104
105 if (Validator.isNull(firstName) || Validator.isNull(lastName)) {
106 String fullName = LDAPUtil.getAttributeValue(
107 attributes, userMappings, UserConverterKeys.FULL_NAME);
108
109 String[] names = LDAPUtil.splitFullName(fullName);
110
111 firstName = names[0];
112 middleName = names[1];
113 lastName = names[2];
114 }
115
116 int prefixId = 0;
117 int suffixId = 0;
118 boolean male = true;
119 int birthdayMonth = Calendar.JANUARY;
120 int birthdayDay = 1;
121 int birthdayYear = 1970;
122 String jobTitle = LDAPUtil.getAttributeValue(
123 attributes, userMappings, UserConverterKeys.JOB_TITLE);
124 long[] groupIds = null;
125 long[] organizationIds = null;
126 long[] roleIds = null;
127 List<UserGroupRole> userGroupRoles = null;
128 long[] userGroupIds = null;
129 boolean sendEmail = false;
130
131 ServiceContext serviceContext = new ServiceContext();
132
133 if (_log.isDebugEnabled()) {
134 _log.debug(
135 "Screen name " + screenName + " and email address " +
136 emailAddress);
137 }
138
139 if (Validator.isNull(screenName) &&
140 !PrefsPropsUtil.getBoolean(
141 companyId, PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE)) {
142
143 throw new UserScreenNameException(
144 "Screen name cannot be null for " +
145 ContactConstants.getFullName(
146 firstName, middleName, lastName));
147 }
148
149 if (Validator.isNull(emailAddress) &&
150 PrefsPropsUtil.getBoolean(
151 companyId, PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED)) {
152
153 throw new UserEmailAddressException(
154 "Email address cannot be null for " +
155 ContactConstants.getFullName(
156 firstName, middleName, lastName));
157 }
158
159 User user = new UserImpl();
160
161 user.setCompanyId(companyId);
162 user.setEmailAddress(emailAddress);
163 user.setFirstName(firstName);
164 user.setJobTitle(jobTitle);
165 user.setLanguageId(locale.getLanguage());
166 user.setLastName(lastName);
167 user.setMiddleName(middleName);
168 user.setOpenId(openId);
169 user.setPasswordUnencrypted(password);
170 user.setScreenName(screenName);
171
172 Map<String, String> userExpandoAttributes = getExpandoAttributes(
173 attributes, userExpandoMappings);
174
175 Contact contact = new ContactImpl();
176
177 contact.setBirthday(
178 CalendarFactoryUtil.getCalendar(
179 birthdayYear, birthdayMonth, birthdayDay).getTime());
180 contact.setMale(male);
181 contact.setPrefixId(prefixId);
182 contact.setSuffixId(suffixId);
183
184 Map<String, String> contactExpandoAttributes = getExpandoAttributes(
185 attributes, contactExpandoMappings);
186
187 LDAPUser ldapUser = new LDAPUser();
188
189 ldapUser.setAutoPassword(autoPassword);
190 ldapUser.setAutoScreenName(autoScreenName);
191 ldapUser.setContact(contact);
192 ldapUser.setContactExpandoAttributes(contactExpandoAttributes);
193 ldapUser.setCreatorUserId(creatorUserId);
194 ldapUser.setGroupIds(groupIds);
195 ldapUser.setOrganizationIds(organizationIds);
196 ldapUser.setPasswordReset(passwordReset);
197 ldapUser.setRoleIds(roleIds);
198 ldapUser.setSendEmail(sendEmail);
199 ldapUser.setServiceContext(serviceContext);
200 ldapUser.setUpdatePassword(updatePassword);
201 ldapUser.setUser(user);
202 ldapUser.setUserExpandoAttributes(userExpandoAttributes);
203 ldapUser.setUserGroupIds(userGroupIds);
204 ldapUser.setUserGroupRoles(userGroupRoles);
205
206 return ldapUser;
207 }
208
209 protected Map<String, String> getExpandoAttributes(
210 Attributes attributes, Properties expandoMappings)
211 throws NamingException {
212
213 Map<String, String> expandoAttributes = new HashMap<String, String>();
214
215 for (Object key : expandoMappings.keySet()) {
216 String name = (String)key;
217
218 String value = LDAPUtil.getAttributeValue(
219 attributes, expandoMappings, name);
220
221 if (Validator.isNotNull(value)) {
222 expandoAttributes.put(name, value);
223 }
224 }
225
226 return expandoAttributes;
227 }
228
229 private static Log _log = LogFactoryUtil.getLog(
230 BaseLDAPToPortalConverter.class);
231
232 }