001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.RequiredUserException;
018 import com.liferay.portal.UserEmailAddressException;
019 import com.liferay.portal.UserFieldException;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.search.Indexer;
024 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
025 import com.liferay.portal.kernel.util.ArrayUtil;
026 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
027 import com.liferay.portal.kernel.util.ListUtil;
028 import com.liferay.portal.kernel.util.ParamUtil;
029 import com.liferay.portal.kernel.util.SetUtil;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.workflow.WorkflowConstants;
032 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
033 import com.liferay.portal.model.Address;
034 import com.liferay.portal.model.Company;
035 import com.liferay.portal.model.CompanyConstants;
036 import com.liferay.portal.model.Contact;
037 import com.liferay.portal.model.EmailAddress;
038 import com.liferay.portal.model.Group;
039 import com.liferay.portal.model.GroupConstants;
040 import com.liferay.portal.model.Organization;
041 import com.liferay.portal.model.Phone;
042 import com.liferay.portal.model.Role;
043 import com.liferay.portal.model.RoleConstants;
044 import com.liferay.portal.model.User;
045 import com.liferay.portal.model.UserGroup;
046 import com.liferay.portal.model.UserGroupRole;
047 import com.liferay.portal.model.Website;
048 import com.liferay.portal.security.auth.PrincipalException;
049 import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
050 import com.liferay.portal.security.membershippolicy.RoleMembershipPolicyUtil;
051 import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
052 import com.liferay.portal.security.membershippolicy.UserGroupMembershipPolicyUtil;
053 import com.liferay.portal.security.permission.ActionKeys;
054 import com.liferay.portal.security.permission.PermissionChecker;
055 import com.liferay.portal.service.ServiceContext;
056 import com.liferay.portal.service.base.UserServiceBaseImpl;
057 import com.liferay.portal.service.permission.GroupPermissionUtil;
058 import com.liferay.portal.service.permission.OrganizationPermissionUtil;
059 import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
060 import com.liferay.portal.service.permission.PortalPermissionUtil;
061 import com.liferay.portal.service.permission.RolePermissionUtil;
062 import com.liferay.portal.service.permission.TeamPermissionUtil;
063 import com.liferay.portal.service.permission.UserGroupPermissionUtil;
064 import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
065 import com.liferay.portal.service.permission.UserPermissionUtil;
066 import com.liferay.portal.util.PropsValues;
067 import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
068 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
069
070 import java.util.ArrayList;
071 import java.util.Calendar;
072 import java.util.Collections;
073 import java.util.List;
074 import java.util.Locale;
075 import java.util.Set;
076
077
087 public class UserServiceImpl extends UserServiceBaseImpl {
088
089
101 @Override
102 public void addGroupUsers(
103 long groupId, long[] userIds, ServiceContext serviceContext)
104 throws PortalException {
105
106 if (userIds.length == 0) {
107 return;
108 }
109
110 PermissionChecker permissionChecker = getPermissionChecker();
111
112 if (!GroupPermissionUtil.contains(
113 permissionChecker, groupId, ActionKeys.ASSIGN_MEMBERS)) {
114
115
116
117 boolean hasPermission = false;
118
119 if (userIds.length == 1) {
120 User user = getUser();
121
122 if (user.getUserId() == userIds[0]) {
123 Group group = groupPersistence.findByPrimaryKey(groupId);
124
125 if (user.getCompanyId() == group.getCompanyId()) {
126 int type = group.getType();
127
128 if (type == GroupConstants.TYPE_SITE_OPEN) {
129 hasPermission = true;
130 }
131 }
132 }
133 }
134
135 if (!hasPermission) {
136 throw new PrincipalException.MustHavePermission(
137 permissionChecker, Group.class.getName(), groupId,
138 ActionKeys.ASSIGN_MEMBERS);
139 }
140 }
141
142 SiteMembershipPolicyUtil.checkMembership(
143 userIds, new long[] {groupId}, null);
144
145 userLocalService.addGroupUsers(groupId, userIds);
146
147 SiteMembershipPolicyUtil.propagateMembership(
148 userIds, new long[] {groupId}, null);
149 }
150
151
162 @Override
163 public void addOrganizationUsers(long organizationId, long[] userIds)
164 throws PortalException {
165
166 if (userIds.length == 0) {
167 return;
168 }
169
170 OrganizationPermissionUtil.check(
171 getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
172
173 validateOrganizationUsers(userIds);
174
175 OrganizationMembershipPolicyUtil.checkMembership(
176 userIds, new long[] {organizationId}, null);
177
178 userLocalService.addOrganizationUsers(organizationId, userIds);
179
180 OrganizationMembershipPolicyUtil.propagateMembership(
181 userIds, new long[] {organizationId}, null);
182 }
183
184
193 @Override
194 public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
195 throws PortalException {
196
197 if (userIds.length == 0) {
198 return;
199 }
200
201 PasswordPolicyPermissionUtil.check(
202 getPermissionChecker(), passwordPolicyId,
203 ActionKeys.ASSIGN_MEMBERS);
204
205 userLocalService.addPasswordPolicyUsers(passwordPolicyId, userIds);
206 }
207
208
218 @Override
219 public void addRoleUsers(long roleId, long[] userIds)
220 throws PortalException {
221
222 if (userIds.length == 0) {
223 return;
224 }
225
226 RolePermissionUtil.check(
227 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
228
229 RoleMembershipPolicyUtil.checkRoles(userIds, new long[] {roleId}, null);
230
231 userLocalService.addRoleUsers(roleId, userIds);
232
233 RoleMembershipPolicyUtil.propagateRoles(
234 userIds, new long[] {roleId}, null);
235 }
236
237
246 @Override
247 public void addTeamUsers(long teamId, long[] userIds)
248 throws PortalException {
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
309 @Override
310 public User addUser(
311 long companyId, boolean autoPassword, String password1,
312 String password2, boolean autoScreenName, String screenName,
313 String emailAddress, long facebookId, String openId, Locale locale,
314 String firstName, String middleName, String lastName, long prefixId,
315 long suffixId, boolean male, int birthdayMonth, int birthdayDay,
316 int birthdayYear, String jobTitle, long[] groupIds,
317 long[] organizationIds, long[] roleIds, long[] userGroupIds,
318 boolean sendEmail, ServiceContext serviceContext)
319 throws PortalException {
320
321 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
322
323 try {
324 WorkflowThreadLocal.setEnabled(false);
325
326 return addUserWithWorkflow(
327 companyId, autoPassword, password1, password2, autoScreenName,
328 screenName, emailAddress, facebookId, openId, locale, firstName,
329 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
330 birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
331 roleIds, userGroupIds, sendEmail, serviceContext);
332 }
333 finally {
334 WorkflowThreadLocal.setEnabled(workflowEnabled);
335 }
336 }
337
338
392 @Override
393 public User addUser(
394 long companyId, boolean autoPassword, String password1,
395 String password2, boolean autoScreenName, String screenName,
396 String emailAddress, long facebookId, String openId, Locale locale,
397 String firstName, String middleName, String lastName, long prefixId,
398 long suffixId, boolean male, int birthdayMonth, int birthdayDay,
399 int birthdayYear, String jobTitle, long[] groupIds,
400 long[] organizationIds, long[] roleIds, long[] userGroupIds,
401 List<Address> addresses, List<EmailAddress> emailAddresses,
402 List<Phone> phones, List<Website> websites,
403 List<AnnouncementsDelivery> announcementsDelivers,
404 boolean sendEmail, ServiceContext serviceContext)
405 throws PortalException {
406
407 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
408
409 try {
410 WorkflowThreadLocal.setEnabled(false);
411
412 return addUserWithWorkflow(
413 companyId, autoPassword, password1, password2, autoScreenName,
414 screenName, emailAddress, facebookId, openId, locale, firstName,
415 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
416 birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
417 roleIds, userGroupIds, addresses, emailAddresses, phones,
418 websites, announcementsDelivers, sendEmail, serviceContext);
419 }
420 finally {
421 WorkflowThreadLocal.setEnabled(workflowEnabled);
422 }
423 }
424
425
435 @Override
436 public void addUserGroupUsers(long userGroupId, long[] userIds)
437 throws PortalException {
438
439 if (userIds.length == 0) {
440 return;
441 }
442
443 UserGroupPermissionUtil.check(
444 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
445
446 UserGroupMembershipPolicyUtil.checkMembership(
447 userIds, new long[] {userGroupId}, null);
448
449 userLocalService.addUserGroupUsers(userGroupId, userIds);
450
451 UserGroupMembershipPolicyUtil.propagateMembership(
452 userIds, new long[] {userGroupId}, null);
453 }
454
455
504 @Override
505 public User addUserWithWorkflow(
506 long companyId, boolean autoPassword, String password1,
507 String password2, boolean autoScreenName, String screenName,
508 String emailAddress, long facebookId, String openId, Locale locale,
509 String firstName, String middleName, String lastName, long prefixId,
510 long suffixId, boolean male, int birthdayMonth, int birthdayDay,
511 int birthdayYear, String jobTitle, long[] groupIds,
512 long[] organizationIds, long[] roleIds, long[] userGroupIds,
513 boolean sendEmail, ServiceContext serviceContext)
514 throws PortalException {
515
516 long creatorUserId = 0;
517
518 try {
519 creatorUserId = getGuestOrUserId();
520 }
521 catch (PrincipalException pe) {
522 if (_log.isWarnEnabled()) {
523 _log.warn("Unable to get guest or current user ID", pe);
524 }
525 }
526
527 checkAddUserPermission(
528 creatorUserId, companyId, emailAddress, groupIds, organizationIds,
529 roleIds, userGroupIds, serviceContext);
530
531 User user = userLocalService.addUserWithWorkflow(
532 creatorUserId, companyId, autoPassword, password1, password2,
533 autoScreenName, screenName, emailAddress, facebookId, openId,
534 locale, firstName, middleName, lastName, prefixId, suffixId, male,
535 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
536 organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
537
538 checkMembership(
539 new long[] {user.getUserId()}, groupIds, organizationIds, roleIds,
540 userGroupIds);
541
542 propagateMembership(
543 new long[] {user.getUserId()}, groupIds, organizationIds, roleIds,
544 userGroupIds);
545
546 return user;
547 }
548
549
603 @Override
604 public User addUserWithWorkflow(
605 long companyId, boolean autoPassword, String password1,
606 String password2, boolean autoScreenName, String screenName,
607 String emailAddress, long facebookId, String openId, Locale locale,
608 String firstName, String middleName, String lastName, long prefixId,
609 long suffixId, boolean male, int birthdayMonth, int birthdayDay,
610 int birthdayYear, String jobTitle, long[] groupIds,
611 long[] organizationIds, long[] roleIds, long[] userGroupIds,
612 List<Address> addresses, List<EmailAddress> emailAddresses,
613 List<Phone> phones, List<Website> websites,
614 List<AnnouncementsDelivery> announcementsDelivers,
615 boolean sendEmail, ServiceContext serviceContext)
616 throws PortalException {
617
618 boolean indexingEnabled = true;
619
620 if (serviceContext != null) {
621 indexingEnabled = serviceContext.isIndexingEnabled();
622
623 serviceContext.setIndexingEnabled(false);
624 }
625
626 try {
627 User user = addUserWithWorkflow(
628 companyId, autoPassword, password1, password2, autoScreenName,
629 screenName, emailAddress, facebookId, openId, locale, firstName,
630 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
631 birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
632 roleIds, userGroupIds, sendEmail, serviceContext);
633
634 UsersAdminUtil.updateAddresses(
635 Contact.class.getName(), user.getContactId(), addresses);
636
637 UsersAdminUtil.updateEmailAddresses(
638 Contact.class.getName(), user.getContactId(), emailAddresses);
639
640 UsersAdminUtil.updatePhones(
641 Contact.class.getName(), user.getContactId(), phones);
642
643 UsersAdminUtil.updateWebsites(
644 Contact.class.getName(), user.getContactId(), websites);
645
646 updateAnnouncementsDeliveries(
647 user.getUserId(), announcementsDelivers);
648
649 if (indexingEnabled) {
650 Indexer<User> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
651 User.class);
652
653 indexer.reindex(user);
654 }
655
656 return user;
657 }
658 finally {
659 if (serviceContext != null) {
660 serviceContext.setIndexingEnabled(indexingEnabled);
661 }
662 }
663 }
664
665
673 @Override
674 public void deletePortrait(long userId) throws PortalException {
675 UserPermissionUtil.check(
676 getPermissionChecker(), userId, ActionKeys.UPDATE);
677
678 userLocalService.deletePortrait(userId);
679 }
680
681
690 @Override
691 public void deleteRoleUser(long roleId, long userId)
692 throws PortalException {
693
694 RolePermissionUtil.check(
695 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
696
697 userLocalService.deleteRoleUser(roleId, userId);
698 }
699
700
707 @Override
708 public void deleteUser(long userId) throws PortalException {
709 if (getUserId() == userId) {
710 throw new RequiredUserException();
711 }
712
713 UserPermissionUtil.check(
714 getPermissionChecker(), userId, ActionKeys.DELETE);
715
716 userLocalService.deleteUser(userId);
717 }
718
719 @Override
720 public List<User> getCompanyUsers(long companyId, int start, int end)
721 throws PortalException {
722
723 PermissionChecker permissionChecker = getPermissionChecker();
724
725 if (!permissionChecker.isCompanyAdmin(companyId)) {
726 throw new PrincipalException.MustBeCompanyAdmin(permissionChecker);
727 }
728
729 return userPersistence.findByCompanyId(companyId, start, end);
730 }
731
732 @Override
733 public int getCompanyUsersCount(long companyId) throws PortalException {
734 PermissionChecker permissionChecker = getPermissionChecker();
735
736 if (!permissionChecker.isCompanyAdmin(companyId)) {
737 throw new PrincipalException.MustBeCompanyAdmin(permissionChecker);
738 }
739
740 return userPersistence.countByCompanyId(companyId);
741 }
742
743 @Override
744 public User getCurrentUser() throws PortalException {
745 return getUser();
746 }
747
748
756 @Override
757 public long[] getGroupUserIds(long groupId) throws PortalException {
758 GroupPermissionUtil.check(
759 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
760
761 return userLocalService.getGroupUserIds(groupId);
762 }
763
764
772 @Override
773 public List<User> getGroupUsers(long groupId) throws PortalException {
774 GroupPermissionUtil.check(
775 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
776
777 return userLocalService.getGroupUsers(groupId);
778 }
779
780
788 @Override
789 public long[] getOrganizationUserIds(long organizationId)
790 throws PortalException {
791
792 OrganizationPermissionUtil.check(
793 getPermissionChecker(), organizationId, ActionKeys.VIEW_MEMBERS);
794
795 return userLocalService.getOrganizationUserIds(organizationId);
796 }
797
798
806 @Override
807 public List<User> getOrganizationUsers(long organizationId)
808 throws PortalException {
809
810 OrganizationPermissionUtil.check(
811 getPermissionChecker(), organizationId, ActionKeys.VIEW_MEMBERS);
812
813 return userLocalService.getOrganizationUsers(organizationId);
814 }
815
816
824 @Override
825 public long[] getRoleUserIds(long roleId) throws PortalException {
826 RolePermissionUtil.check(
827 getPermissionChecker(), roleId, ActionKeys.VIEW);
828
829 return userLocalService.getRoleUserIds(roleId);
830 }
831
832
842 @Override
843 public User getUserByEmailAddress(long companyId, String emailAddress)
844 throws PortalException {
845
846 User user = userLocalService.getUserByEmailAddress(
847 companyId, emailAddress);
848
849 UserPermissionUtil.check(
850 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
851
852 return user;
853 }
854
855
863 @Override
864 public User getUserById(long userId) throws PortalException {
865 User user = userPersistence.findByPrimaryKey(userId);
866
867 UserPermissionUtil.check(
868 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
869
870 return user;
871 }
872
873
882 @Override
883 public User getUserByScreenName(long companyId, String screenName)
884 throws PortalException {
885
886 User user = userLocalService.getUserByScreenName(companyId, screenName);
887
888 UserPermissionUtil.check(
889 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
890
891 return user;
892 }
893
894 @Override
895 public List<User> getUserGroupUsers(long userGroupId)
896 throws PortalException {
897
898 UserGroupPermissionUtil.check(
899 getPermissionChecker(), userGroupId, ActionKeys.VIEW_MEMBERS);
900
901 return userGroupPersistence.getUsers(userGroupId);
902 }
903
904
913 @Override
914 public long getUserIdByEmailAddress(long companyId, String emailAddress)
915 throws PortalException {
916
917 User user = getUserByEmailAddress(companyId, emailAddress);
918
919 UserPermissionUtil.check(
920 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
921
922 return user.getUserId();
923 }
924
925
933 @Override
934 public long getUserIdByScreenName(long companyId, String screenName)
935 throws PortalException {
936
937 User user = getUserByScreenName(companyId, screenName);
938
939 UserPermissionUtil.check(
940 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
941
942 return user.getUserId();
943 }
944
945
955 @Override
956 public boolean hasGroupUser(long groupId, long userId)
957 throws PortalException {
958
959 if (!UserPermissionUtil.contains(
960 getPermissionChecker(), userId, ActionKeys.VIEW)) {
961
962 GroupPermissionUtil.check(
963 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
964 }
965
966 return userLocalService.hasGroupUser(groupId, userId);
967 }
968
969
979 @Override
980 public boolean hasRoleUser(long roleId, long userId)
981 throws PortalException {
982
983 if (!UserPermissionUtil.contains(
984 getPermissionChecker(), userId, ActionKeys.VIEW)) {
985
986 RolePermissionUtil.check(
987 getPermissionChecker(), roleId, ActionKeys.VIEW_MEMBERS);
988 }
989
990 return userLocalService.hasRoleUser(roleId, userId);
991 }
992
993
1007 @Override
1008 public boolean hasRoleUser(
1009 long companyId, String name, long userId, boolean inherited)
1010 throws PortalException {
1011
1012 if (!UserPermissionUtil.contains(
1013 getPermissionChecker(), userId, ActionKeys.VIEW)) {
1014
1015 Role role = roleLocalService.getRole(companyId, name);
1016
1017 RolePermissionUtil.check(
1018 getPermissionChecker(), role.getRoleId(),
1019 ActionKeys.VIEW_MEMBERS);
1020 }
1021
1022 return userLocalService.hasRoleUser(companyId, name, userId, inherited);
1023 }
1024
1025
1047 @Override
1048 public boolean sendPasswordByEmailAddress(
1049 long companyId, String emailAddress)
1050 throws PortalException {
1051
1052 return userLocalService.sendPasswordByEmailAddress(
1053 companyId, emailAddress);
1054 }
1055
1056
1076 @Override
1077 public boolean sendPasswordByScreenName(long companyId, String screenName)
1078 throws PortalException {
1079
1080 return userLocalService.sendPasswordByScreenName(companyId, screenName);
1081 }
1082
1083
1102 @Override
1103 public boolean sendPasswordByUserId(long userId) throws PortalException {
1104 return userLocalService.sendPasswordByUserId(userId);
1105 }
1106
1107
1117 @Override
1118 public void setRoleUsers(long roleId, long[] userIds)
1119 throws PortalException {
1120
1121 RolePermissionUtil.check(
1122 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
1123
1124 Set<Long> unsetUserIds = SetUtil.fromArray(
1125 rolePersistence.getUserPrimaryKeys(roleId));
1126
1127 unsetUserIds.removeAll(SetUtil.fromArray(userIds));
1128
1129 if (!unsetUserIds.isEmpty()) {
1130 RoleMembershipPolicyUtil.checkRoles(
1131 ArrayUtil.toLongArray(unsetUserIds), null, new long[] {roleId});
1132 }
1133
1134 if (userIds.length > 0) {
1135 RoleMembershipPolicyUtil.checkRoles(
1136 userIds, new long[] {roleId}, null);
1137 }
1138
1139 userLocalService.setRoleUsers(roleId, userIds);
1140
1141 if (!unsetUserIds.isEmpty()) {
1142 RoleMembershipPolicyUtil.propagateRoles(
1143 ArrayUtil.toLongArray(unsetUserIds), null, new long[] {roleId});
1144 }
1145
1146 if (userIds.length > 0) {
1147 RoleMembershipPolicyUtil.propagateRoles(
1148 userIds, new long[] {roleId}, null);
1149 }
1150 }
1151
1152
1161 @Override
1162 public void setUserGroupUsers(long userGroupId, long[] userIds)
1163 throws PortalException {
1164
1165 UserGroupPermissionUtil.check(
1166 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
1167
1168 Set<Long> unsetUserIds = SetUtil.fromArray(
1169 userGroupPersistence.getUserPrimaryKeys(userGroupId));
1170
1171 unsetUserIds.removeAll(SetUtil.fromArray(userIds));
1172
1173 if (!unsetUserIds.isEmpty()) {
1174 UserGroupMembershipPolicyUtil.checkMembership(
1175 ArrayUtil.toLongArray(unsetUserIds), null,
1176 new long[] {userGroupId});
1177 }
1178
1179 if (userIds.length > 0) {
1180 UserGroupMembershipPolicyUtil.checkMembership(
1181 userIds, new long[] {userGroupId}, null);
1182 }
1183
1184 userLocalService.setUserGroupUsers(userGroupId, userIds);
1185
1186 if (!unsetUserIds.isEmpty()) {
1187 UserGroupMembershipPolicyUtil.propagateMembership(
1188 ArrayUtil.toLongArray(unsetUserIds), null,
1189 new long[] {userGroupId});
1190 }
1191
1192 if (userIds.length > 0) {
1193 UserGroupMembershipPolicyUtil.propagateMembership(
1194 userIds, new long[] {userGroupId}, null);
1195 }
1196 }
1197
1198
1206 @Override
1207 public void unsetGroupTeamsUsers(long groupId, long[] userIds)
1208 throws PortalException {
1209
1210 if (userIds.length == 0) {
1211 return;
1212 }
1213
1214 UserGroupPermissionUtil.check(
1215 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
1216
1217 userLocalService.unsetGroupTeamsUsers(groupId, userIds);
1218 }
1219
1220
1231 @Override
1232 public void unsetGroupUsers(
1233 long groupId, long[] userIds, ServiceContext serviceContext)
1234 throws PortalException {
1235
1236 userIds = UsersAdminUtil.filterUnsetGroupUserIds(
1237 getPermissionChecker(), groupId, userIds);
1238
1239 if (userIds.length == 0) {
1240 return;
1241 }
1242
1243 PermissionChecker permissionChecker = getPermissionChecker();
1244
1245 if (!GroupPermissionUtil.contains(
1246 permissionChecker, groupId, ActionKeys.ASSIGN_MEMBERS)) {
1247
1248
1249
1250 boolean hasPermission = false;
1251
1252 if (userIds.length == 1) {
1253 User user = getUser();
1254
1255 if (user.getUserId() == userIds[0]) {
1256 Group group = groupPersistence.findByPrimaryKey(groupId);
1257
1258 if (user.getCompanyId() == group.getCompanyId()) {
1259 int type = group.getType();
1260
1261 if ((type == GroupConstants.TYPE_SITE_OPEN) ||
1262 (type == GroupConstants.TYPE_SITE_RESTRICTED)) {
1263
1264 hasPermission = true;
1265 }
1266 }
1267 }
1268 }
1269
1270 if (!hasPermission) {
1271 throw new PrincipalException.MustHavePermission(
1272 permissionChecker, Group.class.getName(), groupId,
1273 ActionKeys.ASSIGN_MEMBERS);
1274 }
1275 }
1276
1277 SiteMembershipPolicyUtil.checkMembership(
1278 userIds, null, new long[] {groupId});
1279
1280 userLocalService.unsetGroupUsers(groupId, userIds, serviceContext);
1281
1282 SiteMembershipPolicyUtil.propagateMembership(
1283 userIds, null, new long[] {groupId});
1284 }
1285
1286
1295 @Override
1296 public void unsetOrganizationUsers(long organizationId, long[] userIds)
1297 throws PortalException {
1298
1299 userIds = UsersAdminUtil.filterUnsetOrganizationUserIds(
1300 getPermissionChecker(), organizationId, userIds);
1301
1302 if (userIds.length == 0) {
1303 return;
1304 }
1305
1306 OrganizationPermissionUtil.check(
1307 getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
1308
1309 OrganizationMembershipPolicyUtil.checkMembership(
1310 userIds, null, new long[] {organizationId});
1311
1312 userLocalService.unsetOrganizationUsers(organizationId, userIds);
1313
1314 OrganizationMembershipPolicyUtil.propagateMembership(
1315 userIds, null, new long[] {organizationId});
1316 }
1317
1318
1326 @Override
1327 public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
1328 throws PortalException {
1329
1330 if (userIds.length == 0) {
1331 return;
1332 }
1333
1334 PasswordPolicyPermissionUtil.check(
1335 getPermissionChecker(), passwordPolicyId,
1336 ActionKeys.ASSIGN_MEMBERS);
1337
1338 userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
1339 }
1340
1341
1350 @Override
1351 public void unsetRoleUsers(long roleId, long[] userIds)
1352 throws PortalException {
1353
1354 if (userIds.length == 0) {
1355 return;
1356 }
1357
1358 RolePermissionUtil.check(
1359 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
1360
1361 RoleMembershipPolicyUtil.checkRoles(userIds, null, new long[] {roleId});
1362
1363 userLocalService.unsetRoleUsers(roleId, userIds);
1364
1365 RoleMembershipPolicyUtil.propagateRoles(
1366 userIds, null, new long[] {roleId});
1367 }
1368
1369
1377 @Override
1378 public void unsetTeamUsers(long teamId, long[] userIds)
1379 throws PortalException {
1380
1381 if (userIds.length == 0) {
1382 return;
1383 }
1384
1385 TeamPermissionUtil.check(
1386 getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
1387
1388 userLocalService.unsetTeamUsers(teamId, userIds);
1389 }
1390
1391
1400 @Override
1401 public void unsetUserGroupUsers(long userGroupId, long[] userIds)
1402 throws PortalException {
1403
1404 if (userIds.length == 0) {
1405 return;
1406 }
1407
1408 UserGroupPermissionUtil.check(
1409 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
1410
1411 UserGroupMembershipPolicyUtil.checkMembership(
1412 userIds, null, new long[] {userGroupId});
1413
1414 userLocalService.unsetUserGroupUsers(userGroupId, userIds);
1415
1416 UserGroupMembershipPolicyUtil.propagateMembership(
1417 userIds, null, new long[] {userGroupId});
1418 }
1419
1420
1429 @Override
1430 public User updateAgreedToTermsOfUse(
1431 long userId, boolean agreedToTermsOfUse)
1432 throws PortalException {
1433
1434 UserPermissionUtil.check(
1435 getPermissionChecker(), userId, ActionKeys.UPDATE);
1436
1437 return userLocalService.updateAgreedToTermsOfUse(
1438 userId, agreedToTermsOfUse);
1439 }
1440
1441
1455 @Override
1456 public User updateEmailAddress(
1457 long userId, String password, String emailAddress1,
1458 String emailAddress2, ServiceContext serviceContext)
1459 throws PortalException {
1460
1461 UserPermissionUtil.check(
1462 getPermissionChecker(), userId, ActionKeys.UPDATE);
1463
1464 User user = userPersistence.findByPrimaryKey(userId);
1465
1466 validateEmailAddress(user, emailAddress2);
1467
1468 return userLocalService.updateEmailAddress(
1469 userId, password, emailAddress1, emailAddress2, serviceContext);
1470 }
1471
1472
1510 @Override
1511 public User updateIncompleteUser(
1512 long companyId, boolean autoPassword, String password1,
1513 String password2, boolean autoScreenName, String screenName,
1514 String emailAddress, long facebookId, String openId, Locale locale,
1515 String firstName, String middleName, String lastName, long prefixId,
1516 long suffixId, boolean male, int birthdayMonth, int birthdayDay,
1517 int birthdayYear, String jobTitle, boolean updateUserInformation,
1518 boolean sendEmail, ServiceContext serviceContext)
1519 throws PortalException {
1520
1521 long creatorUserId = 0;
1522
1523 try {
1524 creatorUserId = getGuestOrUserId();
1525 }
1526 catch (PrincipalException pe) {
1527 if (_log.isWarnEnabled()) {
1528 _log.warn("Unable to get guest or current user ID", pe);
1529 }
1530 }
1531
1532 checkAddUserPermission(
1533 creatorUserId, companyId, emailAddress, null, null, null, null,
1534 serviceContext);
1535
1536 return userLocalService.updateIncompleteUser(
1537 creatorUserId, companyId, autoPassword, password1, password2,
1538 autoScreenName, screenName, emailAddress, facebookId, openId,
1539 locale, firstName, middleName, lastName, prefixId, suffixId, male,
1540 birthdayMonth, birthdayDay, birthdayYear, jobTitle,
1541 updateUserInformation, sendEmail, serviceContext);
1542 }
1543
1544
1553 @Override
1554 public User updateLockoutById(long userId, boolean lockout)
1555 throws PortalException {
1556
1557 UserPermissionUtil.check(
1558 getPermissionChecker(), userId, ActionKeys.DELETE);
1559
1560 return userLocalService.updateLockoutById(userId, lockout);
1561 }
1562
1563
1572 @Override
1573 public User updateOpenId(long userId, String openId)
1574 throws PortalException {
1575
1576 UserPermissionUtil.check(
1577 getPermissionChecker(), userId, ActionKeys.UPDATE);
1578
1579 return userLocalService.updateOpenId(userId, openId);
1580 }
1581
1582
1593 @Override
1594 public void updateOrganizations(
1595 long userId, long[] organizationIds, ServiceContext serviceContext)
1596 throws PortalException {
1597
1598 UserPermissionUtil.check(
1599 getPermissionChecker(), userId, ActionKeys.UPDATE);
1600
1601 checkOrganizations(userId, organizationIds);
1602
1603 userLocalService.updateOrganizations(
1604 userId, organizationIds, serviceContext);
1605 }
1606
1607
1619 @Override
1620 public User updatePassword(
1621 long userId, String password1, String password2,
1622 boolean passwordReset)
1623 throws PortalException {
1624
1625 UserPermissionUtil.check(
1626 getPermissionChecker(), userId, ActionKeys.UPDATE);
1627
1628 return userLocalService.updatePassword(
1629 userId, password1, password2, passwordReset);
1630 }
1631
1632
1642 @Override
1643 public User updatePortrait(long userId, byte[] bytes)
1644 throws PortalException {
1645
1646 UserPermissionUtil.check(
1647 getPermissionChecker(), userId, ActionKeys.UPDATE);
1648
1649 return userLocalService.updatePortrait(userId, bytes);
1650 }
1651
1652
1663 @Override
1664 public User updateReminderQuery(long userId, String question, String answer)
1665 throws PortalException {
1666
1667 UserPermissionUtil.check(
1668 getPermissionChecker(), userId, ActionKeys.UPDATE);
1669
1670 return userLocalService.updateReminderQuery(userId, question, answer);
1671 }
1672
1673
1683 @Override
1684 public User updateScreenName(long userId, String screenName)
1685 throws PortalException {
1686
1687 UserPermissionUtil.check(
1688 getPermissionChecker(), userId, ActionKeys.UPDATE);
1689
1690 return userLocalService.updateScreenName(userId, screenName);
1691 }
1692
1693
1707 @Deprecated
1708 @Override
1709 public User updateStatus(long userId, int status) throws PortalException {
1710 return updateStatus(userId, status, new ServiceContext());
1711 }
1712
1713
1728 @Override
1729 public User updateStatus(
1730 long userId, int status, ServiceContext serviceContext)
1731 throws PortalException {
1732
1733 if ((getUserId() == userId) &&
1734 (status != WorkflowConstants.STATUS_APPROVED)) {
1735
1736 throw new RequiredUserException();
1737 }
1738
1739 UserPermissionUtil.check(
1740 getPermissionChecker(), userId, ActionKeys.DELETE);
1741
1742 return userLocalService.updateStatus(userId, status, serviceContext);
1743 }
1744
1745
1808 @Override
1809 public User updateUser(
1810 long userId, String oldPassword, String newPassword1,
1811 String newPassword2, boolean passwordReset,
1812 String reminderQueryQuestion, String reminderQueryAnswer,
1813 String screenName, String emailAddress, long facebookId,
1814 String openId, boolean portrait, byte[] portraitBytes,
1815 String languageId, String timeZoneId, String greeting,
1816 String comments, String firstName, String middleName,
1817 String lastName, long prefixId, long suffixId, boolean male,
1818 int birthdayMonth, int birthdayDay, int birthdayYear, String smsSn,
1819 String aimSn, String facebookSn, String icqSn, String jabberSn,
1820 String mySpaceSn, String skypeSn, String twitterSn, String ymSn,
1821 String jobTitle, long[] groupIds, long[] organizationIds,
1822 long[] roleIds, List<UserGroupRole> userGroupRoles,
1823 long[] userGroupIds, List<Address> addresses,
1824 List<EmailAddress> emailAddresses, List<Phone> phones,
1825 List<Website> websites,
1826 List<AnnouncementsDelivery> announcementsDelivers,
1827 ServiceContext serviceContext)
1828 throws PortalException {
1829
1830 UserPermissionUtil.check(
1831 getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
1832
1833 User user = userPersistence.findByPrimaryKey(userId);
1834
1835 if (addresses != null) {
1836 UsersAdminUtil.updateAddresses(
1837 Contact.class.getName(), user.getContactId(), addresses);
1838 }
1839
1840 if (emailAddresses != null) {
1841 UsersAdminUtil.updateEmailAddresses(
1842 Contact.class.getName(), user.getContactId(), emailAddresses);
1843 }
1844
1845 if (phones != null) {
1846 UsersAdminUtil.updatePhones(
1847 Contact.class.getName(), user.getContactId(), phones);
1848 }
1849
1850 if (websites != null) {
1851 UsersAdminUtil.updateWebsites(
1852 Contact.class.getName(), user.getContactId(), websites);
1853 }
1854
1855 if (announcementsDelivers != null) {
1856 updateAnnouncementsDeliveries(
1857 user.getUserId(), announcementsDelivers);
1858 }
1859
1860 long curUserId = getUserId();
1861
1862 if (curUserId == userId) {
1863 emailAddress = StringUtil.toLowerCase(emailAddress.trim());
1864
1865 if (!StringUtil.equalsIgnoreCase(
1866 emailAddress, user.getEmailAddress())) {
1867
1868 validateEmailAddress(user, emailAddress);
1869 }
1870 }
1871
1872 validateUpdatePermission(
1873 user, screenName, emailAddress, firstName, middleName, lastName,
1874 prefixId, suffixId, birthdayMonth, birthdayDay, birthdayYear, male,
1875 jobTitle);
1876
1877
1878
1879 long[] oldGroupIds = user.getGroupIds();
1880
1881 List<Long> addGroupIds = new ArrayList<>();
1882 List<Long> removeGroupIds = Collections.emptyList();
1883
1884 if (groupIds != null) {
1885 removeGroupIds = ListUtil.toList(oldGroupIds);
1886
1887 groupIds = checkGroups(userId, groupIds);
1888
1889 for (long groupId : groupIds) {
1890 if (ArrayUtil.contains(oldGroupIds, groupId)) {
1891 removeGroupIds.remove(groupId);
1892 }
1893 else {
1894 addGroupIds.add(groupId);
1895 }
1896 }
1897
1898 if (!addGroupIds.isEmpty() || !removeGroupIds.isEmpty()) {
1899 SiteMembershipPolicyUtil.checkMembership(
1900 new long[] {userId}, ArrayUtil.toLongArray(addGroupIds),
1901 ArrayUtil.toLongArray(removeGroupIds));
1902 }
1903 }
1904
1905
1906
1907 long[] oldOrganizationIds = user.getOrganizationIds();
1908
1909 List<Long> addOrganizationIds = new ArrayList<>();
1910 List<Long> removeOrganizationIds = Collections.emptyList();
1911
1912 if (organizationIds != null) {
1913 removeOrganizationIds = ListUtil.toList(oldOrganizationIds);
1914
1915 organizationIds = checkOrganizations(userId, organizationIds);
1916
1917 for (long organizationId : organizationIds) {
1918 if (ArrayUtil.contains(oldOrganizationIds, organizationId)) {
1919 removeOrganizationIds.remove(organizationId);
1920 }
1921 else {
1922 addOrganizationIds.add(organizationId);
1923 }
1924 }
1925
1926 if (!addOrganizationIds.isEmpty() ||
1927 !removeOrganizationIds.isEmpty()) {
1928
1929 OrganizationMembershipPolicyUtil.checkMembership(
1930 new long[] {userId},
1931 ArrayUtil.toLongArray(addOrganizationIds),
1932 ArrayUtil.toLongArray(removeOrganizationIds));
1933 }
1934 }
1935
1936
1937
1938 long[] oldRoleIds = user.getRoleIds();
1939
1940 List<Long> addRoleIds = new ArrayList<>();
1941 List<Long> removeRoleIds = Collections.emptyList();
1942
1943 if (roleIds != null) {
1944 removeRoleIds = ListUtil.toList(oldRoleIds);
1945
1946 roleIds = checkRoles(userId, roleIds);
1947
1948 for (long roleId : roleIds) {
1949 if (ArrayUtil.contains(oldRoleIds, roleId)) {
1950 removeRoleIds.remove(roleId);
1951 }
1952 else {
1953 addRoleIds.add(roleId);
1954 }
1955 }
1956
1957 if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
1958 RoleMembershipPolicyUtil.checkRoles(
1959 new long[] {userId}, ArrayUtil.toLongArray(addRoleIds),
1960 ArrayUtil.toLongArray(removeRoleIds));
1961 }
1962 }
1963
1964 List<UserGroupRole> oldOrganizationUserGroupRoles = new ArrayList<>();
1965 List<UserGroupRole> oldSiteUserGroupRoles = new ArrayList<>();
1966
1967 List<UserGroupRole> oldUserGroupRoles =
1968 userGroupRolePersistence.findByUserId(userId);
1969
1970 for (UserGroupRole oldUserGroupRole : oldUserGroupRoles) {
1971 Role role = oldUserGroupRole.getRole();
1972
1973 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
1974 oldOrganizationUserGroupRoles.add(oldUserGroupRole);
1975 }
1976 else if (role.getType() == RoleConstants.TYPE_SITE) {
1977 oldSiteUserGroupRoles.add(oldUserGroupRole);
1978 }
1979 }
1980
1981 List<UserGroupRole> addOrganizationUserGroupRoles = new ArrayList<>();
1982 List<UserGroupRole> removeOrganizationUserGroupRoles =
1983 Collections.emptyList();
1984 List<UserGroupRole> addSiteUserGroupRoles = new ArrayList<>();
1985 List<UserGroupRole> removeSiteUserGroupRoles = Collections.emptyList();
1986
1987 if (userGroupRoles != null) {
1988 userGroupRoles = checkUserGroupRoles(userId, userGroupRoles);
1989
1990 removeOrganizationUserGroupRoles = ListUtil.copy(
1991 oldOrganizationUserGroupRoles);
1992 removeSiteUserGroupRoles = ListUtil.copy(oldSiteUserGroupRoles);
1993
1994 for (UserGroupRole userGroupRole : userGroupRoles) {
1995 Role role = userGroupRole.getRole();
1996
1997 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
1998 if (oldOrganizationUserGroupRoles.contains(userGroupRole)) {
1999 removeOrganizationUserGroupRoles.remove(userGroupRole);
2000 }
2001 else {
2002 addOrganizationUserGroupRoles.add(userGroupRole);
2003 }
2004 }
2005 else if (role.getType() == RoleConstants.TYPE_SITE) {
2006 if (oldSiteUserGroupRoles.contains(userGroupRole)) {
2007 removeSiteUserGroupRoles.remove(userGroupRole);
2008 }
2009 else {
2010 addSiteUserGroupRoles.add(userGroupRole);
2011 }
2012 }
2013 }
2014
2015 if (!addOrganizationUserGroupRoles.isEmpty() ||
2016 !removeOrganizationUserGroupRoles.isEmpty()) {
2017
2018 OrganizationMembershipPolicyUtil.checkRoles(
2019 addOrganizationUserGroupRoles,
2020 removeOrganizationUserGroupRoles);
2021 }
2022
2023 if (!addSiteUserGroupRoles.isEmpty() ||
2024 !removeSiteUserGroupRoles.isEmpty()) {
2025
2026 SiteMembershipPolicyUtil.checkRoles(
2027 addSiteUserGroupRoles, removeSiteUserGroupRoles);
2028 }
2029 }
2030
2031
2032
2033 long[] oldUserGroupIds = user.getUserGroupIds();
2034
2035 List<Long> addUserGroupIds = new ArrayList<>();
2036 List<Long> removeUserGroupIds = Collections.emptyList();
2037
2038 if (userGroupIds != null) {
2039 removeUserGroupIds = ListUtil.toList(oldUserGroupIds);
2040
2041 userGroupIds = checkUserGroupIds(userId, userGroupIds);
2042
2043 for (long userGroupId : userGroupIds) {
2044 if (ArrayUtil.contains(oldUserGroupIds, userGroupId)) {
2045 removeUserGroupIds.remove(userGroupId);
2046 }
2047 else {
2048 addUserGroupIds.add(userGroupId);
2049 }
2050 }
2051
2052 if (!addUserGroupIds.isEmpty() || !removeUserGroupIds.isEmpty()) {
2053 UserGroupMembershipPolicyUtil.checkMembership(
2054 new long[] {userId}, ArrayUtil.toLongArray(addUserGroupIds),
2055 ArrayUtil.toLongArray(removeUserGroupIds));
2056 }
2057 }
2058
2059 user = userLocalService.updateUser(
2060 userId, oldPassword, newPassword1, newPassword2, passwordReset,
2061 reminderQueryQuestion, reminderQueryAnswer, screenName,
2062 emailAddress, facebookId, openId, portrait, portraitBytes,
2063 languageId, timeZoneId, greeting, comments, firstName, middleName,
2064 lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay,
2065 birthdayYear, smsSn, aimSn, facebookSn, icqSn, jabberSn, mySpaceSn,
2066 skypeSn, twitterSn, ymSn, jobTitle, groupIds, organizationIds,
2067 roleIds, userGroupRoles, userGroupIds, serviceContext);
2068
2069 if (!addGroupIds.isEmpty() || !removeGroupIds.isEmpty()) {
2070 SiteMembershipPolicyUtil.propagateMembership(
2071 new long[] {user.getUserId()},
2072 ArrayUtil.toLongArray(addGroupIds),
2073 ArrayUtil.toLongArray(removeGroupIds));
2074 }
2075
2076 if (!addOrganizationIds.isEmpty() || !removeOrganizationIds.isEmpty()) {
2077 OrganizationMembershipPolicyUtil.propagateMembership(
2078 new long[] {user.getUserId()},
2079 ArrayUtil.toLongArray(addOrganizationIds),
2080 ArrayUtil.toLongArray(removeOrganizationIds));
2081 }
2082
2083 if (!addRoleIds.isEmpty() || !removeRoleIds.isEmpty()) {
2084 RoleMembershipPolicyUtil.propagateRoles(
2085 new long[] {user.getUserId()},
2086 ArrayUtil.toLongArray(addRoleIds),
2087 ArrayUtil.toLongArray(removeRoleIds));
2088 }
2089
2090 if (!addSiteUserGroupRoles.isEmpty() ||
2091 !removeSiteUserGroupRoles.isEmpty()) {
2092
2093 SiteMembershipPolicyUtil.propagateRoles(
2094 addSiteUserGroupRoles, removeSiteUserGroupRoles);
2095 }
2096
2097 if (!addOrganizationUserGroupRoles.isEmpty() ||
2098 !removeOrganizationUserGroupRoles.isEmpty()) {
2099
2100 OrganizationMembershipPolicyUtil.propagateRoles(
2101 addOrganizationUserGroupRoles,
2102 removeOrganizationUserGroupRoles);
2103 }
2104
2105 if (!addUserGroupIds.isEmpty() || !removeUserGroupIds.isEmpty()) {
2106 UserGroupMembershipPolicyUtil.propagateMembership(
2107 new long[] {user.getUserId()},
2108 ArrayUtil.toLongArray(addUserGroupIds),
2109 ArrayUtil.toLongArray(removeUserGroupIds));
2110 }
2111
2112 return user;
2113 }
2114
2115
2183 @Deprecated
2184 @Override
2185 public User updateUser(
2186 long userId, String oldPassword, String newPassword1,
2187 String newPassword2, boolean passwordReset,
2188 String reminderQueryQuestion, String reminderQueryAnswer,
2189 String screenName, String emailAddress, long facebookId,
2190 String openId, String languageId, String timeZoneId,
2191 String greeting, String comments, String firstName,
2192 String middleName, String lastName, long prefixId, long suffixId,
2193 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
2194 String smsSn, String aimSn, String facebookSn, String icqSn,
2195 String jabberSn, String mySpaceSn, String skypeSn, String twitterSn,
2196 String ymSn, String jobTitle, long[] groupIds,
2197 long[] organizationIds, long[] roleIds,
2198 List<UserGroupRole> userGroupRoles, long[] userGroupIds,
2199 List<Address> addresses, List<EmailAddress> emailAddresses,
2200 List<Phone> phones, List<Website> websites,
2201 List<AnnouncementsDelivery> announcementsDelivers,
2202 ServiceContext serviceContext)
2203 throws PortalException {
2204
2205 return updateUser(
2206 userId, oldPassword, newPassword1, newPassword2, passwordReset,
2207 reminderQueryQuestion, reminderQueryAnswer, screenName,
2208 emailAddress, facebookId, openId, true, null, languageId,
2209 timeZoneId, greeting, comments, firstName, middleName, lastName,
2210 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
2211 smsSn, aimSn, facebookSn, icqSn, jabberSn, mySpaceSn, skypeSn,
2212 twitterSn, ymSn, jobTitle, groupIds, organizationIds, roleIds,
2213 userGroupRoles, userGroupIds, addresses, emailAddresses, phones,
2214 websites, announcementsDelivers, serviceContext);
2215 }
2216
2217
2273 @Override
2274 public User updateUser(
2275 long userId, String oldPassword, String newPassword1,
2276 String newPassword2, boolean passwordReset,
2277 String reminderQueryQuestion, String reminderQueryAnswer,
2278 String screenName, String emailAddress, long facebookId,
2279 String openId, String languageId, String timeZoneId,
2280 String greeting, String comments, String firstName,
2281 String middleName, String lastName, long prefixId, long suffixId,
2282 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
2283 String smsSn, String aimSn, String facebookSn, String icqSn,
2284 String jabberSn, String mySpaceSn, String skypeSn, String twitterSn,
2285 String ymSn, String jobTitle, long[] groupIds,
2286 long[] organizationIds, long[] roleIds,
2287 List<UserGroupRole> userGroupRoles, long[] userGroupIds,
2288 ServiceContext serviceContext)
2289 throws PortalException {
2290
2291 return updateUser(
2292 userId, oldPassword, newPassword1, newPassword2, passwordReset,
2293 reminderQueryQuestion, reminderQueryAnswer, screenName,
2294 emailAddress, facebookId, openId, true, null, languageId,
2295 timeZoneId, greeting, comments, firstName, middleName, lastName,
2296 prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
2297 smsSn, aimSn, facebookSn, icqSn, jabberSn, mySpaceSn, skypeSn,
2298 twitterSn, ymSn, jobTitle, groupIds, organizationIds, roleIds,
2299 userGroupRoles, userGroupIds, null, null, null, null, null,
2300 serviceContext);
2301 }
2302
2303 protected void checkAddUserPermission(
2304 long creatorUserId, long companyId, String emailAddress,
2305 long[] groupIds, long[] organizationIds, long[] roleIds,
2306 long[] userGroupIds, ServiceContext serviceContext)
2307 throws PortalException {
2308
2309 Company company = companyPersistence.findByPrimaryKey(companyId);
2310
2311 if (groupIds != null) {
2312 checkGroups(0, groupIds);
2313 }
2314
2315 if (organizationIds != null) {
2316 checkOrganizations(0, organizationIds);
2317 }
2318
2319 if (roleIds != null) {
2320 checkRoles(0, roleIds);
2321 }
2322
2323 if (userGroupIds != null) {
2324 checkUserGroupIds(0, userGroupIds);
2325 }
2326
2327 boolean anonymousUser = ParamUtil.getBoolean(
2328 serviceContext, "anonymousUser");
2329
2330 long defaultUserId = userLocalService.getDefaultUserId(companyId);
2331
2332 if (((creatorUserId != 0) && (creatorUserId != defaultUserId)) ||
2333 (!company.isStrangers() && !anonymousUser)) {
2334
2335 PermissionChecker permissionChecker = getPermissionChecker();
2336
2337 if (!PortalPermissionUtil.contains(
2338 permissionChecker, ActionKeys.ADD_USER) &&
2339 !OrganizationPermissionUtil.contains(
2340 getPermissionChecker(), organizationIds,
2341 ActionKeys.ASSIGN_MEMBERS)) {
2342
2343 throw new PrincipalException.MustHavePermission(
2344 permissionChecker, Organization.class.getName(), 0,
2345 ActionKeys.ADD_USER, ActionKeys.ASSIGN_MEMBERS);
2346 }
2347 }
2348
2349 if ((creatorUserId == 0) || (creatorUserId == defaultUserId)) {
2350 if (!company.isStrangersWithMx() &&
2351 company.hasCompanyMx(emailAddress)) {
2352
2353 throw new UserEmailAddressException.MustNotUseCompanyMx(
2354 emailAddress);
2355 }
2356 }
2357 }
2358
2359 protected long[] checkGroups(long userId, long[] groupIds)
2360 throws PortalException {
2361
2362 long[] oldGroupIds = null;
2363
2364 PermissionChecker permissionChecker = getPermissionChecker();
2365
2366 User user = null;
2367
2368 if (userId != CompanyConstants.SYSTEM) {
2369
2370
2371
2372
2373
2374 user = userPersistence.findByPrimaryKey(userId);
2375
2376 List<Group> oldGroups = groupLocalService.getUserGroups(userId);
2377
2378 oldGroupIds = new long[oldGroups.size()];
2379
2380 for (int i = 0; i < oldGroups.size(); i++) {
2381 Group group = oldGroups.get(i);
2382
2383 if (!ArrayUtil.contains(groupIds, group.getGroupId()) &&
2384 (!GroupPermissionUtil.contains(
2385 permissionChecker, group, ActionKeys.ASSIGN_MEMBERS) ||
2386 SiteMembershipPolicyUtil.isMembershipProtected(
2387 permissionChecker, user.getUserId(),
2388 group.getGroupId()) ||
2389 SiteMembershipPolicyUtil.isMembershipRequired(
2390 userId, group.getGroupId()))) {
2391
2392 groupIds = ArrayUtil.append(groupIds, group.getGroupId());
2393 }
2394
2395 oldGroupIds[i] = group.getGroupId();
2396 }
2397 }
2398
2399
2400
2401
2402 for (long groupId : groupIds) {
2403 if ((oldGroupIds != null) &&
2404 ArrayUtil.contains(oldGroupIds, groupId)) {
2405
2406 continue;
2407 }
2408
2409 Group group = groupPersistence.findByPrimaryKey(groupId);
2410
2411 GroupPermissionUtil.check(
2412 permissionChecker, group, ActionKeys.ASSIGN_MEMBERS);
2413 }
2414
2415 return groupIds;
2416 }
2417
2418 protected void checkMembership(
2419 long[] userIds, long[] groupIds, long[] organizationIds,
2420 long[] roleIds, long[] userGroupIds)
2421 throws PortalException {
2422
2423 if (groupIds != null) {
2424 SiteMembershipPolicyUtil.checkMembership(userIds, groupIds, null);
2425 }
2426
2427 if (organizationIds != null) {
2428 OrganizationMembershipPolicyUtil.checkMembership(
2429 userIds, organizationIds, null);
2430 }
2431
2432 if (roleIds != null) {
2433 RoleMembershipPolicyUtil.checkRoles(userIds, roleIds, null);
2434 }
2435
2436 if (userGroupIds != null) {
2437 UserGroupMembershipPolicyUtil.checkMembership(
2438 userIds, userGroupIds, null);
2439 }
2440 }
2441
2442 protected long[] checkOrganizations(long userId, long[] organizationIds)
2443 throws PortalException {
2444
2445 long[] oldOrganizationIds = null;
2446
2447 PermissionChecker permissionChecker = getPermissionChecker();
2448
2449 if (userId != CompanyConstants.SYSTEM) {
2450
2451
2452
2453
2454
2455 List<Organization> oldOrganizations =
2456 organizationLocalService.getUserOrganizations(userId);
2457
2458 oldOrganizationIds = new long[oldOrganizations.size()];
2459
2460 for (int i = 0; i < oldOrganizations.size(); i++) {
2461 Organization organization = oldOrganizations.get(i);
2462
2463 if (!ArrayUtil.contains(
2464 organizationIds, organization.getOrganizationId()) &&
2465 (!OrganizationPermissionUtil.contains(
2466 permissionChecker, organization,
2467 ActionKeys.ASSIGN_MEMBERS) ||
2468 OrganizationMembershipPolicyUtil.isMembershipProtected(
2469 permissionChecker, userId,
2470 organization.getOrganizationId()) ||
2471 OrganizationMembershipPolicyUtil.isMembershipRequired(
2472 userId, organization.getOrganizationId()))) {
2473
2474 organizationIds = ArrayUtil.append(
2475 organizationIds, organization.getOrganizationId());
2476 }
2477
2478 oldOrganizationIds[i] = organization.getOrganizationId();
2479 }
2480 }
2481
2482
2483
2484
2485 for (long organizationId : organizationIds) {
2486 if ((oldOrganizationIds != null) &&
2487 ArrayUtil.contains(oldOrganizationIds, organizationId)) {
2488
2489 continue;
2490 }
2491
2492 Organization organization =
2493 organizationPersistence.findByPrimaryKey(organizationId);
2494
2495 OrganizationPermissionUtil.check(
2496 permissionChecker, organization, ActionKeys.ASSIGN_MEMBERS);
2497 }
2498
2499 return organizationIds;
2500 }
2501
2502 protected long[] checkRoles(long userId, long[] roleIds)
2503 throws PortalException {
2504
2505 long[] oldRoleIds = null;
2506
2507 PermissionChecker permissionChecker = getPermissionChecker();
2508
2509 if (userId != CompanyConstants.SYSTEM) {
2510
2511
2512
2513
2514
2515 List<Role> oldRoles = roleLocalService.getUserRoles(userId);
2516
2517 oldRoleIds = new long[oldRoles.size()];
2518
2519 for (int i = 0; i < oldRoles.size(); i++) {
2520 Role role = oldRoles.get(i);
2521
2522 if (!ArrayUtil.contains(roleIds, role.getRoleId()) &&
2523 (!RolePermissionUtil.contains(
2524 permissionChecker, role.getRoleId(),
2525 ActionKeys.ASSIGN_MEMBERS) ||
2526 RoleMembershipPolicyUtil.isRoleRequired(
2527 userId, role.getRoleId()))) {
2528
2529 roleIds = ArrayUtil.append(roleIds, role.getRoleId());
2530 }
2531
2532 oldRoleIds[i] = role.getRoleId();
2533 }
2534 }
2535
2536
2537
2538
2539 for (long roleId : roleIds) {
2540 if ((oldRoleIds != null) &&
2541 ArrayUtil.contains(oldRoleIds, roleId)) {
2542
2543 continue;
2544 }
2545
2546 RolePermissionUtil.check(
2547 permissionChecker, roleId, ActionKeys.ASSIGN_MEMBERS);
2548 }
2549
2550 if (userId != CompanyConstants.SYSTEM) {
2551 return UsersAdminUtil.addRequiredRoles(userId, roleIds);
2552 }
2553
2554 return roleIds;
2555 }
2556
2557 protected long[] checkUserGroupIds(long userId, long[] userGroupIds)
2558 throws PortalException {
2559
2560 long[] oldUserGroupIds = null;
2561
2562 PermissionChecker permissionChecker = getPermissionChecker();
2563
2564 if (userId != CompanyConstants.SYSTEM) {
2565
2566
2567
2568
2569 List<UserGroup> oldUserGroups =
2570 userGroupLocalService.getUserUserGroups(userId);
2571
2572 oldUserGroupIds = new long[oldUserGroups.size()];
2573
2574 for (int i = 0; i < oldUserGroups.size(); i++) {
2575 UserGroup userGroup = oldUserGroups.get(i);
2576
2577 if (!ArrayUtil.contains(
2578 userGroupIds, userGroup.getUserGroupId()) &&
2579 (!UserGroupPermissionUtil.contains(
2580 permissionChecker, userGroup.getUserGroupId(),
2581 ActionKeys.ASSIGN_MEMBERS) ||
2582 UserGroupMembershipPolicyUtil.isMembershipRequired(
2583 userId, userGroup.getUserGroupId()))) {
2584
2585 userGroupIds = ArrayUtil.append(
2586 userGroupIds, userGroup.getUserGroupId());
2587 }
2588
2589 oldUserGroupIds[i] = userGroup.getUserGroupId();
2590 }
2591 }
2592
2593
2594
2595
2596 for (long userGroupId : userGroupIds) {
2597 if ((oldUserGroupIds == null) ||
2598 !ArrayUtil.contains(oldUserGroupIds, userGroupId)) {
2599
2600 UserGroupPermissionUtil.check(
2601 permissionChecker, userGroupId, ActionKeys.ASSIGN_MEMBERS);
2602 }
2603 }
2604
2605 return userGroupIds;
2606 }
2607
2608 protected List<UserGroupRole> checkUserGroupRoles(
2609 long userId, List<UserGroupRole> userGroupRoles)
2610 throws PortalException {
2611
2612 List<UserGroupRole> oldUserGroupRoles = null;
2613
2614 PermissionChecker permissionChecker = getPermissionChecker();
2615
2616 if (userId != CompanyConstants.SYSTEM) {
2617
2618
2619
2620
2621 oldUserGroupRoles = userGroupRoleLocalService.getUserGroupRoles(
2622 userId);
2623
2624 for (UserGroupRole oldUserGroupRole : oldUserGroupRoles) {
2625 Role role = oldUserGroupRole.getRole();
2626 Group group = oldUserGroupRole.getGroup();
2627
2628 if (userGroupRoles.contains(oldUserGroupRole)) {
2629 continue;
2630 }
2631
2632 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
2633 Organization organization =
2634 organizationPersistence.findByPrimaryKey(
2635 group.getOrganizationId());
2636
2637 if (!UserGroupRolePermissionUtil.contains(
2638 permissionChecker, group, role) ||
2639 OrganizationMembershipPolicyUtil.isRoleProtected(
2640 getPermissionChecker(), userId,
2641 organization.getOrganizationId(),
2642 role.getRoleId()) ||
2643 OrganizationMembershipPolicyUtil.isRoleRequired(
2644 userId, organization.getOrganizationId(),
2645 role.getRoleId())) {
2646
2647 userGroupRoles.add(oldUserGroupRole);
2648 }
2649 }
2650 else if (role.getType() == RoleConstants.TYPE_SITE) {
2651 if (!userGroupRoles.contains(oldUserGroupRole) &&
2652 (!UserGroupRolePermissionUtil.contains(
2653 permissionChecker, group, role) ||
2654 SiteMembershipPolicyUtil.isRoleProtected(
2655 getPermissionChecker(), userId, group.getGroupId(),
2656 role.getRoleId()) ||
2657 SiteMembershipPolicyUtil.isRoleRequired(
2658 userId, group.getGroupId(), role.getRoleId()))) {
2659
2660 userGroupRoles.add(oldUserGroupRole);
2661 }
2662 }
2663 }
2664 }
2665
2666
2667
2668
2669 for (UserGroupRole userGroupRole : userGroupRoles) {
2670 if ((oldUserGroupRoles == null) ||
2671 !oldUserGroupRoles.contains(userGroupRole)) {
2672
2673 UserGroupRolePermissionUtil.check(
2674 permissionChecker, userGroupRole.getGroupId(),
2675 userGroupRole.getRoleId());
2676 }
2677 }
2678
2679 return userGroupRoles;
2680 }
2681
2682 protected void propagateMembership(
2683 long[] userIds, long[] groupIds, long[] organizationIds,
2684 long[] roleIds, long[] userGroupIds)
2685 throws PortalException {
2686
2687 if (groupIds != null) {
2688 SiteMembershipPolicyUtil.propagateMembership(
2689 userIds, groupIds, null);
2690 }
2691
2692 if (organizationIds != null) {
2693 OrganizationMembershipPolicyUtil.propagateMembership(
2694 userIds, organizationIds, null);
2695 }
2696
2697 if (roleIds != null) {
2698 RoleMembershipPolicyUtil.propagateRoles(userIds, roleIds, null);
2699 }
2700
2701 if (userGroupIds != null) {
2702 UserGroupMembershipPolicyUtil.propagateMembership(
2703 userIds, userGroupIds, null);
2704 }
2705 }
2706
2707 protected void updateAnnouncementsDeliveries(
2708 long userId, List<AnnouncementsDelivery> announcementsDeliveries)
2709 throws PortalException {
2710
2711 for (AnnouncementsDelivery announcementsDelivery :
2712 announcementsDeliveries) {
2713
2714 announcementsDeliveryService.updateDelivery(
2715 userId, announcementsDelivery.getType(),
2716 announcementsDelivery.getEmail(),
2717 announcementsDelivery.getSms(),
2718 announcementsDelivery.getWebsite());
2719 }
2720 }
2721
2722 protected void validateEmailAddress(User user, String emailAddress)
2723 throws PortalException {
2724
2725 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
2726 Company company = companyPersistence.findByPrimaryKey(
2727 user.getCompanyId());
2728
2729 if (!company.isStrangersWithMx()) {
2730 throw new UserEmailAddressException.MustNotUseCompanyMx(
2731 emailAddress);
2732 }
2733 }
2734 }
2735
2736 protected void validateOrganizationUsers(long[] userIds)
2737 throws PortalException {
2738
2739 PermissionChecker permissionChecker = getPermissionChecker();
2740
2741 if (!PropsValues.ORGANIZATIONS_ASSIGNMENT_STRICT ||
2742 permissionChecker.isCompanyAdmin()) {
2743
2744 return;
2745 }
2746
2747 for (long userId : userIds) {
2748 boolean allowed = false;
2749
2750 List<Organization> organizations =
2751 organizationLocalService.getUserOrganizations(userId);
2752
2753 for (Organization organization : organizations) {
2754 if (OrganizationPermissionUtil.contains(
2755 permissionChecker, organization,
2756 ActionKeys.MANAGE_USERS)) {
2757
2758 allowed = true;
2759
2760 break;
2761 }
2762 }
2763
2764 if (!allowed) {
2765 throw new PrincipalException.MustHavePermission(
2766 permissionChecker, Organization.class.getName(), 0,
2767 ActionKeys.MANAGE_USERS);
2768 }
2769 }
2770 }
2771
2772 protected void validateUpdatePermission(
2773 User user, String screenName, String emailAddress, String firstName,
2774 String middleName, String lastName, long prefixId, long suffixId,
2775 int birthdayMonth, int birthdayDay, int birthdayYear, boolean male,
2776 String jobTitle)
2777 throws PortalException {
2778
2779 List<String> fields = new ArrayList<>();
2780
2781 Contact contact = user.getContact();
2782
2783 Calendar birthday = CalendarFactoryUtil.getCalendar();
2784
2785 birthday.setTime(contact.getBirthday());
2786
2787 if ((birthdayMonth != birthday.get(Calendar.MONTH)) ||
2788 (birthdayDay != birthday.get(Calendar.DAY_OF_MONTH)) ||
2789 (birthdayYear != birthday.get(Calendar.YEAR))) {
2790
2791 fields.add("birthday");
2792 }
2793
2794 if (!StringUtil.equalsIgnoreCase(
2795 emailAddress, user.getEmailAddress())) {
2796
2797 fields.add("emailAddress");
2798 }
2799
2800 if (!StringUtil.equalsIgnoreCase(firstName, user.getFirstName())) {
2801 fields.add("firstName");
2802 }
2803
2804 if (male != contact.getMale()) {
2805 fields.add("gender");
2806 }
2807
2808 if (!StringUtil.equalsIgnoreCase(jobTitle, user.getJobTitle())) {
2809 fields.add("jobTitle");
2810 }
2811
2812 if (!StringUtil.equalsIgnoreCase(lastName, user.getLastName())) {
2813 fields.add("lastName");
2814 }
2815
2816 if (!StringUtil.equalsIgnoreCase(middleName, user.getMiddleName())) {
2817 fields.add("middleName");
2818 }
2819
2820 if (prefixId != contact.getPrefixId()) {
2821 fields.add("prefix");
2822 }
2823
2824 if (!StringUtil.equalsIgnoreCase(screenName, user.getScreenName())) {
2825 fields.add("screenName");
2826 }
2827
2828 if (suffixId != contact.getSuffixId()) {
2829 fields.add("suffix");
2830 }
2831
2832 UserFieldException ufe = new UserFieldException();
2833
2834 for (String field : fields) {
2835 if (!UsersAdminUtil.hasUpdateFieldPermission(
2836 getPermissionChecker(), getUser(), user, field)) {
2837
2838 ufe.addField(field);
2839 }
2840 }
2841
2842 if (ufe.hasFields()) {
2843 throw ufe;
2844 }
2845 }
2846
2847 private static final Log _log = LogFactoryUtil.getLog(
2848 UserServiceImpl.class);
2849
2850 }