Skip to content

Commit 190f0f0

Browse files
committed
[core] User::get_anonymous() function
1 parent 95880a1 commit 190f0f0

File tree

7 files changed

+12
-8
lines changed

7 files changed

+12
-8
lines changed

core/Testing/ShimmiePHPUnitTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ protected static function log_in_as_user(): void
238238

239239
protected static function log_out(): void
240240
{
241-
send_event(new UserLoginEvent(User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID))));
241+
send_event(new UserLoginEvent(User::get_anonymous()));
242242
}
243243

244244
// post things

core/User/User.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ public static function by_name_and_pass(string $name, string $pass): User
182182
}
183183
}
184184

185+
public static function get_anonymous(): User
186+
{
187+
return User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID));
188+
}
185189

186190
/* useful user object functions start here */
187191

core/Util/util.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function _get_user(): User
331331
$my_user = User::by_session(Ctx::$page->get_cookie("user"), Ctx::$page->get_cookie("session"));
332332
}
333333
if (is_null($my_user)) {
334-
$my_user = User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID));
334+
$my_user = User::get_anonymous();
335335
}
336336

337337
return $my_user;

ext/danbooru_api/main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private function authenticate_user(PageRequestEvent $event): void
7878
$pass = $event->POST->req('password');
7979
Ctx::$user = User::by_name_and_pass($name, $pass);
8080
} catch (UserNotFound $e) {
81-
Ctx::$user = User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID));
81+
Ctx::$user = User::get_anonymous();
8282
}
8383
send_event(new UserLoginEvent(Ctx::$user));
8484
}

ext/ouroboros_api/main.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,15 @@ private function tryAuth(): void
556556
//Auth by session data from query
557557
$name = $_REQUEST['user'];
558558
$session = $_REQUEST['session'];
559-
$user = User::by_session($name, $session) ?? User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID));
559+
$user = User::by_session($name, $session) ?? User::get_anonymous();
560560
send_event(new UserLoginEvent($user));
561561
} elseif (isset($_COOKIE[SysConfig::getCookiePrefix() . '_' . 'session']) &&
562562
isset($_COOKIE[SysConfig::getCookiePrefix() . '_' . 'user'])
563563
) {
564564
//Auth by session data from cookies
565565
$session = $_COOKIE[SysConfig::getCookiePrefix() . '_' . 'session'];
566566
$user = $_COOKIE[SysConfig::getCookiePrefix() . '_' . 'user'];
567-
$user = User::by_session($user, $session) ?? User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID));
567+
$user = User::by_session($user, $session) ?? User::get_anonymous();
568568
send_event(new UserLoginEvent($user));
569569
}
570570
}

ext/user/main.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static function login(string $username, string $password): LoginResult
9292
);
9393
} catch (UserNotFound $ex) {
9494
return new LoginResult(
95-
User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID)),
95+
User::get_anonymous(),
9696
null,
9797
"No user found"
9898
);
@@ -111,7 +111,7 @@ public static function create_user(string $username, string $password1, string $
111111
);
112112
} catch (UserCreationException $ex) {
113113
return new LoginResult(
114-
User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID)),
114+
User::get_anonymous(),
115115
null,
116116
$ex->getMessage()
117117
);

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
Ctx::setEventBus(new EventBus());
5151
send_event(new DatabaseUpgradeEvent());
5252
send_event(new InitExtEvent());
53-
Ctx::setUser(User::by_id(Ctx::$config->req(UserAccountsConfig::ANON_ID)));
53+
Ctx::setUser(User::get_anonymous());
5454
$userPage = new UserPage();
5555
$userPage->onUserCreation(new UserCreationEvent("demo", "demo", "demo", "[email protected]", false));
5656
$userPage->onUserCreation(new UserCreationEvent("test", "test", "test", "[email protected]", false));

0 commit comments

Comments
 (0)