001    /**
002     * Copyright (c) 2000-2013 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.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    }