001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(ThemeDisplay themeDisplay)
162                    throws PortalException, SystemException {
163    
164                    return getDisplayURL(
165                            themeDisplay.getPortalURL(), themeDisplay.getPathMain());
166            }
167    
168            public String getDisplayURL(String portalURL, String mainPath)
169                    throws PortalException, SystemException {
170    
171                    if (isDefaultUser()) {
172                            return StringPool.BLANK;
173                    }
174    
175                    Group group = getGroup();
176    
177                    int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
178    
179                    if (publicLayoutsPageCount > 0) {
180                            StringBundler sb = new StringBundler(5);
181    
182                            sb.append(portalURL);
183                            sb.append(mainPath);
184                            sb.append("/my_sites/view?groupId=");
185                            sb.append(group.getGroupId());
186                            sb.append("&privateLayout=0");
187    
188                            return sb.toString();
189                    }
190    
191                    return StringPool.BLANK;
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, QueryUtil.ALL_POS);
264            }
265    
266            public List<Group> getMySites(int max)
267                    throws PortalException, SystemException {
268    
269                    return getMySites(null, max);
270            }
271    
272            public List<Group> getMySites(String[] classNames, int max)
273                    throws PortalException, SystemException {
274    
275                    ThreadLocalCache<List<Group>> threadLocalCache =
276                            ThreadLocalCacheManager.getThreadLocalCache(
277                                    Lifecycle.REQUEST, UserImpl.class.getName());
278    
279                    String key = StringUtil.toHexString(max);
280    
281                    if ((classNames != null) && (classNames.length > 0)) {
282                            key = StringUtil.merge(classNames).concat(StringPool.POUND).concat(
283                                    key);
284                    }
285    
286                    List<Group> myPlaces = threadLocalCache.get(key);
287    
288                    if (myPlaces != null) {
289                            return myPlaces;
290                    }
291    
292                    myPlaces = GroupServiceUtil.getUserPlaces(getUserId(), classNames, max);
293    
294                    threadLocalCache.put(key, myPlaces);
295    
296                    return myPlaces;
297            }
298    
299            public long[] getOrganizationIds() throws PortalException, SystemException {
300                    List<Organization> organizations = getOrganizations();
301    
302                    long[] organizationIds = new long[organizations.size()];
303    
304                    for (int i = 0; i < organizations.size(); i++) {
305                            Organization organization = organizations.get(i);
306    
307                            organizationIds[i] = organization.getOrganizationId();
308                    }
309    
310                    return organizationIds;
311            }
312    
313            public List<Organization> getOrganizations()
314                    throws PortalException, SystemException {
315    
316                    return OrganizationLocalServiceUtil.getUserOrganizations(
317                            getUserId());
318            }
319    
320            public boolean getPasswordModified() {
321                    return _passwordModified;
322            }
323    
324            public PasswordPolicy getPasswordPolicy()
325                    throws PortalException, SystemException {
326    
327                    if (_passwordPolicy == null) {
328                            _passwordPolicy =
329                                    PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
330                                            getUserId());
331                    }
332    
333                    return _passwordPolicy;
334            }
335    
336            public String getPasswordUnencrypted() {
337                    return _passwordUnencrypted;
338            }
339    
340            public List<Phone> getPhones() throws SystemException {
341                    return PhoneLocalServiceUtil.getPhones(
342                            getCompanyId(), Contact.class.getName(), getContactId());
343            }
344    
345            public String getPortraitURL(ThemeDisplay themeDisplay)
346                    throws PortalException, SystemException {
347    
348                    return UserConstants.getPortraitURL(
349                            themeDisplay.getPathImage(), isMale(), getPortraitId());
350            }
351    
352            public int getPrivateLayoutsPageCount()
353                    throws PortalException, SystemException {
354    
355                    Group group = getGroup();
356    
357                    return group.getPrivateLayoutsPageCount();
358            }
359    
360            public int getPublicLayoutsPageCount()
361                    throws PortalException, SystemException {
362    
363                    Group group = getGroup();
364    
365                    return group.getPublicLayoutsPageCount();
366            }
367    
368            public Set<String> getReminderQueryQuestions()
369                    throws PortalException, SystemException {
370    
371                    Set<String> questions = new TreeSet<String>();
372    
373                    List<Organization> organizations =
374                            OrganizationLocalServiceUtil.getUserOrganizations(
375                                    getUserId(), true);
376    
377                    for (Organization organization : organizations) {
378                            Set<String> organizationQuestions =
379                                    organization.getReminderQueryQuestions(getLanguageId());
380    
381                            if (organizationQuestions.size() == 0) {
382                                    Organization parentOrganization =
383                                            organization.getParentOrganization();
384    
385                                    while ((organizationQuestions.size() == 0) &&
386                                                    (parentOrganization != null)) {
387    
388                                            organizationQuestions =
389                                                    parentOrganization.getReminderQueryQuestions(
390                                                            getLanguageId());
391    
392                                            parentOrganization =
393                                                    parentOrganization.getParentOrganization();
394                                    }
395                            }
396    
397                            questions.addAll(organizationQuestions);
398                    }
399    
400                    if (questions.size() == 0) {
401                            Set<String> defaultQuestions = SetUtil.fromArray(
402                                    PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
403    
404                            questions.addAll(defaultQuestions);
405                    }
406    
407                    return questions;
408            }
409    
410            public long[] getRoleIds() throws SystemException {
411                    List<Role> roles = getRoles();
412    
413                    long[] roleIds = new long[roles.size()];
414    
415                    for (int i = 0; i < roles.size(); i++) {
416                            Role role = roles.get(i);
417    
418                            roleIds[i] = role.getRoleId();
419                    }
420    
421                    return roleIds;
422            }
423    
424            public List<Role> getRoles() throws SystemException {
425                    return RoleLocalServiceUtil.getUserRoles(getUserId());
426            }
427    
428            public long[] getTeamIds() throws SystemException {
429                    List<Team> teams = getTeams();
430    
431                    long[] teamIds = new long[teams.size()];
432    
433                    for (int i = 0; i < teams.size(); i++) {
434                            Team team = teams.get(i);
435    
436                            teamIds[i] = team.getTeamId();
437                    }
438    
439                    return teamIds;
440            }
441    
442            public List<Team> getTeams() throws SystemException {
443                    return TeamLocalServiceUtil.getUserTeams(getUserId());
444            }
445    
446            public long[] getUserGroupIds() throws SystemException {
447                    List<UserGroup> userGroups = getUserGroups();
448    
449                    long[] userGroupIds = new long[userGroups.size()];
450    
451                    for (int i = 0; i < userGroups.size(); i++) {
452                            UserGroup userGroup = userGroups.get(i);
453    
454                            userGroupIds[i] = userGroup.getUserGroupId();
455                    }
456    
457                    return userGroupIds;
458            }
459    
460            public List<UserGroup> getUserGroups() throws SystemException {
461                    return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
462            }
463    
464            public TimeZone getTimeZone() {
465                    return _timeZone;
466            }
467    
468            public boolean hasCompanyMx() throws PortalException, SystemException {
469                    return hasCompanyMx(getEmailAddress());
470            }
471    
472            public boolean hasCompanyMx(String emailAddress)
473                    throws PortalException, SystemException {
474    
475                    if (Validator.isNull(emailAddress)) {
476                            return false;
477                    }
478    
479                    Company company = CompanyLocalServiceUtil.getCompanyById(
480                            getCompanyId());
481    
482                    return company.hasCompanyMx(emailAddress);
483            }
484    
485            public boolean hasMySites() throws PortalException, SystemException {
486                    if (isDefaultUser()) {
487                            return false;
488                    }
489    
490                    List<Group> groups = getMySites(PropsValues.MY_SITES_MAX_ELEMENTS);
491    
492                    if (groups.size() > 0) {
493                            return true;
494                    }
495                    else {
496                            return false;
497                    }
498            }
499    
500            public boolean hasOrganization() throws PortalException, SystemException {
501                    if (getOrganizations().size() > 0) {
502                            return true;
503                    }
504                    else {
505                            return false;
506                    }
507            }
508    
509            public boolean hasPrivateLayouts() throws PortalException, SystemException {
510                    if (getPrivateLayoutsPageCount() > 0) {
511                            return true;
512                    }
513                    else {
514                            return false;
515                    }
516            }
517    
518            public boolean hasPublicLayouts() throws PortalException, SystemException {
519                    if (getPublicLayoutsPageCount() > 0) {
520                            return true;
521                    }
522                    else {
523                            return false;
524                    }
525            }
526    
527            public boolean hasReminderQuery() {
528                    if (Validator.isNotNull(getReminderQueryQuestion()) &&
529                            Validator.isNotNull(getReminderQueryAnswer())) {
530    
531                            return true;
532                    }
533                    else {
534                            return false;
535                    }
536            }
537    
538            public boolean isActive() {
539                    if (getStatus() == WorkflowConstants.STATUS_APPROVED) {
540                            return true;
541                    }
542                    else {
543                            return false;
544                    }
545            }
546    
547            public boolean isFemale() throws PortalException, SystemException {
548                    return getFemale();
549            }
550    
551            public boolean isMale() throws PortalException, SystemException {
552                    return getMale();
553            }
554    
555            public boolean isPasswordModified() {
556                    return _passwordModified;
557            }
558    
559            @Override
560            public void setLanguageId(String languageId) {
561                    _locale = LocaleUtil.fromLanguageId(languageId);
562    
563                    super.setLanguageId(LocaleUtil.toLanguageId(_locale));
564            }
565    
566            public void setPasswordModified(boolean passwordModified) {
567                    _passwordModified = passwordModified;
568            }
569    
570            public void setPasswordUnencrypted(String passwordUnencrypted) {
571                    _passwordUnencrypted = passwordUnencrypted;
572            }
573    
574            @Override
575            public void setTimeZoneId(String timeZoneId) {
576                    if (Validator.isNull(timeZoneId)) {
577                            timeZoneId = TimeZoneUtil.getDefault().getID();
578                    }
579    
580                    _timeZone = TimeZoneUtil.getTimeZone(timeZoneId);
581    
582                    super.setTimeZoneId(timeZoneId);
583            }
584    
585            public List<Website> getWebsites() throws SystemException {
586                    return WebsiteLocalServiceUtil.getWebsites(
587                            getCompanyId(), Contact.class.getName(), getContactId());
588            }
589    
590            private Locale _locale;
591            private boolean _passwordModified;
592            private PasswordPolicy _passwordPolicy;
593            private String _passwordUnencrypted;
594            private TimeZone _timeZone;
595    
596    }