Skip to content

Commit

Permalink
Improve login/logout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Aug 8, 2017
1 parent 9685ab4 commit 6e511d0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
30 changes: 23 additions & 7 deletions system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Grav\Common\Page\Medium\Medium;
use Grav\Common\Page\Page;
use Grav\Common\User\Authentication;
use Grav\Common\User\User;
use RocketTheme\Toolbox\DI\Container;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\Event\EventDispatcher;
Expand Down Expand Up @@ -217,26 +218,41 @@ public function redirectLangSafe($route, $code = null)
}
}

public function login(array $credentials, array $options)
/**
* Login user.
*
* @param array $credentials
* @param array $options
* @return User
*/
public function login(array $credentials, array $options = [])
{
if (isset($this['user'])) {
return false;
if (isset($this['user']) && $this['user']->authenticated) {
return null;
}

unset($this['user']);

$user = Authentication::login($credentials, $options);
if ($user) {
$this['user'] = $user;
}

return $user !== null;
return $user;
}

public function logout()
/**
* Logout user.
*
* @param array $options
*/
public function logout(array $options = [])
{
if (isset($this['user'])) {
Authentication::logout($this['user']);
$user = Authentication::logout($this['user'], $options);

unset($this['user']);
$this['user'] = $user;
}
}

Expand Down Expand Up @@ -451,7 +467,7 @@ protected function registerService($serviceKey, $serviceClass)
*/
public function fallbackUrl($path)
{
$this->fireEvent('onPageFallBackUrl');
$this->fireEvent('onPageFallBackUrl');

/** @var Uri $uri */
$uri = $this['uri'];
Expand Down
57 changes: 36 additions & 21 deletions system/src/Grav/Common/User/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@

use Grav\Common\Grav;
use Grav\Common\User\Events\UserLoginEvent;
use RocketTheme\Toolbox\Event\Event;

abstract class Authentication
{
/**
* Login user.
*
* @param array $credentials
* @param array $options
* @return User|null
* @return User
*/
public static function login(array $credentials, array $options)
public static function login(array $credentials, array $options = [])
{
$grav = Grav::instance();

Expand All @@ -28,44 +29,58 @@ public static function login(array $credentials, array $options)
'options' => $options
];

$event = new UserLoginEvent($eventOptions);

// Attempt to authenticate the user.
$event = new UserLoginEvent($eventOptions);
$grav->fireEvent('onUserLoginAuthenticate', $event);

$event->removeCredentials();

// Allow plugins to prevent login after successful authentication.
if ($event['status'] === UserLoginEvent::AUTHENTICATION_SUCCESS) {
if ($event->status === UserLoginEvent::AUTHENTICATION_SUCCESS) {
$event = new UserLoginEvent($event->toArray());
$grav->fireEvent('onUserLoginAuthorize', $event);
}

// Allow plugins to log errors or do other tasks on failure.
if ($event['status'] !== UserLoginEvent::AUTHENTICATION_SUCCESS) {
if ($event->status !== UserLoginEvent::AUTHENTICATION_SUCCESS) {
// Allow plugins to log errors or do other tasks on failure.
$event = new UserLoginEvent($event->toArray());
$grav->fireEvent('onUserLoginFailure', $event);

return null;
}
$event->user->authenticated = false;

if (empty($event['user']->authenticated)) {
throw new \RuntimeException('Login: User object has not been authenticated!');
}
} else {
// User has been logged in, let plugins know.
$event = new UserLoginEvent($event->toArray());
$grav->fireEvent('onUserLogin', $event);

// User has been logged in, let plugins know.
$grav->fireEvent('onUserLogin', $event);
$event->user->authenticated = true;
}

return $event['user'];
return $event->user;
}

public static function logout($user)
/**
* Logout user.
*
* @param User $user
* @param array $options
* @return User
*/
public static function logout(User $user, array $options = [])
{
$grav = Grav::instance();

$event = new Event;
$event->user = $user;
$eventOptions = [
'user' => $user,
'options' => $options
];

$event = new UserLoginEvent($eventOptions);

// Logout the user.
$grav->fireEvent('onUserLogout', $event);

$event->user->authenticated = false;

return $event->user;
}

/**
Expand Down
17 changes: 8 additions & 9 deletions system/src/Grav/Common/User/Events/UserLoginEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*
* @property int $status
* @property array $credentials
* @property string $authorize
* @property array $options
* @property User $user
* @property string $message
Expand Down Expand Up @@ -58,20 +59,18 @@ public function __construct(array $items = [])
{
$defaults = [
'credentials' => ['username' => '', 'password' => ''],
'options' => ['remember_me' => false],
'options' => [],
'authorize' => 'site.login',
'status' => static::AUTHENTICATION_UNDEFINED,
'user' => null,
'message' => ''
];

parent::__construct(array_merge_recursive($defaults, $items));
parent::__construct(array_replace_recursive($defaults, $items));

$username = $this['credentials']['username'];
$this['user'] = $username ? User::load($username, false) : new User;
}

public function removeCredentials()
{
unset($this['credentials']);
if (!isset($this->user)) {
$username = $this->credentials['username'];
$this->user = $username ? User::load($username, false) : new User;
}
}
}

0 comments on commit 6e511d0

Please sign in to comment.