1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.PortalPermissionUtil;
41  import com.liferay.portal.service.permission.RolePermissionUtil;
42  import com.liferay.portal.service.permission.UserGroupPermissionUtil;
43  import com.liferay.portal.service.permission.UserPermissionUtil;
44  
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  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         long creatorUserId = 0;
146 
147         try {
148             creatorUserId = getUserId();
149         }
150         catch (PrincipalException pe) {
151         }
152 
153         if ((creatorUserId != 0) || !company.isStrangers()) {
154             if (!PortalPermissionUtil.contains(
155                     getPermissionChecker(), ActionKeys.ADD_USER) &&
156                 !UserPermissionUtil.contains(
157                     getPermissionChecker(), 0, organizationIds,
158                     ActionKeys.ADD_USER)) {
159 
160                 throw new PrincipalException();
161             }
162         }
163 
164         if (creatorUserId == 0) {
165             if (!company.isStrangersWithMx() &&
166                 company.hasCompanyMx(emailAddress)) {
167 
168                 throw new ReservedUserEmailAddressException();
169             }
170         }
171 
172         return userLocalService.addUser(
173             creatorUserId, companyId, autoPassword, password1, password2,
174             autoScreenName, screenName, emailAddress, locale, firstName,
175             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
176             birthdayDay, birthdayYear, jobTitle, organizationIds, sendEmail);
177     }
178 
179     public void deleteRoleUser(long roleId, long userId)
180         throws PortalException, SystemException {
181 
182         RolePermissionUtil.check(
183             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
184 
185         userLocalService.deleteRoleUser(roleId, userId);
186     }
187 
188     public void deleteUser(long userId)
189         throws PortalException, SystemException {
190 
191         if (getUserId() == userId) {
192             throw new RequiredUserException();
193         }
194 
195         UserPermissionUtil.check(
196             getPermissionChecker(), userId, ActionKeys.DELETE);
197 
198         userLocalService.deleteUser(userId);
199     }
200 
201     public long getDefaultUserId(long companyId)
202         throws PortalException, SystemException {
203 
204         return userLocalService.getDefaultUserId(companyId);
205     }
206 
207     public long[] getGroupUserIds(long groupId) throws SystemException {
208         return userLocalService.getGroupUserIds(groupId);
209     }
210 
211     public long[] getOrganizationUserIds(long organizationId)
212         throws SystemException {
213 
214         return userLocalService.getOrganizationUserIds(organizationId);
215     }
216 
217     public long[] getRoleUserIds(long roleId) throws SystemException {
218         return userLocalService.getRoleUserIds(roleId);
219     }
220 
221     public User getUserByEmailAddress(long companyId, String emailAddress)
222         throws PortalException, SystemException {
223 
224         User user = userLocalService.getUserByEmailAddress(
225             companyId, emailAddress);
226 
227         UserPermissionUtil.check(
228             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
229 
230         return user;
231     }
232 
233     public User getUserById(long userId)
234         throws PortalException, SystemException {
235 
236         User user = userLocalService.getUserById(userId);
237 
238         UserPermissionUtil.check(
239             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
240 
241         return user;
242     }
243 
244     public User getUserByScreenName(long companyId, String screenName)
245         throws PortalException, SystemException {
246 
247         User user = userLocalService.getUserByScreenName(
248             companyId, screenName);
249 
250         UserPermissionUtil.check(
251             getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
252 
253         return user;
254     }
255 
256     public long getUserIdByEmailAddress(long companyId, String emailAddress)
257         throws PortalException, SystemException {
258 
259         User user = getUserByEmailAddress(companyId, emailAddress);
260 
261         return user.getUserId();
262     }
263 
264     public long getUserIdByScreenName(long companyId, String screenName)
265         throws PortalException, SystemException {
266 
267         User user = getUserByScreenName(companyId, screenName);
268 
269         return user.getUserId();
270     }
271 
272     public boolean hasGroupUser(long groupId, long userId)
273         throws SystemException {
274 
275         return userLocalService.hasGroupUser(groupId, userId);
276     }
277 
278     public boolean hasRoleUser(long roleId, long userId)
279         throws SystemException {
280 
281         return userLocalService.hasRoleUser(roleId, userId);
282     }
283 
284     public boolean hasRoleUser(
285             long companyId, String name, long userId, boolean inherited)
286         throws PortalException, SystemException {
287 
288         return userLocalService.hasRoleUser(companyId, name, userId, inherited);
289     }
290 
291     public void setRoleUsers(long roleId, long[] userIds)
292         throws PortalException, SystemException {
293 
294         RolePermissionUtil.check(
295             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
296 
297         userLocalService.setRoleUsers(roleId, userIds);
298     }
299 
300     public void setUserGroupUsers(long userGroupId, long[] userIds)
301         throws PortalException, SystemException {
302 
303         UserGroupPermissionUtil.check(
304             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
305 
306         userLocalService.setUserGroupUsers(userGroupId, userIds);
307     }
308 
309     public void unsetGroupUsers(long groupId, long[] userIds)
310         throws PortalException, SystemException {
311 
312         try {
313             GroupPermissionUtil.check(
314                 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
315         }
316         catch (PrincipalException pe) {
317 
318             // Allow any user to leave open and restricted communities
319 
320             boolean hasPermission = false;
321 
322             if (userIds.length == 0) {
323                 hasPermission = true;
324             }
325             else if (userIds.length == 1) {
326                 User user = getUser();
327 
328                 if (user.getUserId() == userIds[0]) {
329                     Group group = groupPersistence.findByPrimaryKey(groupId);
330 
331                     if (user.getCompanyId() == group.getCompanyId()) {
332                         int type = group.getType();
333 
334                         if ((type == GroupConstants.TYPE_COMMUNITY_OPEN) ||
335                             (type ==
336                                 GroupConstants.TYPE_COMMUNITY_RESTRICTED)) {
337 
338                             hasPermission = true;
339                         }
340                     }
341                 }
342             }
343 
344             if (!hasPermission) {
345                 throw new PrincipalException();
346             }
347         }
348 
349         userLocalService.unsetGroupUsers(groupId, userIds);
350     }
351 
352     public void unsetOrganizationUsers(long organizationId, long[] userIds)
353         throws PortalException, SystemException {
354 
355         OrganizationPermissionUtil.check(
356             getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
357 
358         userLocalService.unsetOrganizationUsers(organizationId, userIds);
359     }
360 
361     public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
362         throws PortalException, SystemException {
363 
364         PasswordPolicyPermissionUtil.check(
365             getPermissionChecker(), passwordPolicyId,
366             ActionKeys.ASSIGN_MEMBERS);
367 
368         userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
369     }
370 
371     public void unsetRoleUsers(long roleId, long[] userIds)
372         throws PortalException, SystemException {
373 
374         RolePermissionUtil.check(
375             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
376 
377         userLocalService.unsetRoleUsers(roleId, userIds);
378     }
379 
380     public void unsetUserGroupUsers(long userGroupId, long[] userIds)
381         throws PortalException, SystemException {
382 
383         UserGroupPermissionUtil.check(
384             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
385 
386         userLocalService.unsetUserGroupUsers(userGroupId, userIds);
387     }
388 
389     public User updateActive(long userId, boolean active)
390         throws PortalException, SystemException {
391 
392         if ((getUserId() == userId) && !active) {
393             throw new RequiredUserException();
394         }
395 
396         UserPermissionUtil.check(
397             getPermissionChecker(), userId, ActionKeys.DELETE);
398 
399         return userLocalService.updateActive(userId, active);
400     }
401 
402     public User updateAgreedToTermsOfUse(
403             long userId, boolean agreedToTermsOfUse)
404         throws PortalException, SystemException {
405 
406         UserPermissionUtil.check(
407             getPermissionChecker(), userId, ActionKeys.UPDATE);
408 
409         return userLocalService.updateAgreedToTermsOfUse(
410             userId, agreedToTermsOfUse);
411     }
412 
413     public User updateLockout(long userId, boolean lockout)
414         throws PortalException, SystemException {
415 
416         UserPermissionUtil.check(
417             getPermissionChecker(), userId, ActionKeys.DELETE);
418 
419         return userLocalService.updateLockoutById(userId, lockout);
420     }
421 
422     public void updateOpenId(long userId, String openId)
423         throws PortalException, SystemException {
424 
425         UserPermissionUtil.check(
426             getPermissionChecker(), userId, ActionKeys.UPDATE);
427 
428         userLocalService.updateOpenId(userId, openId);
429     }
430 
431     public void updateOrganizations(long userId, long[] organizationIds)
432         throws PortalException, SystemException {
433 
434         UserPermissionUtil.check(
435             getPermissionChecker(), userId, ActionKeys.UPDATE);
436 
437         userLocalService.updateOrganizations(userId, organizationIds);
438     }
439 
440     public User updatePassword(
441             long userId, String password1, String password2,
442             boolean passwordReset)
443         throws PortalException, SystemException {
444 
445         UserPermissionUtil.check(
446             getPermissionChecker(), userId, ActionKeys.UPDATE);
447 
448         return userLocalService.updatePassword(
449             userId, password1, password2, passwordReset);
450     }
451 
452     public void updatePortrait(long userId, byte[] bytes)
453         throws PortalException, SystemException {
454 
455         UserPermissionUtil.check(
456             getPermissionChecker(), userId, ActionKeys.UPDATE);
457 
458         userLocalService.updatePortrait(userId, bytes);
459     }
460 
461     public void updateScreenName(long userId, String screenName)
462         throws PortalException, SystemException {
463 
464         UserPermissionUtil.check(
465             getPermissionChecker(), userId, ActionKeys.UPDATE);
466 
467         userLocalService.updateScreenName(userId, screenName);
468     }
469 
470     public User updateUser(
471             long userId, String oldPassword, boolean passwordReset,
472             String screenName, String emailAddress, String languageId,
473             String timeZoneId, String greeting, String comments,
474             String firstName, String middleName, String lastName, int prefixId,
475             int suffixId, boolean male, int birthdayMonth, int birthdayDay,
476             int birthdayYear, String smsSn, String aimSn, String facebookSn,
477             String icqSn, String jabberSn, String msnSn, String mySpaceSn,
478             String skypeSn, String twitterSn, String ymSn, String jobTitle,
479             long[] organizationIds)
480         throws PortalException, SystemException {
481 
482         String newPassword1 = StringPool.BLANK;
483         String newPassword2 = StringPool.BLANK;
484 
485         return updateUser(
486             userId, oldPassword, newPassword1, newPassword2, passwordReset,
487             screenName, emailAddress, languageId, timeZoneId, greeting,
488             comments, firstName, middleName, lastName, prefixId, suffixId, male,
489             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
490             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
491             jobTitle, organizationIds);
492     }
493 
494     public User updateUser(
495             long userId, String oldPassword, String newPassword1,
496             String newPassword2, boolean passwordReset, String screenName,
497             String emailAddress, String languageId, String timeZoneId,
498             String greeting, String comments, String firstName,
499             String middleName, String lastName, int prefixId, int suffixId,
500             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
501             String smsSn, String aimSn, String facebookSn, String icqSn,
502             String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
503             String twitterSn, String ymSn, String jobTitle,
504             long[] organizationIds)
505         throws PortalException, SystemException {
506 
507         UserPermissionUtil.check(
508             getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
509 
510         long curUserId = getUserId();
511 
512         if (curUserId == userId) {
513             emailAddress = emailAddress.trim().toLowerCase();
514 
515             User user = userPersistence.findByPrimaryKey(userId);
516 
517             if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
518                 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
519                     Company company = companyPersistence.findByPrimaryKey(
520                         user.getCompanyId());
521 
522                     if (!company.isStrangersWithMx()) {
523                         throw new ReservedUserEmailAddressException();
524                     }
525                 }
526             }
527         }
528 
529         return userLocalService.updateUser(
530             userId, oldPassword, newPassword1, newPassword2, passwordReset,
531             screenName, emailAddress, languageId, timeZoneId, greeting,
532             comments, firstName, middleName, lastName, prefixId, suffixId, male,
533             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
534             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
535             jobTitle, organizationIds);
536     }
537 
538 }