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