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.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            @Override
038            public String[] login(
039                    HttpServletRequest request, HttpServletResponse response) {
040    
041                    String[] credentials = null;
042    
043                    try {
044                            long companyId = PortalUtil.getCompanyId(request);
045    
046                            if (!FacebookConnectUtil.isEnabled(companyId)) {
047                                    return credentials;
048                            }
049    
050                            HttpSession session = request.getSession();
051    
052                            String emailAddress = (String)session.getAttribute(
053                                    WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
054    
055                            User user = null;
056    
057                            if (Validator.isNotNull(emailAddress)) {
058                                    session.removeAttribute(WebKeys.FACEBOOK_USER_EMAIL_ADDRESS);
059    
060                                    try {
061                                            user = UserLocalServiceUtil.getUserByEmailAddress(
062                                                    companyId, emailAddress);
063                                    }
064                                    catch (NoSuchUserException nsue) {
065                                    }
066                            }
067                            else {
068                                    long facebookId = GetterUtil.getLong(
069                                            (String)session.getAttribute(WebKeys.FACEBOOK_USER_ID));
070    
071                                    if (facebookId > 0) {
072                                            try {
073                                                    user = UserLocalServiceUtil.getUserByFacebookId(
074                                                            companyId, facebookId);
075                                            }
076                                            catch (NoSuchUserException nsue) {
077                                                    return credentials;
078                                            }
079                                    }
080                                    else {
081                                            return credentials;
082                                    }
083                            }
084    
085                            credentials = new String[3];
086    
087                            credentials[0] = String.valueOf(user.getUserId());
088                            credentials[1] = user.getPassword();
089                            credentials[2] = Boolean.FALSE.toString();
090                    }
091                    catch (Exception e) {
092                            _log.error(e, e);
093                    }
094    
095                    return credentials;
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(FacebookAutoLogin.class);
099    
100    }