PageRenderTime 96ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/portal-impl/src/com/liferay/portal/service/impl/UserServiceImpl.java

https://github.com/viktorkovacs/liferay-portal-trunk
Java | 989 lines | 717 code | 241 blank | 31 comment | 82 complexity | 2fc459cffd479f9110251ecf9a7f5855 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.service.impl;
  15. import com.liferay.portal.RequiredUserException;
  16. import com.liferay.portal.ReservedUserEmailAddressException;
  17. import com.liferay.portal.UserEmailAddressException;
  18. import com.liferay.portal.UserScreenNameException;
  19. import com.liferay.portal.kernel.exception.PortalException;
  20. import com.liferay.portal.kernel.exception.SystemException;
  21. import com.liferay.portal.kernel.util.ArrayUtil;
  22. import com.liferay.portal.kernel.workflow.WorkflowConstants;
  23. import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
  24. import com.liferay.portal.model.Address;
  25. import com.liferay.portal.model.Company;
  26. import com.liferay.portal.model.Contact;
  27. import com.liferay.portal.model.EmailAddress;
  28. import com.liferay.portal.model.Group;
  29. import com.liferay.portal.model.GroupConstants;
  30. import com.liferay.portal.model.Organization;
  31. import com.liferay.portal.model.Phone;
  32. import com.liferay.portal.model.Role;
  33. import com.liferay.portal.model.User;
  34. import com.liferay.portal.model.UserGroupRole;
  35. import com.liferay.portal.model.Website;
  36. import com.liferay.portal.security.auth.PrincipalException;
  37. import com.liferay.portal.security.permission.ActionKeys;
  38. import com.liferay.portal.security.permission.PermissionChecker;
  39. import com.liferay.portal.service.ServiceContext;
  40. import com.liferay.portal.service.base.UserServiceBaseImpl;
  41. import com.liferay.portal.service.permission.GroupPermissionUtil;
  42. import com.liferay.portal.service.permission.OrganizationPermissionUtil;
  43. import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
  44. import com.liferay.portal.service.permission.PortalPermissionUtil;
  45. import com.liferay.portal.service.permission.RolePermissionUtil;
  46. import com.liferay.portal.service.permission.TeamPermissionUtil;
  47. import com.liferay.portal.service.permission.UserGroupPermissionUtil;
  48. import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
  49. import com.liferay.portal.service.permission.UserPermissionUtil;
  50. import com.liferay.portal.util.PropsValues;
  51. import com.liferay.portlet.announcements.model.AnnouncementsDelivery;
  52. import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
  53. import java.util.List;
  54. import java.util.Locale;
  55. /**
  56. * @author Brian Wing Shun Chan
  57. * @author Brian Myunghun Kim
  58. * @author Scott Lee
  59. * @author Jorge Ferrer
  60. * @author Julio Camarero
  61. */
  62. public class UserServiceImpl extends UserServiceBaseImpl {
  63. public void addGroupUsers(long groupId, long[] userIds)
  64. throws PortalException, SystemException {
  65. try {
  66. GroupPermissionUtil.check(
  67. getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
  68. }
  69. catch (PrincipalException pe) {
  70. // Allow any user to join open sites
  71. boolean hasPermission = false;
  72. if (userIds.length == 0) {
  73. hasPermission = true;
  74. }
  75. else if (userIds.length == 1) {
  76. User user = getUser();
  77. if (user.getUserId() == userIds[0]) {
  78. Group group = groupPersistence.findByPrimaryKey(groupId);
  79. if (user.getCompanyId() == group.getCompanyId()) {
  80. int type = group.getType();
  81. if (type == GroupConstants.TYPE_COMMUNITY_OPEN) {
  82. hasPermission = true;
  83. }
  84. }
  85. }
  86. }
  87. if (!hasPermission) {
  88. throw new PrincipalException();
  89. }
  90. }
  91. userLocalService.addGroupUsers(groupId, userIds);
  92. }
  93. public void addOrganizationUsers(long organizationId, long[] userIds)
  94. throws PortalException, SystemException {
  95. OrganizationPermissionUtil.check(
  96. getPermissionChecker(), organizationId, ActionKeys.ASSIGN_MEMBERS);
  97. validateOrganizationUsers(userIds);
  98. userLocalService.addOrganizationUsers(organizationId, userIds);
  99. }
  100. public void addPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
  101. throws PortalException, SystemException {
  102. PasswordPolicyPermissionUtil.check(
  103. getPermissionChecker(), passwordPolicyId,
  104. ActionKeys.ASSIGN_MEMBERS);
  105. userLocalService.addPasswordPolicyUsers(passwordPolicyId, userIds);
  106. }
  107. public void addRoleUsers(long roleId, long[] userIds)
  108. throws PortalException, SystemException {
  109. RolePermissionUtil.check(
  110. getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
  111. userLocalService.addRoleUsers(roleId, userIds);
  112. }
  113. public void addTeamUsers(long teamId, long[] userIds)
  114. throws PortalException, SystemException {
  115. TeamPermissionUtil.check(
  116. getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
  117. userLocalService.addTeamUsers(teamId, userIds);
  118. }
  119. public User addUser(
  120. long companyId, boolean autoPassword, String password1,
  121. String password2, boolean autoScreenName, String screenName,
  122. String emailAddress, long facebookId, String openId, Locale locale,
  123. String firstName, String middleName, String lastName, int prefixId,
  124. int suffixId, boolean male, int birthdayMonth, int birthdayDay,
  125. int birthdayYear, String jobTitle, long[] groupIds,
  126. long[] organizationIds, long[] roleIds, long[] userGroupIds,
  127. boolean sendEmail, List<Address> addresses,
  128. List<EmailAddress> emailAddresses, List<Phone> phones,
  129. List<Website> websites,
  130. List<AnnouncementsDelivery> announcementsDelivers,
  131. ServiceContext serviceContext)
  132. throws PortalException, SystemException {
  133. boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
  134. try {
  135. WorkflowThreadLocal.setEnabled(false);
  136. return addUserWithWorkflow(
  137. companyId, autoPassword, password1, password2, autoScreenName,
  138. screenName, emailAddress, facebookId, openId, locale, firstName,
  139. middleName, lastName, prefixId, suffixId, male, birthdayMonth,
  140. birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
  141. roleIds, userGroupIds, sendEmail, addresses, emailAddresses,
  142. phones, websites, announcementsDelivers, serviceContext);
  143. }
  144. finally {
  145. WorkflowThreadLocal.setEnabled(workflowEnabled);
  146. }
  147. }
  148. public User addUser(
  149. long companyId, boolean autoPassword, String password1,
  150. String password2, boolean autoScreenName, String screenName,
  151. String emailAddress, long facebookId, String openId, Locale locale,
  152. String firstName, String middleName, String lastName, int prefixId,
  153. int suffixId, boolean male, int birthdayMonth, int birthdayDay,
  154. int birthdayYear, String jobTitle, long[] groupIds,
  155. long[] organizationIds, long[] roleIds, long[] userGroupIds,
  156. boolean sendEmail, ServiceContext serviceContext)
  157. throws PortalException, SystemException {
  158. boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
  159. try {
  160. WorkflowThreadLocal.setEnabled(false);
  161. return addUserWithWorkflow(
  162. companyId, autoPassword, password1, password2, autoScreenName,
  163. screenName, emailAddress, facebookId, openId, locale, firstName,
  164. middleName, lastName, prefixId, suffixId, male, birthdayMonth,
  165. birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
  166. roleIds, userGroupIds, sendEmail, serviceContext);
  167. }
  168. finally {
  169. WorkflowThreadLocal.setEnabled(workflowEnabled);
  170. }
  171. }
  172. public User addUserWithWorkflow(
  173. long companyId, boolean autoPassword, String password1,
  174. String password2, boolean autoScreenName, String screenName,
  175. String emailAddress, long facebookId, String openId, Locale locale,
  176. String firstName, String middleName, String lastName, int prefixId,
  177. int suffixId, boolean male, int birthdayMonth, int birthdayDay,
  178. int birthdayYear, String jobTitle, long[] groupIds,
  179. long[] organizationIds, long[] roleIds, long[] userGroupIds,
  180. boolean sendEmail, List<Address> addresses,
  181. List<EmailAddress> emailAddresses, List<Phone> phones,
  182. List<Website> websites,
  183. List<AnnouncementsDelivery> announcementsDelivers,
  184. ServiceContext serviceContext)
  185. throws PortalException, SystemException {
  186. User user = addUserWithWorkflow(
  187. companyId, autoPassword, password1, password2, autoScreenName,
  188. screenName, emailAddress, facebookId, openId, locale, firstName,
  189. middleName, lastName, prefixId, suffixId, male, birthdayMonth,
  190. birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
  191. roleIds, userGroupIds, sendEmail, serviceContext);
  192. EnterpriseAdminUtil.updateAddresses(
  193. Contact.class.getName(), user.getContactId(), addresses);
  194. EnterpriseAdminUtil.updateEmailAddresses(
  195. Contact.class.getName(), user.getContactId(), emailAddresses);
  196. EnterpriseAdminUtil.updatePhones(
  197. Contact.class.getName(), user.getContactId(), phones);
  198. EnterpriseAdminUtil.updateWebsites(
  199. Contact.class.getName(), user.getContactId(), websites);
  200. updateAnnouncementsDeliveries(user.getUserId(), announcementsDelivers);
  201. return user;
  202. }
  203. public User addUserWithWorkflow(
  204. long companyId, boolean autoPassword, String password1,
  205. String password2, boolean autoScreenName, String screenName,
  206. String emailAddress, long facebookId, String openId, Locale locale,
  207. String firstName, String middleName, String lastName, int prefixId,
  208. int suffixId, boolean male, int birthdayMonth, int birthdayDay,
  209. int birthdayYear, String jobTitle, long[] groupIds,
  210. long[] organizationIds, long[] roleIds, long[] userGroupIds,
  211. boolean sendEmail, ServiceContext serviceContext)
  212. throws PortalException, SystemException {
  213. Company company = companyPersistence.findByPrimaryKey(companyId);
  214. long creatorUserId = 0;
  215. try {
  216. creatorUserId = getUserId();
  217. }
  218. catch (PrincipalException pe) {
  219. }
  220. if ((creatorUserId != 0) || !company.isStrangers()) {
  221. if (!PortalPermissionUtil.contains(
  222. getPermissionChecker(), ActionKeys.ADD_USER) &&
  223. !UserPermissionUtil.contains(
  224. getPermissionChecker(), 0, organizationIds,
  225. ActionKeys.ADD_USER)) {
  226. throw new PrincipalException();
  227. }
  228. }
  229. if (creatorUserId == 0) {
  230. if (!company.isStrangersWithMx() &&
  231. company.hasCompanyMx(emailAddress)) {
  232. throw new ReservedUserEmailAddressException();
  233. }
  234. }
  235. return userLocalService.addUserWithWorkflow(
  236. creatorUserId, companyId, autoPassword, password1, password2,
  237. autoScreenName, screenName, emailAddress, facebookId, openId,
  238. locale, firstName, middleName, lastName, prefixId, suffixId, male,
  239. birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
  240. organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
  241. }
  242. public void addUserGroupUsers(long userGroupId, long[] userIds)
  243. throws PortalException, SystemException {
  244. UserGroupPermissionUtil.check(
  245. getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
  246. userLocalService.addUserGroupUsers(userGroupId, userIds);
  247. }
  248. public void deletePortrait(long userId)
  249. throws PortalException, SystemException {
  250. UserPermissionUtil.check(
  251. getPermissionChecker(), userId, ActionKeys.UPDATE);
  252. userLocalService.deletePortrait(userId);
  253. }
  254. public void deleteRoleUser(long roleId, long userId)
  255. throws PortalException, SystemException {
  256. RolePermissionUtil.check(
  257. getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
  258. userLocalService.deleteRoleUser(roleId, userId);
  259. }
  260. public void deleteUser(long userId)
  261. throws PortalException, SystemException {
  262. if (getUserId() == userId) {
  263. throw new RequiredUserException();
  264. }
  265. UserPermissionUtil.check(
  266. getPermissionChecker(), userId, ActionKeys.DELETE);
  267. userLocalService.deleteUser(userId);
  268. }
  269. public long getDefaultUserId(long companyId)
  270. throws PortalException, SystemException {
  271. return userLocalService.getDefaultUserId(companyId);
  272. }
  273. public long[] getGroupUserIds(long groupId) throws SystemException {
  274. return userLocalService.getGroupUserIds(groupId);
  275. }
  276. public long[] getOrganizationUserIds(long organizationId)
  277. throws SystemException {
  278. return userLocalService.getOrganizationUserIds(organizationId);
  279. }
  280. public long[] getRoleUserIds(long roleId) throws SystemException {
  281. return userLocalService.getRoleUserIds(roleId);
  282. }
  283. public User getUserByEmailAddress(long companyId, String emailAddress)
  284. throws PortalException, SystemException {
  285. User user = userLocalService.getUserByEmailAddress(
  286. companyId, emailAddress);
  287. UserPermissionUtil.check(
  288. getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
  289. return user;
  290. }
  291. public User getUserById(long userId)
  292. throws PortalException, SystemException {
  293. User user = userLocalService.getUserById(userId);
  294. UserPermissionUtil.check(
  295. getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
  296. return user;
  297. }
  298. public User getUserByScreenName(long companyId, String screenName)
  299. throws PortalException, SystemException {
  300. User user = userLocalService.getUserByScreenName(
  301. companyId, screenName);
  302. UserPermissionUtil.check(
  303. getPermissionChecker(), user.getUserId(), ActionKeys.VIEW);
  304. return user;
  305. }
  306. public long getUserIdByEmailAddress(long companyId, String emailAddress)
  307. throws PortalException, SystemException {
  308. User user = getUserByEmailAddress(companyId, emailAddress);
  309. return user.getUserId();
  310. }
  311. public long getUserIdByScreenName(long companyId, String screenName)
  312. throws PortalException, SystemException {
  313. User user = getUserByScreenName(companyId, screenName);
  314. return user.getUserId();
  315. }
  316. public boolean hasGroupUser(long groupId, long userId)
  317. throws SystemException {
  318. return userLocalService.hasGroupUser(groupId, userId);
  319. }
  320. public boolean hasRoleUser(long roleId, long userId)
  321. throws SystemException {
  322. return userLocalService.hasRoleUser(roleId, userId);
  323. }
  324. public boolean hasRoleUser(
  325. long companyId, String name, long userId, boolean inherited)
  326. throws PortalException, SystemException {
  327. return userLocalService.hasRoleUser(companyId, name, userId, inherited);
  328. }
  329. public void setRoleUsers(long roleId, long[] userIds)
  330. throws PortalException, SystemException {
  331. RolePermissionUtil.check(
  332. getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
  333. userLocalService.setRoleUsers(roleId, userIds);
  334. }
  335. public void setUserGroupUsers(long userGroupId, long[] userIds)
  336. throws PortalException, SystemException {
  337. UserGroupPermissionUtil.check(
  338. getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
  339. userLocalService.setUserGroupUsers(userGroupId, userIds);
  340. }
  341. public void unsetGroupUsers(long groupId, long[] userIds)
  342. throws PortalException, SystemException {
  343. try {
  344. GroupPermissionUtil.check(
  345. getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
  346. }
  347. catch (PrincipalException pe) {
  348. // Allow any user to leave open and restricted sites
  349. boolean hasPermission = false;
  350. if (userIds.length == 0) {
  351. hasPermission = true;
  352. }
  353. else if (userIds.length == 1) {
  354. User user = getUser();
  355. if (user.getUserId() == userIds[0]) {
  356. Group group = groupPersistence.findByPrimaryKey(groupId);
  357. if (user.getCompanyId() == group.getCompanyId()) {
  358. int type = group.getType();
  359. if ((type == GroupConstants.TYPE_COMMUNITY_OPEN) ||
  360. (type ==
  361. GroupConstants.TYPE_COMMUNITY_RESTRICTED)) {
  362. hasPermission = true;
  363. }
  364. }
  365. }
  366. }
  367. if (!hasPermission) {
  368. throw new PrincipalException();
  369. }
  370. }
  371. userLocalService.unsetGroupUsers(groupId, userIds);
  372. }
  373. public void unsetOrganizationUsers(long organizationId, long[] userIds)
  374. throws PortalException, SystemException {
  375. OrganizationPermissionUtil.check(
  376. getPermissionChecker(), organizationId,
  377. ActionKeys.ASSIGN_MEMBERS);
  378. userLocalService.unsetOrganizationUsers(organizationId, userIds);
  379. }
  380. public void unsetPasswordPolicyUsers(long passwordPolicyId, long[] userIds)
  381. throws PortalException, SystemException {
  382. PasswordPolicyPermissionUtil.check(
  383. getPermissionChecker(), passwordPolicyId,
  384. ActionKeys.ASSIGN_MEMBERS);
  385. userLocalService.unsetPasswordPolicyUsers(passwordPolicyId, userIds);
  386. }
  387. public void unsetRoleUsers(long roleId, long[] userIds)
  388. throws PortalException, SystemException {
  389. RolePermissionUtil.check(
  390. getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
  391. userLocalService.unsetRoleUsers(roleId, userIds);
  392. }
  393. public void unsetTeamUsers(long teamId, long[] userIds)
  394. throws PortalException, SystemException {
  395. TeamPermissionUtil.check(
  396. getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
  397. userLocalService.unsetTeamUsers(teamId, userIds);
  398. }
  399. public void unsetUserGroupUsers(long userGroupId, long[] userIds)
  400. throws PortalException, SystemException {
  401. UserGroupPermissionUtil.check(
  402. getPermissionChecker(), userGroupId, ActionKeys.ASSIGN_MEMBERS);
  403. userLocalService.unsetUserGroupUsers(userGroupId, userIds);
  404. }
  405. public User updateAgreedToTermsOfUse(
  406. long userId, boolean agreedToTermsOfUse)
  407. throws PortalException, SystemException {
  408. UserPermissionUtil.check(
  409. getPermissionChecker(), userId, ActionKeys.UPDATE);
  410. return userLocalService.updateAgreedToTermsOfUse(
  411. userId, agreedToTermsOfUse);
  412. }
  413. public User updateEmailAddress(
  414. long userId, String password, String emailAddress1,
  415. String emailAddress2)
  416. throws PortalException, SystemException {
  417. UserPermissionUtil.check(
  418. getPermissionChecker(), userId, ActionKeys.UPDATE);
  419. return userLocalService.updateEmailAddress(
  420. userId, password, emailAddress1, emailAddress2);
  421. }
  422. public User updateLockoutById(long userId, boolean lockout)
  423. throws PortalException, SystemException {
  424. UserPermissionUtil.check(
  425. getPermissionChecker(), userId, ActionKeys.DELETE);
  426. return userLocalService.updateLockoutById(userId, lockout);
  427. }
  428. public User updateOpenId(long userId, String openId)
  429. throws PortalException, SystemException {
  430. UserPermissionUtil.check(
  431. getPermissionChecker(), userId, ActionKeys.UPDATE);
  432. return userLocalService.updateOpenId(userId, openId);
  433. }
  434. public void updateOrganizations(long userId, long[] organizationIds)
  435. throws PortalException, SystemException {
  436. UserPermissionUtil.check(
  437. getPermissionChecker(), userId, ActionKeys.UPDATE);
  438. userLocalService.updateOrganizations(userId, organizationIds);
  439. }
  440. public User updatePassword(
  441. long userId, String password1, String password2,
  442. boolean passwordReset)
  443. throws PortalException, SystemException {
  444. UserPermissionUtil.check(
  445. getPermissionChecker(), userId, ActionKeys.UPDATE);
  446. return userLocalService.updatePassword(
  447. userId, password1, password2, passwordReset);
  448. }
  449. public User updatePortrait(long userId, byte[] bytes)
  450. throws PortalException, SystemException {
  451. UserPermissionUtil.check(
  452. getPermissionChecker(), userId, ActionKeys.UPDATE);
  453. return userLocalService.updatePortrait(userId, bytes);
  454. }
  455. public User updateReminderQuery(
  456. long userId, String question, String answer)
  457. throws PortalException, SystemException {
  458. UserPermissionUtil.check(
  459. getPermissionChecker(), userId, ActionKeys.UPDATE);
  460. return userLocalService.updateReminderQuery(userId, question, answer);
  461. }
  462. public User updateScreenName(long userId, String screenName)
  463. throws PortalException, SystemException {
  464. UserPermissionUtil.check(
  465. getPermissionChecker(), userId, ActionKeys.UPDATE);
  466. return userLocalService.updateScreenName(userId, screenName);
  467. }
  468. public User updateStatus(long userId, int status)
  469. throws PortalException, SystemException {
  470. if ((getUserId() == userId) &&
  471. (status != WorkflowConstants.STATUS_APPROVED)) {
  472. throw new RequiredUserException();
  473. }
  474. UserPermissionUtil.check(
  475. getPermissionChecker(), userId, ActionKeys.DELETE);
  476. return userLocalService.updateStatus(userId, status);
  477. }
  478. public User updateUser(
  479. long userId, String oldPassword, String newPassword1,
  480. String newPassword2, boolean passwordReset,
  481. String reminderQueryQuestion, String reminderQueryAnswer,
  482. String screenName, String emailAddress, long facebookId,
  483. String openId, String languageId, String timeZoneId,
  484. String greeting, String comments, String firstName,
  485. String middleName, String lastName, int prefixId, int suffixId,
  486. boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
  487. String smsSn, String aimSn, String facebookSn, String icqSn,
  488. String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
  489. String twitterSn, String ymSn, String jobTitle, long[] groupIds,
  490. long[] organizationIds, long[] roleIds,
  491. List<UserGroupRole> userGroupRoles, long[] userGroupIds,
  492. List<Address> addresses, List<EmailAddress> emailAddresses,
  493. List<Phone> phones, List<Website> websites,
  494. List<AnnouncementsDelivery> announcementsDelivers,
  495. ServiceContext serviceContext)
  496. throws PortalException, SystemException {
  497. User user = updateUser(
  498. userId, oldPassword, newPassword1, newPassword2, passwordReset,
  499. reminderQueryQuestion, reminderQueryAnswer, screenName,
  500. emailAddress, facebookId, openId, languageId, timeZoneId, greeting,
  501. comments, firstName, middleName, lastName, prefixId, suffixId, male,
  502. birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
  503. icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
  504. jobTitle, groupIds, organizationIds, roleIds,
  505. userGroupRoles, userGroupIds, serviceContext);
  506. EnterpriseAdminUtil.updateAddresses(
  507. Contact.class.getName(), user.getContactId(), addresses);
  508. EnterpriseAdminUtil.updateEmailAddresses(
  509. Contact.class.getName(), user.getContactId(), emailAddresses);
  510. EnterpriseAdminUtil.updatePhones(
  511. Contact.class.getName(), user.getContactId(), phones);
  512. EnterpriseAdminUtil.updateWebsites(
  513. Contact.class.getName(), user.getContactId(), websites);
  514. updateAnnouncementsDeliveries(user.getUserId(), announcementsDelivers);
  515. return user;
  516. }
  517. public User updateUser(
  518. long userId, String oldPassword, String newPassword1,
  519. String newPassword2, boolean passwordReset,
  520. String reminderQueryQuestion, String reminderQueryAnswer,
  521. String screenName, String emailAddress, long facebookId,
  522. String openId, String languageId, String timeZoneId,
  523. String greeting, String comments, String firstName,
  524. String middleName, String lastName, int prefixId, int suffixId,
  525. boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
  526. String smsSn, String aimSn, String facebookSn, String icqSn,
  527. String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
  528. String twitterSn, String ymSn, String jobTitle, long[] groupIds,
  529. long[] organizationIds, long[] roleIds,
  530. List<UserGroupRole> userGroupRoles, long[] userGroupIds,
  531. ServiceContext serviceContext)
  532. throws PortalException, SystemException {
  533. UserPermissionUtil.check(
  534. getPermissionChecker(), userId, organizationIds, ActionKeys.UPDATE);
  535. long curUserId = getUserId();
  536. if (curUserId == userId) {
  537. User user = userPersistence.findByPrimaryKey(userId);
  538. screenName = screenName.trim().toLowerCase();
  539. if (!screenName.equalsIgnoreCase(user.getScreenName())) {
  540. validateScreenName(user, screenName);
  541. }
  542. emailAddress = emailAddress.trim().toLowerCase();
  543. if (!emailAddress.equalsIgnoreCase(user.getEmailAddress())) {
  544. validateEmailAddress(user, emailAddress);
  545. }
  546. }
  547. if (groupIds != null) {
  548. groupIds = checkGroups(userId, groupIds);
  549. }
  550. if (organizationIds != null) {
  551. organizationIds = checkOrganizations(userId, organizationIds);
  552. }
  553. if (roleIds != null) {
  554. roleIds = checkRoles(userId, roleIds);
  555. }
  556. if (userGroupRoles != null) {
  557. userGroupRoles = checkUserGroupRoles(userId, userGroupRoles);
  558. }
  559. return userLocalService.updateUser(
  560. userId, oldPassword, newPassword1, newPassword2, passwordReset,
  561. reminderQueryQuestion, reminderQueryAnswer, screenName,
  562. emailAddress, facebookId, openId, languageId, timeZoneId, greeting,
  563. comments, firstName, middleName, lastName, prefixId, suffixId, male,
  564. birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
  565. icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn,
  566. jobTitle, groupIds, organizationIds, roleIds, userGroupRoles,
  567. userGroupIds, serviceContext);
  568. }
  569. protected long[] checkGroups(long userId, long[] groupIds)
  570. throws PortalException, SystemException {
  571. // Add back any groups that the administrator does not have the rights
  572. // to remove and check that he has the permission to add any new group
  573. List<Group> oldGroups = groupLocalService.getUserGroups(userId);
  574. long[] oldGroupIds = new long[oldGroups.size()];
  575. for (int i = 0; i < oldGroups.size(); i++) {
  576. Group group = oldGroups.get(i);
  577. if (!ArrayUtil.contains(groupIds, group.getGroupId()) &&
  578. !GroupPermissionUtil.contains(
  579. getPermissionChecker(), group.getGroupId(),
  580. ActionKeys.ASSIGN_MEMBERS)) {
  581. groupIds = ArrayUtil.append(groupIds, group.getGroupId());
  582. }
  583. oldGroupIds[i] = group.getGroupId();
  584. }
  585. for (long groupId : groupIds) {
  586. if (!ArrayUtil.contains(oldGroupIds, groupId)) {
  587. GroupPermissionUtil.check(
  588. getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
  589. }
  590. }
  591. return groupIds;
  592. }
  593. protected long[] checkOrganizations(long userId, long[] organizationIds)
  594. throws PortalException, SystemException {
  595. // Add back any organizations that the administrator does not have the
  596. // rights to remove and check that he has the permission to add any new
  597. // organization
  598. List<Organization> oldOrganizations =
  599. organizationLocalService.getUserOrganizations(userId);
  600. long[] oldOrganizationIds = new long[oldOrganizations.size()];
  601. for (int i = 0; i < oldOrganizations.size(); i++) {
  602. Organization organization = oldOrganizations.get(i);
  603. if (!ArrayUtil.contains(
  604. organizationIds, organization.getOrganizationId()) &&
  605. !OrganizationPermissionUtil.contains(
  606. getPermissionChecker(), organization.getOrganizationId(),
  607. ActionKeys.ASSIGN_MEMBERS)) {
  608. organizationIds = ArrayUtil.append(
  609. organizationIds, organization.getOrganizationId());
  610. }
  611. oldOrganizationIds[i] = organization.getOrganizationId();
  612. }
  613. for (long organizationId : organizationIds) {
  614. if (!ArrayUtil.contains(oldOrganizationIds, organizationId)) {
  615. OrganizationPermissionUtil.check(
  616. getPermissionChecker(), organizationId,
  617. ActionKeys.ASSIGN_MEMBERS);
  618. }
  619. }
  620. return organizationIds;
  621. }
  622. protected long[] checkRoles(long userId, long[] roleIds)
  623. throws PrincipalException, SystemException {
  624. // Add back any roles that the administrator does not have the rights to
  625. // remove and check that he has the permission to add any new role
  626. List<Role> oldRoles = roleLocalService.getUserRoles(userId);
  627. long[] oldRoleIds = new long[oldRoles.size()];
  628. for (int i = 0; i < oldRoles.size(); i++) {
  629. Role role = oldRoles.get(i);
  630. if (!ArrayUtil.contains(roleIds, role.getRoleId()) &&
  631. !RolePermissionUtil.contains(
  632. getPermissionChecker(), role.getRoleId(),
  633. ActionKeys.ASSIGN_MEMBERS)) {
  634. roleIds = ArrayUtil.append(roleIds, role.getRoleId());
  635. }
  636. oldRoleIds[i] = role.getRoleId();
  637. }
  638. for (long roleId : roleIds) {
  639. if (!ArrayUtil.contains(oldRoleIds, roleId)) {
  640. RolePermissionUtil.check(
  641. getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
  642. }
  643. }
  644. return roleIds;
  645. }
  646. protected List<UserGroupRole> checkUserGroupRoles(
  647. long userId, List<UserGroupRole> userGroupRoles)
  648. throws PortalException, SystemException {
  649. // Add back any group roles that the administrator does not have the
  650. // rights to remove
  651. List<UserGroupRole> oldUserGroupRoles =
  652. userGroupRoleLocalService.getUserGroupRoles(userId);
  653. for (UserGroupRole oldUserGroupRole : oldUserGroupRoles) {
  654. if (!userGroupRoles.contains(oldUserGroupRole) &&
  655. !UserGroupRolePermissionUtil.contains(
  656. getPermissionChecker(), oldUserGroupRole.getGroupId(),
  657. oldUserGroupRole.getRoleId())) {
  658. userGroupRoles.add(oldUserGroupRole);
  659. }
  660. }
  661. for (UserGroupRole userGroupRole : userGroupRoles) {
  662. if (!oldUserGroupRoles.contains(userGroupRole)) {
  663. UserGroupRolePermissionUtil.check(
  664. getPermissionChecker(), userGroupRole.getGroupId(),
  665. userGroupRole.getRoleId());
  666. }
  667. }
  668. return userGroupRoles;
  669. }
  670. protected void updateAnnouncementsDeliveries(
  671. long userId, List<AnnouncementsDelivery> announcementsDeliveries)
  672. throws PortalException, SystemException {
  673. for (AnnouncementsDelivery announcementsDelivery :
  674. announcementsDeliveries) {
  675. announcementsDeliveryService.updateDelivery(
  676. userId, announcementsDelivery.getType(),
  677. announcementsDelivery.getEmail(),
  678. announcementsDelivery.getSms(),
  679. announcementsDelivery.getWebsite());
  680. }
  681. }
  682. protected void validateEmailAddress(User user, String emailAddress)
  683. throws PortalException, SystemException {
  684. PermissionChecker permissionChecker = getPermissionChecker();
  685. if (!EnterpriseAdminUtil.hasUpdateEmailAddress(
  686. permissionChecker, user)) {
  687. throw new UserEmailAddressException();
  688. }
  689. if (!user.hasCompanyMx() && user.hasCompanyMx(emailAddress)) {
  690. Company company = companyPersistence.findByPrimaryKey(
  691. user.getCompanyId());
  692. if (!company.isStrangersWithMx()) {
  693. throw new ReservedUserEmailAddressException();
  694. }
  695. }
  696. }
  697. protected void validateOrganizationUsers(long[] userIds)
  698. throws PortalException, SystemException {
  699. PermissionChecker permissionChecker = getPermissionChecker();
  700. if (!PropsValues.ORGANIZATIONS_ASSIGNMENT_STRICT ||
  701. permissionChecker.isCompanyAdmin()) {
  702. return;
  703. }
  704. List<Organization> organizations =
  705. organizationLocalService.getUserOrganizations(
  706. permissionChecker.getUserId());
  707. for (long userId : userIds) {
  708. boolean allowed = false;
  709. for (Organization organization : organizations) {
  710. boolean manageUsers = OrganizationPermissionUtil.contains(
  711. permissionChecker, organization, ActionKeys.MANAGE_USERS);
  712. boolean manageSuborganizations =
  713. OrganizationPermissionUtil.contains(
  714. permissionChecker, organization,
  715. ActionKeys.MANAGE_SUBORGANIZATIONS);
  716. if (!manageUsers && !manageSuborganizations) {
  717. continue;
  718. }
  719. boolean inherited = false;
  720. boolean includeSpecifiedOrganization = false;
  721. if (manageUsers && manageSuborganizations) {
  722. inherited = true;
  723. includeSpecifiedOrganization = true;
  724. }
  725. else if (!manageUsers && manageSuborganizations) {
  726. inherited = true;
  727. includeSpecifiedOrganization = false;
  728. }
  729. if (organizationLocalService.hasUserOrganization(
  730. userId, organization.getOrganizationId(), inherited,
  731. false, includeSpecifiedOrganization)) {
  732. allowed = true;
  733. break;
  734. }
  735. }
  736. if (!allowed) {
  737. throw new PrincipalException();
  738. }
  739. }
  740. }
  741. protected void validateScreenName(User user, String screenName)
  742. throws PortalException, SystemException {
  743. PermissionChecker permissionChecker = getPermissionChecker();
  744. if (!EnterpriseAdminUtil.hasUpdateScreenName(permissionChecker, user)) {
  745. throw new UserScreenNameException();
  746. }
  747. }
  748. }