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