1   /**
2    * Copyright (c) 2000-2007 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.security.permission.ActionKeys;
30  import com.liferay.portal.model.Company;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.Role;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.model.impl.GroupImpl;
35  import com.liferay.portal.model.impl.RoleImpl;
36  import com.liferay.portal.security.auth.PrincipalException;
37  import com.liferay.portal.service.GroupLocalServiceUtil;
38  import com.liferay.portal.service.RoleLocalServiceUtil;
39  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
40  import com.liferay.portal.service.UserLocalServiceUtil;
41  import com.liferay.portal.service.UserService;
42  import com.liferay.portal.service.permission.GroupPermissionUtil;
43  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
44  import com.liferay.portal.service.permission.RolePermissionUtil;
45  import com.liferay.portal.service.permission.UserGroupPermissionUtil;
46  import com.liferay.portal.service.permission.UserPermissionUtil;
47  import com.liferay.portal.service.persistence.CompanyUtil;
48  import com.liferay.portal.service.persistence.UserUtil;
49  
50  import java.util.List;
51  import java.util.Locale;
52  
53  /**
54   * <a href="UserServiceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Brian Myunghun Kim
58   * @author Scott Lee
59   *
60   */
61  public class UserServiceImpl extends PrincipalBean implements UserService {
62  
63      public void addGroupUsers(long groupId, long[] userIds)
64          throws PortalException, SystemException {
65  
66          if ((userIds != null) && (userIds.length > 0)) {
67              checkUpdatePermission(groupId, userIds);
68  
69              UserLocalServiceUtil.addGroupUsers(groupId, userIds);
70          }
71      }
72  
73      public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
74          throws PortalException, SystemException {
75  
76          PasswordPolicyPermissionUtil.check(
77              getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
78  
79          UserLocalServiceUtil.addPasswordPolicyUsers(passwordPolicyId, userIds);
80      }
81  
82      public void addRoleUsers(long roleId, long[] userIds)
83          throws PortalException, SystemException {
84  
85          RolePermissionUtil.check(
86              getPermissionChecker(), roleId, ActionKeys.UPDATE);
87  
88          UserLocalServiceUtil.addRoleUsers(roleId, userIds);
89      }
90  
91      public void addUserGroupUsers(long userGroupId, long[] userIds)
92          throws PortalException, SystemException {
93  
94          if (!UserGroupPermissionUtil.contains(
95                  getPermissionChecker(), userGroupId, ActionKeys.UPDATE) &&
96              !UserGroupPermissionUtil.contains(
97                  getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_USERS)) {
98  
99              throw new PrincipalException();
100         }
101 
102         UserLocalServiceUtil.addUserGroupUsers(userGroupId, userIds);
103     }
104 
105     public User addUser(
106             long companyId, boolean autoPassword, String password1,
107             String password2, boolean autoScreenName, String screenName,
108             String emailAddress, Locale locale, String firstName,
109             String middleName, String lastName, int prefixId, int suffixId,
110             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
111             String jobTitle, long organizationId, long locationId,
112             boolean sendEmail)
113         throws PortalException, SystemException {
114 
115         Company company = CompanyUtil.findByPrimaryKey(companyId);
116 
117         if (!company.isStrangers()) {
118             checkPermission(0, organizationId, locationId, ActionKeys.ADD_USER);
119         }
120 
121         long creatorUserId = 0;
122 
123         try {
124             creatorUserId = getUserId();
125         }
126         catch (PrincipalException pe) {
127         }
128 
129         if (creatorUserId == 0) {
130             if (!company.isStrangersWithMx() &&
131                 company.hasCompanyMx(emailAddress)) {
132 
133                 throw new ReservedUserEmailAddressException();
134             }
135         }
136 
137         return UserLocalServiceUtil.addUser(
138             creatorUserId, companyId, autoPassword, password1, password2,
139             autoScreenName, screenName, emailAddress, locale, firstName,
140             middleName, lastName, prefixId, suffixId, male, birthdayMonth,
141             birthdayDay, birthdayYear, jobTitle, organizationId, locationId,
142             sendEmail);
143     }
144 
145     public void deleteRoleUser(long roleId, long userId)
146         throws PortalException, SystemException {
147 
148         checkPermission(userId, ActionKeys.UPDATE);
149 
150         UserLocalServiceUtil.deleteRoleUser(roleId, userId);
151     }
152 
153     public void deleteUser(long userId)
154         throws PortalException, SystemException {
155 
156         if (getUserId() == userId) {
157             throw new RequiredUserException();
158         }
159 
160         checkPermission(userId, ActionKeys.DELETE);
161 
162         UserLocalServiceUtil.deleteUser(userId);
163     }
164 
165     public long getDefaultUserId(long companyId)
166         throws PortalException, SystemException {
167 
168         return UserLocalServiceUtil.getDefaultUserId(companyId);
169     }
170 
171     public List getGroupUsers(long groupId)
172         throws PortalException, SystemException {
173 
174         return UserLocalServiceUtil.getGroupUsers(groupId);
175     }
176 
177     public List getRoleUsers(long roleId)
178         throws PortalException, SystemException {
179 
180         return UserLocalServiceUtil.getRoleUsers(roleId);
181     }
182 
183     public User getUserByEmailAddress(long companyId, String emailAddress)
184         throws PortalException, SystemException {
185 
186         User user = UserLocalServiceUtil.getUserByEmailAddress(
187             companyId, emailAddress);
188 
189         checkPermission(user.getUserId(), ActionKeys.VIEW);
190 
191         return user;
192     }
193 
194     public User getUserById(long userId)
195         throws PortalException, SystemException {
196 
197         User user = UserLocalServiceUtil.getUserById(userId);
198 
199         checkPermission(user.getUserId(), ActionKeys.VIEW);
200 
201         return user;
202     }
203 
204     public User getUserByScreenName(long companyId, String screenName)
205         throws PortalException, SystemException {
206 
207         User user = UserLocalServiceUtil.getUserByScreenName(
208             companyId, screenName);
209 
210         checkPermission(user.getUserId(), ActionKeys.VIEW);
211 
212         return user;
213     }
214 
215     public boolean hasGroupUser(long groupId, long userId)
216         throws PortalException, SystemException {
217 
218         return UserLocalServiceUtil.hasGroupUser(groupId, userId);
219     }
220 
221     public boolean hasRoleUser(long roleId, long userId)
222         throws PortalException, SystemException {
223 
224         return UserLocalServiceUtil.hasRoleUser(roleId, userId);
225     }
226 
227     public void setGroupUsers(long groupId, long[] userIds)
228         throws PortalException, SystemException {
229 
230         GroupPermissionUtil.check(
231             getPermissionChecker(), groupId, ActionKeys.UPDATE);
232 
233         UserLocalServiceUtil.setGroupUsers(groupId, userIds);
234     }
235 
236     public void setRoleUsers(long roleId, long[] userIds)
237         throws PortalException, SystemException {
238 
239         RolePermissionUtil.check(
240             getPermissionChecker(), roleId, ActionKeys.UPDATE);
241 
242         UserLocalServiceUtil.setRoleUsers(roleId, userIds);
243     }
244 
245     public void setUserGroupUsers(long userGroupId, long[] userIds)
246         throws PortalException, SystemException {
247 
248         if (!UserGroupPermissionUtil.contains(
249                 getPermissionChecker(), userGroupId, ActionKeys.UPDATE) &&
250             !UserGroupPermissionUtil.contains(
251                 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_USERS)) {
252 
253             throw new PrincipalException();
254         }
255 
256         UserLocalServiceUtil.setUserGroupUsers(userGroupId, userIds);
257     }
258 
259     public void unsetGroupUsers(long groupId, long[] userIds)
260         throws PortalException, SystemException {
261 
262         if ((userIds != null) && (userIds.length > 0)) {
263             checkUnsetPermission(groupId, userIds);
264 
265             UserLocalServiceUtil.unsetGroupUsers(groupId, userIds);
266         }
267     }
268 
269     public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
270         throws PortalException, SystemException {
271 
272         PasswordPolicyPermissionUtil.check(
273             getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
274 
275         UserLocalServiceUtil.unsetPasswordPolicyUsers(
276             passwordPolicyId, userIds);
277     }
278 
279     public void unsetRoleUsers(long roleId, long[] userIds)
280         throws PortalException, SystemException {
281 
282         RolePermissionUtil.check(
283             getPermissionChecker(), roleId, ActionKeys.UPDATE);
284 
285         UserLocalServiceUtil.unsetRoleUsers(roleId, userIds);
286     }
287 
288     public void unsetUserGroupUsers(long userGroupId, long[] userIds)
289         throws PortalException, SystemException {
290 
291         if (!UserGroupPermissionUtil.contains(
292                 getPermissionChecker(), userGroupId, ActionKeys.UPDATE) &&
293             !UserGroupPermissionUtil.contains(
294                 getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_USERS)) {
295 
296             throw new PrincipalException();
297         }
298 
299         UserLocalServiceUtil.unsetUserGroupUsers(userGroupId, userIds);
300     }
301 
302     public User updateActive(long userId, boolean active)
303         throws PortalException, SystemException {
304 
305         if ((getUserId() == userId) && !active) {
306             throw new RequiredUserException();
307         }
308 
309         checkPermission(userId, ActionKeys.DELETE);
310 
311         return UserLocalServiceUtil.updateActive(userId, active);
312     }
313 
314     public User updateAgreedToTermsOfUse(
315             long userId, boolean agreedToTermsOfUse)
316         throws PortalException, SystemException {
317 
318         checkPermission(userId, ActionKeys.UPDATE);
319 
320         return UserLocalServiceUtil.updateAgreedToTermsOfUse(
321             userId, agreedToTermsOfUse);
322     }
323 
324     public User updateLockout(long userId, boolean lockout)
325         throws PortalException, SystemException {
326 
327         checkPermission(userId, ActionKeys.DELETE);
328 
329         return UserLocalServiceUtil.updateLockoutById(userId, lockout);
330     }
331 
332     public void updateOrganizations(
333             long userId, long organizationId, long locationId)
334         throws PortalException, SystemException {
335 
336         checkPermission(userId, ActionKeys.UPDATE);
337 
338         UserLocalServiceUtil.updateOrganizations(
339             userId, organizationId, locationId);
340     }
341 
342     public User updatePassword(
343             long userId, String password1, String password2,
344             boolean passwordReset)
345         throws PortalException, SystemException {
346 
347         checkPermission(userId, ActionKeys.UPDATE);
348 
349         return UserLocalServiceUtil.updatePassword(
350             userId, password1, password2, passwordReset);
351     }
352 
353     public void updatePortrait(long userId, byte[] bytes)
354         throws PortalException, SystemException {
355 
356         checkPermission(userId, ActionKeys.UPDATE);
357 
358         UserLocalServiceUtil.updatePortrait(userId, bytes);
359     }
360 
361     public User updateUser(
362             long userId, String password, String screenName,
363             String emailAddress, String languageId, String timeZoneId,
364             String greeting, String comments, String firstName,
365             String middleName, String lastName, int prefixId, int suffixId,
366             boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
367             String smsSn, String aimSn, String icqSn, String jabberSn,
368             String msnSn, String skypeSn, String ymSn, String jobTitle,
369             long organizationId, long locationId)
370         throws PortalException, SystemException {
371 
372         checkPermission(userId, organizationId, locationId, ActionKeys.UPDATE);
373 
374         long curUserId = getUserId();
375 
376         if (curUserId == userId) {
377             emailAddress = emailAddress.trim().toLowerCase();
378 
379             User user = UserUtil.findByPrimaryKey(userId);
380 
381             if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
382                 if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
383                     Company company = CompanyUtil.findByPrimaryKey(
384                         user.getCompanyId());
385 
386                     if (!company.isStrangersWithMx()) {
387                         throw new ReservedUserEmailAddressException();
388                     }
389                 }
390             }
391         }
392 
393         return UserLocalServiceUtil.updateUser(
394             userId, password, screenName, emailAddress, languageId, timeZoneId,
395             greeting, comments, firstName, middleName, lastName, prefixId,
396             suffixId, male, birthdayMonth, birthdayDay, birthdayYear, smsSn,
397             aimSn, icqSn, jabberSn, msnSn, skypeSn, ymSn, jobTitle,
398             organizationId, locationId);
399     }
400 
401     protected void checkPermission(long userId, String actionId)
402         throws PortalException, SystemException {
403 
404         User user = UserUtil.findByPrimaryKey(userId);
405 
406         checkPermission(
407             userId, user.getOrganization().getOrganizationId(),
408             user.getLocation().getOrganizationId(), actionId);
409     }
410 
411     protected void checkPermission(
412             long userId, long organizationId, long locationId, String actionId)
413         throws PortalException, SystemException {
414 
415         UserPermissionUtil.check(
416             getPermissionChecker(), userId, organizationId, locationId,
417             actionId);
418     }
419 
420     protected void checkUnsetPermission(long groupId, long[] userIds)
421         throws PortalException, SystemException {
422 
423         User user = getUser();
424 
425         Role ownerRole = RoleLocalServiceUtil.getRole(
426             user.getCompanyId(), RoleImpl.COMMUNITY_OWNER);
427 
428         if (!UserGroupRoleLocalServiceUtil.hasUserGroupRole(
429                 user.getUserId(), groupId, ownerRole.getRoleId())) {
430 
431             Role adminRole = RoleLocalServiceUtil.getRole(
432                 user.getCompanyId(), RoleImpl.COMMUNITY_ADMINISTRATOR);
433 
434             for (int i = 0; i < userIds.length; i++) {
435                 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
436                         userIds[i], groupId, ownerRole.getRoleId()) ||
437                     UserGroupRoleLocalServiceUtil.hasUserGroupRole(
438                         userIds[i], groupId, adminRole.getRoleId()) ) {
439 
440                     throw new PrincipalException();
441                 }
442             }
443         }
444 
445         checkUpdatePermission(groupId, userIds);
446     }
447 
448     protected void checkUpdatePermission(long groupId, long[] userIds)
449         throws PortalException, SystemException {
450 
451         try {
452             GroupPermissionUtil.check(
453                 getPermissionChecker(), groupId, ActionKeys.UPDATE);
454         }
455         catch (PrincipalException pe) {
456 
457             // Allow users to join and leave open communities
458 
459             boolean hasPermission = false;
460 
461             long userId = getUserId();
462 
463             if ((userIds.length == 1) && (userId == userIds[0])) {
464                 Group group = GroupLocalServiceUtil.getGroup(groupId);
465 
466                 if (group.getType().equals(GroupImpl.TYPE_COMMUNITY_OPEN)) {
467                     hasPermission = true;
468                 }
469             }
470 
471             if (!hasPermission) {
472                 throw new PrincipalException();
473             }
474         }
475     }
476 
477 }