001    /**
002     * Copyright (c) 2000-2012 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.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                    return getDisplayURL(portalURL, mainPath, false);
168            }
169    
170            public String getDisplayURL(
171                            String portalURL, String mainPath, boolean privateLayout)
172                    throws PortalException, SystemException {
173    
174                    if (isDefaultUser()) {
175                            return StringPool.BLANK;
176                    }
177    
178                    Group group = getGroup();
179    
180                    int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
181    
182                    if (publicLayoutsPageCount > 0) {
183                            StringBundler sb = new StringBundler(5);
184    
185                            sb.append(portalURL);
186                            sb.append(mainPath);
187                            sb.append("/my_sites/view?groupId=");
188                            sb.append(group.getGroupId());
189    
190                            if (privateLayout) {
191                                    sb.append("&privateLayout=1");
192                            }
193                            else {
194                                    sb.append("&privateLayout=0");
195                            }
196    
197                            return sb.toString();
198                    }
199    
200                    return StringPool.BLANK;
201            }
202    
203            public String getDisplayURL(ThemeDisplay themeDisplay)
204                    throws PortalException, SystemException {
205    
206                    return getDisplayURL(
207                            themeDisplay.getPortalURL(), themeDisplay.getPathMain(), false);
208            }
209    
210            public String getDisplayURL(
211                            ThemeDisplay themeDisplay, boolean privateLayout)
212                    throws PortalException, SystemException {
213    
214                    return getDisplayURL(
215                            themeDisplay.getPortalURL(), themeDisplay.getPathMain(),
216                            privateLayout);
217            }
218    
219            public List<EmailAddress> getEmailAddresses() throws SystemException {
220                    return EmailAddressLocalServiceUtil.getEmailAddresses(
221                            getCompanyId(), Contact.class.getName(), getContactId());
222            }
223    
224            public boolean getFemale() throws PortalException, SystemException {
225                    return !getMale();
226            }
227    
228            @AutoEscape
229            public String getFullName() {
230                    FullNameGenerator fullNameGenerator =
231                            FullNameGeneratorFactory.getInstance();
232    
233                    return fullNameGenerator.getFullName(
234                            getFirstName(), getMiddleName(), getLastName());
235            }
236    
237            public Group getGroup() throws PortalException, SystemException {
238                    return GroupLocalServiceUtil.getUserGroup(getCompanyId(), getUserId());
239            }
240    
241            public long getGroupId() throws PortalException, SystemException {
242                    Group group = getGroup();
243    
244                    return group.getGroupId();
245            }
246    
247            public long[] getGroupIds() throws PortalException, SystemException {
248                    List<Group> groups = getGroups();
249    
250                    long[] groupIds = new long[groups.size()];
251    
252                    for (int i = 0; i < groups.size(); i++) {
253                            Group group = groups.get(i);
254    
255                            groupIds[i] = group.getGroupId();
256                    }
257    
258                    return groupIds;
259            }
260    
261            public List<Group> getGroups() throws PortalException, SystemException {
262                    return GroupLocalServiceUtil.getUserGroups(getUserId());
263            }
264    
265            public Locale getLocale() {
266                    return _locale;
267            }
268    
269            public String getLogin() throws PortalException, SystemException {
270                    String login = null;
271    
272                    Company company = CompanyLocalServiceUtil.getCompanyById(
273                            getCompanyId());
274    
275                    if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
276                            login = getEmailAddress();
277                    }
278                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
279                            login = getScreenName();
280                    }
281                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
282                            login = String.valueOf(getUserId());
283                    }
284    
285                    return login;
286            }
287    
288            public boolean getMale() throws PortalException, SystemException {
289                    return getContact().getMale();
290            }
291    
292            public List<Group> getMySites() throws PortalException, SystemException {
293                    return getMySites(null, false, QueryUtil.ALL_POS);
294            }
295    
296            public List<Group> getMySites(boolean includeControlPanel, int max)
297                    throws PortalException, SystemException {
298    
299                    return getMySites(null, includeControlPanel, max);
300            }
301    
302            public List<Group> getMySites(int max)
303                    throws PortalException, SystemException {
304    
305                    return getMySites(null, false, max);
306            }
307    
308            public List<Group> getMySites(
309                            String[] classNames, boolean includeControlPanel, int max)
310                    throws PortalException, SystemException {
311    
312                    ThreadLocalCache<List<Group>> threadLocalCache =
313                            ThreadLocalCacheManager.getThreadLocalCache(
314                                    Lifecycle.REQUEST, UserImpl.class.getName());
315    
316                    String key = StringUtil.toHexString(max);
317    
318                    if ((classNames != null) && (classNames.length > 0)) {
319                            key = StringUtil.merge(classNames).concat(StringPool.POUND).concat(
320                                    key);
321                    }
322    
323                    key = key.concat(StringPool.POUND).concat(
324                            String.valueOf(includeControlPanel));
325    
326                    List<Group> myPlaces = threadLocalCache.get(key);
327    
328                    if (myPlaces != null) {
329                            return myPlaces;
330                    }
331    
332                    myPlaces = GroupServiceUtil.getUserPlaces(
333                            getUserId(), classNames, includeControlPanel, max);
334    
335                    threadLocalCache.put(key, myPlaces);
336    
337                    return myPlaces;
338            }
339    
340            public List<Group> getMySites(String[] classNames, int max)
341                    throws PortalException, SystemException {
342    
343                    return getMySites(classNames, false, max);
344            }
345    
346            public long[] getOrganizationIds() throws PortalException, SystemException {
347                    return getOrganizationIds(false);
348            }
349    
350            public long[] getOrganizationIds(boolean includeAdministrative)
351                    throws PortalException, SystemException {
352    
353                    List<Organization> organizations = getOrganizations(
354                            includeAdministrative);
355    
356                    long[] organizationIds = new long[organizations.size()];
357    
358                    for (int i = 0; i < organizations.size(); i++) {
359                            Organization organization = organizations.get(i);
360    
361                            organizationIds[i] = organization.getOrganizationId();
362                    }
363    
364                    return organizationIds;
365            }
366    
367            public List<Organization> getOrganizations()
368                    throws PortalException, SystemException {
369    
370                    return getOrganizations(false);
371            }
372    
373            public List<Organization> getOrganizations(boolean includeAdministrative)
374                    throws PortalException, SystemException {
375    
376                    return OrganizationLocalServiceUtil.getUserOrganizations(
377                            getUserId(), includeAdministrative);
378            }
379    
380            public boolean getPasswordModified() {
381                    return _passwordModified;
382            }
383    
384            public PasswordPolicy getPasswordPolicy()
385                    throws PortalException, SystemException {
386    
387                    if (_passwordPolicy == null) {
388                            _passwordPolicy =
389                                    PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
390                                            getUserId());
391                    }
392    
393                    return _passwordPolicy;
394            }
395    
396            public String getPasswordUnencrypted() {
397                    return _passwordUnencrypted;
398            }
399    
400            public List<Phone> getPhones() throws SystemException {
401                    return PhoneLocalServiceUtil.getPhones(
402                            getCompanyId(), Contact.class.getName(), getContactId());
403            }
404    
405            public String getPortraitURL(ThemeDisplay themeDisplay)
406                    throws PortalException, SystemException {
407    
408                    return UserConstants.getPortraitURL(
409                            themeDisplay.getPathImage(), isMale(), getPortraitId());
410            }
411    
412            public int getPrivateLayoutsPageCount()
413                    throws PortalException, SystemException {
414    
415                    return LayoutLocalServiceUtil.getLayoutsCount(this, true);
416            }
417    
418            public int getPublicLayoutsPageCount()
419                    throws PortalException, SystemException {
420    
421                    return LayoutLocalServiceUtil.getLayoutsCount(this, false);
422            }
423    
424            public Set<String> getReminderQueryQuestions()
425                    throws PortalException, SystemException {
426    
427                    Set<String> questions = new TreeSet<String>();
428    
429                    List<Organization> organizations =
430                            OrganizationLocalServiceUtil.getUserOrganizations(getUserId());
431    
432                    for (Organization organization : organizations) {
433                            Set<String> organizationQuestions =
434                                    organization.getReminderQueryQuestions(getLanguageId());
435    
436                            if (organizationQuestions.size() == 0) {
437                                    Organization parentOrganization =
438                                            organization.getParentOrganization();
439    
440                                    while ((organizationQuestions.size() == 0) &&
441                                               (parentOrganization != null)) {
442    
443                                            organizationQuestions =
444                                                    parentOrganization.getReminderQueryQuestions(
445                                                            getLanguageId());
446    
447                                            parentOrganization =
448                                                    parentOrganization.getParentOrganization();
449                                    }
450                            }
451    
452                            questions.addAll(organizationQuestions);
453                    }
454    
455                    if (questions.size() == 0) {
456                            Set<String> defaultQuestions = SetUtil.fromArray(
457                                    PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
458    
459                            questions.addAll(defaultQuestions);
460                    }
461    
462                    return questions;
463            }
464    
465            public long[] getRoleIds() throws SystemException {
466                    List<Role> roles = getRoles();
467    
468                    long[] roleIds = new long[roles.size()];
469    
470                    for (int i = 0; i < roles.size(); i++) {
471                            Role role = roles.get(i);
472    
473                            roleIds[i] = role.getRoleId();
474                    }
475    
476                    return roleIds;
477            }
478    
479            public List<Role> getRoles() throws SystemException {
480                    return RoleLocalServiceUtil.getUserRoles(getUserId());
481            }
482    
483            public long[] getTeamIds() throws SystemException {
484                    List<Team> teams = getTeams();
485    
486                    long[] teamIds = new long[teams.size()];
487    
488                    for (int i = 0; i < teams.size(); i++) {
489                            Team team = teams.get(i);
490    
491                            teamIds[i] = team.getTeamId();
492                    }
493    
494                    return teamIds;
495            }
496    
497            public List<Team> getTeams() throws SystemException {
498                    return TeamLocalServiceUtil.getUserTeams(getUserId());
499            }
500    
501            public TimeZone getTimeZone() {
502                    return _timeZone;
503            }
504    
505            public long[] getUserGroupIds() throws SystemException {
506                    List<UserGroup> userGroups = getUserGroups();
507    
508                    long[] userGroupIds = new long[userGroups.size()];
509    
510                    for (int i = 0; i < userGroups.size(); i++) {
511                            UserGroup userGroup = userGroups.get(i);
512    
513                            userGroupIds[i] = userGroup.getUserGroupId();
514                    }
515    
516                    return userGroupIds;
517            }
518    
519            public List<UserGroup> getUserGroups() throws SystemException {
520                    return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
521            }
522    
523            public List<Website> getWebsites() throws SystemException {
524                    return WebsiteLocalServiceUtil.getWebsites(
525                            getCompanyId(), Contact.class.getName(), getContactId());
526            }
527    
528            public boolean hasCompanyMx() throws PortalException, SystemException {
529                    return hasCompanyMx(getEmailAddress());
530            }
531    
532            public boolean hasCompanyMx(String emailAddress)
533                    throws PortalException, SystemException {
534    
535                    if (Validator.isNull(emailAddress)) {
536                            return false;
537                    }
538    
539                    Company company = CompanyLocalServiceUtil.getCompanyById(
540                            getCompanyId());
541    
542                    return company.hasCompanyMx(emailAddress);
543            }
544    
545            public boolean hasMySites() throws PortalException, SystemException {
546                    if (isDefaultUser()) {
547                            return false;
548                    }
549    
550                    int max = PropsValues.MY_SITES_MAX_ELEMENTS;
551    
552                    if (max == 1) {
553    
554                            // Increment so that we return more than just the Control Panel
555                            // group
556    
557                            max++;
558                    }
559    
560                    List<Group> groups = getMySites(true, max);
561    
562                    return !groups.isEmpty();
563            }
564    
565            public boolean hasOrganization() throws PortalException, SystemException {
566                    List<Organization> organizations = getOrganizations();
567    
568                    return !organizations.isEmpty();
569            }
570    
571            public boolean hasPrivateLayouts() throws PortalException, SystemException {
572                    return LayoutLocalServiceUtil.hasLayouts(this, true);
573            }
574    
575            public boolean hasPublicLayouts() throws PortalException, SystemException {
576                    return LayoutLocalServiceUtil.hasLayouts(this, false);
577            }
578    
579            public boolean hasReminderQuery() {
580                    if (Validator.isNotNull(getReminderQueryQuestion()) &&
581                            Validator.isNotNull(getReminderQueryAnswer())) {
582    
583                            return true;
584                    }
585                    else {
586                            return false;
587                    }
588            }
589    
590            public boolean isActive() {
591                    if (getStatus() == WorkflowConstants.STATUS_APPROVED) {
592                            return true;
593                    }
594                    else {
595                            return false;
596                    }
597            }
598    
599            public boolean isFemale() throws PortalException, SystemException {
600                    return getFemale();
601            }
602    
603            public boolean isMale() throws PortalException, SystemException {
604                    return getMale();
605            }
606    
607            public boolean isPasswordModified() {
608                    return _passwordModified;
609            }
610    
611            @Override
612            public void setLanguageId(String languageId) {
613                    _locale = LocaleUtil.fromLanguageId(languageId);
614    
615                    super.setLanguageId(LocaleUtil.toLanguageId(_locale));
616            }
617    
618            public void setPasswordModified(boolean passwordModified) {
619                    _passwordModified = passwordModified;
620            }
621    
622            public void setPasswordUnencrypted(String passwordUnencrypted) {
623                    _passwordUnencrypted = passwordUnencrypted;
624            }
625    
626            @Override
627            public void setTimeZoneId(String timeZoneId) {
628                    if (Validator.isNull(timeZoneId)) {
629                            timeZoneId = TimeZoneUtil.getDefault().getID();
630                    }
631    
632                    _timeZone = TimeZoneUtil.getTimeZone(timeZoneId);
633    
634                    super.setTimeZoneId(timeZoneId);
635            }
636    
637            private Locale _locale;
638            private boolean _passwordModified;
639            private PasswordPolicy _passwordPolicy;
640            private String _passwordUnencrypted;
641            private TimeZone _timeZone;
642    
643    }