001    /**
002     * Copyright (c) 2000-2010 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.NoSuchUserException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
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.FacebookConnectUtil;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.WebKeys;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    import javax.servlet.http.HttpSession;
031    
032    /**
033     * @author Wilson Man
034     */
035    public class FacebookAutoLogin implements AutoLogin {
036    
037            public String[] login(
038                    HttpServletRequest request, HttpServletResponse response) {
039    
040                    String[] credentials = null;
041    
042                    try {
043                            long companyId = PortalUtil.getCompanyId(request);
044    
045                            if (!FacebookConnectUtil.isEnabled(companyId)) {
046                                    return credentials;
047                            }
048    
049                            HttpSession session = request.getSession();
050    
051                            String emailAddress = (String)session.getAttribute(
052                                    WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
053    
054                            User user = null;
055    
056                            if (Validator.isNotNull(emailAddress)) {
057                                    session.removeAttribute(WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
058    
059                                    try {
060                                            user = UserLocalServiceUtil.getUserByEmailAddress(
061                                                    companyId, emailAddress);
062                                    }
063                                    catch (NoSuchUserException nsue) {
064                                    }
065                            }
066                            else {
067                                    long facebookId = GetterUtil.getLong(
068                                            (String)session.getAttribute(WebKeys.FACEBOOK_USER_ID));
069    
070                                    if (facebookId > 0) {
071                                            try {
072                                                    user = UserLocalServiceUtil.getUserByFacebookId(
073                                                            companyId, facebookId);
074                                            }
075                                            catch (NoSuchUserException nsue) {
076                                                    return credentials;
077                                            }
078                                    }
079                                    else {
080                                            return credentials;
081                                    }
082                            }
083    
084                            credentials = new String[3];
085    
086                            credentials[0] = String.valueOf(user.getUserId());
087                            credentials[1] = user.getPassword();
088                            credentials[2] = Boolean.FALSE.toString();
089                    }
090                    catch (Exception e) {
091                            _log.error(e, e);
092                    }
093    
094                    return credentials;
095            }
096    
097            private static Log _log = LogFactoryUtil.getLog(FacebookAutoLogin.class);
098    
099    }