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 void setRoleUsers(long roleId, long[] userIds)
277         throws PortalException, SystemException {
278 
279         RolePermissionUtil.check(
280             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
281 
282         userLocalService.setRoleUsers(roleId, userIds);
283     }
284 
285     public void setUserGroupUsers(long userGroupId, long[] userIds)
286         throws PortalException, SystemException {
287 
288         UserGroupPermissionUtil.check(
289             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
290 
291         userLocalService.setUserGroupUsers(userGroupId, userIds);
292     }
293 
294     public void unsetGroupUsers(long groupId, long[] userIds)
295         throws PortalException, SystemException {
296 
297         try {
298             GroupPermissionUtil.check(
299                 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
300         }
301         catch (PrincipalException pe) {
302 
303             // Allow any user to leave open and restricted communities
304 
305             boolean hasPermission = false;
306 
307             if (userIds.length == 0) {
308                 hasPermission = true;
309             }
310             else if (userIds.length == 1) {
311                 User user = getUser();
312 
313                 if (user.getUserId() == userIds[0]) {
314                     Group group = groupPersistence.findByPrimaryKey(groupId);
315 
316                     if (user.getCompanyId() == group.getCompanyId()) {
317                         int type = group.getType();
318 
319                         if ((type == GroupConstants.TYPE_COMMUNITY_OPEN) ||
320                             (type ==
321                                 GroupConstants.TYPE_COMMUNITY_RESTRICTED)) {
322 
323                             hasPermission = true;
324                         }
325                     }
326                 }
327             }
328 
329             if (!hasPermission) {
330                 throw new PrincipalException();
331             }
332         }
333 
334         userLocalService.unsetGroupUsers(groupId, userIds);
335     }
336 
337     public void unsetOrganizationUsers(long organizationId, long[] userIds)
338         throws PortalException, SystemException {
339 
340         OrganizationPermissionUtil.check(
341             getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
342 
343         userLocalService.unsetOrganizationUsers(organizationId, userIds);
344     }
345 
346     public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
347         throws PortalException, SystemException {
348 
349         PasswordPolicyPermissionUtil.check(
350             getPermissionChecker(), passwordPolicyId,
351             ActionKeys.ASSIGN_MEMBERS);
352 
353         userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
354     }
355 
356     public void unsetRoleUsers(long roleId, long[] userIds)
357         throws PortalException, SystemException {
358 
359         RolePermissionUtil.check(
360             getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
361 
362         userLocalService.unsetRoleUsers(roleId, userIds);
363     }
364 
365     public void unsetUserGroupUsers(long userGroupId, long[] userIds)
366         throws PortalException, SystemException {
367 
368         UserGroupPermissionUtil.check(
369             getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
370 
371         userLocalService.unsetUserGroupUsers(userGroupId, userIds);
372     }
373 
374     public User updateActive(long userId, boolean active)
375         throws PortalException, SystemException {
376 
377         if ((getUserId() == userId) && !active) {
378             throw new RequiredUserException();
379         }
380 
381         UserPermissionUtil.check(
382             getPermissionChecker(), userId, ActionKeys.DELETE);
383 
384         return userLocalService.updateActive(userId, active);
385     }
386 
387     public User updateAgreedToTermsOfUse(
388             long userId, boolean agreedToTermsOfUse)
389         throws PortalException, SystemException {
390 
391         UserPermissionUtil.check(
392             getPermissionChecker(), userId, ActionKeys.UPDATE);
393 
394         return userLocalService.updateAgreedToTermsOfUse(
395             userId, agreedToTermsOfUse);
396     }
397 
398     public User updateLockout(long userId, boolean lockout)
399         throws PortalException, SystemException {
400 
401         UserPermissionUtil.check(
402             getPermissionChecker(), userId, ActionKeys.DELETE);
403 
404         return userLocalService.updateLockoutById(userId, lockout);
405     }
406 
407     public void updateOpenId(long userId, String openId)
408         throws PortalException, SystemException {
409 
410         UserPermissionUtil.check(
411             getPermissionChecker(), userId, ActionKeys.UPDATE);
412 
413         userLocalService.updateOpenId(userId, openId);
414     }
415 
416     public void updateOrganizations(long userId, long[] organizationIds)
417         throws PortalException, SystemException {
418 
419         UserPermissionUtil.check(
420             getPermissionChecker(), userId, ActionKeys.UPDATE);
421 
422         userLocalService.updateOrganizations(userId, organizationIds);
423     }
424 
425     public User updatePassword(
426             long userId, String password1, String password2,
427             boolean passwordReset)
428         throws PortalException, SystemException {
429 
430         UserPermissionUtil.check(
431             getPermissionChecker(), userId, ActionKeys.UPDATE);
432 
433         return userLocalService.updatePassword(
434             userId, password1, password2, passwordReset);
435     }
436 
437     public void updatePortrait(long userId, byte[] bytes)
438         throws PortalException, SystemException {
439 
440         UserPermissionUtil.check(
441             getPermissionChecker(), userId, ActionKeys.UPDATE);
442 
443         userLocalService.updatePortrait(userId, bytes);
444     }
445 
446     public void updateScreenName(long userId, String screenName)
447         throws PortalException, SystemException {
448 
449         UserPermissionUtil.check(
450             getPermissionChecker(), userId, ActionKeys.UPDATE);
451 
452         userLocalService.updateScreenName(userId, screenName);
453     }
454 
455     public User updateUser(
456             long userId, String oldPassword, boolean passwordReset,
457             String screenName, String emailAddress, String languageId,
458             String timeZoneId, String greeting, String comments,
459             String firstName, String middleName, String lastName, int prefixId,
460             int suffixId, boolean male, int birthdayMonth, int birthdayDay,
461             int birthdayYear, String smsSn, String aimSn, String facebookSn,
462             String icqSn, String jabberSn, String msnSn, String mySpaceSn,
463             String skypeSn, String twitterSn, String ymSn, String jobTitle,
464             long[] organizationIds)
465         throws PortalException, SystemException {
466 
467         String newPassword1 = StringPool.BLANK;
468         String newPassword2 = StringPool.BLANK;
469 
470         return updateUser(
471             userId, oldPassword, newPassword1, newPassword2, passwordReset,
472             screenName, emailAddress, languageId, timeZoneId, greeting,
473             comments, firstName, middleName, lastName, prefixId, suffixId, male,
474             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
475             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
476             jobTitle, organizationIds);
477     }
478 
479     public User updateUser(
480             long userId, String oldPassword, String newPassword1,
481             String newPassword2, boolean passwordReset, String screenName,
482             String emailAddress, String languageId, String timeZoneId,
483             String greeting, String comments, String firstName,
484             String middleName, String lastName, int prefixId, int suffixId,
485             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
486             String smsSn, String aimSn, String facebookSn, String icqSn,
487             String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
488             String twitterSn, String ymSn, String jobTitle,
489             long[] organizationIds)
490         throws PortalException, SystemException {
491 
492         UserPermissionUtil.check(
493             getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
494 
495         long curUserId = getUserId();
496 
497         if (curUserId == userId) {
498             emailAddress = emailAddress.trim().toLowerCase();
499 
500             User user = userPersistence.findByPrimaryKey(userId);
501 
502             if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
503                 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
504                     Company company = companyPersistence.findByPrimaryKey(
505                         user.getCompanyId());
506 
507                     if (!company.isStrangersWithMx()) {
508                         throw new ReservedUserEmailAddressException();
509                     }
510                 }
511             }
512         }
513 
514         return userLocalService.updateUser(
515             userId, oldPassword, newPassword1, newPassword2, passwordReset,
516             screenName, emailAddress, languageId, timeZoneId, greeting,
517             comments, firstName, middleName, lastName, prefixId, suffixId, male,
518             birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
519             icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
520             jobTitle, organizationIds);
521     }
522 
523 }