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