001    /**
002     * Copyright (c) 2000-2010 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.NoSuchUserException;
018    import com.liferay.portal.kernel.facebook.FacebookConnectUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.UserLocalServiceUtil;
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    }