1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.PortalException;
18 import com.liferay.portal.RequiredUserException;
19 import com.liferay.portal.ReservedUserEmailAddressException;
20 import com.liferay.portal.SystemException;
21 import com.liferay.portal.UserEmailAddressException;
22 import com.liferay.portal.UserScreenNameException;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.model.Company;
25 import com.liferay.portal.model.Group;
26 import com.liferay.portal.model.GroupConstants;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.security.auth.PrincipalException;
29 import com.liferay.portal.security.permission.ActionKeys;
30 import com.liferay.portal.security.permission.PermissionChecker;
31 import com.liferay.portal.service.base.UserServiceBaseImpl;
32 import com.liferay.portal.service.permission.GroupPermissionUtil;
33 import com.liferay.portal.service.permission.OrganizationPermissionUtil;
34 import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
35 import com.liferay.portal.service.permission.PortalPermissionUtil;
36 import com.liferay.portal.service.permission.RolePermissionUtil;
37 import com.liferay.portal.service.permission.UserGroupPermissionUtil;
38 import com.liferay.portal.service.permission.UserPermissionUtil;
39 import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
40
41 import java.util.Locale;
42
43
51 public class UserServiceImpl extends UserServiceBaseImpl {
52
53 public void addGroupUsers(long groupId, long[] userIds)
54 throws PortalException, SystemException {
55
56 try {
57 GroupPermissionUtil.check(
58 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
59 }
60 catch (PrincipalException pe) {
61
62
64 boolean hasPermission = false;
65
66 if (userIds.length == 0) {
67 hasPermission = true;
68 }
69 else if (userIds.length == 1) {
70 User user = getUser();
71
72 if (user.getUserId() == userIds[0]) {
73 Group group = groupPersistence.findByPrimaryKey(groupId);
74
75 if (user.getCompanyId() == group.getCompanyId()) {
76 int type = group.getType();
77
78 if (type == GroupConstants.TYPE_COMMUNITY_OPEN) {
79 hasPermission = true;
80 }
81 }
82 }
83 }
84
85 if (!hasPermission) {
86 throw new PrincipalException();
87 }
88 }
89
90 userLocalService.addGroupUsers(groupId, userIds);
91 }
92
93 public void addOrganizationUsers(long organizationId, long[] userIds)
94 throws PortalException, SystemException {
95
96 OrganizationPermissionUtil.check(
97 getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
98
99 userLocalService.addOrganizationUsers(organizationId, userIds);
100 }
101
102 public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
103 throws PortalException, SystemException {
104
105 PasswordPolicyPermissionUtil.check(
106 getPermissionChecker(), passwordPolicyId,
107 ActionKeys.ASSIGN_MEMBERS);
108
109 userLocalService.addPasswordPolicyUsers(passwordPolicyId, userIds);
110 }
111
112 public void addRoleUsers(long roleId, long[] userIds)
113 throws PortalException, SystemException {
114
115 RolePermissionUtil.check(
116 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
117
118 userLocalService.addRoleUsers(roleId, userIds);
119 }
120
121 public void addUserGroupUsers(long userGroupId, long[] userIds)
122 throws PortalException, SystemException {
123
124 UserGroupPermissionUtil.check(
125 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
126
127 userLocalService.addUserGroupUsers(userGroupId, userIds);
128 }
129
130 public User addUser(
131 long companyId, boolean autoPassword, String password1,
132 String password2, boolean autoScreenName, String screenName,
133 String emailAddress, Locale locale, String firstName,
134 String middleName, String lastName, int prefixId, int suffixId,
135 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
136 String jobTitle, long[] organizationIds, boolean sendEmail)
137 throws PortalException, SystemException {
138
139 Company company = companyPersistence.findByPrimaryKey(companyId);
140
141 long creatorUserId = 0;
142
143 try {
144 creatorUserId = getUserId();
145 }
146 catch (PrincipalException pe) {
147 }
148
149 if ((creatorUserId != 0) || !company.isStrangers()) {
150 if (!PortalPermissionUtil.contains(
151 getPermissionChecker(), ActionKeys.ADD_USER) &&
152 !UserPermissionUtil.contains(
153 getPermissionChecker(), 0, organizationIds,
154 ActionKeys.ADD_USER)) {
155
156 throw new PrincipalException();
157 }
158 }
159
160 if (creatorUserId == 0) {
161 if (!company.isStrangersWithMx() &&
162 company.hasCompanyMx(emailAddress)) {
163
164 throw new ReservedUserEmailAddressException();
165 }
166 }
167
168 return userLocalService.addUser(
169 creatorUserId, companyId, autoPassword, password1, password2,
170 autoScreenName, screenName, emailAddress, locale, firstName,
171 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
172 birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
173 }
174
175 public void deleteRoleUser(long roleId, long userId)
176 throws PortalException, SystemException {
177
178 RolePermissionUtil.check(
179 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
180
181 userLocalService.deleteRoleUser(roleId, userId);
182 }
183
184 public void deleteUser(long userId)
185 throws PortalException, SystemException {
186
187 if (getUserId() == userId) {
188 throw new RequiredUserException();
189 }
190
191 UserPermissionUtil.check(
192 getPermissionChecker(), userId, ActionKeys.DELETE);
193
194 userLocalService.deleteUser(userId);
195 }
196
197 public long getDefaultUserId(long companyId)
198 throws PortalException, SystemException {
199
200 return userLocalService.getDefaultUserId(companyId);
201 }
202
203 public long[] getGroupUserIds(long groupId) throws SystemException {
204 return userLocalService.getGroupUserIds(groupId);
205 }
206
207 public long[] getOrganizationUserIds(long organizationId)
208 throws SystemException {
209
210 return userLocalService.getOrganizationUserIds(organizationId);
211 }
212
213 public long[] getRoleUserIds(long roleId) throws SystemException {
214 return userLocalService.getRoleUserIds(roleId);
215 }
216
217 public User getUserByEmailAddress(long companyId, String emailAddress)
218 throws PortalException, SystemException {
219
220 User user = userLocalService.getUserByEmailAddress(
221 companyId, emailAddress);
222
223 UserPermissionUtil.check(
224 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
225
226 return user;
227 }
228
229 public User getUserById(long userId)
230 throws PortalException, SystemException {
231
232 User user = userLocalService.getUserById(userId);
233
234 UserPermissionUtil.check(
235 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
236
237 return user;
238 }
239
240 public User getUserByScreenName(long companyId, String screenName)
241 throws PortalException, SystemException {
242
243 User user = userLocalService.getUserByScreenName(
244 companyId, screenName);
245
246 UserPermissionUtil.check(
247 getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
248
249 return user;
250 }
251
252 public long getUserIdByEmailAddress(long companyId, String emailAddress)
253 throws PortalException, SystemException {
254
255 User user = getUserByEmailAddress(companyId, emailAddress);
256
257 return user.getUserId();
258 }
259
260 public long getUserIdByScreenName(long companyId, String screenName)
261 throws PortalException, SystemException {
262
263 User user = getUserByScreenName(companyId, screenName);
264
265 return user.getUserId();
266 }
267
268 public boolean hasGroupUser(long groupId, long userId)
269 throws SystemException {
270
271 return userLocalService.hasGroupUser(groupId, userId);
272 }
273
274 public boolean hasRoleUser(long roleId, long userId)
275 throws SystemException {
276
277 return userLocalService.hasRoleUser(roleId, userId);
278 }
279
280 public boolean hasRoleUser(
281 long companyId, String name, long userId, boolean inherited)
282 throws PortalException, SystemException {
283
284 return userLocalService.hasRoleUser(companyId, name, userId, inherited);
285 }
286
287 public void setRoleUsers(long roleId, long[] userIds)
288 throws PortalException, SystemException {
289
290 RolePermissionUtil.check(
291 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
292
293 userLocalService.setRoleUsers(roleId, userIds);
294 }
295
296 public void setUserGroupUsers(long userGroupId, long[] userIds)
297 throws PortalException, SystemException {
298
299 UserGroupPermissionUtil.check(
300 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
301
302 userLocalService.setUserGroupUsers(userGroupId, userIds);
303 }
304
305 public void unsetGroupUsers(long groupId, long[] userIds)
306 throws PortalException, SystemException {
307
308 try {
309 GroupPermissionUtil.check(
310 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
311 }
312 catch (PrincipalException pe) {
313
314
316 boolean hasPermission = false;
317
318 if (userIds.length == 0) {
319 hasPermission = true;
320 }
321 else if (userIds.length == 1) {
322 User user = getUser();
323
324 if (user.getUserId() == userIds[0]) {
325 Group group = groupPersistence.findByPrimaryKey(groupId);
326
327 if (user.getCompanyId() == group.getCompanyId()) {
328 int type = group.getType();
329
330 if ((type == GroupConstants.TYPE_COMMUNITY_OPEN) ||
331 (type ==
332 GroupConstants.TYPE_COMMUNITY_RESTRICTED)) {
333
334 hasPermission = true;
335 }
336 }
337 }
338 }
339
340 if (!hasPermission) {
341 throw new PrincipalException();
342 }
343 }
344
345 userLocalService.unsetGroupUsers(groupId, userIds);
346 }
347
348 public void unsetOrganizationUsers(long organizationId, long[] userIds)
349 throws PortalException, SystemException {
350
351 OrganizationPermissionUtil.check(
352 getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
353
354 userLocalService.unsetOrganizationUsers(organizationId, userIds);
355 }
356
357 public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
358 throws PortalException, SystemException {
359
360 PasswordPolicyPermissionUtil.check(
361 getPermissionChecker(), passwordPolicyId,
362 ActionKeys.ASSIGN_MEMBERS);
363
364 userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
365 }
366
367 public void unsetRoleUsers(long roleId, long[] userIds)
368 throws PortalException, SystemException {
369
370 RolePermissionUtil.check(
371 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
372
373 userLocalService.unsetRoleUsers(roleId, userIds);
374 }
375
376 public void unsetUserGroupUsers(long userGroupId, long[] userIds)
377 throws PortalException, SystemException {
378
379 UserGroupPermissionUtil.check(
380 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
381
382 userLocalService.unsetUserGroupUsers(userGroupId, userIds);
383 }
384
385 public User updateActive(long userId, boolean active)
386 throws PortalException, SystemException {
387
388 if ((getUserId() == userId) && !active) {
389 throw new RequiredUserException();
390 }
391
392 UserPermissionUtil.check(
393 getPermissionChecker(), userId, ActionKeys.DELETE);
394
395 return userLocalService.updateActive(userId, active);
396 }
397
398 public User updateAgreedToTermsOfUse(
399 long userId, boolean agreedToTermsOfUse)
400 throws PortalException, SystemException {
401
402 UserPermissionUtil.check(
403 getPermissionChecker(), userId, ActionKeys.UPDATE);
404
405 return userLocalService.updateAgreedToTermsOfUse(
406 userId, agreedToTermsOfUse);
407 }
408
409 public User updateLockout(long userId, boolean lockout)
410 throws PortalException, SystemException {
411
412 UserPermissionUtil.check(
413 getPermissionChecker(), userId, ActionKeys.DELETE);
414
415 return userLocalService.updateLockoutById(userId, lockout);
416 }
417
418 public void updateOpenId(long userId, String openId)
419 throws PortalException, SystemException {
420
421 UserPermissionUtil.check(
422 getPermissionChecker(), userId, ActionKeys.UPDATE);
423
424 userLocalService.updateOpenId(userId, openId);
425 }
426
427 public void updateOrganizations(long userId, long[] organizationIds)
428 throws PortalException, SystemException {
429
430 UserPermissionUtil.check(
431 getPermissionChecker(), userId, ActionKeys.UPDATE);
432
433 userLocalService.updateOrganizations(userId, organizationIds);
434 }
435
436 public User updatePassword(
437 long userId, String password1, String password2,
438 boolean passwordReset)
439 throws PortalException, SystemException {
440
441 UserPermissionUtil.check(
442 getPermissionChecker(), userId, ActionKeys.UPDATE);
443
444 return userLocalService.updatePassword(
445 userId, password1, password2, passwordReset);
446 }
447
448 public void updatePortrait(long userId, byte[] bytes)
449 throws PortalException, SystemException {
450
451 UserPermissionUtil.check(
452 getPermissionChecker(), userId, ActionKeys.UPDATE);
453
454 userLocalService.updatePortrait(userId, bytes);
455 }
456
457 public void updateScreenName(long userId, String screenName)
458 throws PortalException, SystemException {
459
460 UserPermissionUtil.check(
461 getPermissionChecker(), userId, ActionKeys.UPDATE);
462
463 userLocalService.updateScreenName(userId, screenName);
464 }
465
466 public User updateUser(
467 long userId, String oldPassword, boolean passwordReset,
468 String screenName, String emailAddress, String languageId,
469 String timeZoneId, String greeting, String comments,
470 String firstName, String middleName, String lastName, int prefixId,
471 int suffixId, boolean male, int birthdayMonth, int birthdayDay,
472 int birthdayYear, String smsSn, String aimSn, String facebookSn,
473 String icqSn, String jabberSn, String msnSn, String mySpaceSn,
474 String skypeSn, String twitterSn, String ymSn, String jobTitle,
475 long[] organizationIds)
476 throws PortalException, SystemException {
477
478 String newPassword1 = StringPool.BLANK;
479 String newPassword2 = StringPool.BLANK;
480
481 return updateUser(
482 userId, oldPassword, newPassword1, newPassword2, passwordReset,
483 screenName, emailAddress, languageId, timeZoneId, greeting,
484 comments, firstName, middleName, lastName, prefixId, suffixId, male,
485 birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
486 icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
487 jobTitle, organizationIds);
488 }
489
490 public User updateUser(
491 long userId, String oldPassword, String newPassword1,
492 String newPassword2, boolean passwordReset, String screenName,
493 String emailAddress, String languageId, String timeZoneId,
494 String greeting, String comments, String firstName,
495 String middleName, String lastName, int prefixId, int suffixId,
496 boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
497 String smsSn, String aimSn, String facebookSn, String icqSn,
498 String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
499 String twitterSn, String ymSn, String jobTitle,
500 long[] organizationIds)
501 throws PortalException, SystemException {
502
503 UserPermissionUtil.check(
504 getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
505
506 long curUserId = getUserId();
507
508 if (curUserId == userId) {
509 User user = userPersistence.findByPrimaryKey(userId);
510
511 screenName = screenName.trim().toLowerCase();
512
513 if (!screenName.equalsIgnoreCase(user.getScreenName())) {
514 validateScreenName(user, screenName);
515 }
516
517 emailAddress = emailAddress.trim().toLowerCase();
518
519 if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
520 validateEmailAddress(user, emailAddress);
521 }
522 }
523
524 return userLocalService.updateUser(
525 userId, oldPassword, newPassword1, newPassword2, passwordReset,
526 screenName, emailAddress, languageId, timeZoneId, greeting,
527 comments, firstName, middleName, lastName, prefixId, suffixId, male,
528 birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
529 icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
530 jobTitle, organizationIds);
531 }
532
533 protected void validateEmailAddress(User user, String emailAddress)
534 throws PortalException, SystemException {
535
536 PermissionChecker permissionChecker = getPermissionChecker();
537
538 if (!EnterpriseAdminUtil.hasUpdateEmailAddress(
539 permissionChecker, user)) {
540
541 throw new UserEmailAddressException();
542 }
543
544 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
545 Company company = companyPersistence.findByPrimaryKey(
546 user.getCompanyId());
547
548 if (!company.isStrangersWithMx()) {
549 throw new ReservedUserEmailAddressException();
550 }
551 }
552 }
553
554 protected void validateScreenName(User user, String screenName)
555 throws PortalException {
556
557 PermissionChecker permissionChecker = getPermissionChecker();
558
559 if (!EnterpriseAdminUtil.hasUpdateScreenName(permissionChecker, user)) {
560 throw new UserScreenNameException();
561 }
562 }
563
564 }