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.User;
33  import com.liferay.portal.model.impl.GroupImpl;
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.List;
45  import java.util.Locale;
46  
47  /**
48   * <a href="UserServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Brian Myunghun Kim
52   * @author Scott Lee
53   * @author Jorge Ferrer
54   *
55   */
56  public class UserServiceImpl extends UserServiceBaseImpl {
57  
58      public void addGroupUsers(long groupId, long[] userIds)
59          throws PortalException, SystemException {
60  
61          try {
62              GroupPermissionUtil.check(
63                  getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
64          }
65          catch (PrincipalException pe) {
66  
67              // Allow any user to join open communities
68  
69              boolean hasPermission = false;
70  
71              if (userIds.length == 0) {
72                  hasPermission = true;
73              }
74              else if (userIds.length == 1) {
75                  User user = getUser();
76  
77                  if (user.getUserId() == userIds[0]) {
78                      Group group = groupPersistence.findByPrimaryKey(groupId);
79  
80                      if (user.getCompanyId() == group.getCompanyId()) {
81                          int type = group.getType();
82  
83                          if (type == GroupImpl.TYPE_COMMUNITY_OPEN) {
84                              hasPermission = true;
85                          }
86                      }
87                  }
88              }
89  
90              if (!hasPermission) {
91                  throw new PrincipalException();
92              }
93          }
94  
95          userLocalService.addGroupUsers(groupId, userIds);
96      }
97  
98      public void addOrganizationUsers(long organizationId, long[] userIds)
99          throws PortalException, SystemException {
100 
101         OrganizationPermissionUtil.check(
102             getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
103 
104         userLocalService.addOrganizationUsers(organizationId, userIds);
105     }
106 
107     public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
108         throws PortalException, SystemException {
109 
110         PasswordPolicyPermissionUtil.check(
111             getPermissionChecker(), passwordPolicyId,
112             ActionKeys.ASSIGN_MEMBERS);
113 
114         userLocalService.addPasswordPolicyUsers(passwordPolicyId, userIds);
115     }
116 
117     public void addRoleUsers(long roleId, long[] userIds)
118         throws PortalException, SystemException {
119 
120         RolePermissionUtil.check(
121             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
122 
123         userLocalService.addRoleUsers(roleId, userIds);
124     }
125 
126     public void addUserGroupUsers(long userGroupId, long[] userIds)
127         throws PortalException, SystemException {
128 
129         UserGroupPermissionUtil.check(
130             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
131 
132         userLocalService.addUserGroupUsers(userGroupId, userIds);
133     }
134 
135     public User addUser(
136             long companyId, boolean autoPassword, String password1,
137             String password2, boolean autoScreenName, String screenName,
138             String emailAddress, Locale locale, String firstName,
139             String middleName, String lastName, int prefixId, int suffixId,
140             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
141             String jobTitle, long[] organizationIds, boolean sendEmail)
142         throws PortalException, SystemException {
143 
144         Company company = companyPersistence.findByPrimaryKey(companyId);
145 
146         if (!company.isStrangers()) {
147             UserPermissionUtil.check(
148                 getPermissionChecker(), 0, organizationIds,
149                 ActionKeys.ADD_USER);
150         }
151 
152         long creatorUserId = 0;
153 
154         try {
155             creatorUserId = getUserId();
156         }
157         catch (PrincipalException pe) {
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 List<User> getGroupUsers(long groupId) throws SystemException {
204         return userLocalService.getGroupUsers(groupId);
205     }
206 
207     public List<User> getRoleUsers(long roleId) throws SystemException {
208         return userLocalService.getRoleUsers(roleId);
209     }
210 
211     public User getUserByEmailAddress(long companyId, String emailAddress)
212         throws PortalException, SystemException {
213 
214         User user = userLocalService.getUserByEmailAddress(
215             companyId, emailAddress);
216 
217         UserPermissionUtil.check(
218             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
219 
220         return user;
221     }
222 
223     public User getUserById(long userId)
224         throws PortalException, SystemException {
225 
226         User user = userLocalService.getUserById(userId);
227 
228         UserPermissionUtil.check(
229             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
230 
231         return user;
232     }
233 
234     public User getUserByScreenName(long companyId, String screenName)
235         throws PortalException, SystemException {
236 
237         User user = userLocalService.getUserByScreenName(
238             companyId, screenName);
239 
240         UserPermissionUtil.check(
241             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
242 
243         return user;
244     }
245 
246     public long getUserIdByEmailAddress(long companyId, String emailAddress)
247         throws PortalException, SystemException {
248 
249         User user = getUserByEmailAddress(companyId, emailAddress);
250 
251         return user.getUserId();
252     }
253 
254     public long getUserIdByScreenName(long companyId, String screenName)
255         throws PortalException, SystemException {
256 
257         User user = getUserByScreenName(companyId, screenName);
258 
259         return user.getUserId();
260     }
261 
262     public boolean hasGroupUser(long groupId, long userId)
263         throws SystemException {
264 
265         return userLocalService.hasGroupUser(groupId, userId);
266     }
267 
268     public boolean hasRoleUser(long roleId, long userId)
269         throws SystemException {
270 
271         return userLocalService.hasRoleUser(roleId, userId);
272     }
273 
274     public void setRoleUsers(long roleId, long[] userIds)
275         throws PortalException, SystemException {
276 
277         RolePermissionUtil.check(
278             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
279 
280         userLocalService.setRoleUsers(roleId, userIds);
281     }
282 
283     public void setUserGroupUsers(long userGroupId, long[] userIds)
284         throws PortalException, SystemException {
285 
286         UserGroupPermissionUtil.check(
287             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
288 
289         userLocalService.setUserGroupUsers(userGroupId, userIds);
290     }
291 
292     public void unsetGroupUsers(long groupId, long[] userIds)
293         throws PortalException, SystemException {
294 
295         try {
296             GroupPermissionUtil.check(
297                 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
298         }
299         catch (PrincipalException pe) {
300 
301             // Allow any user to leave open and restricted communities
302 
303             boolean hasPermission = false;
304 
305             if (userIds.length == 0) {
306                 hasPermission = true;
307             }
308             else if (userIds.length == 1) {
309                 User user = getUser();
310 
311                 if (user.getUserId() == userIds[0]) {
312                     Group group = groupPersistence.findByPrimaryKey(groupId);
313 
314                     if (user.getCompanyId() == group.getCompanyId()) {
315                         int type = group.getType();
316 
317                         if ((type == GroupImpl.TYPE_COMMUNITY_OPEN) ||
318                             (type == GroupImpl.TYPE_COMMUNITY_RESTRICTED)) {
319 
320                             hasPermission = true;
321                         }
322                     }
323                 }
324             }
325 
326             if (!hasPermission) {
327                 throw new PrincipalException();
328             }
329         }
330 
331         userLocalService.unsetGroupUsers(groupId, userIds);
332     }
333 
334     public void unsetOrganizationUsers(long organizationId, long[] userIds)
335         throws PortalException, SystemException {
336 
337         OrganizationPermissionUtil.check(
338             getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
339 
340         userLocalService.unsetOrganizationUsers(organizationId, userIds);
341     }
342 
343     public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
344         throws PortalException, SystemException {
345 
346         PasswordPolicyPermissionUtil.check(
347             getPermissionChecker(), passwordPolicyId,
348             ActionKeys.ASSIGN_MEMBERS);
349 
350         userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
351     }
352 
353     public void unsetRoleUsers(long roleId, long[] userIds)
354         throws PortalException, SystemException {
355 
356         RolePermissionUtil.check(
357             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
358 
359         userLocalService.unsetRoleUsers(roleId, userIds);
360     }
361 
362     public void unsetUserGroupUsers(long userGroupId, long[] userIds)
363         throws PortalException, SystemException {
364 
365         UserGroupPermissionUtil.check(
366             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
367 
368         userLocalService.unsetUserGroupUsers(userGroupId, userIds);
369     }
370 
371     public User updateActive(long userId, boolean active)
372         throws PortalException, SystemException {
373 
374         if ((getUserId() == userId) && !active) {
375             throw new RequiredUserException();
376         }
377 
378         UserPermissionUtil.check(
379             getPermissionChecker(), userId, ActionKeys.DELETE);
380 
381         return userLocalService.updateActive(userId, active);
382     }
383 
384     public User updateAgreedToTermsOfUse(
385             long userId, boolean agreedToTermsOfUse)
386         throws PortalException, SystemException {
387 
388         UserPermissionUtil.check(
389             getPermissionChecker(), userId, ActionKeys.UPDATE);
390 
391         return userLocalService.updateAgreedToTermsOfUse(
392             userId, agreedToTermsOfUse);
393     }
394 
395     public User updateLockout(long userId, boolean lockout)
396         throws PortalException, SystemException {
397 
398         UserPermissionUtil.check(
399             getPermissionChecker(), userId, ActionKeys.DELETE);
400 
401         return userLocalService.updateLockoutById(userId, lockout);
402     }
403 
404     public void updateOrganizations(long userId, long[] organizationIds)
405         throws PortalException, SystemException {
406 
407         UserPermissionUtil.check(
408             getPermissionChecker(), userId, ActionKeys.UPDATE);
409 
410         userLocalService.updateOrganizations(userId, organizationIds);
411     }
412 
413     public User updatePassword(
414             long userId, String password1, String password2,
415             boolean passwordReset)
416         throws PortalException, SystemException {
417 
418         UserPermissionUtil.check(
419             getPermissionChecker(), userId, ActionKeys.UPDATE);
420 
421         return userLocalService.updatePassword(
422             userId, password1, password2, passwordReset);
423     }
424 
425     public void updatePortrait(long userId, byte[] bytes)
426         throws PortalException, SystemException {
427 
428         UserPermissionUtil.check(
429             getPermissionChecker(), userId, ActionKeys.UPDATE);
430 
431         userLocalService.updatePortrait(userId, bytes);
432     }
433 
434     public void updateScreenName(long userId, String screenName)
435         throws PortalException, SystemException {
436 
437         UserPermissionUtil.check(
438             getPermissionChecker(), userId, ActionKeys.UPDATE);
439 
440         userLocalService.updateScreenName(userId, screenName);
441     }
442 
443     public void updateOpenId(long userId, String openId)
444         throws PortalException, SystemException {
445 
446         UserPermissionUtil.check(
447             getPermissionChecker(), userId, ActionKeys.UPDATE);
448 
449         userLocalService.updateOpenId(userId, openId);
450     }
451 
452     public User updateUser(
453             long userId, String oldPassword, boolean passwordReset,
454             String screenName, String emailAddress, String languageId,
455             String timeZoneId, String greeting, String comments,
456             String firstName, String middleName, String lastName, int prefixId,
457             int suffixId, boolean male, int birthdayMonth, int birthdayDay,
458             int birthdayYear, String smsSn, String aimSn, String facebookSn,
459             String icqSn, String jabberSn, String msnSn, String mySpaceSn,
460             String skypeSn, String twitterSn, String ymSn, String jobTitle,
461             long[] organizationIds)
462         throws PortalException, SystemException {
463 
464         String newPassword1 = StringPool.BLANK;
465         String newPassword2 = StringPool.BLANK;
466 
467         return updateUser(
468             userId, oldPassword, newPassword1, newPassword2, passwordReset,
469             screenName, emailAddress, languageId, timeZoneId, greeting,
470             comments, firstName, middleName, lastName, prefixId, suffixId, male,
471             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
472             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
473             jobTitle, organizationIds);
474     }
475 
476     public User updateUser(
477             long userId, String oldPassword, String newPassword1,
478             String newPassword2, boolean passwordReset, String screenName,
479             String emailAddress, String languageId, String timeZoneId,
480             String greeting, String comments, String firstName,
481             String middleName, String lastName, int prefixId, int suffixId,
482             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
483             String smsSn, String aimSn, String facebookSn, String icqSn,
484             String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
485             String twitterSn, String ymSn, String jobTitle,
486             long[] organizationIds)
487         throws PortalException, SystemException {
488 
489         UserPermissionUtil.check(
490             getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
491 
492         long curUserId = getUserId();
493 
494         if (curUserId == userId) {
495             emailAddress = emailAddress.trim().toLowerCase();
496 
497             User user = userPersistence.findByPrimaryKey(userId);
498 
499             if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
500                 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
501                     Company company = companyPersistence.findByPrimaryKey(
502                         user.getCompanyId());
503 
504                     if (!company.isStrangersWithMx()) {
505                         throw new ReservedUserEmailAddressException();
506                     }
507                 }
508             }
509         }
510 
511         return userLocalService.updateUser(
512             userId, oldPassword, newPassword1, newPassword2, passwordReset,
513             screenName, emailAddress, languageId, timeZoneId, greeting,
514             comments, firstName, middleName, lastName, prefixId, suffixId, male,
515             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
516             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
517             jobTitle, organizationIds);
518     }
519 
520 }