/** * @param Facebook\Facebook $facebook */ public function __construct(Facebook\Facebook $facebook) { $this->facebook = $facebook; $this->config = $facebook->config; $this->currentUrl = $facebook->getCurrentUrl(); parent::__construct(); }
/** * @param Facebook\Facebook $facebook */ public function __construct(Facebook\Facebook $facebook) { $this->facebook = $facebook; $this->config = $facebook->config; $this->currentUrl = $facebook->getCurrentUrl(); $this->monitor('Nette\\Application\\IPresenter'); parent::__construct(); }
/** * @param User $user * @return bool */ public function doesUserLikePage(User $user) { $this->facebook->setAccessToken($user->facebookAccessToken); try { return count($this->facebook->api("me/likes/{$this->pageId}")->data) !== 0; } catch (FacebookApiException $e) { return FALSE; // safe fallback } }
/** @return \Kdyby\Facebook\Dialog\LoginDialog */ protected function createComponentFbLogin() { $dialog = $this->facebook->createDialog('login'); /** @var \Kdyby\Facebook\Dialog\LoginDialog $dialog */ $dialog->onResponse[] = function (\Kdyby\Facebook\Dialog\LoginDialog $dialog) { $fb = $dialog->getFacebook(); if (!$fb->getUser()) { $this->flashMessage("Facebook authentication failed."); return; } /** * If we get here, it means that the user was recognized * and we can call the Facebook API */ try { $me = $fb->api('/me'); if (!($existing = $this->usersModel->findByFacebookId($fb->getUser()))) { /** * Variable $me contains all the public information about the user * including facebook id, name and email, if he allowed you to see it. */ $existing = $this->usersModel->registerFromFacebook($fb->getUser(), $me, $this->user); } /** * You should save the access token to database for later usage. * * You will need it when you'll want to call Facebook API, * when the user is not logged in to your website, * with the access token in his session. */ $this->usersModel->updateFacebookAccessToken($fb->getUser(), $fb->getAccessToken()); /** * Nette\Security\User accepts not only textual credentials, * but even an identity instance! */ $this->user->setExpiration('365 days', FALSE); $this->user->login(new \Nette\Security\Identity($existing->id, NULL, $existing)); /** * You can celebrate now! The user is authenticated :) */ } catch (\Kdyby\Facebook\FacebookApiException $e) { /** * You might wanna know what happened, so let's log the exception. * * Rendering entire bluescreen is kind of slow task, * so might wanna log only $e->getMessage(), it's up to you */ \Tracy\Debugger::log($e, 'facebook'); $this->flashMessage("Facebook authentication failed."); } $this->redirect('this'); }; return $dialog; }
/** * Loads first data source. */ private function load() { if ($this->lastResult === NULL) { $this->lastResult = $this->facebook->api($this->pathOrParams, $this->method, $this->params); } elseif ($this->hasNextPage()) { $this->lastResult = $this->facebook->api($this->getNextPath()); } }
/** @return \Kdyby\Facebook\Dialog\LoginDialog */ protected function createComponentFbLogin() { $dialog = $this->facebook->createDialog('login'); /** @var \Kdyby\Facebook\Dialog\LoginDialog $dialog */ $dialog->onResponse[] = function (\Kdyby\Facebook\Dialog\LoginDialog $dialog) { $fb = $dialog->getFacebook(); if (!$fb->getUser()) { $this->flashMessage("Facebook authentication failed."); return; } /** * If we get here, it means that the user was recognized * and we can call the Facebook API */ try { // $me = $fb->api('/me'); if (!($existing = $this->findUserByFacebookId($fb->getUser()))) { throw new \Exception('Uživatel není aktivní nebo neexistuje.'); /** * Variable $me contains all the public information about the user * including facebook id, name and email, if he allowed you to see it. */ // $existing = $this->usersModel->registerFromFacebook($fb->getUser(), $me); } /** * You should save the access token to database for later usage. * * You will need it when you'll want to call Facebook API, * when the user is not logged in to your website, * with the access token in his session. */ $this->updateFacebookAccessToken($fb->getUser(), $fb->getAccessToken()); $this->user->setExpiration('14 days', FALSE); /** * Nette\Security\User accepts not only textual credentials, * but even an identity instance! */ $this->user->login(new \Nette\Security\Identity($existing->id, explode(';', $existing->role), $existing)); $this->flashMessage('Byl/a jste úspěšně přihlášen/a prostřednictvím Facebooku jako "' . $existing->username . '"', 'success'); $this->redirect('Homepage:'); } catch (\Kdyby\Facebook\FacebookApiException $e) { /** * You might wanna know what happened, so let's log the exception. * * Rendering entire bluescreen is kind of slow task, * so might wanna log only $e->getMessage(), it's up to you */ \Tracy\Debugger::log($e, 'facebook'); $this->flashMessage("Facebook authentication failed hard."); } catch (\Nette\Application\AbortException $e) { throw $e; } catch (\Exception $e) { $this->flashMessage("Při přihlašování došlo k chybě.", "danger"); } $this->redirect('this'); }; return $dialog; }
/** * Returns collections of data from data source at one page. * * @return ArrayHash */ public function getNextPage() { if ($this->lastResult === NULL) { $this->lastResult = $this->facebook->api($this->pathOrParams, $this->method, $this->params); } elseif ($this->hasNextPage()) { $this->lastResult = $this->facebook->api($this->getNextPath()); } else { $this->lastResult = ArrayHash::from(array('data' => [])); } return $this->lastResult ? $this->lastResult->data : ArrayHash::from(array('data' => [])); }
/** * @param array $params * @return NULL|ArrayHash */ public function getPermissions(array $params = array()) { $params = array_merge($params, array('access_token' => $this->facebook->getAccessToken())); try { $response = $this->facebook->api("/{$this->profileId}/permissions", 'GET', $params); if ($response && !empty($response->data[0])) { return ArrayHash::from($response->data[0]); } } catch (FacebookApiException $e) { return NULL; } }
/** * Analyzes the supplied result to see if it was thrown * because the access token is no longer valid. If that is * the case, then we destroy the session. * * @param $result array A record storing the error message returned by a failed API call. * @throws Facebook\FacebookApiException */ protected function resolveAPIException($result) { $e = new Facebook\FacebookApiException($result); switch ($e->getType()) { case 'OAuthException': // OAuth 2.0 Draft 00 style // OAuth 2.0 Draft 00 style case 'invalid_token': // OAuth 2.0 Draft 10 style // OAuth 2.0 Draft 10 style case 'Exception': // REST server errors are just Exceptions if ($this->apiErrorRequiresSessionDestroy($e->getMessage())) { $this->fb->destroySession(); } break; } return $e; }
/** * @param array $params * @return NULL|ArrayHash */ public function getPermissions(array $params = array()) { $params = array_merge($params, array('access_token' => $this->facebook->getAccessToken())); try { $response = $this->facebook->api("/{$this->profileId}/permissions", 'GET', $params); if ($response && !empty($response->data)) { $items = array(); if (isset($response->data[0]['permission'])) { foreach ($response->data as $permissionsItem) { $items[$permissionsItem->permission] = $permissionsItem->status === 'granted'; } } elseif (isset($response->data[0])) { $items = (array) $response->data[0]; } return ArrayHash::from($items); } } catch (FacebookApiException $e) { return NULL; } }
public function facebookRegistrationFormSucceeded($form, $values) { $fbUserId = $values->id; unset($values->id); try { if ($this->facebookUserModel->exists($fbUserId)) { $this->flashMessage("Uživatel s tímto FacebookID již je v systému zaregistrován.", "danger"); $this->redirect("in"); } else { $id = $this->model->insert($values); $this->model->update($id, array("enabled" => 1, "active" => 1)); $this->flashMessage("Uživatel byl úspěšně zaregistrován.", "success"); $this->facebookUserModel->add(array("user_id" => $id, "facebook_id" => $fbUserId, "username" => $values->firstname . " " . $values->lastname, "email" => $values->email)); $this->facebookUserModel->authenticate(array($this->facebook->getUser())); $this->facebookUserModel->updateToken($this->facebook->getUser(), $this->facebook->getAccessToken()); } } catch (Exception $e) { $this->flashMessage("Registrace uživatele se nepodařila.", "danger"); } $this->redirect(":Front:Homepage:default"); }
/** * @return \Kdyby\Facebook\Dialog\LoginDialog */ protected function createComponentFbLogin() { $dialog = $this->facebookClient->createDialog('login'); /** @var LoginDialog $dialog */ $dialog->onResponse[] = function (LoginDialog $dialog) { $fb = $dialog->getFacebook(); try { $me = $fb->api('/me?fields=name,first_name,last_name,email'); try { $this->user->login("facebook", array("email" => $me->email, "firstName" => $me->first_name, "lastName" => $me->last_name)); $this->notify("Uživatel se úspěšně přihlášil."); $this->getPresenter(true)->redirect("Homepage:"); } catch (AuthenticationException $e) { $this->notify("Uživateli se nepovedlo přihlásit přes službu Facebook. " . $e->getMessage()); $this->flashMessage($e->getMessage()); $this->getPresenter(true)->redirect("Homepage:"); } } catch (FacebookApiException $e) { $this['form']->addError("Uživateli se nepovedlo přihlásit přes službu Facebook. " . $e->getMessage()); } }; return $dialog; }
/** @return FacebookLoginDialog */ protected function createComponentFacebookLogin() { /** @var FacebookLoginDialog $dialog */ $dialog = $this->facebook->createDialog('login'); $presenter = $this; $dialog->onResponse[] = function (FacebookLoginDialog $dialog) use($presenter) { $fb = $dialog->getFacebook(); if (!$fb->getUser()) { $presenter->flashMessageLoginFailed('Facebook'); return; } try { $facebookMe = $fb->api('/me'); $facebookUserId = $fb->getUser(); $presenter->getUser()->login($presenter->usersFacade->authenticateUserFromFacebook($facebookUserId, $facebookMe)); $presenter->flashMessageLoginSuccess('Facebook'); } catch (FacebookApiException $e) { $presenter->flashMessageLoginFailed('Facebook'); $presenter->redirect('login'); } $presenter->finalRedirect(); }; return $dialog; }
/** * Synchronize user state with facebook. */ public function syncFacebookSession() { if ($this->facebook->getUser() && $this->isUserChangeState()) { $this->facebook->destroySession(); } }