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