001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.RequiredUserException;
018 import com.liferay.portal.ReservedUserEmailAddressException;
019 import com.liferay.portal.UserFieldException;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.search.Indexer;
023 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024 import com.liferay.portal.kernel.util.ArrayUtil;
025 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
026 import com.liferay.portal.kernel.util.ListUtil;
027 import com.liferay.portal.kernel.util.ParamUtil;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.kernel.workflow.WorkflowConstants;
030 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
031 import com.liferay.portal.model.Address;
032 import com.liferay.portal.model.Company;
033 import com.liferay.portal.model.CompanyConstants;
034 import com.liferay.portal.model.Contact;
035 import com.liferay.portal.model.EmailAddress;
036 import com.liferay.portal.model.Group;
037 import com.liferay.portal.model.GroupConstants;
038 import com.liferay.portal.model.Organization;
039 import com.liferay.portal.model.Phone;
040 import com.liferay.portal.model.Role;
041 import com.liferay.portal.model.RoleConstants;
042 import com.liferay.portal.model.User;
043 import com.liferay.portal.model.UserGroup;
044 import com.liferay.portal.model.UserGroupRole;
045 import com.liferay.portal.model.Website;
046 import com.liferay.portal.security.auth.PrincipalException;
047 import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
048 import com.liferay.portal.security.membershippolicy.RoleMembershipPolicyUtil;
049 import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
050 import com.liferay.portal.security.membershippolicy.UserGroupMembershipPolicyUtil;
051 import com.liferay.portal.security.permission.ActionKeys;
052 import com.liferay.portal.security.permission.PermissionChecker;
053 import com.liferay.portal.service.ServiceContext;
054 import com.liferay.portal.service.base.UserServiceBaseImpl;
055 import com.liferay.portal.service.permission.GroupPermissionUtil;
056 import com.liferay.portal.service.permission.OrganizationPermissionUtil;
057 import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
058 import com.liferay.portal.service.permission.PortalPermissionUtil;
059 import com.liferay.portal.service.permission.RolePermissionUtil;
060 import com.liferay.portal.service.permission.TeamPermissionUtil;
061 import com.liferay.portal.service.permission.UserGroupPermissionUtil;
062 import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
063 import com.liferay.portal.service.permission.UserPermissionUtil;
064 import com.liferay.portal.util.PropsValues;
065 import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
066 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
067
068 import java.util.ArrayList;
069 import java.util.Calendar;
070 import java.util.List;
071 import java.util.Locale;
072
073
083 public class UserServiceImpl extends UserServiceBaseImpl {
084
085
098 @Override
099 public void addGroupUsers(
100 long groupId, long[] userIds, ServiceContext serviceContext)
101 throws PortalException, SystemException {
102
103 if (userIds.length == 0) {
104 return;
105 }
106
107 try {
108 GroupPermissionUtil.check(
109 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
110 }
111 catch (PrincipalException pe) {
112
113
114
115 boolean hasPermission = false;
116
117 if (userIds.length == 1) {
118 User user = getUser();
119
120 if (user.getUserId() == userIds[0]) {
121 Group group = groupPersistence.findByPrimaryKey(groupId);
122
123 if (user.getCompanyId() == group.getCompanyId()) {
124 int type = group.getType();
125
126 if (type == GroupConstants.TYPE_SITE_OPEN) {
127 hasPermission = true;
128 }
129 }
130 }
131 }
132
133 if (!hasPermission) {
134 throw new PrincipalException();
135 }
136 }
137
138 SiteMembershipPolicyUtil.checkMembership(
139 userIds, new long[] {groupId}, null);
140
141 userLocalService.addGroupUsers(groupId, userIds);
142
143 SiteMembershipPolicyUtil.propagateMembership(
144 userIds, new long[] {groupId}, null);
145 }
146
147
159 @Override
160 public void addOrganizationUsers(long organizationId, long[] userIds)
161 throws PortalException, SystemException {
162
163 if (userIds.length == 0) {
164 return;
165 }
166
167 OrganizationPermissionUtil.check(
168 getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
169
170 validateOrganizationUsers(userIds);
171
172 OrganizationMembershipPolicyUtil.checkMembership(
173 userIds, new long[] {organizationId}, null);
174
175 userLocalService.addOrganizationUsers(organizationId, userIds);
176
177 OrganizationMembershipPolicyUtil.propagateMembership(
178 userIds, new long[] {organizationId}, null);
179 }
180
181
191 @Override
192 public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
193 throws PortalException, SystemException {
194
195 if (userIds.length == 0) {
196 return;
197 }
198
199 PasswordPolicyPermissionUtil.check(
200 getPermissionChecker(), passwordPolicyId,
201 ActionKeys.ASSIGN_MEMBERS);
202
203 userLocalService.addPasswordPolicyUsers(passwordPolicyId, userIds);
204 }
205
206
217 @Override
218 public void addRoleUsers(long roleId, long[] userIds)
219 throws PortalException, SystemException {
220
221 if (userIds.length == 0) {
222 return;
223 }
224
225 RolePermissionUtil.check(
226 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
227
228 RoleMembershipPolicyUtil.checkRoles(userIds, new long[] {roleId}, null);
229
230 userLocalService.addRoleUsers(roleId, userIds);
231
232 RoleMembershipPolicyUtil.propagateRoles(
233 userIds, new long[] {roleId}, null);
234 }
235
236
246 @Override
247 public void addTeamUsers(long teamId, long[] userIds)
248 throws PortalException, SystemException {
249
250 if (userIds.length == 0) {
251 return;
252 }
253
254 TeamPermissionUtil.check(
255 getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
256
257 userLocalService.addTeamUsers(teamId, userIds);
258 }
259
260
310 @Override
311 public User addUser(
312 long companyId, boolean autoPassword, String password1,
313 String password2, boolean autoScreenName, String screenName,
314 String emailAddress, long facebookId, String openId, Locale locale,
315 String firstName, String middleName, String lastName, int prefixId,
316 int suffixId, boolean male, int birthdayMonth, int birthdayDay,
317 int birthdayYear, String jobTitle, long[] groupIds,
318 long[] organizationIds, long[] roleIds, long[] userGroupIds,
319 boolean sendEmail, ServiceContext serviceContext)
320 throws PortalException, SystemException {
321
322 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
323
324 try {
325 WorkflowThreadLocal.setEnabled(false);
326
327 return addUserWithWorkflow(
328 companyId, autoPassword, password1, password2, autoScreenName,
329 screenName, emailAddress, facebookId, openId, locale, firstName,
330 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
331 birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
332 roleIds, userGroupIds, sendEmail, serviceContext);
333 }
334 finally {
335 WorkflowThreadLocal.setEnabled(workflowEnabled);
336 }
337 }
338
339
394 @Override
395 public User addUser(
396 long companyId, boolean autoPassword, String password1,
397 String password2, boolean autoScreenName, String screenName,
398 String emailAddress, long facebookId, String openId, Locale locale,
399 String firstName, String middleName, String lastName, int prefixId,
400 int suffixId, boolean male, int birthdayMonth, int birthdayDay,
401 int birthdayYear, String jobTitle, long[] groupIds,
402 long[] organizationIds, long[] roleIds, long[] userGroupIds,
403 List<Address> addresses, List<EmailAddress> emailAddresses,
404 List<Phone> phones, List<Website> websites,
405 List<AnnouncementsDelivery> announcementsDelivers,
406 boolean sendEmail, ServiceContext serviceContext)
407 throws PortalException, SystemException {
408
409 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
410
411 try {
412 WorkflowThreadLocal.setEnabled(false);
413
414 return addUserWithWorkflow(
415 companyId, autoPassword, password1, password2, autoScreenName,
416 screenName, emailAddress, facebookId, openId, locale, firstName,
417 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
418 birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
419 roleIds, userGroupIds, addresses, emailAddresses, phones,
420 websites, announcementsDelivers, sendEmail, serviceContext);
421 }
422 finally {
423 WorkflowThreadLocal.setEnabled(workflowEnabled);
424 }
425 }
426
427
438 @Override
439 public void addUserGroupUsers(long userGroupId, long[] userIds)
440 throws PortalException, SystemException {
441
442 if (userIds.length == 0) {
443 return;
444 }
445
446 UserGroupPermissionUtil.check(
447 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
448
449 UserGroupMembershipPolicyUtil.checkMembership(
450 userIds, new long[] {userGroupId}, null);
451
452 userLocalService.addUserGroupUsers(userGroupId, userIds);
453
454 UserGroupMembershipPolicyUtil.propagateMembership(
455 userIds, new long[] {userGroupId}, null);
456 }
457
458
508 @Override
509 public User addUserWithWorkflow(
510 long companyId, boolean autoPassword, String password1,
511 String password2, boolean autoScreenName, String screenName,
512 String emailAddress, long facebookId, String openId, Locale locale,
513 String firstName, String middleName, String lastName, int prefixId,
514 int suffixId, boolean male, int birthdayMonth, int birthdayDay,
515 int birthdayYear, String jobTitle, long[] groupIds,
516 long[] organizationIds, long[] roleIds, long[] userGroupIds,
517 boolean sendEmail, ServiceContext serviceContext)
518 throws PortalException, SystemException {
519
520 long creatorUserId = 0;
521
522 try {
523 creatorUserId = getGuestOrUserId();
524 }
525 catch (PrincipalException pe) {
526 }
527
528 checkAddUserPermission(
529 creatorUserId, companyId, emailAddress, groupIds, organizationIds,
530 roleIds, userGroupIds, serviceContext);
531
532 User user = userLocalService.addUserWithWorkflow(
533 creatorUserId, companyId, autoPassword, password1, password2,
534 autoScreenName, screenName, emailAddress, facebookId, openId,
535 locale, firstName, middleName, lastName, prefixId, suffixId, male,
536 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
537 organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
538
539 checkMembership(
540 new long[] {user.getUserId()}, groupIds, organizationIds, roleIds,
541 userGroupIds);
542
543 propagateMembership(
544 new long[] {user.getUserId()}, groupIds, organizationIds, roleIds,
545 userGroupIds);
546
547 return user;
548 }
549
550
605 @Override
606 public User addUserWithWorkflow(
607 long companyId, boolean autoPassword, String password1,
608 String password2, boolean autoScreenName, String screenName,
609 String emailAddress, long facebookId, String openId, Locale locale,
610 String firstName, String middleName, String lastName, int prefixId,
611 int suffixId, boolean male, int birthdayMonth, int birthdayDay,
612 int birthdayYear, String jobTitle, long[] groupIds,
613 long[] organizationIds, long[] roleIds, long[] userGroupIds,
614 List<Address> addresses, List<EmailAddress> emailAddresses,
615 List<Phone> phones, List<Website> websites,
616 List<AnnouncementsDelivery> announcementsDelivers,
617 boolean sendEmail, ServiceContext serviceContext)
618 throws PortalException, SystemException {
619
620 boolean indexingEnabled = serviceContext.isIndexingEnabled();
621
622 serviceContext.setIndexingEnabled(false);
623
624 try {
625 User user = addUserWithWorkflow(
626 companyId, autoPassword, password1, password2, autoScreenName,
627 screenName, emailAddress, facebookId, openId, locale, firstName,
628 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
629 birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
630 roleIds, userGroupIds, sendEmail, serviceContext);
631
632 UsersAdminUtil.updateAddresses(
633 Contact.class.getName(), user.getContactId(), addresses);
634
635 UsersAdminUtil.updateEmailAddresses(
636 Contact.class.getName(), user.getContactId(), emailAddresses);
637
638 UsersAdminUtil.updatePhones(
639 Contact.class.getName(), user.getContactId(), phones);
640
641 UsersAdminUtil.updateWebsites(
642 Contact.class.getName(), user.getContactId(), websites);
643
644 updateAnnouncementsDeliveries(
645 user.getUserId(), announcementsDelivers);
646
647 if (indexingEnabled) {
648 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
649 User.class);
650
651 indexer.reindex(user);
652 }
653
654 return user;
655 }
656 finally {
657 serviceContext.setIndexingEnabled(indexingEnabled);
658 }
659 }
660
661
670 @Override
671 public void deletePortrait(long userId)
672 throws PortalException, SystemException {
673
674 UserPermissionUtil.check(
675 getPermissionChecker(), userId, ActionKeys.UPDATE);
676
677 userLocalService.deletePortrait(userId);
678 }
679
680
690 @Override
691 public void deleteRoleUser(long roleId, long userId)
692 throws PortalException, SystemException {
693
694 RolePermissionUtil.check(
695 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
696
697 userLocalService.deleteRoleUser(roleId, userId);
698 }
699
700
708 @Override
709 public void deleteUser(long userId)
710 throws PortalException, SystemException {
711
712 if (getUserId() == userId) {
713 throw new RequiredUserException();
714 }
715
716 UserPermissionUtil.check(
717 getPermissionChecker(), userId, ActionKeys.DELETE);
718
719 userLocalService.deleteUser(userId);
720 }
721
722 @Override
723 public List<User> getCompanyUsers(long companyId, int start, int end)
724 throws PortalException, SystemException {
725
726 PermissionChecker permissionChecker = getPermissionChecker();
727
728 if (!permissionChecker.isCompanyAdmin(companyId)) {
729 throw new PrincipalException();
730 }
731
732 return userPersistence.findByCompanyId(companyId, start, end);
733 }
734
735 @Override
736 public int getCompanyUsersCount(long companyId)
737 throws PortalException, SystemException {
738
739 PermissionChecker permissionChecker = getPermissionChecker();
740
741 if (!permissionChecker.isCompanyAdmin(companyId)) {
742 throw new PrincipalException();
743 }
744
745 return userPersistence.countByCompanyId(companyId);
746 }
747
748
757 @Override
758 public long[] getGroupUserIds(long groupId)
759 throws PortalException, SystemException {
760
761 GroupPermissionUtil.check(
762 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
763
764 return userLocalService.getGroupUserIds(groupId);
765 }
766
767
776 @Override
777 public List<User> getGroupUsers(long groupId)
778 throws PortalException, SystemException {
779
780 GroupPermissionUtil.check(
781 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
782
783 return userLocalService.getGroupUsers(groupId);
784 }
785
786
795 @Override
796 public long[] getOrganizationUserIds(long organizationId)
797 throws PortalException, SystemException {
798
799 OrganizationPermissionUtil.check(
800 getPermissionChecker(), organizationId, ActionKeys.VIEW_MEMBERS);
801
802 return userLocalService.getOrganizationUserIds(organizationId);
803 }
804
805
814 @Override
815 public List<User> getOrganizationUsers(long organizationId)
816 throws PortalException, SystemException {
817
818 OrganizationPermissionUtil.check(
819 getPermissionChecker(), organizationId, ActionKeys.VIEW_MEMBERS);
820
821 return userLocalService.getOrganizationUsers(organizationId);
822 }
823
824
833 @Override
834 public long[] getRoleUserIds(long roleId)
835 throws PortalException, SystemException {
836
837 RolePermissionUtil.check(
838 getPermissionChecker(), roleId, ActionKeys.VIEW);
839
840 return userLocalService.getRoleUserIds(roleId);
841 }
842
843
854 @Override
855 public User getUserByEmailAddress(long companyId, String emailAddress)
856 throws PortalException, SystemException {
857
858 User user = userLocalService.getUserByEmailAddress(
859 companyId, emailAddress);
860
861 UserPermissionUtil.check(
862 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
863
864 return user;
865 }
866
867
876 @Override
877 public User getUserById(long userId)
878 throws PortalException, SystemException {
879
880 User user = userPersistence.findByPrimaryKey(userId);
881
882 UserPermissionUtil.check(
883 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
884
885 return user;
886 }
887
888
898 @Override
899 public User getUserByScreenName(long companyId, String screenName)
900 throws PortalException, SystemException {
901
902 User user = userLocalService.getUserByScreenName(companyId, screenName);
903
904 UserPermissionUtil.check(
905 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
906
907 return user;
908 }
909
910 @Override
911 public List<User> getUserGroupUsers(long userGroupId)
912 throws PortalException, SystemException {
913
914 UserGroupPermissionUtil.check(
915 getPermissionChecker(), userGroupId, ActionKeys.VIEW_MEMBERS);
916
917 return userGroupPersistence.getUsers(userGroupId);
918 }
919
920
930 @Override
931 public long getUserIdByEmailAddress(long companyId, String emailAddress)
932 throws PortalException, SystemException {
933
934 User user = getUserByEmailAddress(companyId, emailAddress);
935
936 UserPermissionUtil.check(
937 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
938
939 return user.getUserId();
940 }
941
942
951 @Override
952 public long getUserIdByScreenName(long companyId, String screenName)
953 throws PortalException, SystemException {
954
955 User user = getUserByScreenName(companyId, screenName);
956
957 UserPermissionUtil.check(
958 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
959
960 return user.getUserId();
961 }
962
963
974 @Override
975 public boolean hasGroupUser(long groupId, long userId)
976 throws PortalException, SystemException {
977
978 try {
979 UserPermissionUtil.check(
980 getPermissionChecker(), userId, ActionKeys.VIEW);
981 }
982 catch (PrincipalException pe) {
983 GroupPermissionUtil.check(
984 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
985 }
986
987 return userLocalService.hasGroupUser(groupId, userId);
988 }
989
990
1001 @Override
1002 public boolean hasRoleUser(long roleId, long userId)
1003 throws PortalException, SystemException {
1004
1005 try {
1006 UserPermissionUtil.check(
1007 getPermissionChecker(), userId, ActionKeys.VIEW);
1008 }
1009 catch (PrincipalException pe) {
1010 RolePermissionUtil.check(
1011 getPermissionChecker(), roleId, ActionKeys.VIEW_MEMBERS);
1012 }
1013
1014 return userLocalService.hasRoleUser(roleId, userId);
1015 }
1016
1017
1032 @Override
1033 public boolean hasRoleUser(
1034 long companyId, String name, long userId, boolean inherited)
1035 throws PortalException, SystemException {
1036
1037 try {
1038 UserPermissionUtil.check(
1039 getPermissionChecker(), userId, ActionKeys.VIEW);
1040 }
1041 catch (PrincipalException pe) {
1042 Role role = roleLocalService.getRole(companyId, name);
1043
1044 RolePermissionUtil.check(
1045 getPermissionChecker(), role.getRoleId(),
1046 ActionKeys.VIEW_MEMBERS);
1047 }
1048
1049 return userLocalService.hasRoleUser(companyId, name, userId, inherited);
1050 }
1051
1052
1063 @Override
1064 public void setRoleUsers(long roleId, long[] userIds)
1065 throws PortalException, SystemException {
1066
1067 RolePermissionUtil.check(
1068 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
1069
1070 List<Long> unsetUserIds = new ArrayList<Long>(userIds.length);
1071
1072 List<User> users = rolePersistence.getUsers(roleId);
1073
1074 for (User user : users) {
1075 if (!ArrayUtil.contains(userIds, user.getUserId())) {
1076 unsetUserIds.add(user.getUserId());
1077 }
1078 }
1079
1080 if (!unsetUserIds.isEmpty()) {
1081 RoleMembershipPolicyUtil.checkRoles(
1082 ArrayUtil.toLongArray(unsetUserIds), null, new long[] {roleId});
1083 }
1084
1085 if (userIds.length > 0) {
1086 RoleMembershipPolicyUtil.checkRoles(
1087 userIds, new long[] {roleId}, null);
1088 }
1089
1090 userLocalService.setRoleUsers(roleId, userIds);
1091
1092 if (!unsetUserIds.isEmpty()) {
1093 RoleMembershipPolicyUtil.propagateRoles(
1094 ArrayUtil.toLongArray(unsetUserIds), null, new long[] {roleId});
1095 }
1096
1097 if (userIds.length > 0) {
1098 RoleMembershipPolicyUtil.propagateRoles(
1099 userIds, new long[] {roleId}, null);
1100 }
1101 }
1102
1103
1113 @Override
1114 public void setUserGroupUsers(long userGroupId, long[] userIds)
1115 throws PortalException, SystemException {
1116
1117 UserGroupPermissionUtil.check(
1118 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
1119
1120 List<Long> unsetUserIds = new ArrayList<Long>(userIds.length);
1121
1122 List<User> users = userGroupPersistence.getUsers(userGroupId);
1123
1124 for (User user : users) {
1125 if (!ArrayUtil.contains(userIds, user.getUserId())) {
1126 unsetUserIds.add(user.getUserId());
1127 }
1128 }
1129
1130 if (!unsetUserIds.isEmpty()) {
1131 UserGroupMembershipPolicyUtil.checkMembership(
1132 ArrayUtil.toLongArray(unsetUserIds), null,
1133 new long[] {userGroupId});
1134 }
1135
1136 if (userIds.length > 0) {
1137 UserGroupMembershipPolicyUtil.checkMembership(
1138 userIds, new long[] {userGroupId}, null);
1139 }
1140
1141 userLocalService.setUserGroupUsers(userGroupId, userIds);
1142
1143 if (!unsetUserIds.isEmpty()) {
1144 UserGroupMembershipPolicyUtil.propagateMembership(
1145 ArrayUtil.toLongArray(unsetUserIds), null,
1146 new long[] {userGroupId});
1147 }
1148
1149 if (userIds.length > 0) {
1150 UserGroupMembershipPolicyUtil.propagateMembership(
1151 userIds, new long[] {userGroupId}, null);
1152 }
1153 }
1154
1155
1164 @Override
1165 public void unsetGroupTeamsUsers(long groupId, long[] userIds)
1166 throws PortalException, SystemException {
1167
1168 if (userIds.length == 0) {
1169 return;
1170 }
1171
1172 UserGroupPermissionUtil.check(
1173 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
1174
1175 userLocalService.unsetGroupTeamsUsers(groupId, userIds);
1176 }
1177
1178
1190 @Override
1191 public void unsetGroupUsers(
1192 long groupId, long[] userIds, ServiceContext serviceContext)
1193 throws PortalException, SystemException {
1194
1195 userIds = UsersAdminUtil.filterUnsetGroupUserIds(
1196 getPermissionChecker(), groupId, userIds);
1197
1198 if (userIds.length == 0) {
1199 return;
1200 }
1201
1202 try {
1203 GroupPermissionUtil.check(
1204 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
1205 }
1206 catch (PrincipalException pe) {
1207
1208
1209
1210 boolean hasPermission = false;
1211
1212 if (userIds.length == 1) {
1213 User user = getUser();
1214
1215 if (user.getUserId() == userIds[0]) {
1216 Group group = groupPersistence.findByPrimaryKey(groupId);
1217
1218 if (user.getCompanyId() == group.getCompanyId()) {
1219 int type = group.getType();
1220
1221 if ((type == GroupConstants.TYPE_SITE_OPEN) ||
1222 (type == GroupConstants.TYPE_SITE_RESTRICTED)) {
1223
1224 hasPermission = true;
1225 }
1226 }
1227 }
1228 }
1229
1230 if (!hasPermission) {
1231 throw new PrincipalException();
1232 }
1233 }
1234
1235 SiteMembershipPolicyUtil.checkMembership(
1236 userIds, null, new long[] {groupId});
1237
1238 userLocalService.unsetGroupUsers(groupId, userIds, serviceContext);
1239
1240 SiteMembershipPolicyUtil.propagateMembership(
1241 userIds, null, new long[] {groupId});
1242 }
1243
1244
1254 @Override
1255 public void unsetOrganizationUsers(long organizationId, long[] userIds)
1256 throws PortalException, SystemException {
1257
1258 userIds = UsersAdminUtil.filterUnsetOrganizationUserIds(
1259 getPermissionChecker(), organizationId, userIds);
1260
1261 if (userIds.length == 0) {
1262 return;
1263 }
1264
1265 OrganizationPermissionUtil.check(
1266 getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
1267
1268 OrganizationMembershipPolicyUtil.checkMembership(
1269 userIds, null, new long[] {organizationId});
1270
1271 userLocalService.unsetOrganizationUsers(organizationId, userIds);
1272
1273 OrganizationMembershipPolicyUtil.propagateMembership(
1274 userIds, null, new long[] {organizationId});
1275 }
1276
1277
1286 @Override
1287 public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
1288 throws PortalException, SystemException {
1289
1290 if (userIds.length == 0) {
1291 return;
1292 }
1293
1294 PasswordPolicyPermissionUtil.check(
1295 getPermissionChecker(), passwordPolicyId,
1296 ActionKeys.ASSIGN_MEMBERS);
1297
1298 userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
1299 }
1300
1301
1311 @Override
1312 public void unsetRoleUsers(long roleId, long[] userIds)
1313 throws PortalException, SystemException {
1314
1315 if (userIds.length == 0) {
1316 return;
1317 }
1318
1319 RolePermissionUtil.check(
1320 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
1321
1322 RoleMembershipPolicyUtil.checkRoles(userIds, null, new long[] {roleId});
1323
1324 userLocalService.unsetRoleUsers(roleId, userIds);
1325
1326 RoleMembershipPolicyUtil.propagateRoles(
1327 userIds, null, new long[] {roleId});
1328 }
1329
1330
1339 @Override
1340 public void unsetTeamUsers(long teamId, long[] userIds)
1341 throws PortalException, SystemException {
1342
1343 if (userIds.length == 0) {
1344 return;
1345 }
1346
1347 TeamPermissionUtil.check(
1348 getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
1349
1350 userLocalService.unsetTeamUsers(teamId, userIds);
1351 }
1352
1353
1363 @Override
1364 public void unsetUserGroupUsers(long userGroupId, long[] userIds)
1365 throws PortalException, SystemException {
1366
1367 if (userIds.length == 0) {
1368 return;
1369 }
1370
1371 UserGroupPermissionUtil.check(
1372 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
1373
1374 UserGroupMembershipPolicyUtil.checkMembership(
1375 userIds, null, new long[] {userGroupId});
1376
1377 userLocalService.unsetUserGroupUsers(userGroupId, userIds);
1378
1379 UserGroupMembershipPolicyUtil.propagateMembership(
1380 userIds, null, new long[] {userGroupId});
1381 }
1382
1383
1393 @Override
1394 public User updateAgreedToTermsOfUse(
1395 long userId, boolean agreedToTermsOfUse)
1396 throws PortalException, SystemException {
1397
1398 UserPermissionUtil.check(
1399 getPermissionChecker(), userId, ActionKeys.UPDATE);
1400
1401 return userLocalService.updateAgreedToTermsOfUse(
1402 userId, agreedToTermsOfUse);
1403 }
1404
1405
1420 @Override
1421 public User updateEmailAddress(
1422 long userId, String password, String emailAddress1,
1423 String emailAddress2, ServiceContext serviceContext)
1424 throws PortalException, SystemException {
1425
1426 UserPermissionUtil.check(
1427 getPermissionChecker(), userId, ActionKeys.UPDATE);
1428
1429 User user = userPersistence.findByPrimaryKey(userId);
1430
1431 validateEmailAddress(user, emailAddress2);
1432
1433 return userLocalService.updateEmailAddress(
1434 userId, password, emailAddress1, emailAddress2, serviceContext);
1435 }
1436
1437
1476 @Override
1477 public User updateIncompleteUser(
1478 long companyId, boolean autoPassword, String password1,
1479 String password2, boolean autoScreenName, String screenName,
1480 String emailAddress, long facebookId, String openId, Locale locale,
1481 String firstName, String middleName, String lastName, int prefixId,
1482 int suffixId, boolean male, int birthdayMonth, int birthdayDay,
1483 int birthdayYear, String jobTitle, boolean updateUserInformation,
1484 boolean sendEmail, ServiceContext serviceContext)
1485 throws PortalException, SystemException {
1486
1487 long creatorUserId = 0;
1488
1489 try {
1490 creatorUserId = getGuestOrUserId();
1491 }
1492 catch (PrincipalException pe) {
1493 }
1494
1495 checkAddUserPermission(
1496 creatorUserId, companyId, emailAddress, null, null, null, null,
1497 serviceContext);
1498
1499 return userLocalService.updateIncompleteUser(
1500 creatorUserId, companyId, autoPassword, password1, password2,
1501 autoScreenName, screenName, emailAddress, facebookId, openId,
1502 locale, firstName, middleName, lastName, prefixId, suffixId, male,
1503 birthdayMonth, birthdayDay, birthdayYear, jobTitle,
1504 updateUserInformation, sendEmail, serviceContext);
1505 }
1506
1507
1517 @Override
1518 public User updateLockoutById(long userId, boolean lockout)
1519 throws PortalException, SystemException {
1520
1521 UserPermissionUtil.check(
1522 getPermissionChecker(), userId, ActionKeys.DELETE);
1523
1524 return userLocalService.updateLockoutById(userId, lockout);
1525 }
1526
1527
1537 @Override
1538 public User updateOpenId(long userId, String openId)
1539 throws PortalException, SystemException {
1540
1541 UserPermissionUtil.check(
1542 getPermissionChecker(), userId, ActionKeys.UPDATE);
1543
1544 return userLocalService.updateOpenId(userId, openId);
1545 }
1546
1547
1559 @Override
1560 public void updateOrganizations(
1561 long userId, long[] organizationIds, ServiceContext serviceContext)
1562 throws PortalException, SystemException {
1563
1564 UserPermissionUtil.check(
1565 getPermissionChecker(), userId, ActionKeys.UPDATE);
1566
1567 checkOrganizations(userId, organizationIds);
1568
1569 userLocalService.updateOrganizations(
1570 userId, organizationIds, serviceContext);
1571 }
1572
1573
1586 @Override
1587 public User updatePassword(
1588 long userId, String password1, String password2,
1589 boolean passwordReset)
1590 throws PortalException, SystemException {
1591
1592 UserPermissionUtil.check(
1593 getPermissionChecker(), userId, ActionKeys.UPDATE);
1594
1595 return userLocalService.updatePassword(
1596 userId, password1, password2, passwordReset);
1597 }
1598
1599
1610 @Override
1611 public User updatePortrait(long userId, byte[] bytes)
1612 throws PortalException, SystemException {
1613
1614 UserPermissionUtil.check(
1615 getPermissionChecker(), userId, ActionKeys.UPDATE);
1616
1617 return userLocalService.updatePortrait(userId, bytes);
1618 }
1619
1620
1632 @Override
1633 public User updateReminderQuery(long userId, String question, String answer)
1634 throws PortalException, SystemException {
1635
1636 UserPermissionUtil.check(
1637 getPermissionChecker(), userId, ActionKeys.UPDATE);
1638
1639 return userLocalService.updateReminderQuery(userId, question, answer);
1640 }
1641
1642
1653 @Override
1654 public User updateScreenName(long userId, String screenName)
1655 throws PortalException, SystemException {
1656
1657 UserPermissionUtil.check(
1658 getPermissionChecker(), userId, ActionKeys.UPDATE);
1659
1660 return userLocalService.updateScreenName(userId, screenName);
1661 }
1662
1663
1679 @Deprecated
1680 @Override
1681 public User updateStatus(long userId, int status)
1682 throws PortalException, SystemException {
1683
1684 return updateStatus(userId, status, new ServiceContext());
1685 }
1686
1687
1704 @Override
1705 public User updateStatus(
1706 long userId, int status, ServiceContext serviceContext)
1707 throws PortalException, SystemException {
1708
1709 if ((getUserId() == userId) &&
1710 (status != WorkflowConstants.STATUS_APPROVED)) {
1711
1712 throw new RequiredUserException();
1713 }
1714
1715 UserPermissionUtil.check(
1716 getPermissionChecker(), userId, ActionKeys.DELETE);
1717
1718 return userLocalService.updateStatus(userId, status, serviceContext);
1719 }
1720
1721
1784 @Override
1785 public User updateUser(
1786 long userId, String oldPassword, String newPassword1,
1787 String newPassword2, boolean passwordReset,
1788 String reminderQueryQuestion, String reminderQueryAnswer,
1789 String screenName, String emailAddress, long facebookId,
1790 String openId, String languageId, String timeZoneId,
1791 String greeting, String comments, String firstName,
1792 String middleName, String lastName, int prefixId, int suffixId,
1793 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
1794 String smsSn, String aimSn, String facebookSn, String icqSn,
1795 String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
1796 String twitterSn, String ymSn, String jobTitle, long[] groupIds,
1797 long[] organizationIds, long[] roleIds,
1798 List<UserGroupRole> userGroupRoles, long[] userGroupIds,
1799 List<Address> addresses, List<EmailAddress> emailAddresses,
1800 List<Phone> phones, List<Website> websites,
1801 List<AnnouncementsDelivery> announcementsDelivers,
1802 ServiceContext serviceContext)
1803 throws PortalException, SystemException {
1804
1805 UserPermissionUtil.check(
1806 getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
1807
1808 User user = userPersistence.findByPrimaryKey(userId);
1809
1810 if (addresses != null) {
1811 UsersAdminUtil.updateAddresses(
1812 Contact.class.getName(), user.getContactId(), addresses);
1813 }
1814
1815 if (emailAddresses != null) {
1816 UsersAdminUtil.updateEmailAddresses(
1817 Contact.class.getName(), user.getContactId(), emailAddresses);
1818 }
1819
1820 if (phones != null) {
1821 UsersAdminUtil.updatePhones(
1822 Contact.class.getName(), user.getContactId(), phones);
1823 }
1824
1825 if (websites != null) {
1826 UsersAdminUtil.updateWebsites(
1827 Contact.class.getName(), user.getContactId(), websites);
1828 }
1829
1830 if (announcementsDelivers != null) {
1831 updateAnnouncementsDeliveries(
1832 user.getUserId(), announcementsDelivers);
1833 }
1834
1835 long curUserId = getUserId();
1836
1837 if (curUserId == userId) {
1838 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
1839
1840 if (!StringUtil.equalsIgnoreCase(
1841 emailAddress, user.getEmailAddress())) {
1842
1843 validateEmailAddress(user, emailAddress);
1844 }
1845 }
1846
1847 validateUpdatePermission(
1848 user, screenName, emailAddress, firstName, middleName, lastName,
1849 prefixId, suffixId, birthdayMonth, birthdayDay, birthdayYear, male,
1850 jobTitle);
1851
1852
1853
1854 long[] oldGroupIds = user.getGroupIds();
1855
1856 List<Long> addGroupIds = new ArrayList<Long>();
1857 List<Long> removeGroupIds = ListUtil.toList(oldGroupIds);
1858
1859 if (groupIds != null) {
1860 groupIds = checkGroups(userId, groupIds);
1861
1862 for (long groupId : groupIds) {
1863 if (ArrayUtil.contains(oldGroupIds, groupId)) {
1864 removeGroupIds.remove(groupId);
1865 }
1866 else {
1867 addGroupIds.add(groupId);
1868 }
1869 }
1870
1871 if (!addGroupIds.isEmpty() || !removeGroupIds.isEmpty()) {
1872 SiteMembershipPolicyUtil.checkMembership(
1873 new long[] {userId}, ArrayUtil.toLongArray(addGroupIds),
1874 ArrayUtil.toLongArray(removeGroupIds));
1875 }
1876 }
1877
1878
1879
1880 long[] oldOrganizationIds = user.getOrganizationIds();
1881
1882 List<Long> addOrganizationIds = new ArrayList<Long>();
1883 List<Long> removeOrganizationIds = ListUtil.toList(oldOrganizationIds);
1884
1885 if (organizationIds != null) {
1886 organizationIds = checkOrganizations(userId, organizationIds);
1887
1888 for (long organizationId : organizationIds) {
1889 if (ArrayUtil.contains(oldOrganizationIds, organizationId)) {
1890 removeOrganizationIds.remove(organizationId);
1891 }
1892 else {
1893 addOrganizationIds.add(organizationId);
1894 }
1895 }
1896
1897 if (!addOrganizationIds.isEmpty() ||
1898 !removeOrganizationIds.isEmpty()) {
1899
1900 OrganizationMembershipPolicyUtil.checkMembership(
1901 new long[] {userId},
1902 ArrayUtil.toLongArray(addOrganizationIds),
1903 ArrayUtil.toLongArray(removeOrganizationIds));
1904 }
1905 }
1906
1907
1908
1909 long[] oldRoleIds = user.getRoleIds();
1910
1911 List<Long> addRoleIds = new ArrayList<Long>();
1912 List<Long> removeRoleIds = ListUtil.toList(oldRoleIds);
1913
1914 if (roleIds != null) {
1915 roleIds = checkRoles(userId, roleIds);
1916
1917 for (long roleId : roleIds) {
1918 if (ArrayUtil.contains(oldRoleIds, roleId)) {
1919 removeRoleIds.remove(roleId);
1920 }
1921 else {
1922 addRoleIds.add(roleId);
1923 }
1924 }
1925
1926 if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
1927 RoleMembershipPolicyUtil.checkRoles(
1928 new long[] {userId}, ArrayUtil.toLongArray(addRoleIds),
1929 ArrayUtil.toLongArray(removeRoleIds));
1930 }
1931 }
1932
1933 List<UserGroupRole> oldOrganizationUserGroupRoles =
1934 new ArrayList<UserGroupRole>();
1935 List<UserGroupRole> oldSiteUserGroupRoles =
1936 new ArrayList<UserGroupRole>();
1937
1938 List<UserGroupRole> oldUserGroupRoles =
1939 userGroupRolePersistence.findByUserId(userId);
1940
1941 for (UserGroupRole oldUserGroupRole : oldUserGroupRoles) {
1942 Role role = oldUserGroupRole.getRole();
1943
1944 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
1945 oldOrganizationUserGroupRoles.add(oldUserGroupRole);
1946 }
1947 else if (role.getType() == RoleConstants.TYPE_SITE) {
1948 oldSiteUserGroupRoles.add(oldUserGroupRole);
1949 }
1950 }
1951
1952 List<UserGroupRole> addOrganizationUserGroupRoles =
1953 new ArrayList<UserGroupRole>();
1954 List<UserGroupRole> removeOrganizationUserGroupRoles = ListUtil.copy(
1955 oldOrganizationUserGroupRoles);
1956 List<UserGroupRole> addSiteUserGroupRoles =
1957 new ArrayList<UserGroupRole>();
1958 List<UserGroupRole> removeSiteUserGroupRoles = ListUtil.copy(
1959 oldSiteUserGroupRoles);
1960
1961 if (userGroupRoles != null) {
1962 userGroupRoles = checkUserGroupRoles(userId, userGroupRoles);
1963
1964 for (UserGroupRole userGroupRole : userGroupRoles) {
1965 Role role = userGroupRole.getRole();
1966
1967 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
1968 if (oldOrganizationUserGroupRoles.contains(userGroupRole)) {
1969 removeOrganizationUserGroupRoles.remove(userGroupRole);
1970 }
1971 else {
1972 addOrganizationUserGroupRoles.add(userGroupRole);
1973 }
1974 }
1975 else if (role.getType() == RoleConstants.TYPE_SITE) {
1976 if (oldSiteUserGroupRoles.contains(userGroupRole)) {
1977 removeSiteUserGroupRoles.remove(userGroupRole);
1978 }
1979 else {
1980 addSiteUserGroupRoles.add(userGroupRole);
1981 }
1982 }
1983 }
1984
1985 if (!addOrganizationUserGroupRoles.isEmpty() ||
1986 !removeOrganizationUserGroupRoles.isEmpty()) {
1987
1988 OrganizationMembershipPolicyUtil.checkRoles(
1989 addOrganizationUserGroupRoles,
1990 removeOrganizationUserGroupRoles);
1991 }
1992
1993 if (!addSiteUserGroupRoles.isEmpty() ||
1994 !removeSiteUserGroupRoles.isEmpty()) {
1995
1996 SiteMembershipPolicyUtil.checkRoles(
1997 addSiteUserGroupRoles, removeSiteUserGroupRoles);
1998 }
1999 }
2000
2001
2002
2003 long[] oldUserGroupIds = user.getUserGroupIds();
2004
2005 List<Long> addUserGroupIds = new ArrayList<Long>();
2006 List<Long> removeUserGroupIds = ListUtil.toList(oldUserGroupIds);
2007
2008 if (userGroupIds != null) {
2009 userGroupIds = checkUserGroupIds(userId, userGroupIds);
2010
2011 for (long userGroupId : userGroupIds) {
2012 if (ArrayUtil.contains(oldUserGroupIds, userGroupId)) {
2013 removeUserGroupIds.remove(userGroupId);
2014 }
2015 else {
2016 addUserGroupIds.add(userGroupId);
2017 }
2018 }
2019
2020 if (!addUserGroupIds.isEmpty() || !removeUserGroupIds.isEmpty()) {
2021 UserGroupMembershipPolicyUtil.checkMembership(
2022 new long[] {userId}, ArrayUtil.toLongArray(addUserGroupIds),
2023 ArrayUtil.toLongArray(removeUserGroupIds));
2024 }
2025 }
2026
2027 user = userLocalService.updateUser(
2028 userId, oldPassword, newPassword1, newPassword2, passwordReset,
2029 reminderQueryQuestion, reminderQueryAnswer, screenName,
2030 emailAddress, facebookId, openId, languageId, timeZoneId, greeting,
2031 comments, firstName, middleName, lastName, prefixId, suffixId, male,
2032 birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
2033 icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
2034 jobTitle, groupIds, organizationIds, roleIds, userGroupRoles,
2035 userGroupIds, serviceContext);
2036
2037 if (!addGroupIds.isEmpty() || !removeGroupIds.isEmpty()) {
2038 SiteMembershipPolicyUtil.propagateMembership(
2039 new long[] {user.getUserId()},
2040 ArrayUtil.toLongArray(addGroupIds),
2041 ArrayUtil.toLongArray(removeGroupIds));
2042 }
2043
2044 if (!addOrganizationIds.isEmpty() || !removeOrganizationIds.isEmpty()) {
2045 OrganizationMembershipPolicyUtil.propagateMembership(
2046 new long[] {user.getUserId()},
2047 ArrayUtil.toLongArray(addOrganizationIds),
2048 ArrayUtil.toLongArray(removeOrganizationIds));
2049 }
2050
2051 if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
2052 RoleMembershipPolicyUtil.propagateRoles(
2053 new long[] {user.getUserId()},
2054 ArrayUtil.toLongArray(addRoleIds),
2055 ArrayUtil.toLongArray(removeRoleIds));
2056 }
2057
2058 if (!addSiteUserGroupRoles.isEmpty() ||
2059 !removeSiteUserGroupRoles.isEmpty()) {
2060
2061 SiteMembershipPolicyUtil.propagateRoles(
2062 addSiteUserGroupRoles, removeSiteUserGroupRoles);
2063 }
2064
2065 if (!addOrganizationUserGroupRoles.isEmpty() ||
2066 !removeOrganizationUserGroupRoles.isEmpty()) {
2067
2068 OrganizationMembershipPolicyUtil.propagateRoles(
2069 addOrganizationUserGroupRoles,
2070 removeOrganizationUserGroupRoles);
2071 }
2072
2073 if (!addUserGroupIds.isEmpty() || !removeGroupIds.isEmpty()) {
2074 UserGroupMembershipPolicyUtil.propagateMembership(
2075 new long[] {user.getUserId()},
2076 ArrayUtil.toLongArray(addUserGroupIds),
2077 ArrayUtil.toLongArray(removeUserGroupIds));
2078 }
2079
2080 return user;
2081 }
2082
2083
2141 @Override
2142 public User updateUser(
2143 long userId, String oldPassword, String newPassword1,
2144 String newPassword2, boolean passwordReset,
2145 String reminderQueryQuestion, String reminderQueryAnswer,
2146 String screenName, String emailAddress, long facebookId,
2147 String openId, String languageId, String timeZoneId,
2148 String greeting, String comments, String firstName,
2149 String middleName, String lastName, int prefixId, int suffixId,
2150 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
2151 String smsSn, String aimSn, String facebookSn, String icqSn,
2152 String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
2153 String twitterSn, String ymSn, String jobTitle, long[] groupIds,
2154 long[] organizationIds, long[] roleIds,
2155 List<UserGroupRole> userGroupRoles, long[] userGroupIds,
2156 ServiceContext serviceContext)
2157 throws PortalException, SystemException {
2158
2159 return updateUser(
2160 userId, oldPassword, newPassword1, newPassword2, passwordReset,
2161 reminderQueryQuestion, reminderQueryAnswer, screenName,
2162 emailAddress, facebookId, openId, languageId, timeZoneId, greeting,
2163 comments, firstName, middleName, lastName, prefixId, suffixId, male,
2164 birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
2165 icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
2166 jobTitle, groupIds, organizationIds, roleIds, userGroupRoles,
2167 userGroupIds, null, null, null, null, null, serviceContext);
2168 }
2169
2170 protected void checkAddUserPermission(
2171 long creatorUserId, long companyId, String emailAddress,
2172 long[] groupIds, long[] organizationIds, long[] roleIds,
2173 long[] userGroupIds, ServiceContext serviceContext)
2174 throws PortalException, SystemException {
2175
2176 Company company = companyPersistence.findByPrimaryKey(companyId);
2177
2178 if (groupIds != null) {
2179 checkGroups(0, groupIds);
2180 }
2181
2182 if (organizationIds != null) {
2183 checkOrganizations(0, organizationIds);
2184 }
2185
2186 if (roleIds != null) {
2187 checkRoles(0, roleIds);
2188 }
2189
2190 if (userGroupIds != null) {
2191 checkUserGroupIds(0, userGroupIds);
2192 }
2193
2194 boolean anonymousUser = ParamUtil.getBoolean(
2195 serviceContext, "anonymousUser");
2196
2197 long defaultUserId = userLocalService.getDefaultUserId(companyId);
2198
2199 if (((creatorUserId != 0) && (creatorUserId != defaultUserId)) ||
2200 (!company.isStrangers() && !anonymousUser)) {
2201
2202 if (!PortalPermissionUtil.contains(
2203 getPermissionChecker(), ActionKeys.ADD_USER) &&
2204 !OrganizationPermissionUtil.contains(
2205 getPermissionChecker(), organizationIds,
2206 ActionKeys.ASSIGN_MEMBERS)) {
2207
2208 throw new PrincipalException();
2209 }
2210 }
2211
2212 if ((creatorUserId == 0) || (creatorUserId == defaultUserId)) {
2213 if (!company.isStrangersWithMx() &&
2214 company.hasCompanyMx(emailAddress)) {
2215
2216 throw new ReservedUserEmailAddressException();
2217 }
2218 }
2219 }
2220
2221 protected long[] checkGroups(long userId, long[] groupIds)
2222 throws PortalException, SystemException {
2223
2224 long[] oldGroupIds = null;
2225
2226 PermissionChecker permissionChecker = getPermissionChecker();
2227
2228 User user = null;
2229
2230 if (userId != CompanyConstants.SYSTEM) {
2231
2232
2233
2234
2235
2236 user = userPersistence.findByPrimaryKey(userId);
2237
2238 List<Group> oldGroups = groupLocalService.getUserGroups(userId);
2239
2240 oldGroupIds = new long[oldGroups.size()];
2241
2242 for (int i = 0; i < oldGroups.size(); i++) {
2243 Group group = oldGroups.get(i);
2244
2245 if (!ArrayUtil.contains(groupIds, group.getGroupId()) &&
2246 (!GroupPermissionUtil.contains(
2247 permissionChecker, group.getGroupId(),
2248 ActionKeys.ASSIGN_MEMBERS) ||
2249 SiteMembershipPolicyUtil.isMembershipProtected(
2250 permissionChecker, user.getUserId(),
2251 group.getGroupId()) ||
2252 SiteMembershipPolicyUtil.isMembershipRequired(
2253 userId, group.getGroupId()))) {
2254
2255 groupIds = ArrayUtil.append(groupIds, group.getGroupId());
2256 }
2257
2258 oldGroupIds[i] = group.getGroupId();
2259 }
2260 }
2261
2262
2263
2264
2265 for (long groupId : groupIds) {
2266 if ((oldGroupIds != null) &&
2267 ArrayUtil.contains(oldGroupIds, groupId)) {
2268
2269 continue;
2270 }
2271
2272 Group group = groupPersistence.findByPrimaryKey(groupId);
2273
2274 GroupPermissionUtil.check(
2275 permissionChecker, group, ActionKeys.ASSIGN_MEMBERS);
2276 }
2277
2278 return groupIds;
2279 }
2280
2281 protected void checkMembership(
2282 long[] userIds, long[] groupIds, long[] organizationIds,
2283 long[] roleIds, long[] userGroupIds)
2284 throws PortalException, SystemException {
2285
2286 if (groupIds != null) {
2287 SiteMembershipPolicyUtil.checkMembership(userIds, groupIds, null);
2288 }
2289
2290 if (organizationIds != null) {
2291 OrganizationMembershipPolicyUtil.checkMembership(
2292 userIds, organizationIds, null);
2293 }
2294
2295 if (roleIds != null) {
2296 RoleMembershipPolicyUtil.checkRoles(userIds, roleIds, null);
2297 }
2298
2299 if (userGroupIds != null) {
2300 UserGroupMembershipPolicyUtil.checkMembership(
2301 userIds, userGroupIds, null);
2302 }
2303 }
2304
2305 protected long[] checkOrganizations(long userId, long[] organizationIds)
2306 throws PortalException, SystemException {
2307
2308 long[] oldOrganizationIds = null;
2309
2310 PermissionChecker permissionChecker = getPermissionChecker();
2311
2312 if (userId != CompanyConstants.SYSTEM) {
2313
2314
2315
2316
2317
2318 List<Organization> oldOrganizations =
2319 organizationLocalService.getUserOrganizations(userId);
2320
2321 oldOrganizationIds = new long[oldOrganizations.size()];
2322
2323 for (int i = 0; i < oldOrganizations.size(); i++) {
2324 Organization organization = oldOrganizations.get(i);
2325
2326 if (!ArrayUtil.contains(
2327 organizationIds, organization.getOrganizationId()) &&
2328 (!OrganizationPermissionUtil.contains(
2329 permissionChecker, organization.getOrganizationId(),
2330 ActionKeys.ASSIGN_MEMBERS) ||
2331 OrganizationMembershipPolicyUtil.isMembershipProtected(
2332 permissionChecker, userId,
2333 organization.getOrganizationId()) ||
2334 OrganizationMembershipPolicyUtil.isMembershipRequired(
2335 userId, organization.getOrganizationId()))) {
2336
2337 organizationIds = ArrayUtil.append(
2338 organizationIds, organization.getOrganizationId());
2339 }
2340
2341 oldOrganizationIds[i] = organization.getOrganizationId();
2342 }
2343 }
2344
2345
2346
2347
2348 for (long organizationId : organizationIds) {
2349 if ((oldOrganizationIds != null) &&
2350 ArrayUtil.contains(oldOrganizationIds, organizationId)) {
2351
2352 continue;
2353 }
2354
2355 Organization organization =
2356 organizationPersistence.findByPrimaryKey(organizationId);
2357
2358 OrganizationPermissionUtil.check(
2359 permissionChecker, organization, ActionKeys.ASSIGN_MEMBERS);
2360 }
2361
2362 return organizationIds;
2363 }
2364
2365 protected long[] checkRoles(long userId, long[] roleIds)
2366 throws PortalException, SystemException {
2367
2368 long[] oldRoleIds = null;
2369
2370 PermissionChecker permissionChecker = getPermissionChecker();
2371
2372 if (userId != CompanyConstants.SYSTEM) {
2373
2374
2375
2376
2377
2378 List<Role> oldRoles = roleLocalService.getUserRoles(userId);
2379
2380 oldRoleIds = new long[oldRoles.size()];
2381
2382 for (int i = 0; i < oldRoles.size(); i++) {
2383 Role role = oldRoles.get(i);
2384
2385 if (!ArrayUtil.contains(roleIds, role.getRoleId()) &&
2386 (!RolePermissionUtil.contains(
2387 permissionChecker, role.getRoleId(),
2388 ActionKeys.ASSIGN_MEMBERS) ||
2389 RoleMembershipPolicyUtil.isRoleRequired(
2390 userId, role.getRoleId()))) {
2391
2392 roleIds = ArrayUtil.append(roleIds, role.getRoleId());
2393 }
2394
2395 oldRoleIds[i] = role.getRoleId();
2396 }
2397 }
2398
2399
2400
2401
2402 for (long roleId : roleIds) {
2403 if ((oldRoleIds != null) &&
2404 ArrayUtil.contains(oldRoleIds, roleId)) {
2405
2406 continue;
2407 }
2408
2409 RolePermissionUtil.check(
2410 permissionChecker, roleId, ActionKeys.ASSIGN_MEMBERS);
2411 }
2412
2413 if (userId != CompanyConstants.SYSTEM) {
2414 return UsersAdminUtil.addRequiredRoles(userId, roleIds);
2415 }
2416
2417 return roleIds;
2418 }
2419
2420 protected long[] checkUserGroupIds(long userId, long[] userGroupIds)
2421 throws PortalException, SystemException {
2422
2423 long[] oldUserGroupIds = null;
2424
2425 PermissionChecker permissionChecker = getPermissionChecker();
2426
2427 if (userId != CompanyConstants.SYSTEM) {
2428
2429
2430
2431
2432 List<UserGroup> oldUserGroups =
2433 userGroupLocalService.getUserUserGroups(userId);
2434
2435 oldUserGroupIds = new long[oldUserGroups.size()];
2436
2437 for (int i = 0; i < oldUserGroups.size(); i++) {
2438 UserGroup userGroup = oldUserGroups.get(i);
2439
2440 if (!ArrayUtil.contains(
2441 userGroupIds, userGroup.getUserGroupId()) &&
2442 (!UserGroupPermissionUtil.contains(
2443 permissionChecker, userGroup.getUserGroupId(),
2444 ActionKeys.ASSIGN_MEMBERS) ||
2445 UserGroupMembershipPolicyUtil.isMembershipRequired(
2446 userId, userGroup.getUserGroupId()))) {
2447
2448 userGroupIds = ArrayUtil.append(
2449 userGroupIds, userGroup.getUserGroupId());
2450 }
2451
2452 oldUserGroupIds[i] = userGroup.getUserGroupId();
2453 }
2454 }
2455
2456
2457
2458
2459 for (long userGroupId : userGroupIds) {
2460 if ((oldUserGroupIds == null) ||
2461 !ArrayUtil.contains(oldUserGroupIds, userGroupId)) {
2462
2463 UserGroupPermissionUtil.check(
2464 permissionChecker, userGroupId, ActionKeys.ASSIGN_MEMBERS);
2465 }
2466 }
2467
2468 return userGroupIds;
2469 }
2470
2471 protected List<UserGroupRole> checkUserGroupRoles(
2472 long userId, List<UserGroupRole> userGroupRoles)
2473 throws PortalException, SystemException {
2474
2475 List<UserGroupRole> oldUserGroupRoles = null;
2476
2477 PermissionChecker permissionChecker = getPermissionChecker();
2478
2479 if (userId != CompanyConstants.SYSTEM) {
2480
2481
2482
2483
2484 oldUserGroupRoles = userGroupRoleLocalService.getUserGroupRoles(
2485 userId);
2486
2487 for (UserGroupRole oldUserGroupRole : oldUserGroupRoles) {
2488 Role role = oldUserGroupRole.getRole();
2489 Group group = oldUserGroupRole.getGroup();
2490
2491 if (userGroupRoles.contains(oldUserGroupRole)) {
2492 continue;
2493 }
2494
2495 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
2496 Organization organization =
2497 organizationPersistence.findByPrimaryKey(
2498 group.getOrganizationId());
2499
2500 if (!UserGroupRolePermissionUtil.contains(
2501 permissionChecker, oldUserGroupRole.getGroupId(),
2502 oldUserGroupRole.getRoleId()) ||
2503 OrganizationMembershipPolicyUtil.isRoleProtected(
2504 getPermissionChecker(), userId,
2505 organization.getOrganizationId(),
2506 role.getRoleId()) ||
2507 OrganizationMembershipPolicyUtil.isRoleRequired(
2508 userId, organization.getOrganizationId(),
2509 role.getRoleId())) {
2510
2511 userGroupRoles.add(oldUserGroupRole);
2512 }
2513 }
2514 else if (role.getType() == RoleConstants.TYPE_SITE) {
2515 if (!userGroupRoles.contains(oldUserGroupRole) &&
2516 (!UserGroupRolePermissionUtil.contains(
2517 permissionChecker, oldUserGroupRole.getGroupId(),
2518 oldUserGroupRole.getRoleId()) ||
2519 SiteMembershipPolicyUtil.isRoleProtected(
2520 getPermissionChecker(), userId, group.getGroupId(),
2521 role.getRoleId()) ||
2522 SiteMembershipPolicyUtil.isRoleRequired(
2523 userId, group.getGroupId(), role.getRoleId()))) {
2524
2525 userGroupRoles.add(oldUserGroupRole);
2526 }
2527 }
2528 }
2529 }
2530
2531
2532
2533
2534 for (UserGroupRole userGroupRole : userGroupRoles) {
2535 if ((oldUserGroupRoles == null) ||
2536 !oldUserGroupRoles.contains(userGroupRole)) {
2537
2538 UserGroupRolePermissionUtil.check(
2539 permissionChecker, userGroupRole.getGroupId(),
2540 userGroupRole.getRoleId());
2541 }
2542 }
2543
2544 return userGroupRoles;
2545 }
2546
2547 protected void propagateMembership(
2548 long[] userIds, long[] groupIds, long[] organizationIds,
2549 long[] roleIds, long[] userGroupIds)
2550 throws PortalException, SystemException {
2551
2552 if (groupIds != null) {
2553 SiteMembershipPolicyUtil.propagateMembership(
2554 userIds, groupIds, null);
2555 }
2556
2557 if (organizationIds != null) {
2558 OrganizationMembershipPolicyUtil.propagateMembership(
2559 userIds, organizationIds, null);
2560 }
2561
2562 if (roleIds != null) {
2563 RoleMembershipPolicyUtil.propagateRoles(userIds, roleIds, null);
2564 }
2565
2566 if (userGroupIds != null) {
2567 UserGroupMembershipPolicyUtil.propagateMembership(
2568 userIds, userGroupIds, null);
2569 }
2570 }
2571
2572 protected void updateAnnouncementsDeliveries(
2573 long userId, List<AnnouncementsDelivery> announcementsDeliveries)
2574 throws PortalException, SystemException {
2575
2576 for (AnnouncementsDelivery announcementsDelivery :
2577 announcementsDeliveries) {
2578
2579 announcementsDeliveryService.updateDelivery(
2580 userId, announcementsDelivery.getType(),
2581 announcementsDelivery.getEmail(),
2582 announcementsDelivery.getSms(),
2583 announcementsDelivery.getWebsite());
2584 }
2585 }
2586
2587 protected void validateEmailAddress(User user, String emailAddress)
2588 throws PortalException, SystemException {
2589
2590 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
2591 Company company = companyPersistence.findByPrimaryKey(
2592 user.getCompanyId());
2593
2594 if (!company.isStrangersWithMx()) {
2595 throw new ReservedUserEmailAddressException();
2596 }
2597 }
2598 }
2599
2600 protected void validateOrganizationUsers(long[] userIds)
2601 throws PortalException, SystemException {
2602
2603 PermissionChecker permissionChecker = getPermissionChecker();
2604
2605 if (!PropsValues.ORGANIZATIONS_ASSIGNMENT_STRICT ||
2606 permissionChecker.isCompanyAdmin()) {
2607
2608 return;
2609 }
2610
2611 for (long userId : userIds) {
2612 boolean allowed = false;
2613
2614 List<Organization> organizations =
2615 organizationLocalService.getUserOrganizations(userId);
2616
2617 for (Organization organization : organizations) {
2618 if (OrganizationPermissionUtil.contains(
2619 permissionChecker, organization,
2620 ActionKeys.MANAGE_USERS)) {
2621
2622 allowed = true;
2623
2624 break;
2625 }
2626 }
2627
2628 if (!allowed) {
2629 throw new PrincipalException();
2630 }
2631 }
2632 }
2633
2634 protected void validateUpdatePermission(
2635 User user, String screenName, String emailAddress, String firstName,
2636 String middleName, String lastName, int prefixId, int suffixId,
2637 int birthdayMonth, int birthdayDay, int birthdayYear, boolean male,
2638 String jobTitle)
2639 throws PortalException, SystemException {
2640
2641 List<String> fields = new ArrayList<String>();
2642
2643 Contact contact = user.getContact();
2644
2645 Calendar birthday = CalendarFactoryUtil.getCalendar();
2646
2647 birthday.setTime(contact.getBirthday());
2648
2649 if ((birthdayMonth != birthday.get(Calendar.MONTH)) ||
2650 (birthdayDay != birthday.get(Calendar.DAY_OF_MONTH)) ||
2651 (birthdayYear != birthday.get(Calendar.YEAR))) {
2652
2653 fields.add("birthday");
2654 }
2655
2656 if (!StringUtil.equalsIgnoreCase(
2657 emailAddress, user.getEmailAddress())) {
2658
2659 fields.add("emailAddress");
2660 }
2661
2662 if (!StringUtil.equalsIgnoreCase(firstName, user.getFirstName())) {
2663 fields.add("firstName");
2664 }
2665
2666 if (male != contact.getMale()) {
2667 fields.add("gender");
2668 }
2669
2670 if (!StringUtil.equalsIgnoreCase(jobTitle, user.getJobTitle())) {
2671 fields.add("jobTitle");
2672 }
2673
2674 if (!StringUtil.equalsIgnoreCase(lastName, user.getLastName())) {
2675 fields.add("lastName");
2676 }
2677
2678 if (!StringUtil.equalsIgnoreCase(middleName, user.getMiddleName())) {
2679 fields.add("middleName");
2680 }
2681
2682 if (prefixId != contact.getPrefixId()) {
2683 fields.add("prefix");
2684 }
2685
2686 if (!StringUtil.equalsIgnoreCase(screenName, user.getScreenName())) {
2687 fields.add("screenName");
2688 }
2689
2690 if (suffixId != contact.getSuffixId()) {
2691 fields.add("suffix");
2692 }
2693
2694 UserFieldException ufe = new UserFieldException();
2695
2696 for (String field : fields) {
2697 if (!UsersAdminUtil.hasUpdateFieldPermission(user, field)) {
2698 ufe.addField(field);
2699 }
2700 }
2701
2702 if (ufe.hasFields()) {
2703 throw ufe;
2704 }
2705 }
2706
2707 }