001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.admin.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.Contact;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.model.UserGroupRole;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.service.UserServiceUtil;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.util.Calendar;
029    import java.util.List;
030    
031    import javax.portlet.ActionRequest;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class AdminUtil {
039    
040            public static String getUpdateUserPassword(
041                    ActionRequest actionRequest, long userId) {
042    
043                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
044                            actionRequest);
045    
046                    return getUpdateUserPassword(request, userId);
047            }
048    
049            public static String getUpdateUserPassword(
050                    HttpServletRequest request, long userId) {
051    
052                    String password = PortalUtil.getUserPassword(request);
053    
054                    if (userId != PortalUtil.getUserId(request)) {
055                            password = StringPool.BLANK;
056                    }
057    
058                    if (password == null) {
059                            password = StringPool.BLANK;
060                    }
061    
062                    return password;
063            }
064    
065            public static User updateUser(
066                            ActionRequest actionRequest, long userId, String screenName,
067                            String emailAddress, long facebookId, String openId,
068                            String languageId, String timeZoneId, String greeting,
069                            String comments, String smsSn, String facebookSn, String jabberSn,
070                            String skypeSn, String twitterSn)
071                    throws PortalException {
072    
073                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
074                            actionRequest);
075    
076                    return updateUser(
077                            request, userId, screenName, emailAddress, facebookId, openId,
078                            languageId, timeZoneId, greeting, comments, smsSn, facebookSn,
079                            jabberSn, skypeSn, twitterSn);
080            }
081    
082            public static User updateUser(
083                            HttpServletRequest request, long userId, String screenName,
084                            String emailAddress, long facebookId, String openId,
085                            String languageId, String timeZoneId, String greeting,
086                            String comments, String smsSn, String facebookSn, String jabberSn,
087                            String skypeSn, String twitterSn)
088                    throws PortalException {
089    
090                    String password = getUpdateUserPassword(request, userId);
091    
092                    User user = UserLocalServiceUtil.getUserById(userId);
093    
094                    Contact contact = user.getContact();
095    
096                    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
097    
098                    birthdayCal.setTime(contact.getBirthday());
099    
100                    int birthdayMonth = birthdayCal.get(Calendar.MONTH);
101                    int birthdayDay = birthdayCal.get(Calendar.DATE);
102                    int birthdayYear = birthdayCal.get(Calendar.YEAR);
103    
104                    long[] groupIds = null;
105                    long[] organizationIds = null;
106                    long[] roleIds = null;
107                    List<UserGroupRole> userGroupRoles = null;
108                    long[] userGroupIds = null;
109                    ServiceContext serviceContext = new ServiceContext();
110    
111                    return UserServiceUtil.updateUser(
112                            userId, password, StringPool.BLANK, StringPool.BLANK,
113                            user.isPasswordReset(), user.getReminderQueryQuestion(),
114                            user.getReminderQueryAnswer(), screenName, emailAddress, facebookId,
115                            openId, languageId, timeZoneId, greeting, comments,
116                            contact.getFirstName(), contact.getMiddleName(),
117                            contact.getLastName(), contact.getPrefixId(), contact.getSuffixId(),
118                            contact.isMale(), birthdayMonth, birthdayDay, birthdayYear, smsSn,
119                            facebookSn, jabberSn, skypeSn, twitterSn, contact.getJobTitle(),
120                            groupIds, organizationIds, roleIds, userGroupRoles, userGroupIds,
121                            serviceContext);
122            }
123    
124    }