diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index d2a34ac92..b60a44448 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -14,6 +14,7 @@ use Grav\Common\Page\Medium\ImageMedium; 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; @@ -217,26 +218,41 @@ class Grav extends Container } } - 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; } } @@ -451,7 +467,7 @@ class Grav extends Container */ public function fallbackUrl($path) { - $this->fireEvent('onPageFallBackUrl'); + $this->fireEvent('onPageFallBackUrl'); /** @var Uri $uri */ $uri = $this['uri']; diff --git a/system/src/Grav/Common/User/Authentication.php b/system/src/Grav/Common/User/Authentication.php index ba0d78eda..9de431657 100644 --- a/system/src/Grav/Common/User/Authentication.php +++ b/system/src/Grav/Common/User/Authentication.php @@ -10,16 +10,17 @@ namespace Grav\Common\User; 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(); @@ -28,44 +29,58 @@ abstract class Authentication '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; + + } else { + // User has been logged in, let plugins know. + $event = new UserLoginEvent($event->toArray()); + $grav->fireEvent('onUserLogin', $event); + + $event->user->authenticated = true; } - if (empty($event['user']->authenticated)) { - throw new \RuntimeException('Login: User object has not been authenticated!'); - } - - // User has been logged in, let plugins know. - $grav->fireEvent('onUserLogin', $event); - - 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; } /** diff --git a/system/src/Grav/Common/User/Events/UserLoginEvent.php b/system/src/Grav/Common/User/Events/UserLoginEvent.php index add97aa65..d472470bc 100644 --- a/system/src/Grav/Common/User/Events/UserLoginEvent.php +++ b/system/src/Grav/Common/User/Events/UserLoginEvent.php @@ -17,6 +17,7 @@ use RocketTheme\Toolbox\Event\Event; * * @property int $status * @property array $credentials + * @property string $authorize * @property array $options * @property User $user * @property string $message @@ -58,20 +59,18 @@ class UserLoginEvent extends Event { $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; + } } }