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