From 94b40093977423514190dbd2416b8df9b3bfcb3a Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 6 Nov 2024 13:06:54 +0100 Subject: [PATCH] Improve code style in Security\Authentication --- src/Security/Authentication.php | 60 ++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/src/Security/Authentication.php b/src/Security/Authentication.php index ee7005936c..9e1b7956a6 100644 --- a/src/Security/Authentication.php +++ b/src/Security/Authentication.php @@ -9,6 +9,9 @@ namespace Friendica\Security; use Exception; use Friendica\App; +use Friendica\App\BaseURL; +use Friendica\App\Mode; +use Friendica\App\Request; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Hook; @@ -25,6 +28,7 @@ use LightOpenID; use Friendica\Core\L10n; use Friendica\Core\Worker; use Friendica\Model\Contact; +use Friendica\Model\User\Cookie; use Friendica\Util\Strings; use Psr\Log\LoggerInterface; @@ -35,9 +39,9 @@ class Authentication { /** @var IManageConfigValues */ private $config; - /** @var App\Mode */ + /** @var Mode */ private $mode; - /** @var App\BaseURL */ + /** @var BaseURL */ private $baseUrl; /** @var L10n */ private $l10n; @@ -45,7 +49,7 @@ class Authentication private $dba; /** @var LoggerInterface */ private $logger; - /** @var User\Cookie */ + /** @var Cookie */ private $cookie; /** @var IHandleUserSessions */ private $session; @@ -70,18 +74,28 @@ class Authentication * Authentication constructor. * * @param IManageConfigValues $config - * @param App\Mode $mode - * @param App\BaseURL $baseUrl + * @param Mode $mode + * @param BaseURL $baseUrl * @param L10n $l10n * @param Database $dba * @param LoggerInterface $logger - * @param User\Cookie $cookie + * @param Cookie $cookie * @param IHandleUserSessions $session * @param IManagePersonalConfigValues $pConfig - * @param App\Request $request + * @param Request $request */ - public function __construct(IManageConfigValues $config, App\Mode $mode, App\BaseURL $baseUrl, L10n $l10n, Database $dba, LoggerInterface $logger, User\Cookie $cookie, IHandleUserSessions $session, IManagePersonalConfigValues $pConfig, App\Request $request) - { + public function __construct( + IManageConfigValues $config, + Mode $mode, + BaseURL $baseUrl, + L10n $l10n, + Database $dba, + LoggerInterface $logger, + Cookie $cookie, + IHandleUserSessions $session, + IManagePersonalConfigValues $pConfig, + Request $request + ) { $this->config = $config; $this->mode = $mode; $this->baseUrl = $baseUrl; @@ -97,12 +111,12 @@ class Authentication /** * Tries to auth the user from the cookie or session * - * @param App $a The Friendica Application context + * @param App $app The Friendica Application context * * @throws HttpException\InternalServerErrorException In case of Friendica internal exceptions * @throws Exception In case of general exceptions (like SQL Grammar) */ - public function withSession(App $a) + public function withSession(App $app) { // When the "Friendica" cookie is set, take the value to authenticate and renew the cookie. if ($this->cookie->get('uid')) { @@ -133,7 +147,7 @@ class Authentication // Do the authentication if not done by now if (!$this->session->isAuthenticated()) { - $this->setForUser($a, $user); + $this->setForUser($app, $user); if ($this->config->get('system', 'paranoia')) { $this->session->set('addr', $this->cookie->get('ip')); @@ -145,7 +159,7 @@ class Authentication if ($this->session->isVisitor()) { $contact = $this->dba->selectFirst('contact', ['id'], ['id' => $this->session->get('visitor_id')]); if ($this->dba->isResult($contact)) { - $a->setContactId($contact['id']); + $app->setContactId($contact['id']); } } @@ -179,7 +193,7 @@ class Authentication $this->baseUrl->redirect(); } - $this->setForUser($a, $user); + $this->setForUser($app, $user); } } @@ -218,7 +232,7 @@ class Authentication /** * Attempts to authenticate using login/password * - * @param App $a The Friendica Application context + * @param App $app The Friendica Application context * @param string $username * @param string $password Clear password * @param bool $remember Whether to set the session remember flag @@ -230,7 +244,7 @@ class Authentication * @throws HTTPException\MovedPermanentlyException * @throws HTTPException\TemporaryRedirectException */ - public function withPassword(App $a, string $username, string $password, bool $remember, string $return_path = '') + public function withPassword(App $app, string $username, string $password, bool $remember, string $return_path = '') { $record = null; @@ -271,7 +285,7 @@ class Authentication $return_path = '/security/password_too_long?' . http_build_query(['return_path' => $return_path]); } - $this->setForUser($a, $record, true, true); + $this->setForUser($app, $record, true, true); $this->baseUrl->redirect($return_path); } @@ -279,7 +293,7 @@ class Authentication /** * Sets the provided user's authenticated session * - * @param App $a The Friendica application context + * @param App $app The Friendica application context * @param array $user_record The current "user" record * @param bool $login_initial * @param bool $interactive @@ -293,7 +307,7 @@ class Authentication * @throws HTTPException\InternalServerErrorException In case of Friendica specific exceptions * */ - public function setForUser(App $a, array $user_record, bool $login_initial = false, bool $interactive = false, bool $refresh_login = true) + public function setForUser(App $app, array $user_record, bool $login_initial = false, bool $interactive = false, bool $refresh_login = true) { $my_url = $this->baseUrl . '/profile/' . $user_record['nickname']; @@ -315,12 +329,12 @@ class Authentication $this->session->set('new_member', time() < ($member_since + (60 * 60 * 24 * 14))); if (strlen($user_record['timezone'])) { - $a->setTimeZone($user_record['timezone']); + $app->setTimeZone($user_record['timezone']); } $contact = $this->dba->selectFirst('contact', ['id'], ['uid' => $user_record['uid'], 'self' => true]); if ($this->dba->isResult($contact)) { - $a->setContactId($contact['id']); + $app->setContactId($contact['id']); $this->session->set('cid', $contact['id']); } @@ -442,10 +456,10 @@ class Authentication if (Strings::compareLink($this->session->get('visitor_home') ?: '', $url)) { return; } - + $this->session->set('my_url', $url); $this->session->set('authenticated', 0); - + $remote_contact = Contact::getByURL($url, false, ['subscribe']); if (!empty($remote_contact['subscribe'])) { $this->session->set('remote_comment', $remote_contact['subscribe']);