From 22198ea495f1f597d20dd2084c38df0df45778f7 Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 20 Oct 2022 23:35:01 +0200 Subject: [PATCH] UserSession class [6] - Refactor src/Module/ files without DI --- src/Module/BaseNotifications.php | 6 +++--- src/Module/Contact/Conversations.php | 15 ++++++++++----- src/Module/Contact/Posts.php | 15 ++++++++++----- src/Module/Filer/RemoveTag.php | 11 +++++++---- src/Module/Magic.php | 17 ++++++++++------- src/Module/ParseUrl.php | 18 ++++++++++++++++-- src/Module/Security/PasswordTooLong.php | 13 ++++++++----- src/Module/Security/TwoFactor/Verify.php | 17 ++++++++++------- 8 files changed, 74 insertions(+), 38 deletions(-) diff --git a/src/Module/BaseNotifications.php b/src/Module/BaseNotifications.php index 9e7456c7b..a011961b6 100644 --- a/src/Module/BaseNotifications.php +++ b/src/Module/BaseNotifications.php @@ -28,7 +28,7 @@ use Friendica\BaseModule; use Friendica\Content\Pager; use Friendica\Core\L10n; use Friendica\Core\Renderer; -use Friendica\Core\Session; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Navigation\Notifications\ValueObject\FormattedNotify; use Friendica\Network\HTTPException\ForbiddenException; @@ -90,11 +90,11 @@ abstract class BaseNotifications extends BaseModule */ abstract public function getNotifications(); - public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - if (!Session::getLocalUser()) { + if (!$userSession->getLocalUserId()) { throw new ForbiddenException($this->t('Permission denied.')); } diff --git a/src/Module/Contact/Conversations.php b/src/Module/Contact/Conversations.php index a2f969c9c..838e499cc 100644 --- a/src/Module/Contact/Conversations.php +++ b/src/Module/Contact/Conversations.php @@ -29,7 +29,7 @@ use Friendica\Content\Nav; use Friendica\Content\Widget; use Friendica\Core\L10n; use Friendica\Core\Protocol; -use Friendica\Core\Session; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Theme; use Friendica\Model; use Friendica\Module\Contact; @@ -56,25 +56,30 @@ class Conversations extends BaseModule * @var LocalRelationship */ private $localRelationship; + /** + * @var IHandleUserSessions + */ + private $userSession; - public function __construct(L10n $l10n, LocalRelationship $localRelationship, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Page $page, Conversation $conversation, array $server, array $parameters = []) + public function __construct(L10n $l10n, LocalRelationship $localRelationship, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Page $page, Conversation $conversation, IHandleUserSessions $userSession, $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->page = $page; $this->conversation = $conversation; $this->localRelationship = $localRelationship; + $this->userSession = $userSession; } protected function content(array $request = []): string { - if (!Session::getLocalUser()) { + if (!$this->userSession->getLocalUserId()) { return Login::form($_SERVER['REQUEST_URI']); } // Backward compatibility: Ensure to use the public contact when the user contact is provided // Remove by version 2022.03 - $data = Model\Contact::getPublicAndUserContactID(intval($this->parameters['id']), Session::getLocalUser()); + $data = Model\Contact::getPublicAndUserContactID(intval($this->parameters['id']), $this->userSession->getLocalUserId()); if (empty($data)) { throw new NotFoundException($this->t('Contact not found.')); } @@ -89,7 +94,7 @@ class Conversations extends BaseModule throw new NotFoundException($this->t('Contact not found.')); } - $localRelationship = $this->localRelationship->getForUserContact(Session::getLocalUser(), $contact['id']); + $localRelationship = $this->localRelationship->getForUserContact($this->userSession->getLocalUserId(), $contact['id']); if ($localRelationship->rel === Model\Contact::SELF) { $this->baseUrl->redirect('profile/' . $contact['nick']); } diff --git a/src/Module/Contact/Posts.php b/src/Module/Contact/Posts.php index 78cd42b1a..c5a6da0d1 100644 --- a/src/Module/Contact/Posts.php +++ b/src/Module/Contact/Posts.php @@ -28,7 +28,7 @@ use Friendica\Content\Nav; use Friendica\Content\Widget; use Friendica\Core\L10n; use Friendica\Core\Protocol; -use Friendica\Core\Session; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\DBA; use Friendica\Model; use Friendica\Module\Contact; @@ -51,24 +51,29 @@ class Posts extends BaseModule * @var App\Page */ private $page; + /** + * @var IHandleUserSessions + */ + private $userSession; - public function __construct(L10n $l10n, LocalRelationship $localRelationship, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Page $page, array $server, array $parameters = []) + public function __construct(L10n $l10n, LocalRelationship $localRelationship, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Page $page, IHandleUserSessions $userSession, $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->localRelationship = $localRelationship; $this->page = $page; + $this->userSession = $userSession; } protected function content(array $request = []): string { - if (!Session::getLocalUser()) { + if (!$this->userSession->getLocalUserId()) { return Login::form($_SERVER['REQUEST_URI']); } // Backward compatibility: Ensure to use the public contact when the user contact is provided // Remove by version 2022.03 - $data = Model\Contact::getPublicAndUserContactID(intval($this->parameters['id']), Session::getLocalUser()); + $data = Model\Contact::getPublicAndUserContactID(intval($this->parameters['id']), $this->userSession->getLocalUserId()); if (empty($data)) { throw new NotFoundException($this->t('Contact not found.')); } @@ -83,7 +88,7 @@ class Posts extends BaseModule throw new NotFoundException($this->t('Contact not found.')); } - $localRelationship = $this->localRelationship->getForUserContact(Session::getLocalUser(), $contact['id']); + $localRelationship = $this->localRelationship->getForUserContact($this->userSession->getLocalUserId(), $contact['id']); if ($localRelationship->rel === Model\Contact::SELF) { $this->baseUrl->redirect('profile/' . $contact['nick']); } diff --git a/src/Module/Filer/RemoveTag.php b/src/Module/Filer/RemoveTag.php index b940e6983..fccc90f9d 100644 --- a/src/Module/Filer/RemoveTag.php +++ b/src/Module/Filer/RemoveTag.php @@ -24,7 +24,7 @@ namespace Friendica\Module\Filer; use Friendica\App; use Friendica\BaseModule; use Friendica\Core\L10n; -use Friendica\Core\Session; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Model\Post; @@ -41,12 +41,15 @@ class RemoveTag extends BaseModule { /** @var SystemMessages */ private $systemMessages; + /** @var IHandleUserSessions */ + private $userSession; - public function __construct(SystemMessages $systemMessages, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + public function __construct(SystemMessages $systemMessages, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, array $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->systemMessages = $systemMessages; + $this->userSession = $userSession; } protected function post(array $request = []) @@ -56,7 +59,7 @@ class RemoveTag extends BaseModule protected function content(array $request = []): string { - if (!Session::getLocalUser()) { + if (!$this->userSession->getLocalUserId()) { throw new HTTPException\ForbiddenException(); } @@ -108,7 +111,7 @@ class RemoveTag extends BaseModule return 404; } - if (!Post\Category::deleteFileByURIId($item['uri-id'], Session::getLocalUser(), $type, $term)) { + if (!Post\Category::deleteFileByURIId($item['uri-id'], $this->userSession->getLocalUserId(), $type, $term)) { $this->systemMessages->addNotice($this->l10n->t('Item was not removed')); return 500; } diff --git a/src/Module/Magic.php b/src/Module/Magic.php index 42eae695e..c300e5971 100644 --- a/src/Module/Magic.php +++ b/src/Module/Magic.php @@ -24,7 +24,7 @@ namespace Friendica\Module; use Friendica\App; use Friendica\BaseModule; use Friendica\Core\L10n; -use Friendica\Core\Session; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Database\Database; use Friendica\Model\Contact; @@ -50,14 +50,17 @@ class Magic extends BaseModule protected $dba; /** @var ICanSendHttpRequests */ protected $httpClient; + /** @var IHandleUserSessions */ + protected $userSession; - public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Database $dba, ICanSendHttpRequests $httpClient, array $server, array $parameters = []) + public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Database $dba, ICanSendHttpRequests $httpClient, IHandleUserSessions $userSession, $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->app = $app; - $this->dba = $dba; - $this->httpClient = $httpClient; + $this->app = $app; + $this->dba = $dba; + $this->httpClient = $httpClient; + $this->userSession = $userSession; } protected function rawContent(array $request = []) @@ -91,8 +94,8 @@ class Magic extends BaseModule } // OpenWebAuth - if (Session::getLocalUser() && $owa) { - $user = User::getById(Session::getLocalUser()); + if ($this->userSession->getLocalUserId() && $owa) { + $user = User::getById($this->userSession->getLocalUserId()); // Extract the basepath // NOTE: we need another solution because this does only work diff --git a/src/Module/ParseUrl.php b/src/Module/ParseUrl.php index 91d09240b..fc096bbd5 100644 --- a/src/Module/ParseUrl.php +++ b/src/Module/ParseUrl.php @@ -21,19 +21,33 @@ namespace Friendica\Module; +use Friendica\App; use Friendica\BaseModule; use Friendica\Content\Text\BBCode; use Friendica\Core\Hook; -use Friendica\Core\Session; +use Friendica\Core\L10n; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\System; use Friendica\Network\HTTPException\BadRequestException; use Friendica\Util; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; class ParseUrl extends BaseModule { + /** @var IHandleUserSessions */ + protected $userSession; + + public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Session\Capability\IHandleUserSessions $userSession, $server, array $parameters = []) + { + parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->userSession = $userSession; + } + protected function rawContent(array $request = []) { - if (!Session::isAuthenticated()) { + if (!$this->userSession->isAuthenticated()) { throw new \Friendica\Network\HTTPException\ForbiddenException(); } diff --git a/src/Module/Security/PasswordTooLong.php b/src/Module/Security/PasswordTooLong.php index 32008f048..433144419 100644 --- a/src/Module/Security/PasswordTooLong.php +++ b/src/Module/Security/PasswordTooLong.php @@ -24,7 +24,7 @@ namespace Friendica\Module\Security; use Friendica\App; use Friendica\Core\L10n; use Friendica\Core\Renderer; -use Friendica\Core\Session; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\DBA; use Friendica\Model\User; use Friendica\Module\Response; @@ -36,12 +36,15 @@ class PasswordTooLong extends \Friendica\BaseModule { /** @var SystemMessages */ private $sysmsg; + /** @var IHandleUserSessions */ + private $userSession; - public function __construct(SystemMessages $sysmsg, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + public function __construct(SystemMessages $sysmsg, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->sysmsg = $sysmsg; + $this->sysmsg = $sysmsg; + $this->userSession = $userSession; } protected function post(array $request = []) @@ -55,13 +58,13 @@ class PasswordTooLong extends \Friendica\BaseModule } // check if the old password was supplied correctly before changing it to the new value - User::getIdFromPasswordAuthentication(Session::getLocalUser(), $request['password_current']); + User::getIdFromPasswordAuthentication($this->userSession->getLocalUserId(), $request['password_current']); if (strlen($request['password_current']) <= 72) { throw new \Exception($this->l10n->t('Password does not need changing.')); } - $result = User::updatePassword(Session::getLocalUser(), $newpass); + $result = User::updatePassword($this->userSession->getLocalUserId(), $newpass); if (!DBA::isResult($result)) { throw new \Exception($this->l10n->t('Password update failed. Please try again.')); } diff --git a/src/Module/Security/TwoFactor/Verify.php b/src/Module/Security/TwoFactor/Verify.php index 93a9fd4de..efd7e2c73 100644 --- a/src/Module/Security/TwoFactor/Verify.php +++ b/src/Module/Security/TwoFactor/Verify.php @@ -26,8 +26,8 @@ use Friendica\BaseModule; use Friendica\Core\L10n; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\Renderer; -use Friendica\Core\Session; use Friendica\Core\Session\Capability\IHandleSessions; +use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Module\Response; use Friendica\Util\Profiler; use PragmaRX\Google2FA\Google2FA; @@ -47,18 +47,21 @@ class Verify extends BaseModule protected $session; /** @var IManagePersonalConfigValues */ protected $pConfig; + /** @var IHandleUserSessions */ + protected $userSession; - public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, IHandleSessions $session, array $server, array $parameters = []) + public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, IHandleSessions $session, IHandleUserSessions $userSession, $server, array $parameters = []) { parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - $this->session = $session; - $this->pConfig = $pConfig; + $this->session = $session; + $this->pConfig = $pConfig; + $this->userSession = $userSession; } protected function post(array $request = []) { - if (!Session::getLocalUser()) { + if (!$this->userSession->getLocalUserId()) { return; } @@ -67,7 +70,7 @@ class Verify extends BaseModule $code = $request['verify_code'] ?? ''; - $valid = (new Google2FA())->verifyKey($this->pConfig->get(Session::getLocalUser(), '2fa', 'secret'), $code); + $valid = (new Google2FA())->verifyKey($this->pConfig->get($this->userSession->getLocalUserId(), '2fa', 'secret'), $code); // The same code can't be used twice even if it's valid if ($valid && $this->session->get('2fa') !== $code) { @@ -82,7 +85,7 @@ class Verify extends BaseModule protected function content(array $request = []): string { - if (!Session::getLocalUser()) { + if (!$this->userSession->getLocalUserId()) { $this->baseUrl->redirect(); }