diff --git a/src/Kdyby/Facebook/DI/FacebookExtension.php b/src/Kdyby/Facebook/DI/FacebookExtension.php index f143e24d..0b4bb2c6 100644 --- a/src/Kdyby/Facebook/DI/FacebookExtension.php +++ b/src/Kdyby/Facebook/DI/FacebookExtension.php @@ -103,14 +103,20 @@ public function loadConfiguration() $apiClient->addSetup($this->prefix('@panel') . '::register', array('@self')); } + $builder->addDefinition($this->prefix('synchronizeUserFacebook')) + ->setClass('Kdyby\Facebook\SynchronizeUserFacebook') + ->addTag('run') + ->setAutowired(FALSE) + ->setInject(FALSE); + $builder->addDefinition($this->prefix('client')) ->setClass('Kdyby\Facebook\Facebook') ->setInject(FALSE); if ($config['clearAllWithLogout']) { $builder->getDefinition('user') - ->addSetup('$sl = ?; ?->onLoggedOut[] = function () use ($sl) { $sl->getService(?)->clearAll(); }', array( - '@container', '@self', $this->prefix('session') + ->addSetup('$sl = ?; ?->onLoggedOut[] = function () use ($sl) { $sl->getService(?)->syncFacebookSession(); }', array( + '@container', '@self', $this->prefix('synchronizeUserFacebook') )); } } diff --git a/src/Kdyby/Facebook/SessionStorage.php b/src/Kdyby/Facebook/SessionStorage.php index 290a0ce3..fed9b0f0 100644 --- a/src/Kdyby/Facebook/SessionStorage.php +++ b/src/Kdyby/Facebook/SessionStorage.php @@ -69,6 +69,32 @@ public function establishCSRFTokenState() + /** + * Login state, if change state from login to logout. + * + * @return bool + */ + public function checkLogout() + { + if ($this->session->login === TRUE) { + $this->session->login = FALSE; + return TRUE; + } + return FALSE; + } + + + + /** + * Remember user is login. + */ + public function login() + { + $this->session->login = TRUE; + } + + + /** * Stores the given ($key, $value) pair, so that future calls to * getPersistentData($key) return $value. This call may be in another request. diff --git a/src/Kdyby/Facebook/SynchronizeUserFacebook.php b/src/Kdyby/Facebook/SynchronizeUserFacebook.php new file mode 100644 index 00000000..8f627f4c --- /dev/null +++ b/src/Kdyby/Facebook/SynchronizeUserFacebook.php @@ -0,0 +1,59 @@ + + */ +class SynchronizeUserFacebook extends Nette\Object +{ + /** @var Nette\Security\IUserStorage */ + private $userStorage; + + /** @var SessionStorage */ + private $sessionStorage; + + /** @var Facebook */ + private $facebook; + + + public function __construct(Nette\Security\IUserStorage $userStorage, SessionStorage $sessionStorage, Facebook $facebook) + { + $this->userStorage = $userStorage; + $this->sessionStorage = $sessionStorage; + $this->facebook = $facebook; + $this->syncFacebookSession(); + } + + + /** + * Synchronize user state with facebook. + */ + public function syncFacebookSession() + { + if ($this->facebook->getUser() && $this->isUserChangeState()) { + $this->facebook->destroySession(); + } + } + + + /** @return bool */ + private function isUserChangeState() + { + if (!$this->userStorage->isAuthenticated()) { + return $this->sessionStorage->checkLogout(); + } + $this->sessionStorage->login(); + return FALSE; + } + +} diff --git a/tests/KdybyTests/Facebook/SessionStorage.phpt b/tests/KdybyTests/Facebook/SessionStorage.phpt index ab9848b5..57d9f5be 100644 --- a/tests/KdybyTests/Facebook/SessionStorage.phpt +++ b/tests/KdybyTests/Facebook/SessionStorage.phpt @@ -98,6 +98,16 @@ class SessionStorageTest extends KdybyTests\Facebook\FacebookTestCase Assert::false($this->session->state); } + + + public function testLoginLogout() + { + Assert::false($this->session->checkLogout()); + $this->session->login(); + Assert::true($this->session->checkLogout()); + Assert::false($this->session->checkLogout()); + } + } KdybyTests\run(new SessionStorageTest());