001
014
015 package com.liferay.portal.model.impl;
016
017 import com.liferay.portal.kernel.bean.AutoEscape;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.shard.ShardUtil;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.util.Digester;
023 import com.liferay.portal.kernel.util.DigesterUtil;
024 import com.liferay.portal.kernel.util.HtmlUtil;
025 import com.liferay.portal.kernel.util.LocaleUtil;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.kernel.util.RemotePreference;
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.EmailAddress;
040 import com.liferay.portal.model.Group;
041 import com.liferay.portal.model.Organization;
042 import com.liferay.portal.model.PasswordPolicy;
043 import com.liferay.portal.model.Phone;
044 import com.liferay.portal.model.Role;
045 import com.liferay.portal.model.Team;
046 import com.liferay.portal.model.UserConstants;
047 import com.liferay.portal.model.UserGroup;
048 import com.liferay.portal.model.Website;
049 import com.liferay.portal.security.auth.EmailAddressGenerator;
050 import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
051 import com.liferay.portal.security.auth.FullNameGenerator;
052 import com.liferay.portal.security.auth.FullNameGeneratorFactory;
053 import com.liferay.portal.service.AddressLocalServiceUtil;
054 import com.liferay.portal.service.CompanyLocalServiceUtil;
055 import com.liferay.portal.service.ContactLocalServiceUtil;
056 import com.liferay.portal.service.EmailAddressLocalServiceUtil;
057 import com.liferay.portal.service.GroupLocalServiceUtil;
058 import com.liferay.portal.service.GroupServiceUtil;
059 import com.liferay.portal.service.LayoutLocalServiceUtil;
060 import com.liferay.portal.service.OrganizationLocalServiceUtil;
061 import com.liferay.portal.service.PasswordPolicyLocalServiceUtil;
062 import com.liferay.portal.service.PhoneLocalServiceUtil;
063 import com.liferay.portal.service.RoleLocalServiceUtil;
064 import com.liferay.portal.service.TeamLocalServiceUtil;
065 import com.liferay.portal.service.UserGroupLocalServiceUtil;
066 import com.liferay.portal.service.WebsiteLocalServiceUtil;
067 import com.liferay.portal.theme.ThemeDisplay;
068 import com.liferay.portal.util.Portal;
069 import com.liferay.portal.util.PortalUtil;
070 import com.liferay.portal.util.PropsUtil;
071 import com.liferay.portal.util.PropsValues;
072
073 import java.util.Collection;
074 import java.util.Collections;
075 import java.util.Date;
076 import java.util.HashMap;
077 import java.util.List;
078 import java.util.Locale;
079 import java.util.Map;
080 import java.util.Set;
081 import java.util.TimeZone;
082 import java.util.TreeSet;
083
084
089 public class UserImpl extends UserBaseImpl {
090
091 public UserImpl() {
092 }
093
094 @Override
095 public void addRemotePreference(RemotePreference remotePreference) {
096 _remotePreferences.put(remotePreference.getName(), remotePreference);
097 }
098
099 @Override
100 public List<Address> getAddresses() throws SystemException {
101 return AddressLocalServiceUtil.getAddresses(
102 getCompanyId(), Contact.class.getName(), getContactId());
103 }
104
105 @Override
106 public Date getBirthday() throws PortalException, SystemException {
107 return getContact().getBirthday();
108 }
109
110 @Override
111 public String getCompanyMx() throws PortalException, SystemException {
112 Company company = CompanyLocalServiceUtil.getCompanyById(
113 getCompanyId());
114
115 return company.getMx();
116 }
117
118 @Override
119 public Contact getContact() throws PortalException, SystemException {
120 try {
121 ShardUtil.pushCompanyService(getCompanyId());
122
123 return ContactLocalServiceUtil.getContact(getContactId());
124 }
125 finally {
126 ShardUtil.popCompanyService();
127 }
128 }
129
130 @Override
131 public String getDigest() {
132 String digest = super.getDigest();
133
134 if (Validator.isNull(digest) && !isPasswordEncrypted()) {
135 digest = getDigest(getPassword());
136 }
137
138 return digest;
139 }
140
141 @Override
142 public String getDigest(String password) {
143 if (Validator.isNull(getScreenName())) {
144 throw new IllegalStateException("Screen name is null");
145 }
146 else if (Validator.isNull(getEmailAddress())) {
147 throw new IllegalStateException("Email address is null");
148 }
149
150 StringBundler sb = new StringBundler(5);
151
152 String digest1 = DigesterUtil.digestHex(
153 Digester.MD5, getEmailAddress(), Portal.PORTAL_REALM, password);
154
155 sb.append(digest1);
156 sb.append(StringPool.COMMA);
157
158 String digest2 = DigesterUtil.digestHex(
159 Digester.MD5, getScreenName(), Portal.PORTAL_REALM, password);
160
161 sb.append(digest2);
162 sb.append(StringPool.COMMA);
163
164 String digest3 = DigesterUtil.digestHex(
165 Digester.MD5, String.valueOf(getUserId()), Portal.PORTAL_REALM,
166 password);
167
168 sb.append(digest3);
169
170 return sb.toString();
171 }
172
173 @Override
174 public String getDisplayEmailAddress() {
175 String emailAddress = super.getEmailAddress();
176
177 EmailAddressGenerator emailAddressGenerator =
178 EmailAddressGeneratorFactory.getInstance();
179
180 if (emailAddressGenerator.isFake(emailAddress)) {
181 emailAddress = StringPool.BLANK;
182 }
183
184 return emailAddress;
185 }
186
187 @Override
188 public String getDisplayURL(String portalURL, String mainPath)
189 throws PortalException, SystemException {
190
191 return getDisplayURL(portalURL, mainPath, false);
192 }
193
194 @Override
195 public String getDisplayURL(
196 String portalURL, String mainPath, boolean privateLayout)
197 throws PortalException, SystemException {
198
199 if (isDefaultUser()) {
200 return StringPool.BLANK;
201 }
202
203 String profileFriendlyURL = getProfileFriendlyURL();
204
205 if (Validator.isNotNull(profileFriendlyURL)) {
206 return portalURL.concat(PortalUtil.getPathContext()).concat(
207 profileFriendlyURL);
208 }
209
210 Group group = getGroup();
211
212 int publicLayoutsPageCount = group.getPublicLayoutsPageCount();
213
214 if (publicLayoutsPageCount > 0) {
215 StringBundler sb = new StringBundler(5);
216
217 sb.append(portalURL);
218 sb.append(mainPath);
219 sb.append("/my_sites/view?groupId=");
220 sb.append(group.getGroupId());
221
222 if (privateLayout) {
223 sb.append("&privateLayout=1");
224 }
225 else {
226 sb.append("&privateLayout=0");
227 }
228
229 return sb.toString();
230 }
231
232 return StringPool.BLANK;
233 }
234
235 @Override
236 public String getDisplayURL(ThemeDisplay themeDisplay)
237 throws PortalException, SystemException {
238
239 return getDisplayURL(
240 themeDisplay.getPortalURL(), themeDisplay.getPathMain(), false);
241 }
242
243 @Override
244 public String getDisplayURL(
245 ThemeDisplay themeDisplay, boolean privateLayout)
246 throws PortalException, SystemException {
247
248 return getDisplayURL(
249 themeDisplay.getPortalURL(), themeDisplay.getPathMain(),
250 privateLayout);
251 }
252
253 @Override
254 public List<EmailAddress> getEmailAddresses() throws SystemException {
255 return EmailAddressLocalServiceUtil.getEmailAddresses(
256 getCompanyId(), Contact.class.getName(), getContactId());
257 }
258
259 @Override
260 public boolean getFemale() throws PortalException, SystemException {
261 return !getMale();
262 }
263
264 @AutoEscape
265 @Override
266 public String getFullName() {
267 FullNameGenerator fullNameGenerator =
268 FullNameGeneratorFactory.getInstance();
269
270 return fullNameGenerator.getFullName(
271 getFirstName(), getMiddleName(), getLastName());
272 }
273
274 @Override
275 public Group getGroup() throws PortalException, SystemException {
276 return GroupLocalServiceUtil.getUserGroup(getCompanyId(), getUserId());
277 }
278
279 @Override
280 public long getGroupId() throws PortalException, SystemException {
281 Group group = getGroup();
282
283 return group.getGroupId();
284 }
285
286 @Override
287 public long[] getGroupIds() throws SystemException {
288 List<Group> groups = getGroups();
289
290 long[] groupIds = new long[groups.size()];
291
292 for (int i = 0; i < groups.size(); i++) {
293 Group group = groups.get(i);
294
295 groupIds[i] = group.getGroupId();
296 }
297
298 return groupIds;
299 }
300
301 @Override
302 public List<Group> getGroups() throws SystemException {
303 return GroupLocalServiceUtil.getUserGroups(getUserId());
304 }
305
306 @Override
307 public Locale getLocale() {
308 return _locale;
309 }
310
311 @Override
312 public String getLogin() throws PortalException, SystemException {
313 String login = null;
314
315 Company company = CompanyLocalServiceUtil.getCompanyById(
316 getCompanyId());
317
318 if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
319 login = getEmailAddress();
320 }
321 else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
322 login = getScreenName();
323 }
324 else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
325 login = String.valueOf(getUserId());
326 }
327
328 return login;
329 }
330
331 @Override
332 public boolean getMale() throws PortalException, SystemException {
333 return getContact().getMale();
334 }
335
336 @Override
337 public List<Group> getMySiteGroups()
338 throws PortalException, SystemException {
339
340 return getMySiteGroups(null, false, QueryUtil.ALL_POS);
341 }
342
343 @Override
344 public List<Group> getMySiteGroups(boolean includeControlPanel, int max)
345 throws PortalException, SystemException {
346
347 return getMySiteGroups(null, includeControlPanel, max);
348 }
349
350 @Override
351 public List<Group> getMySiteGroups(int max)
352 throws PortalException, SystemException {
353
354 return getMySiteGroups(null, false, max);
355 }
356
357 @Override
358 public List<Group> getMySiteGroups(
359 String[] classNames, boolean includeControlPanel, int max)
360 throws PortalException, SystemException {
361
362 return GroupServiceUtil.getUserSitesGroups(
363 getUserId(), classNames, includeControlPanel, max);
364 }
365
366 @Override
367 public List<Group> getMySiteGroups(String[] classNames, int max)
368 throws PortalException, SystemException {
369
370 return getMySiteGroups(classNames, false, max);
371 }
372
373
376 @Override
377 public List<Group> getMySites() throws PortalException, SystemException {
378 return getMySiteGroups();
379 }
380
381
385 @Override
386 public List<Group> getMySites(boolean includeControlPanel, int max)
387 throws PortalException, SystemException {
388
389 return getMySiteGroups(includeControlPanel, max);
390 }
391
392
395 @Override
396 public List<Group> getMySites(int max)
397 throws PortalException, SystemException {
398
399 return getMySiteGroups(max);
400 }
401
402
406 @Override
407 public List<Group> getMySites(
408 String[] classNames, boolean includeControlPanel, int max)
409 throws PortalException, SystemException {
410
411 return getMySiteGroups(classNames, includeControlPanel, max);
412 }
413
414
418 @Override
419 public List<Group> getMySites(String[] classNames, int max)
420 throws PortalException, SystemException {
421
422 return getMySiteGroups(classNames, max);
423 }
424
425 @Override
426 public long[] getOrganizationIds() throws PortalException, SystemException {
427 return getOrganizationIds(false);
428 }
429
430 @Override
431 public long[] getOrganizationIds(boolean includeAdministrative)
432 throws PortalException, SystemException {
433
434 List<Organization> organizations = getOrganizations(
435 includeAdministrative);
436
437 long[] organizationIds = new long[organizations.size()];
438
439 for (int i = 0; i < organizations.size(); i++) {
440 Organization organization = organizations.get(i);
441
442 organizationIds[i] = organization.getOrganizationId();
443 }
444
445 return organizationIds;
446 }
447
448 @Override
449 public List<Organization> getOrganizations()
450 throws PortalException, SystemException {
451
452 return getOrganizations(false);
453 }
454
455 @Override
456 public List<Organization> getOrganizations(boolean includeAdministrative)
457 throws PortalException, SystemException {
458
459 return OrganizationLocalServiceUtil.getUserOrganizations(
460 getUserId(), includeAdministrative);
461 }
462
463 @Override
464 public boolean getPasswordModified() {
465 return _passwordModified;
466 }
467
468 @Override
469 public PasswordPolicy getPasswordPolicy()
470 throws PortalException, SystemException {
471
472 if (_passwordPolicy == null) {
473 _passwordPolicy =
474 PasswordPolicyLocalServiceUtil.getPasswordPolicyByUserId(
475 getUserId());
476 }
477
478 return _passwordPolicy;
479 }
480
481 @Override
482 public String getPasswordUnencrypted() {
483 return _passwordUnencrypted;
484 }
485
486 @Override
487 public List<Phone> getPhones() throws SystemException {
488 return PhoneLocalServiceUtil.getPhones(
489 getCompanyId(), Contact.class.getName(), getContactId());
490 }
491
492 @Override
493 public String getPortraitURL(ThemeDisplay themeDisplay)
494 throws PortalException, SystemException {
495
496 return UserConstants.getPortraitURL(
497 themeDisplay.getPathImage(), isMale(), getPortraitId());
498 }
499
500 @Override
501 public int getPrivateLayoutsPageCount()
502 throws PortalException, SystemException {
503
504 return LayoutLocalServiceUtil.getLayoutsCount(this, true);
505 }
506
507 @Override
508 public int getPublicLayoutsPageCount()
509 throws PortalException, SystemException {
510
511 return LayoutLocalServiceUtil.getLayoutsCount(this, false);
512 }
513
514 @Override
515 public Set<String> getReminderQueryQuestions()
516 throws PortalException, SystemException {
517
518 Set<String> questions = new TreeSet<String>();
519
520 List<Organization> organizations =
521 OrganizationLocalServiceUtil.getUserOrganizations(getUserId());
522
523 for (Organization organization : organizations) {
524 Set<String> organizationQuestions =
525 organization.getReminderQueryQuestions(getLanguageId());
526
527 if (organizationQuestions.size() == 0) {
528 Organization parentOrganization =
529 organization.getParentOrganization();
530
531 while ((organizationQuestions.size() == 0) &&
532 (parentOrganization != null)) {
533
534 organizationQuestions =
535 parentOrganization.getReminderQueryQuestions(
536 getLanguageId());
537
538 parentOrganization =
539 parentOrganization.getParentOrganization();
540 }
541 }
542
543 questions.addAll(organizationQuestions);
544 }
545
546 if (questions.size() == 0) {
547 Set<String> defaultQuestions = SetUtil.fromArray(
548 PropsUtil.getArray(PropsKeys.USERS_REMINDER_QUERIES_QUESTIONS));
549
550 questions.addAll(defaultQuestions);
551 }
552
553 return questions;
554 }
555
556 @Override
557 public RemotePreference getRemotePreference(String name) {
558 return _remotePreferences.get(name);
559 }
560
561 @Override
562 public Iterable<RemotePreference> getRemotePreferences() {
563 Collection<RemotePreference> values = _remotePreferences.values();
564
565 return Collections.unmodifiableCollection(values);
566 }
567
568 @Override
569 public long[] getRoleIds() throws SystemException {
570 List<Role> roles = getRoles();
571
572 long[] roleIds = new long[roles.size()];
573
574 for (int i = 0; i < roles.size(); i++) {
575 Role role = roles.get(i);
576
577 roleIds[i] = role.getRoleId();
578 }
579
580 return roleIds;
581 }
582
583 @Override
584 public List<Role> getRoles() throws SystemException {
585 return RoleLocalServiceUtil.getUserRoles(getUserId());
586 }
587
588 @Override
589 public List<Group> getSiteGroups() throws PortalException, SystemException {
590 return getSiteGroups(false);
591 }
592
593 @Override
594 public List<Group> getSiteGroups(boolean includeAdministrative)
595 throws PortalException, SystemException {
596
597 return GroupLocalServiceUtil.getUserSitesGroups(
598 getUserId(), includeAdministrative);
599 }
600
601 @Override
602 public long[] getTeamIds() throws SystemException {
603 List<Team> teams = getTeams();
604
605 long[] teamIds = new long[teams.size()];
606
607 for (int i = 0; i < teams.size(); i++) {
608 Team team = teams.get(i);
609
610 teamIds[i] = team.getTeamId();
611 }
612
613 return teamIds;
614 }
615
616 @Override
617 public List<Team> getTeams() throws SystemException {
618 return TeamLocalServiceUtil.getUserTeams(getUserId());
619 }
620
621 @Override
622 public TimeZone getTimeZone() {
623 return _timeZone;
624 }
625
626 @Override
627 public long[] getUserGroupIds() throws SystemException {
628 List<UserGroup> userGroups = getUserGroups();
629
630 long[] userGroupIds = new long[userGroups.size()];
631
632 for (int i = 0; i < userGroups.size(); i++) {
633 UserGroup userGroup = userGroups.get(i);
634
635 userGroupIds[i] = userGroup.getUserGroupId();
636 }
637
638 return userGroupIds;
639 }
640
641 @Override
642 public List<UserGroup> getUserGroups() throws SystemException {
643 return UserGroupLocalServiceUtil.getUserUserGroups(getUserId());
644 }
645
646 @Override
647 public List<Website> getWebsites() throws SystemException {
648 return WebsiteLocalServiceUtil.getWebsites(
649 getCompanyId(), Contact.class.getName(), getContactId());
650 }
651
652 @Override
653 public boolean hasCompanyMx() throws PortalException, SystemException {
654 return hasCompanyMx(getEmailAddress());
655 }
656
657 @Override
658 public boolean hasCompanyMx(String emailAddress)
659 throws PortalException, SystemException {
660
661 if (Validator.isNull(emailAddress)) {
662 return false;
663 }
664
665 Company company = CompanyLocalServiceUtil.getCompanyById(
666 getCompanyId());
667
668 return company.hasCompanyMx(emailAddress);
669 }
670
671 @Override
672 public boolean hasMySites() throws PortalException, SystemException {
673 if (isDefaultUser()) {
674 return false;
675 }
676
677 int max = PropsValues.MY_SITES_MAX_ELEMENTS;
678
679 if (max == 1) {
680
681
682
683
684 max++;
685 }
686
687 List<Group> groups = getMySiteGroups(true, max);
688
689 return !groups.isEmpty();
690 }
691
692 @Override
693 public boolean hasOrganization() throws PortalException, SystemException {
694 List<Organization> organizations = getOrganizations();
695
696 return !organizations.isEmpty();
697 }
698
699 @Override
700 public boolean hasPrivateLayouts() throws PortalException, SystemException {
701 return LayoutLocalServiceUtil.hasLayouts(this, true);
702 }
703
704 @Override
705 public boolean hasPublicLayouts() throws PortalException, SystemException {
706 return LayoutLocalServiceUtil.hasLayouts(this, false);
707 }
708
709 @Override
710 public boolean hasReminderQuery() {
711 if (Validator.isNotNull(getReminderQueryQuestion()) &&
712 Validator.isNotNull(getReminderQueryAnswer())) {
713
714 return true;
715 }
716 else {
717 return false;
718 }
719 }
720
721 @Override
722 public boolean isActive() {
723 if (getStatus() == WorkflowConstants.STATUS_APPROVED) {
724 return true;
725 }
726 else {
727 return false;
728 }
729 }
730
731 @Override
732 public boolean isFemale() throws PortalException, SystemException {
733 return getFemale();
734 }
735
736 @Override
737 public boolean isMale() throws PortalException, SystemException {
738 return getMale();
739 }
740
741 @Override
742 public boolean isPasswordModified() {
743 return _passwordModified;
744 }
745
746 @Override
747 public void setLanguageId(String languageId) {
748 _locale = LocaleUtil.fromLanguageId(languageId);
749
750 super.setLanguageId(LocaleUtil.toLanguageId(_locale));
751 }
752
753 @Override
754 public void setPasswordModified(boolean passwordModified) {
755 _passwordModified = passwordModified;
756 }
757
758 @Override
759 public void setPasswordUnencrypted(String passwordUnencrypted) {
760 _passwordUnencrypted = passwordUnencrypted;
761 }
762
763 @Override
764 public void setTimeZoneId(String timeZoneId) {
765 if (Validator.isNull(timeZoneId)) {
766 timeZoneId = TimeZoneUtil.getDefault().getID();
767 }
768
769 _timeZone = TimeZoneUtil.getTimeZone(timeZoneId);
770
771 super.setTimeZoneId(timeZoneId);
772 }
773
774 protected String getProfileFriendlyURL() {
775 if (Validator.isNull(PropsValues.USERS_PROFILE_FRIENDLY_URL)) {
776 return null;
777 }
778
779 return StringUtil.replace(
780 PropsValues.USERS_PROFILE_FRIENDLY_URL,
781 new String[] {"${liferay:screenName}", "${liferay:userId}"},
782 new String[] {
783 HtmlUtil.escapeURL(getScreenName()), String.valueOf(getUserId())
784 });
785 }
786
787 private Locale _locale;
788 private boolean _passwordModified;
789 private PasswordPolicy _passwordPolicy;
790 private String _passwordUnencrypted;
791 private transient Map<String, RemotePreference> _remotePreferences =
792 new HashMap<String, RemotePreference>();
793 private TimeZone _timeZone;
794
795 }