Update Facebook auth with new SDK - refs #8264

pull/2487/head
Angel Fernando Quiroz Campos 9 years ago
parent 3da72ceb0b
commit ef3ba79e44
  1. 5
      index.php
  2. 219
      main/auth/external_login/facebook.inc.php
  3. 2
      main/inc/lib/usermanager.lib.php
  4. 2
      main/install/configuration.dist.php

@ -24,11 +24,6 @@ if (!api_is_anonymous()) {
$header_title = " "; $header_title = " ";
} }
// Facebook connexion, if activated
/*if (api_is_facebook_auth_activated() && !api_get_user_id()) {
facebookConnect();
}
*/
$controller = new IndexManager($header_title); $controller = new IndexManager($header_title);
//Actions //Actions

@ -13,19 +13,9 @@
require_once dirname(__FILE__) . '/../../inc/global.inc.php'; require_once dirname(__FILE__) . '/../../inc/global.inc.php';
require_once dirname(__FILE__) . '/facebook.init.php'; require_once dirname(__FILE__) . '/facebook.init.php';
require_once dirname(__FILE__) . '/facebook-php-sdk/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
require_once dirname(__FILE__) . '/functions.inc.php'; require_once dirname(__FILE__) . '/functions.inc.php';
// dont rename $facebook_config to $facebookConfig otherwise get a "Facebook\\FacebookSDKException"
FacebookSession::setDefaultApplication($facebook_config['appId'], $facebook_config['secret']);
/** /**
* This function connect to facebook and retrieves the user info * This function connect to facebook and retrieves the user info
* If user does not exist in chamilo, it creates it and logs in * If user does not exist in chamilo, it creates it and logs in
@ -33,74 +23,144 @@ FacebookSession::setDefaultApplication($facebook_config['appId'], $facebook_conf
*/ */
function facebookConnect() function facebookConnect()
{ {
global $facebook_config; $fb = new \Facebook\Facebook([
global $helper; 'app_id' => $GLOBALS['facebook_config']['appId'],
'app_secret' => $GLOBALS['facebook_config']['secret'],
'default_graph_version' => 'v2.2'
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
Display::addFlash(
Display::return_message('Facebook Graph returned an error: ' . $e->getMessage(), 'error')
);
header('Location: ' . api_get_path(WEB_PATH));
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
Display::addFlash(
Display::return_message('Facebook SDK returned an error: ' . $e->getMessage(), 'error')
);
header('Location: ' . api_get_path(WEB_PATH));
exit;
}
if (!isset($accessToken)) {
if (!$helper->getError()) {
return;
}
if (isset($_GET['loginFailed'])) {
return;
}
$error = implode('<br>', [
'Error: ' . $helper->getError(),
'Error Code: ' . $helper->getErrorCode(),
'Error Reason: ' . $helper->getErrorReason(),
'Error Description: ' . $helper->getErrorDescription()
]);
Display::addFlash(
Display::return_message($error, 'error', false)
);
header('Location: ' . api_get_path(WEB_PATH));
exit;
}
$oAuth2Client = $fb->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
$tokenMetadata->validateAppId($GLOBALS['facebook_config']['appId']);
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
Display::addFlash(
Display::return_message('Error getting long-lived access token: ' . $e->getMessage(), 'error')
);
header('Location: ' . api_get_path(WEB_PATH));
exit;
}
}
try { try {
$helper = new FacebookRedirectLoginHelper($facebook_config['return_url']); $response = $fb->get('/me?fields=id,first_name,last_name,locale,email', $accessToken->getValue());
$session = $helper->getSessionFromRedirect(); } catch(Facebook\Exceptions\FacebookResponseException $e) {
// see if we have a session Display::addFlash(
if (isset($session)) { Display::return_message('Graph returned an error: ' . $e->getMessage(), 'error')
// graph api request for user data );
$request = new FacebookRequest($session, 'GET', '/me?fields=id,first_name,last_name,email,locale');
$response = $request->execute(); header('Location: ' . api_get_path(WEB_PATH));
// get response exit;
$graphObject = $response->getGraphObject(Facebook\GraphUser::className()); } catch(Facebook\Exceptions\FacebookSDKException $e) {
$username = changeToValidChamiloLogin($graphObject->getProperty('email')); Display::addFlash(
$email = $graphObject->getProperty('email'); Display::return_message('Facebook SDK returned an error: ' . $e->getMessage(), 'error')
$locale = $graphObject->getProperty('locale'); );
$language = facebookPluginGetLanguage($locale);
if (!$language) { header('Location: ' . api_get_path(WEB_PATH));
$language='en_US'; exit;
} }
//Checks if user already exists in chamilo $user = $response->getGraphUser();
$u = array( $language = facebookPluginGetLanguage($user['locale']);
'firstname' => $graphObject->getProperty('first_name'),
'lastname' => $graphObject->getProperty('last_name'), if (!$language) {
'status' => STUDENT, $language = 'en_US';
'email' => $graphObject->getProperty('email'), }
'username' => $username,
'language' => $language, $u = [
'password' => 'facebook', 'firstname' => $user->getFirstName(),
'auth_source' => 'facebook', 'lastname' => $user->getLastName(),
// 'courses' => $user_info['courses'], 'status' => STUDENT,
// 'profile_link' => $user_info['profile_link'], 'email' => $user->getEmail(),
// 'worldwide_bu' => $user_info['worlwide_bu'], 'username' => changeToValidChamiloLogin($user->getEmail()),
// 'manager' => $user_info['manager'], 'language' => $language,
'extra' => array() 'password' => 'facebook',
'auth_source' => 'facebook',
'extra' => array()
];
$chamiloUinfo = api_get_user_info_from_email($user->getEmail());
$_user['uidReset'] = true;
$_user['language'] = $language;
if ($chamiloUinfo === false) {
// We have to create the user
$chamilo_uid = external_add_user($u);
if ($chamilo_uid === false) {
Display::addFlash(
Display::return_message(get_lang('UserNotRegistered'), 'error')
); );
$chamiloUinfo = api_get_user_info_from_email($email); header('Location: ' . api_get_path(WEB_PATH));
if ($chamiloUinfo === false) { exit;
// we have to create the user
$chamilo_uid = external_add_user($u);
if ($chamilo_uid !== false) {
$_user['user_id'] = $chamilo_uid;
$_user['uidReset'] = true;
$_SESSION['_user'] = $_user;
header('Location:' . api_get_path(WEB_PATH));
exit();
} else {
return false;
}
} else {
// User already exists, update info and login
$chamilo_uid = $chamiloUinfo['user_id'];
$u['user_id'] = $chamilo_uid;
external_update_user($u);
$_user['user_id'] = $chamilo_uid;
$_user['uidReset'] = true;
$_SESSION['_user'] = $_user;
header('Location:' . api_get_path(WEB_PATH));
exit();
}
} }
} catch (FacebookRequestException $ex) {
echo $ex; $_user['user_id'] = $chamilo_uid;
} catch (Exception $ex) { $_SESSION['_user'] = $_user;
// When validation fails or other local issues
header('Location: ' . api_get_path(WEB_PATH));
exit();
} }
// User already exists, update info and login
$chamilo_uid = $chamiloUinfo['user_id'];
$u['user_id'] = $chamilo_uid;
external_update_user($u);
$_user['user_id'] = $chamilo_uid;
$_SESSION['_user'] = $_user;
header('Location: ' . api_get_path(WEB_PATH));
exit();
} }
/** /**
@ -109,11 +169,16 @@ function facebookConnect()
*/ */
function facebookGetLoginUrl() function facebookGetLoginUrl()
{ {
global $facebook_config; $fb = new \Facebook\Facebook([
$helper = new FacebookRedirectLoginHelper($facebook_config['return_url']); 'app_id' => $GLOBALS['facebook_config']['appId'],
$loginUrl = $helper->getLoginUrl( 'app_secret' => $GLOBALS['facebook_config']['secret'],
array('scope' => 'email') 'default_graph_version' => 'v2.2'
); ]);
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl(api_get_path(WEB_PATH) . '?action=fbconnect', [
'email'
]);
return $loginUrl; return $loginUrl;
} }

