Added UserLoginEvent

This commit is contained in:
matthew 2019-07-04 12:48:33 -05:00 committed by Shish
parent 761834b6fa
commit cf8ed3b134
7 changed files with 27 additions and 8 deletions

View file

@ -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':

View file

@ -58,6 +58,7 @@ class DanbooruApi extends Extension
} else {
$user = User::by_id($config->get_int("anon_id", 0));
}
send_event(new UserLoginEvent($user));
}
}

View file

@ -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));
}
}

View file

@ -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})");
}

View file

@ -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

View file

@ -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 {

View file

@ -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