001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.facebook.FacebookConnectUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    import javax.servlet.http.HttpSession;
030    
031    /**
032     * @author Wilson Man
033     */
034    public class FacebookAutoLogin extends BaseAutoLogin {
035    
036            @Override
037            protected String[] doLogin(
038                            HttpServletRequest request, HttpServletResponse response)
039                    throws Exception {
040    
041                    long companyId = PortalUtil.getCompanyId(request);
042    
043                    if (!FacebookConnectUtil.isEnabled(companyId)) {
044                            return null;
045                    }
046    
047                    User user = getUser(request, companyId);
048    
049                    if (user == null) {
050                            return null;
051                    }
052    
053                    String[] credentials = new String[3];
054    
055                    credentials[0] = String.valueOf(user.getUserId());
056                    credentials[1] = user.getPassword();
057                    credentials[2] = Boolean.FALSE.toString();
058    
059                    return credentials;
060            }
061    
062            protected User getUser(HttpServletRequest request, long companyId)
063                    throws PortalException, SystemException {
064    
065                    HttpSession session = request.getSession();
066    
067                    String emailAddress = (String)session.getAttribute(
068                            WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
069    
070                    if (Validator.isNotNull(emailAddress)) {
071                            session.removeAttribute(WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
072    
073                            return UserLocalServiceUtil.getUserByEmailAddress(
074                                    companyId, emailAddress);
075                    }
076                    else {
077                            long facebookId = GetterUtil.getLong(
078                                    (String)session.getAttribute(WebKeys.FACEBOOK_USER_ID));
079    
080                            if (facebookId > 0) {
081                                    return UserLocalServiceUtil.getUserByFacebookId(
082                                            companyId, facebookId);
083                            }
084                    }
085    
086                    return null;
087            }
088    
089    }