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