1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.RequiredUserException;
27  import com.liferay.portal.ReservedUserEmailAddressException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.model.Company;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.GroupConstants;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.security.auth.PrincipalException;
35  import com.liferay.portal.security.permission.ActionKeys;
36  import com.liferay.portal.service.base.UserServiceBaseImpl;
37  import com.liferay.portal.service.permission.GroupPermissionUtil;
38  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
39  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
40  import com.liferay.portal.service.permission.RolePermissionUtil;
41  import com.liferay.portal.service.permission.UserGroupPermissionUtil;
42  import com.liferay.portal.service.permission.UserPermissionUtil;
43  
44  import java.util.Locale;
45  
46  /**
47   * <a href="UserServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Brian Myunghun Kim
51   * @author Scott Lee
52   * @author Jorge Ferrer
53   *
54   */
55  public class UserServiceImpl extends UserServiceBaseImpl {
56  
57      public void addGroupUsers(long groupId, long[] userIds)
58          throws PortalException, SystemException {
59  
60          try {
61              GroupPermissionUtil.check(
62                  getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
63          }
64          catch (PrincipalException pe) {
65  
66              // Allow any user to join open communities
67  
68              boolean hasPermission = false;
69  
70              if (userIds.length == 0) {
71                  hasPermission = true;
72              }
73              else if (userIds.length == 1) {
74                  User user = getUser();
75  
76                  if (user.getUserId() == userIds[0]) {
77                      Group group = groupPersistence.findByPrimaryKey(groupId);
78  
79                      if (user.getCompanyId() == group.getCompanyId()) {
80                          int type = group.getType();
81  
82                          if (type == GroupConstants.TYPE_COMMUNITY_OPEN) {
83                              hasPermission = true;
84                          }
85                      }
86                  }
87              }
88  
89              if (!hasPermission) {
90                  throw new PrincipalException();
91              }
92          }
93  
94          userLocalService.addGroupUsers(groupId, userIds);
95      }
96  
97      public void addOrganizationUsers(long organizationId, long[] userIds)
98          throws PortalException, SystemException {
99  
100         OrganizationPermissionUtil.check(
101             getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
102 
103         userLocalService.addOrganizationUsers(organizationId, userIds);
104     }
105 
106     public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
107         throws PortalException, SystemException {
108 
109         PasswordPolicyPermissionUtil.check(
110             getPermissionChecker(), passwordPolicyId,
111             ActionKeys.ASSIGN_MEMBERS);
112 
113         userLocalService.addPasswordPolicyUsers(passwordPolicyId, userIds);
114     }
115 
116     public void addRoleUsers(long roleId, long[] userIds)
117         throws PortalException, SystemException {
118 
119         RolePermissionUtil.check(
120             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
121 
122         userLocalService.addRoleUsers(roleId, userIds);
123     }
124 
125     public void addUserGroupUsers(long userGroupId, long[] userIds)
126         throws PortalException, SystemException {
127 
128         UserGroupPermissionUtil.check(
129             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
130 
131         userLocalService.addUserGroupUsers(userGroupId, userIds);
132     }
133 
134     public User addUser(
135             long companyId, boolean autoPassword, String password1,
136             String password2, boolean autoScreenName, String screenName,
137             String emailAddress, Locale locale, String firstName,
138             String middleName, String lastName, int prefixId, int suffixId,
139             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
140             String jobTitle, long[] organizationIds, boolean sendEmail)
141         throws PortalException, SystemException {
142 
143         Company company = companyPersistence.findByPrimaryKey(companyId);
144 
145         if (!company.isStrangers()) {
146             UserPermissionUtil.check(
147                 getPermissionChecker(), 0, organizationIds,
148                 ActionKeys.ADD_USER);
149         }
150 
151         long creatorUserId = 0;
152 
153         try {
154             creatorUserId = getUserId();
155         }
156         catch (PrincipalException pe) {
157         }
158 
159         if (creatorUserId == 0) {
160             if (!company.isStrangersWithMx() &&
161                 company.hasCompanyMx(emailAddress)) {
162 
163                 throw new ReservedUserEmailAddressException();
164             }
165         }
166 
167         return userLocalService.addUser(
168             creatorUserId, companyId, autoPassword, password1, password2,
169             autoScreenName, screenName, emailAddress, locale, firstName,
170             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
171             birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
172     }
173 
174     public void deleteRoleUser(long roleId, long userId)
175         throws PortalException, SystemException {
176 
177         RolePermissionUtil.check(
178             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
179 
180         userLocalService.deleteRoleUser(roleId, userId);
181     }
182 
183     public void deleteUser(long userId)
184         throws PortalException, SystemException {
185 
186         if (getUserId() == userId) {
187             throw new RequiredUserException();
188         }
189 
190         UserPermissionUtil.check(
191             getPermissionChecker(), userId, ActionKeys.DELETE);
192 
193         userLocalService.deleteUser(userId);
194     }
195 
196     public long getDefaultUserId(long companyId)
197         throws PortalException, SystemException {
198 
199         return userLocalService.getDefaultUserId(companyId);
200     }
201 
202     public long[] getGroupUserIds(long groupId) throws SystemException {
203         return userLocalService.getGroupUserIds(groupId);
204     }
205 
206     public long[] getOrganizationUserIds(long organizationId)
207         throws SystemException {
208 
209         return userLocalService.getOrganizationUserIds(organizationId);
210     }
211 
212     public long[] getRoleUserIds(long roleId) throws SystemException {
213         return userLocalService.getRoleUserIds(roleId);
214     }
215 
216     public User getUserByEmailAddress(long companyId, String emailAddress)
217         throws PortalException, SystemException {
218 
219         User user = userLocalService.getUserByEmailAddress(
220             companyId, emailAddress);
221 
222         UserPermissionUtil.check(
223             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
224 
225         return user;
226     }
227 
228     public User getUserById(long userId)
229         throws PortalException, SystemException {
230 
231         User user = userLocalService.getUserById(userId);
232 
233         UserPermissionUtil.check(
234             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
235 
236         return user;
237     }
238 
239     public User getUserByScreenName(long companyId, String screenName)
240         throws PortalException, SystemException {
241 
242         User user = userLocalService.getUserByScreenName(
243             companyId, screenName);
244 
245         UserPermissionUtil.check(
246             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
247 
248         return user;
249     }
250 
251     public long getUserIdByEmailAddress(long companyId, String emailAddress)
252         throws PortalException, SystemException {
253 
254         User user = getUserByEmailAddress(companyId, emailAddress);
255 
256         return user.getUserId();
257     }
258 
259     public long getUserIdByScreenName(long companyId, String screenName)
260         throws PortalException, SystemException {
261 
262         User user = getUserByScreenName(companyId, screenName);
263 
264         return user.getUserId();
265     }
266 
267     public boolean hasGroupUser(long groupId, long userId)
268         throws SystemException {
269 
270         return userLocalService.hasGroupUser(groupId, userId);
271     }
272 
273     public boolean hasRoleUser(long roleId, long userId)
274         throws SystemException {
275 
276         return userLocalService.hasRoleUser(roleId, userId);
277     }
278 
279     public boolean hasRoleUser(
280             long companyId, String name, long userId, boolean inherited)
281         throws PortalException, SystemException {
282 
283         return userLocalService.hasRoleUser(companyId, name, userId, inherited);
284     }
285 
286     public void setRoleUsers(long roleId, long[] userIds)
287         throws PortalException, SystemException {
288 
289         RolePermissionUtil.check(
290             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
291 
292         userLocalService.setRoleUsers(roleId, userIds);
293     }
294 
295     public void setUserGroupUsers(long userGroupId, long[] userIds)
296         throws PortalException, SystemException {
297 
298         UserGroupPermissionUtil.check(
299             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
300 
301         userLocalService.setUserGroupUsers(userGroupId, userIds);
302     }
303 
304     public void unsetGroupUsers(long groupId, long[] userIds)
305         throws PortalException, SystemException {
306 
307         try {
308             GroupPermissionUtil.check(
309                 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
310         }
311         catch (PrincipalException pe) {
312 
313             // Allow any user to leave open and restricted communities
314 
315             boolean hasPermission = false;
316 
317             if (userIds.length == 0) {
318                 hasPermission = true;
319             }
320             else if (userIds.length == 1) {
321                 User user = getUser();
322 
323                 if (user.getUserId() == userIds[0]) {
324                     Group group = groupPersistence.findByPrimaryKey(groupId);
325 
326                     if (user.getCompanyId() == group.getCompanyId()) {
327                         int type = group.getType();
328 
329                         if ((type == GroupConstants.TYPE_COMMUNITY_OPEN) ||
330                             (type ==
331                                 GroupConstants.TYPE_COMMUNITY_RESTRICTED)) {
332 
333                             hasPermission = true;
334                         }
335                     }
336                 }
337             }
338 
339             if (!hasPermission) {
340                 throw new PrincipalException();
341             }
342         }
343 
344         userLocalService.unsetGroupUsers(groupId, userIds);
345     }
346 
347     public void unsetOrganizationUsers(long organizationId, long[] userIds)
348         throws PortalException, SystemException {
349 
350         OrganizationPermissionUtil.check(
351             getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
352 
353         userLocalService.unsetOrganizationUsers(organizationId, userIds);
354     }
355 
356     public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
357         throws PortalException, SystemException {
358 
359         PasswordPolicyPermissionUtil.check(
360             getPermissionChecker(), passwordPolicyId,
361             ActionKeys.ASSIGN_MEMBERS);
362 
363         userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
364     }
365 
366     public void unsetRoleUsers(long roleId, long[] userIds)
367         throws PortalException, SystemException {
368 
369         RolePermissionUtil.check(
370             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
371 
372         userLocalService.unsetRoleUsers(roleId, userIds);
373     }
374 
375     public void unsetUserGroupUsers(long userGroupId, long[] userIds)
376         throws PortalException, SystemException {
377 
378         UserGroupPermissionUtil.check(
379             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
380 
381         userLocalService.unsetUserGroupUsers(userGroupId, userIds);
382     }
383 
384     public User updateActive(long userId, boolean active)
385         throws PortalException, SystemException {
386 
387         if ((getUserId() == userId) && !active) {
388             throw new RequiredUserException();
389         }
390 
391         UserPermissionUtil.check(
392             getPermissionChecker(), userId, ActionKeys.DELETE);
393 
394         return userLocalService.updateActive(userId, active);
395     }
396 
397     public User updateAgreedToTermsOfUse(
398             long userId, boolean agreedToTermsOfUse)
399         throws PortalException, SystemException {
400 
401         UserPermissionUtil.check(
402             getPermissionChecker(), userId, ActionKeys.UPDATE);
403 
404         return userLocalService.updateAgreedToTermsOfUse(
405             userId, agreedToTermsOfUse);
406     }
407 
408     public User updateLockout(long userId, boolean lockout)
409         throws PortalException, SystemException {
410 
411         UserPermissionUtil.check(
412             getPermissionChecker(), userId, ActionKeys.DELETE);
413 
414         return userLocalService.updateLockoutById(userId, lockout);
415     }
416 
417     public void updateOpenId(long userId, String openId)
418         throws PortalException, SystemException {
419 
420         UserPermissionUtil.check(
421             getPermissionChecker(), userId, ActionKeys.UPDATE);
422 
423         userLocalService.updateOpenId(userId, openId);
424     }
425 
426     public void updateOrganizations(long userId, long[] organizationIds)
427         throws PortalException, SystemException {
428 
429         UserPermissionUtil.check(
430             getPermissionChecker(), userId, ActionKeys.UPDATE);
431 
432         userLocalService.updateOrganizations(userId, organizationIds);
433     }
434 
435     public User updatePassword(
436             long userId, String password1, String password2,
437             boolean passwordReset)
438         throws PortalException, SystemException {
439 
440         UserPermissionUtil.check(
441             getPermissionChecker(), userId, ActionKeys.UPDATE);
442 
443         return userLocalService.updatePassword(
444             userId, password1, password2, passwordReset);
445     }
446 
447     public void updatePortrait(long userId, byte[] bytes)
448         throws PortalException, SystemException {
449 
450         UserPermissionUtil.check(
451             getPermissionChecker(), userId, ActionKeys.UPDATE);
452 
453         userLocalService.updatePortrait(userId, bytes);
454     }
455 
456     public void updateScreenName(long userId, String screenName)
457         throws PortalException, SystemException {
458 
459         UserPermissionUtil.check(
460             getPermissionChecker(), userId, ActionKeys.UPDATE);
461 
462         userLocalService.updateScreenName(userId, screenName);
463     }
464 
465     public User updateUser(
466             long userId, String oldPassword, boolean passwordReset,
467             String screenName, String emailAddress, String languageId,
468             String timeZoneId, String greeting, String comments,
469             String firstName, String middleName, String lastName, int prefixId,
470             int suffixId, boolean male, int birthdayMonth, int birthdayDay,
471             int birthdayYear, String smsSn, String aimSn, String facebookSn,
472             String icqSn, String jabberSn, String msnSn, String mySpaceSn,
473             String skypeSn, String twitterSn, String ymSn, String jobTitle,
474             long[] organizationIds)
475         throws PortalException, SystemException {
476 
477         String newPassword1 = StringPool.BLANK;
478         String newPassword2 = StringPool.BLANK;
479 
480         return updateUser(
481             userId, oldPassword, newPassword1, newPassword2, passwordReset,
482             screenName, emailAddress, languageId, timeZoneId, greeting,
483             comments, firstName, middleName, lastName, prefixId, suffixId, male,
484             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
485             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
486             jobTitle, organizationIds);
487     }
488 
489     public User updateUser(
490             long userId, String oldPassword, String newPassword1,
491             String newPassword2, boolean passwordReset, String screenName,
492             String emailAddress, String languageId, String timeZoneId,
493             String greeting, String comments, String firstName,
494             String middleName, String lastName, int prefixId, int suffixId,
495             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
496             String smsSn, String aimSn, String facebookSn, String icqSn,
497             String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
498             String twitterSn, String ymSn, String jobTitle,
499             long[] organizationIds)
500         throws PortalException, SystemException {
501 
502         UserPermissionUtil.check(
503             getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
504 
505         long curUserId = getUserId();
506 
507         if (curUserId == userId) {
508             emailAddress = emailAddress.trim().toLowerCase();
509 
510             User user = userPersistence.findByPrimaryKey(userId);
511 
512             if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
513                 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
514                     Company company = companyPersistence.findByPrimaryKey(
515                         user.getCompanyId());
516 
517                     if (!company.isStrangersWithMx()) {
518                         throw new ReservedUserEmailAddressException();
519                     }
520                 }
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 }