Skip to content
This repository has been archived by the owner on Mar 29, 2019. It is now read-only.

Check user is logout for inactivity than logout from facebook. #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Kdyby/Facebook/DI/FacebookExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
));
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/Kdyby/Facebook/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 59 additions & 0 deletions src/Kdyby/Facebook/SynchronizeUserFacebook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.md that was distributed with this source code.
*/

namespace Kdyby\Facebook;

use Nette;

/**
* @author Milan Matějček <[email protected]>
*/
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a nice name, a method that starts with is* modifies something

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this is better syncFacebookSession? Other is fixed.

{
if (!$this->userStorage->isAuthenticated()) {
return $this->sessionStorage->checkLogout();
}
$this->sessionStorage->login();
return FALSE;
}

}
10 changes: 10 additions & 0 deletions tests/KdybyTests/Facebook/SessionStorage.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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());