1   /**
2    * Copyright (c) 2000-2008 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 User getUserByEmailAddress(long companyId, String emailAddress)
203         throws PortalException, SystemException {
204 
205         User user = userLocalService.getUserByEmailAddress(
206             companyId, emailAddress);
207 
208         UserPermissionUtil.check(
209             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
210 
211         return user;
212     }
213 
214     public User getUserById(long userId)
215         throws PortalException, SystemException {
216 
217         User user = userLocalService.getUserById(userId);
218 
219         UserPermissionUtil.check(
220             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
221 
222         return user;
223     }
224 
225     public User getUserByScreenName(long companyId, String screenName)
226         throws PortalException, SystemException {
227 
228         User user = userLocalService.getUserByScreenName(
229             companyId, screenName);
230 
231         UserPermissionUtil.check(
232             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
233 
234         return user;
235     }
236 
237     public long getUserIdByEmailAddress(long companyId, String emailAddress)
238         throws PortalException, SystemException {
239 
240         User user = getUserByEmailAddress(companyId, emailAddress);
241 
242         return user.getUserId();
243     }
244 
245     public long getUserIdByScreenName(long companyId, String screenName)
246         throws PortalException, SystemException {
247 
248         User user = getUserByScreenName(companyId, screenName);
249 
250         return user.getUserId();
251     }
252 
253     public boolean hasGroupUser(long groupId, long userId)
254         throws SystemException {
255 
256         return userLocalService.hasGroupUser(groupId, userId);
257     }
258 
259     public boolean hasRoleUser(long roleId, long userId)
260         throws SystemException {
261 
262         return userLocalService.hasRoleUser(roleId, userId);
263     }
264 
265     public void setRoleUsers(long roleId, long[] userIds)
266         throws PortalException, SystemException {
267 
268         RolePermissionUtil.check(
269             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
270 
271         userLocalService.setRoleUsers(roleId, userIds);
272     }
273 
274     public void setUserGroupUsers(long userGroupId, long[] userIds)
275         throws PortalException, SystemException {
276 
277         UserGroupPermissionUtil.check(
278             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
279 
280         userLocalService.setUserGroupUsers(userGroupId, userIds);
281     }
282 
283     public void unsetGroupUsers(long groupId, long[] userIds)
284         throws PortalException, SystemException {
285 
286         try {
287             GroupPermissionUtil.check(
288                 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
289         }
290         catch (PrincipalException pe) {
291 
292             // Allow any user to leave open and restricted communities
293 
294             boolean hasPermission = false;
295 
296             if (userIds.length == 0) {
297                 hasPermission = true;
298             }
299             else if (userIds.length == 1) {
300                 User user = getUser();
301 
302                 if (user.getUserId() == userIds[0]) {
303                     Group group = groupPersistence.findByPrimaryKey(groupId);
304 
305                     if (user.getCompanyId() == group.getCompanyId()) {
306                         int type = group.getType();
307 
308                         if ((type == GroupConstants.TYPE_COMMUNITY_OPEN) ||
309                             (type ==
310                                 GroupConstants.TYPE_COMMUNITY_RESTRICTED)) {
311 
312                             hasPermission = true;
313                         }
314                     }
315                 }
316             }
317 
318             if (!hasPermission) {
319                 throw new PrincipalException();
320             }
321         }
322 
323         userLocalService.unsetGroupUsers(groupId, userIds);
324     }
325 
326     public void unsetOrganizationUsers(long organizationId, long[] userIds)
327         throws PortalException, SystemException {
328 
329         OrganizationPermissionUtil.check(
330             getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
331 
332         userLocalService.unsetOrganizationUsers(organizationId, userIds);
333     }
334 
335     public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
336         throws PortalException, SystemException {
337 
338         PasswordPolicyPermissionUtil.check(
339             getPermissionChecker(), passwordPolicyId,
340             ActionKeys.ASSIGN_MEMBERS);
341 
342         userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
343     }
344 
345     public void unsetRoleUsers(long roleId, long[] userIds)
346         throws PortalException, SystemException {
347 
348         RolePermissionUtil.check(
349             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
350 
351         userLocalService.unsetRoleUsers(roleId, userIds);
352     }
353 
354     public void unsetUserGroupUsers(long userGroupId, long[] userIds)
355         throws PortalException, SystemException {
356 
357         UserGroupPermissionUtil.check(
358             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
359 
360         userLocalService.unsetUserGroupUsers(userGroupId, userIds);
361     }
362 
363     public User updateActive(long userId, boolean active)
364         throws PortalException, SystemException {
365 
366         if ((getUserId() == userId) && !active) {
367             throw new RequiredUserException();
368         }
369 
370         UserPermissionUtil.check(
371             getPermissionChecker(), userId, ActionKeys.DELETE);
372 
373         return userLocalService.updateActive(userId, active);
374     }
375 
376     public User updateAgreedToTermsOfUse(
377             long userId, boolean agreedToTermsOfUse)
378         throws PortalException, SystemException {
379 
380         UserPermissionUtil.check(
381             getPermissionChecker(), userId, ActionKeys.UPDATE);
382 
383         return userLocalService.updateAgreedToTermsOfUse(
384             userId, agreedToTermsOfUse);
385     }
386 
387     public User updateLockout(long userId, boolean lockout)
388         throws PortalException, SystemException {
389 
390         UserPermissionUtil.check(
391             getPermissionChecker(), userId, ActionKeys.DELETE);
392 
393         return userLocalService.updateLockoutById(userId, lockout);
394     }
395 
396     public void updateOrganizations(long userId, long[] organizationIds)
397         throws PortalException, SystemException {
398 
399         UserPermissionUtil.check(
400             getPermissionChecker(), userId, ActionKeys.UPDATE);
401 
402         userLocalService.updateOrganizations(userId, organizationIds);
403     }
404 
405     public User updatePassword(
406             long userId, String password1, String password2,
407             boolean passwordReset)
408         throws PortalException, SystemException {
409 
410         UserPermissionUtil.check(
411             getPermissionChecker(), userId, ActionKeys.UPDATE);
412 
413         return userLocalService.updatePassword(
414             userId, password1, password2, passwordReset);
415     }
416 
417     public void updatePortrait(long userId, byte[] bytes)
418         throws PortalException, SystemException {
419 
420         UserPermissionUtil.check(
421             getPermissionChecker(), userId, ActionKeys.UPDATE);
422 
423         userLocalService.updatePortrait(userId, bytes);
424     }
425 
426     public void updateScreenName(long userId, String screenName)
427         throws PortalException, SystemException {
428 
429         UserPermissionUtil.check(
430             getPermissionChecker(), userId, ActionKeys.UPDATE);
431 
432         userLocalService.updateScreenName(userId, screenName);
433     }
434 
435     public void updateOpenId(long userId, String openId)
436         throws PortalException, SystemException {
437 
438         UserPermissionUtil.check(
439             getPermissionChecker(), userId, ActionKeys.UPDATE);
440 
441         userLocalService.updateOpenId(userId, openId);
442     }
443 
444     public User updateUser(
445             long userId, String oldPassword, boolean passwordReset,
446             String screenName, String emailAddress, String languageId,
447             String timeZoneId, String greeting, String comments,
448             String firstName, String middleName, String lastName, int prefixId,
449             int suffixId, boolean male, int birthdayMonth, int birthdayDay,
450             int birthdayYear, String smsSn, String aimSn, String facebookSn,
451             String icqSn, String jabberSn, String msnSn, String mySpaceSn,
452             String skypeSn, String twitterSn, String ymSn, String jobTitle,
453             long[] organizationIds)
454         throws PortalException, SystemException {
455 
456         String newPassword1 = StringPool.BLANK;
457         String newPassword2 = StringPool.BLANK;
458 
459         return updateUser(
460             userId, oldPassword, newPassword1, newPassword2, passwordReset,
461             screenName, emailAddress, languageId, timeZoneId, greeting,
462             comments, firstName, middleName, lastName, prefixId, suffixId, male,
463             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
464             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
465             jobTitle, organizationIds);
466     }
467 
468     public User updateUser(
469             long userId, String oldPassword, String newPassword1,
470             String newPassword2, boolean passwordReset, String screenName,
471             String emailAddress, String languageId, String timeZoneId,
472             String greeting, String comments, String firstName,
473             String middleName, String lastName, int prefixId, int suffixId,
474             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
475             String smsSn, String aimSn, String facebookSn, String icqSn,
476             String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
477             String twitterSn, String ymSn, String jobTitle,
478             long[] organizationIds)
479         throws PortalException, SystemException {
480 
481         UserPermissionUtil.check(
482             getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
483 
484         long curUserId = getUserId();
485 
486         if (curUserId == userId) {
487             emailAddress = emailAddress.trim().toLowerCase();
488 
489             User user = userPersistence.findByPrimaryKey(userId);
490 
491             if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
492                 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
493                     Company company = companyPersistence.findByPrimaryKey(
494                         user.getCompanyId());
495 
496                     if (!company.isStrangersWithMx()) {
497                         throw new ReservedUserEmailAddressException();
498                     }
499                 }
500             }
501         }
502 
503         return userLocalService.updateUser(
504             userId, oldPassword, newPassword1, newPassword2, passwordReset,
505             screenName, emailAddress, languageId, timeZoneId, greeting,
506             comments, firstName, middleName, lastName, prefixId, suffixId, male,
507             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
508             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
509             jobTitle, organizationIds);
510     }
511 
512 }