Source of file FacebookConnectAuthCallback.php
Size: 3,488 Bytes - Last Modified: 2021-12-23T10:59:39+00:00
/var/www/docs.ssmods.com/process/src/code/controllers/FacebookConnectAuthCallback.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | <?php use Facebook\FacebookSession; use Facebook\FacebookRequest; use Facebook\GraphUser; use Facebook\FacebookRequestException; /** * @package facebookconnect */ class FacebookConnectAuthCallback extends Controller { private static $allowed_actions = array( 'connect' ); public function connect() { // check we have a valid session $appId = Config::inst()->get( 'FacebookControllerExtension', 'app_id' ); $secret = Config::inst()->get( 'FacebookControllerExtension', 'api_secret' ); $session = $this->getFacebookHelper()->getSessionFromRedirect(); if ($session) { $token = $session->getAccessToken(); // get a long lived token by default. Access token is saved in // session. try { $long = $token->extend($appId, $secret); if ($long) { $accessTokenValue = (string) $long; } else { $accessTokenValue = (string) $token; } } catch (Exception $e) { $accessTokenValue = (string) $token; } try { Session::set( FacebookControllerExtension::FACEBOOK_ACCESS_TOKEN, $accessTokenValue ); $fields = Config::inst()->get( 'FacebookControllerExtension', 'facebook_fields' ); $user = (new FacebookRequest( $session, 'GET', '/me', array('fields' => implode(',', $fields)) ))->execute()->getGraphObject(GraphUser::className()); if (!$member = Member::currentUser()) { // member is not currently logged into SilverStripe. Look up // for a member with the UID which matches first. $member = Member::get()->filter(array( "FacebookUID" => $user->getId() ))->first(); if (!$member) { // see if we have a match based on email. From a // security point of view, users have to confirm their // email address in facebook so doing a match up is fine $email = $user->getProperty('email'); if ($email) { $member = Member::get()->filter(array( 'Email' => $email ))->first(); } } if (!$member) { $member = Injector::inst()->create('Member'); } } $member->syncFacebookDetails($user); $member->logIn(); // redirect the user to the provided url, otherwise take them // back to the route of the website. if ($url = Session::get(FacebookControllerExtension::SESSION_REDIRECT_URL_FLAG)) { return $this->redirect($url); } else { return $this->redirect(Director::absoluteBaseUrl()); } } catch (Exception $e) { SS_Log::log($e, SS_Log::ERR); } } else { return $this->httpError(400); } return $this->httpError(400); } } |