001    /**
002     * Copyright (c) 2000-2012 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                    String[] credentials = new String[3];
050    
051                    credentials[0] = String.valueOf(user.getUserId());
052                    credentials[1] = user.getPassword();
053                    credentials[2] = Boolean.FALSE.toString();
054    
055                    return credentials;
056            }
057    
058            protected User getUser(HttpServletRequest request, long companyId)
059                    throws PortalException, SystemException {
060    
061                    HttpSession session = request.getSession();
062    
063                    String emailAddress = (String)session.getAttribute(
064                            WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
065    
066                    if (Validator.isNotNull(emailAddress)) {
067                            session.removeAttribute(WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
068    
069                            return UserLocalServiceUtil.getUserByEmailAddress(
070                                    companyId, emailAddress);
071                    }
072                    else {
073                            long facebookId = GetterUtil.getLong(
074                                    (String)session.getAttribute(WebKeys.FACEBOOK_USER_ID));
075    
076                            return UserLocalServiceUtil.getUserByFacebookId(
077                                    companyId, facebookId);
078                    }
079            }
080    
081    }