From cf8ed3b134eb8d122a991e5cc8ba8c4da549cb30 Mon Sep 17 00:00:00 2001 From: matthew Date: Thu, 4 Jul 2019 12:48:33 -0500 Subject: [PATCH] Added UserLoginEvent --- core/event.php | 2 ++ ext/danbooru_api/main.php | 1 + ext/ouroboros_api/main.php | 2 ++ ext/user/main.php | 10 +++++++++- ext/user_config/main.php | 12 +++++++++--- index.php | 2 +- tests/bootstrap.php | 6 +++--- 7 files changed, 27 insertions(+), 8 deletions(-) diff --git a/core/event.php b/core/event.php index c9064c3e..a0e25176 100644 --- a/core/event.php +++ b/core/event.php @@ -191,6 +191,8 @@ class CommandEvent extends Event $user = User::by_name($args[++$i]); if (is_null($user)) { die("Unknown user"); + } else { + send_event(new UserLoginEvent($user)); } break; case '-q': diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php index 75a4a535..a0db4b18 100644 --- a/ext/danbooru_api/main.php +++ b/ext/danbooru_api/main.php @@ -58,6 +58,7 @@ class DanbooruApi extends Extension } else { $user = User::by_id($config->get_int("anon_id", 0)); } + send_event(new UserLoginEvent($user)); } } diff --git a/ext/ouroboros_api/main.php b/ext/ouroboros_api/main.php index 9b012b13..7bf969ea 100644 --- a/ext/ouroboros_api/main.php +++ b/ext/ouroboros_api/main.php @@ -750,6 +750,7 @@ class OuroborosAPI extends Extension } else { $user = User::by_id($config->get_int("anon_id", 0)); } + send_event(new UserLoginEvent($user)); } elseif (isset($_COOKIE[$config->get_string('cookie_prefix', 'shm') . '_' . 'session']) && isset($_COOKIE[$config->get_string('cookie_prefix', 'shm') . '_' . 'user']) ) { @@ -762,6 +763,7 @@ class OuroborosAPI extends Extension } else { $user = User::by_id($config->get_int("anon_id", 0)); } + send_event(new UserLoginEvent($user)); } } diff --git a/ext/user/main.php b/ext/user/main.php index 91147c28..4ea55baf 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -27,6 +27,12 @@ class UserPage extends Extension $config->set_default_bool("login_tac_bbcode", true); } + public function onUserLogin(UserLoginEvent $event) + { + global $user; + $user = $event->user; + } + public function onPageRequest(PageRequestEvent $event) { global $config, $database, $page, $user; @@ -347,7 +353,7 @@ class UserPage extends Extension $duser = User::by_name_and_pass($name, $pass); if (!is_null($duser)) { - $user = $duser; + send_event(new UserLoginEvent($duser)); $this->set_login_cookie($duser->name, $pass); $page->set_mode(PageMode::REDIRECT); @@ -460,6 +466,8 @@ class UserPage extends Extension $uid = $database->get_last_insert_id('users_id_seq'); $user = User::by_name($event->username); $user->set_password($event->password); + send_event(new UserLoginEvent($user)); + log_info("user", "Created User #$uid ({$event->username})"); } diff --git a/ext/user_config/main.php b/ext/user_config/main.php index ed728101..7fd24c34 100644 --- a/ext/user_config/main.php +++ b/ext/user_config/main.php @@ -6,6 +6,10 @@ * Visibility: admin */ +/** @var $user_config Config */ +global $user_config; + + // The user object doesn't exist until after database setup operations and the first wave of InitExtEvents, // so we can't reliably access this data until then. This event is triggered by the system after all of that is done. class InitUserConfigEvent extends Event @@ -13,9 +17,10 @@ class InitUserConfigEvent extends Event public $user; public $user_config; - public function __construct(User $user) + public function __construct(User $user, Config $user_config) { $this->user = $user; + $this->user_config = $user_config; } } @@ -32,11 +37,12 @@ class UserConfig extends Extension } } - public function onInitUserConfig(InitUserConfigEvent $event) { + public function onUserLogin(UserLoginEvent $event) + { global $database, $user_config; $user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id); - $event->user_config = $user_config; + send_event(new InitUserConfigEvent($event->user, $user_config)); } private function install(): void diff --git a/index.php b/index.php index 67855129..0001f5d5 100644 --- a/index.php +++ b/index.php @@ -92,7 +92,7 @@ try { // start the page generation waterfall $user = _get_user(); - send_event(new InitUserConfigEvent($user)); + send_event(new UserLoginEvent($user)); if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') { send_event(new CommandEvent($argv)); } else { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a312076b..dfb761c8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -133,7 +133,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase global $user; $user = User::by_name('demo'); $this->assertNotNull($user); - send_event(new InitUserConfigEvent($user)); + send_event(new UserLoginEvent($user)); } protected function log_in_as_user() @@ -141,7 +141,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase global $user; $user = User::by_name('test'); $this->assertNotNull($user); - send_event(new InitUserConfigEvent($user)); + send_event(new UserLoginEvent($user)); } protected function log_out() @@ -149,7 +149,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase global $user, $config; $user = User::by_id($config->get_int("anon_id", 0)); $this->assertNotNull($user); - send_event(new InitUserConfigEvent($user)); + send_event(new UserLoginEvent($user)); } // post things