From 66e02c5e3a3c012787b37009b3aad2884d00941a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 24 Jun 2025 07:53:42 +0000 Subject: [PATCH] A bit of cleanup for account functions --- boot.php | 20 ---------------- include/account.php | 36 +++++++++++++++++++++++++---- tests/unit/includes/AccountTest.php | 21 ++++++++++++++++- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/boot.php b/boot.php index 04e56a95a..f2aa21ac5 100644 --- a/boot.php +++ b/boot.php @@ -1801,26 +1801,6 @@ function shutdown() { } -/** - * @brief Returns the entity id of locally logged in account or false. - * - * Returns numeric account_id if authenticated or 0. It is possible to be - * authenticated and not connected to a channel. - * - * @return int|bool account_id or false - */ -function get_account_id() { - if (isset($_SESSION['account_id'])) { - return intval($_SESSION['account_id']); - } - - if (App::$account) { - return intval(App::$account['account_id']); - } - - return false; -} - /** * @brief Returns the entity id (channel_id) of locally logged in channel or false. * diff --git a/include/account.php b/include/account.php index ad69b4d9a..e0b643d7b 100644 --- a/include/account.php +++ b/include/account.php @@ -17,10 +17,38 @@ require_once('include/crypto.php'); require_once('include/channel.php'); -function get_account_by_id($account_id) { - $r = q("select * from account where account_id = %d", - intval($account_id) - ); +/** + * Returns the id of a locally logged in account or false. + * + * Returns the numeric account id of the current session if authenticated, or + * false otherwise. + * + * @note It is possible to be authenticated, and not connected to a channel. + * + * @return int|false Numeric account id or false. + */ +function get_account_id(): int|false { + if (isset($_SESSION['account_id'])) { + return intval($_SESSION['account_id']); + } + + if (App::$account) { + return intval(App::$account['account_id']); + } + + return false; +} + +/** + * Get the account with the given id from the database. + * + * @param int $account_id The numeric id of the account to fetch. + * + * @return array|false An array containing the attributes of the requested + * account, or false if it could not be retreived. + */ +function get_account_by_id(int $account_id): array|false { + $r = q("select * from account where account_id = %d", $account_id); return (($r) ? $r[0] : false); } diff --git a/tests/unit/includes/AccountTest.php b/tests/unit/includes/AccountTest.php index 3978f9d04..66c761ef5 100644 --- a/tests/unit/includes/AccountTest.php +++ b/tests/unit/includes/AccountTest.php @@ -1,9 +1,28 @@ assertEquals(false, get_account_id(), 'get_account_id() should return false if not authenticated'); + + App::set_account(['account_id' => 36]); + $this->assertEquals(36, get_account_id(), 'get_account_id() should return account from global App object'); + + $_SESSION['account_id'] = 42; + $this->assertEquals(42, get_account_id(), 'get_account_id() should return the account from the session'); + } -class AccountTest extends Zotlabs\Tests\Unit\UnitTestCase { public function test_get_account_by_id_returns_existing_account() { $account = get_account_by_id(42); $this->assertNotFalse($account);