001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.bean.AutoEscape;
018    import com.liferay.portal.kernel.cache.Lifecycle;
019    import com.liferay.portal.kernel.cache.ThreadLocalCache;
020    import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
021    import com.liferay.portal.kernel.dao.orm.QueryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.Digester;
025    import com.liferay.portal.kernel.util.DigesterUtil;
026    import com.liferay.portal.kernel.util.LocaleUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.kernel.util.SetUtil;
029    import com.liferay.portal.kernel.util.StringBundler;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portal.kernel.util.TimeZoneUtil;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.workflow.WorkflowConstants;
035    import com.liferay.portal.model.Address;
036    import com.liferay.portal.model.Company;
037    import com.liferay.portal.model.CompanyConstants;
038    import com.liferay.portal.model.Contact;
039    import com.liferay.portal.model.EmailAddress;
040    import com.liferay.portal.model.Group;
041    import com.liferay.portal.model.Organization;
042    import com.liferay.portal.model.PasswordPolicy;
043    import com.liferay.portal.model.Phone;
044    import com.liferay.portal.model.Role;
045    import com.liferay.portal.model.Team;
046    import com.liferay.portal.model.UserConstants;
047    import com.liferay.portal.model.UserGroup;
048    import com.liferay.portal.model.Website;
049    import com.liferay.portal.security.auth.EmailAddressGenerator;
050    import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
051    import com.liferay.portal.security.auth.FullNameGenerator;
052    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
053    import com.liferay.portal.service.AddressLocalServiceUtil;
054    import com.liferay.portal.service.CompanyLocalServiceUtil;
055    import com.liferay.portal.service.ContactLocalServiceUtil;
056    import com.liferay.portal.service.EmailAddressLocalServiceUtil;
057    import com.liferay.portal.service.GroupLocalServiceUtil;
058    import com.liferay.portal.service.GroupServiceUtil;
059    import com.liferay.portal.service.LayoutLocalServiceUtil;
060    import com.liferay.portal.service.OrganizationLocalServiceUtil;
061    import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
062    import com.liferay.portal.service.PhoneLocalServiceUtil;
063    import com.liferay.portal.service.RoleLocalServiceUtil;
064    import com.liferay.portal.service.TeamLocalServiceUtil;
065    import com.liferay.portal.service.UserGroupLocalServiceUtil;
066    import com.liferay.portal.service.WebsiteLocalServiceUtil;
067    import com.liferay.portal.theme.ThemeDisplay;
068    import com.liferay.portal.util.Portal;
069    import com.liferay.portal.util.PropsUtil;
070    import com.liferay.portal.util.PropsValues;
071    
072    import java.util.Date;
073    import java.util.List;
074    import java.util.Locale;
075    import java.util.Set;
076    import java.util.TimeZone;
077    import java.util.TreeSet;
078    
079    /**
080     * @author Brian Wing Shun Chan
081     * @author Jorge Ferrer
082     * @author Wesley Gong
083     */
084    public class UserImpl extends UserBaseImpl {
085    
086            public UserImpl() {
087            }
088    
089            public List<Address> getAddresses() throws SystemException {
090                    return AddressLocalServiceUtil.getAddresses(
091                            getCompanyId(), Contact.class.getName(), getContactId());
092            }
093    
094            public Date getBirthday() throws PortalException, SystemException {
095                    return getContact().getBirthday();
096            }
097    
098            public String getCompanyMx() throws PortalException, SystemException {
099                    Company company = CompanyLocalServiceUtil.getCompanyById(
100                            getCompanyId());
101    
102                    return company.getMx();
103            }
104    
105            public Contact getContact() throws PortalException, SystemException {
106                    return ContactLocalServiceUtil.getContact(getContactId());
107            }
108    
109            @Override
110            public String getDigest() {
111                    String digest = super.getDigest();
112    
113                    if (Validator.isNull(digest) && !isPasswordEncrypted()) {
114                            digest = getDigest(getPassword());
115                    }
116    
117                    return digest;
118            }
119    
120            public String getDigest(String password) {
121                    if (Validator.isNull(getScreenName())) {
122                            throw new IllegalStateException("Screen name cannot be null");
123                    }
124                    else if (Validator.isNull(getEmailAddress())) {
125                            throw new IllegalStateException("Email address cannot be null");
126                    }
127    
128                    StringBundler sb = new StringBundler(5);
129    
130                    String digest1 = DigesterUtil.digestHex(
131                            Digester.MD5, getEmailAddress(), Portal.PORTAL_REALM, password);
132    
133                    sb.append(digest1);
134                    sb.append(StringPool.COMMA);
135    
136                    String digest2 = DigesterUtil.digestHex(
137                            Digester.MD5, getScreenName(), Portal.PORTAL_REALM, password);
138    
139                    sb.append(digest2);
140                    sb.append(StringPool.COMMA);
141    
142                    String digest3 = DigesterUtil.digestHex(
143                            Digester.MD5, String.valueOf(getUserId()), Portal.PORTAL_REALM,
144                            password);
145    
146                    sb.append(digest3);
147    
148                    return sb.toString();
149            }
150    
151            public String getDisplayEmailAddress() {
152                    String emailAddress = super.getEmailAddress();
153    
154                    EmailAddressGenerator emailAddressGenerator =
155                            EmailAddressGeneratorFactory.getInstance();
156    
157                    if (emailAddressGenerator.isFake(emailAddress)) {
158                            emailAddress = StringPool.BLANK;
159                    }
160    
161                    return emailAddress;
162            }
163    
164            public String getDisplayURL(String portalURL, String mainPath)
165                    throws PortalException, SystemException {
166    
167                    if (isDefaultUser()) {
168                            return StringPool.BLANK;
169                    }
170    
171                    Group group = getGroup();
172    
173                    int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
174    
175                    if (publicLayoutsPageCount > 0) {
176                            StringBundler sb = new StringBundler(5);
177    
178                            sb.append(portalURL);
179                            sb.append(mainPath);
180                            sb.append("/my_sites/view?groupId=");
181                            sb.append(group.getGroupId());
182                            sb.append("&privateLayout=0");
183    
184                            return sb.toString();
185                    }
186    
187                    return StringPool.BLANK;
188            }
189    
190            public String getDisplayURL(ThemeDisplay themeDisplay)
191                    throws PortalException, SystemException {
192    
193                    return getDisplayURL(
194                            themeDisplay.getPortalURL(), themeDisplay.getPathMain());
195            }
196    
197            public List<EmailAddress> getEmailAddresses() throws SystemException {
198                    return EmailAddressLocalServiceUtil.getEmailAddresses(
199                            getCompanyId(), Contact.class.getName(), getContactId());
200            }
201    
202            public boolean getFemale() throws PortalException, SystemException {
203                    return !getMale();
204            }
205    
206            @AutoEscape
207            public String getFullName() {
208                    FullNameGenerator fullNameGenerator =
209                            FullNameGeneratorFactory.getInstance();
210    
211                    return fullNameGenerator.getFullName(
212                            getFirstName(), getMiddleName(), getLastName());
213            }
214    
215            public Group getGroup() throws PortalException, SystemException {
216                    return GroupLocalServiceUtil.getUserGroup(getCompanyId(), getUserId());
217            }
218    
219            public long getGroupId() throws PortalException, SystemException {
220                    Group group = getGroup();
221    
222                    return group.getGroupId();
223            }
224    
225            public long[] getGroupIds() throws PortalException, SystemException {
226                    List<Group> groups = getGroups();
227    
228                    long[] groupIds = new long[groups.size()];
229    
230                    for (int i = 0; i < groups.size(); i++) {
231                            Group group = groups.get(i);
232    
233                            groupIds[i] = group.getGroupId();
234                    }
235    
236                    return groupIds;
237            }
238    
239            public List<Group> getGroups() throws PortalException, SystemException {
240                    return GroupLocalServiceUtil.getUserGroups(getUserId());
241            }
242    
243            public Locale getLocale() {
244                    return _locale;
245            }
246    
247            public String getLogin() throws PortalException, SystemException {
248                    String login = null;
249    
250                    Company company = CompanyLocalServiceUtil.getCompanyById(
251                            getCompanyId());
252    
253                    if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
254                            login = getEmailAddress();
255                    }
256                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
257                            login = getScreenName();
258                    }
259                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
260                            login = String.valueOf(getUserId());
261                    }
262    
263                    return login;
264            }
265    
266            public boolean getMale() throws PortalException, SystemException {
267                    return getContact().getMale();
268            }
269    
270            public List<Group> getMySites() throws PortalException, SystemException {
271                    return getMySites(null, false, QueryUtil.ALL_POS);
272            }
273    
274            public List<Group> getMySites(boolean includeControlPanel, int max)
275                    throws PortalException, SystemException {
276    
277                    return getMySites(null, includeControlPanel, max);
278            }
279    
280            public List<Group> getMySites(int max)
281                    throws PortalException, SystemException {
282    
283                    return getMySites(null, false, max);
284            }
285    
286            public List<Group> getMySites(
287                            String[] classNames, boolean includeControlPanel, int max)
288                    throws PortalException, SystemException {
289    
290                    ThreadLocalCache<List<Group>> threadLocalCache =
291                            ThreadLocalCacheManager.getThreadLocalCache(
292                                    Lifecycle.REQUEST, UserImpl.class.getName());
293    
294                    String key = StringUtil.toHexString(max);
295    
296                    if ((classNames != null) && (classNames.length > 0)) {
297                            key = StringUtil.merge(classNames).concat(StringPool.POUND).concat(
298                                    key);
299                    }
300    
301                    key = key.concat(StringPool.POUND).concat(
302                            String.valueOf(includeControlPanel));
303    
304                    List<Group> myPlaces = threadLocalCache.get(key);
305    
306                    if (myPlaces != null) {
307                            return myPlaces;
308                    }
309    
310                    myPlaces = GroupServiceUtil.getUserPlaces(
311                            getUserId(), classNames, includeControlPanel, max);
312    
313                    threadLocalCache.put(key, myPlaces);
314    
315                    return myPlaces;
316            }
317    
318            public List<Group> getMySites(String[] classNames, int max)
319                    throws PortalException, SystemException {
320    
321                    return getMySites(classNames, false, max);
322            }
323    
324            public long[] getOrganizationIds() throws PortalException, SystemException {
325                    return getOrganizationIds(false);
326            }
327    
328            public long[] getOrganizationIds(boolean includeAdministrative)
329                    throws PortalException, SystemException {
330    
331                    List<Organization> organizations = getOrganizations(
332                            includeAdministrative);
333    
334                    long[] organizationIds = new long[organizations.size()];
335    
336                    for (int i = 0; i < organizations.size(); i++) {
337                            Organization organization = organizations.get(i);
338    
339                            organizationIds[i] = organization.getOrganizationId();
340                    }
341    
342                    return organizationIds;
343            }
344    
345            public List<Organization> getOrganizations()
346                    throws PortalException, SystemException {
347    
348                    return getOrganizations(false);
349            }
350    
351            public List<Organization> getOrganizations(boolean includeAdministrative)
352                    throws PortalException, SystemException {
353    
354                    return OrganizationLocalServiceUtil.getUserOrganizations(
355                            getUserId(), includeAdministrative);
356            }
357    
358            public boolean getPasswordModified() {
359                    return _passwordModified;
360            }
361    
362            public PasswordPolicy getPasswordPolicy()
363                    throws PortalException, SystemException {
364    
365                    if (_passwordPolicy == null) {
366                            _passwordPolicy =
367                                    PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
368                                            getUserId());
369                    }
370    
371                    return _passwordPolicy;
372            }
373    
374            public String getPasswordUnencrypted() {
375                    return _passwordUnencrypted;
376            }
377    
378            public List<Phone> getPhones() throws SystemException {
379                    return PhoneLocalServiceUtil.getPhones(
380                            getCompanyId(), Contact.class.getName(), getContactId());
381            }
382    
383            public String getPortraitURL(ThemeDisplay themeDisplay)
384                    throws PortalException, SystemException {
385    
386                    return UserConstants.getPortraitURL(
387                            themeDisplay.getPathImage(), isMale(), getPortraitId());
388            }
389    
390            public int getPrivateLayoutsPageCount()
391                    throws PortalException, SystemException {
392    
393                    return LayoutLocalServiceUtil.getLayoutsCount(this, true);
394            }
395    
396            public int getPublicLayoutsPageCount()
397                    throws PortalException, SystemException {
398    
399                    return LayoutLocalServiceUtil.getLayoutsCount(this, false);
400            }
401    
402            public Set<String> getReminderQueryQuestions()
403                    throws PortalException, SystemException {
404    
405                    Set<String> questions = new TreeSet<String>();
406    
407                    List<Organization> organizations =
408                            OrganizationLocalServiceUtil.getUserOrganizations(getUserId());
409    
410                    for (Organization organization : organizations) {
411                            Set<String> organizationQuestions =
412                                    organization.getReminderQueryQuestions(getLanguageId());
413    
414                            if (organizationQuestions.size() == 0) {
415                                    Organization parentOrganization =
416                                            organization.getParentOrganization();
417    
418                                    while ((organizationQuestions.size() == 0) &&
419                                               (parentOrganization != null)) {
420    
421                                            organizationQuestions =
422                                                    parentOrganization.getReminderQueryQuestions(
423                                                            getLanguageId());
424    
425                                            parentOrganization =
426                                                    parentOrganization.getParentOrganization();
427                                    }
428                            }
429    
430                            questions.addAll(organizationQuestions);
431                    }
432    
433                    if (questions.size() == 0) {
434                            Set<String> defaultQuestions = SetUtil.fromArray(
435                                    PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
436    
437                            questions.addAll(defaultQuestions);
438                    }
439    
440                    return questions;
441            }
442    
443            public long[] getRoleIds() throws SystemException {
444                    List<Role> roles = getRoles();
445    
446                    long[] roleIds = new long[roles.size()];
447    
448                    for (int i = 0; i < roles.size(); i++) {
449                            Role role = roles.get(i);
450    
451                            roleIds[i] = role.getRoleId();
452                    }
453    
454                    return roleIds;
455            }
456    
457            public List<Role> getRoles() throws SystemException {
458                    return RoleLocalServiceUtil.getUserRoles(getUserId());
459            }
460    
461            public long[] getTeamIds() throws SystemException {
462                    List<Team> teams = getTeams();
463    
464                    long[] teamIds = new long[teams.size()];
465    
466                    for (int i = 0; i < teams.size(); i++) {
467                            Team team = teams.get(i);
468    
469                            teamIds[i] = team.getTeamId();
470                    }
471    
472                    return teamIds;
473            }
474    
475            public List<Team> getTeams() throws SystemException {
476                    return TeamLocalServiceUtil.getUserTeams(getUserId());
477            }
478    
479            public TimeZone getTimeZone() {
480                    return _timeZone;
481            }
482    
483            public long[] getUserGroupIds() throws SystemException {
484                    List<UserGroup> userGroups = getUserGroups();
485    
486                    long[] userGroupIds = new long[userGroups.size()];
487    
488                    for (int i = 0; i < userGroups.size(); i++) {
489                            UserGroup userGroup = userGroups.get(i);
490    
491                            userGroupIds[i] = userGroup.getUserGroupId();
492                    }
493    
494                    return userGroupIds;
495            }
496    
497            public List<UserGroup> getUserGroups() throws SystemException {
498                    return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
499            }
500    
501            public List<Website> getWebsites() throws SystemException {
502                    return WebsiteLocalServiceUtil.getWebsites(
503                            getCompanyId(), Contact.class.getName(), getContactId());
504            }
505    
506            public boolean hasCompanyMx() throws PortalException, SystemException {
507                    return hasCompanyMx(getEmailAddress());
508            }
509    
510            public boolean hasCompanyMx(String emailAddress)
511                    throws PortalException, SystemException {
512    
513                    if (Validator.isNull(emailAddress)) {
514                            return false;
515                    }
516    
517                    Company company = CompanyLocalServiceUtil.getCompanyById(
518                            getCompanyId());
519    
520                    return company.hasCompanyMx(emailAddress);
521            }
522    
523            public boolean hasMySites() throws PortalException, SystemException {
524                    if (isDefaultUser()) {
525                            return false;
526                    }
527    
528                    int max = PropsValues.MY_SITES_MAX_ELEMENTS;
529    
530                    if (max == 1) {
531    
532                            // Increment so that we return more than just the Control Panel
533                            // group
534    
535                            max++;
536                    }
537    
538                    List<Group> groups = getMySites(true, max);
539    
540                    return !groups.isEmpty();
541            }
542    
543            public boolean hasOrganization() throws PortalException, SystemException {
544                    List<Organization> organizations = getOrganizations();
545    
546                    return !organizations.isEmpty();
547            }
548    
549            public boolean hasPrivateLayouts() throws PortalException, SystemException {
550                    return LayoutLocalServiceUtil.hasLayouts(this, true);
551            }
552    
553            public boolean hasPublicLayouts() throws PortalException, SystemException {
554                    return LayoutLocalServiceUtil.hasLayouts(this, false);
555            }
556    
557            public boolean hasReminderQuery() {
558                    if (Validator.isNotNull(getReminderQueryQuestion()) &&
559                            Validator.isNotNull(getReminderQueryAnswer())) {
560    
561                            return true;
562                    }
563                    else {
564                            return false;
565                    }
566            }
567    
568            public boolean isActive() {
569                    if (getStatus() == WorkflowConstants.STATUS_APPROVED) {
570                            return true;
571                    }
572                    else {
573                            return false;
574                    }
575            }
576    
577            public boolean isFemale() throws PortalException, SystemException {
578                    return getFemale();
579            }
580    
581            public boolean isMale() throws PortalException, SystemException {
582                    return getMale();
583            }
584    
585            public boolean isPasswordModified() {
586                    return _passwordModified;
587            }
588    
589            @Override
590            public void setLanguageId(String languageId) {
591                    _locale = LocaleUtil.fromLanguageId(languageId);
592    
593                    super.setLanguageId(LocaleUtil.toLanguageId(_locale));
594            }
595    
596            public void setPasswordModified(boolean passwordModified) {
597                    _passwordModified = passwordModified;
598            }
599    
600            public void setPasswordUnencrypted(String passwordUnencrypted) {
601                    _passwordUnencrypted = passwordUnencrypted;
602            }
603    
604            @Override
605            public void setTimeZoneId(String timeZoneId) {
606                    if (Validator.isNull(timeZoneId)) {
607                            timeZoneId = TimeZoneUtil.getDefault().getID();
608                    }
609    
610                    _timeZone = TimeZoneUtil.getTimeZone(timeZoneId);
611    
612                    super.setTimeZoneId(timeZoneId);
613            }
614    
615            private Locale _locale;
616            private boolean _passwordModified;
617            private PasswordPolicy _passwordPolicy;
618            private String _passwordUnencrypted;
619            private TimeZone _timeZone;
620    
621    }