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