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