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