@ -860,7 +860,7 @@ class UserManager
$encrypt_method = '', $encrypt_method = '',
$send_email = false, $send_email = false,
$reset_password = 0, $reset_password = 0,
$address $address = null
) { ) {
$hook = HookUpdateUser::create(); $hook = HookUpdateUser::create();
if (!empty($hook)) { if (!empty($hook)) {

@ -67,7 +67,7 @@ $_configuration['url_append'] = '{URL_APPEND_PATH}';
// $extAuthSource["extldap"]["newUser"] = $_configuration['root_sys']."main/auth/external_login/newUser.ldap.php"; // $extAuthSource["extldap"]["newUser"] = $_configuration['root_sys']."main/auth/external_login/newUser.ldap.php";
// //
// FACEBOOK IMPLEMENTATION BASED ON external_login info // FACEBOOK IMPLEMENTATION BASED ON external_login info
// -> Uncomment the line bellow to activate Facebook Auth AND edit main/auth/external_login/ldap.conf.php for configuration // -> Uncomment the line bellow to activate Facebook Auth AND edit app/config/auth.conf.php for configuration
// $_configuration['facebook_auth'] = 1; // $_configuration['facebook_auth'] = 1;
// //
// OTHER EXTERNAL LOGIN INFORMATION // OTHER EXTERNAL LOGIN INFORMATION

Loading…
Cancel
Save