UserSession class [6] - Refactor src/Module/ files without DI

This commit is contained in:
Philipp 2022-10-20 23:35:01 +02:00
parent bf39b5a948
commit 22198ea495
No known key found for this signature in database
GPG Key ID: 24A7501396EB5432
8 changed files with 74 additions and 38 deletions

View File

@ -28,7 +28,7 @@ use Friendica\BaseModule;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Navigation\Notifications\ValueObject\FormattedNotify; use Friendica\Navigation\Notifications\ValueObject\FormattedNotify;
use Friendica\Network\HTTPException\ForbiddenException; use Friendica\Network\HTTPException\ForbiddenException;
@ -90,11 +90,11 @@ abstract class BaseNotifications extends BaseModule
*/ */
abstract public function getNotifications(); 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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
if (!Session::getLocalUser()) { if (!$userSession->getLocalUserId()) {
throw new ForbiddenException($this->t('Permission denied.')); throw new ForbiddenException($this->t('Permission denied.'));
} }

View File

@ -29,7 +29,7 @@ use Friendica\Content\Nav;
use Friendica\Content\Widget; use Friendica\Content\Widget;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\Session; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\Theme; use Friendica\Core\Theme;
use Friendica\Model; use Friendica\Model;
use Friendica\Module\Contact; use Friendica\Module\Contact;
@ -56,25 +56,30 @@ class Conversations extends BaseModule
* @var LocalRelationship * @var LocalRelationship
*/ */
private $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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->page = $page; $this->page = $page;
$this->conversation = $conversation; $this->conversation = $conversation;
$this->localRelationship = $localRelationship; $this->localRelationship = $localRelationship;
$this->userSession = $userSession;
} }
protected function content(array $request = []): string protected function content(array $request = []): string
{ {
if (!Session::getLocalUser()) { if (!$this->userSession->getLocalUserId()) {
return Login::form($_SERVER['REQUEST_URI']); return Login::form($_SERVER['REQUEST_URI']);
} }
// Backward compatibility: Ensure to use the public contact when the user contact is provided // Backward compatibility: Ensure to use the public contact when the user contact is provided
// Remove by version 2022.03 // 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)) { if (empty($data)) {
throw new NotFoundException($this->t('Contact not found.')); throw new NotFoundException($this->t('Contact not found.'));
} }
@ -89,7 +94,7 @@ class Conversations extends BaseModule
throw new NotFoundException($this->t('Contact not found.')); 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) { if ($localRelationship->rel === Model\Contact::SELF) {
$this->baseUrl->redirect('profile/' . $contact['nick']); $this->baseUrl->redirect('profile/' . $contact['nick']);
} }

View File

@ -28,7 +28,7 @@ use Friendica\Content\Nav;
use Friendica\Content\Widget; use Friendica\Content\Widget;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\Session; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model; use Friendica\Model;
use Friendica\Module\Contact; use Friendica\Module\Contact;
@ -51,24 +51,29 @@ class Posts extends BaseModule
* @var App\Page * @var App\Page
*/ */
private $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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->localRelationship = $localRelationship; $this->localRelationship = $localRelationship;
$this->page = $page; $this->page = $page;
$this->userSession = $userSession;
} }
protected function content(array $request = []): string protected function content(array $request = []): string
{ {
if (!Session::getLocalUser()) { if (!$this->userSession->getLocalUserId()) {
return Login::form($_SERVER['REQUEST_URI']); return Login::form($_SERVER['REQUEST_URI']);
} }
// Backward compatibility: Ensure to use the public contact when the user contact is provided // Backward compatibility: Ensure to use the public contact when the user contact is provided
// Remove by version 2022.03 // 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)) { if (empty($data)) {
throw new NotFoundException($this->t('Contact not found.')); throw new NotFoundException($this->t('Contact not found.'));
} }
@ -83,7 +88,7 @@ class Posts extends BaseModule
throw new NotFoundException($this->t('Contact not found.')); 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) { if ($localRelationship->rel === Model\Contact::SELF) {
$this->baseUrl->redirect('profile/' . $contact['nick']); $this->baseUrl->redirect('profile/' . $contact['nick']);
} }

View File

@ -24,7 +24,7 @@ namespace Friendica\Module\Filer;
use Friendica\App; use Friendica\App;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Session; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Post; use Friendica\Model\Post;
@ -41,12 +41,15 @@ class RemoveTag extends BaseModule
{ {
/** @var SystemMessages */ /** @var SystemMessages */
private $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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->systemMessages = $systemMessages; $this->systemMessages = $systemMessages;
$this->userSession = $userSession;
} }
protected function post(array $request = []) protected function post(array $request = [])
@ -56,7 +59,7 @@ class RemoveTag extends BaseModule
protected function content(array $request = []): string protected function content(array $request = []): string
{ {
if (!Session::getLocalUser()) { if (!$this->userSession->getLocalUserId()) {
throw new HTTPException\ForbiddenException(); throw new HTTPException\ForbiddenException();
} }
@ -108,7 +111,7 @@ class RemoveTag extends BaseModule
return 404; 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')); $this->systemMessages->addNotice($this->l10n->t('Item was not removed'));
return 500; return 500;
} }

View File

@ -24,7 +24,7 @@ namespace Friendica\Module;
use Friendica\App; use Friendica\App;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Session; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Model\Contact; use Friendica\Model\Contact;
@ -50,14 +50,17 @@ class Magic extends BaseModule
protected $dba; protected $dba;
/** @var ICanSendHttpRequests */ /** @var ICanSendHttpRequests */
protected $httpClient; 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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->app = $app; $this->app = $app;
$this->dba = $dba; $this->dba = $dba;
$this->httpClient = $httpClient; $this->httpClient = $httpClient;
$this->userSession = $userSession;
} }
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
@ -91,8 +94,8 @@ class Magic extends BaseModule
} }
// OpenWebAuth // OpenWebAuth
if (Session::getLocalUser() && $owa) { if ($this->userSession->getLocalUserId() && $owa) {
$user = User::getById(Session::getLocalUser()); $user = User::getById($this->userSession->getLocalUserId());
// Extract the basepath // Extract the basepath
// NOTE: we need another solution because this does only work // NOTE: we need another solution because this does only work

View File

@ -21,19 +21,33 @@
namespace Friendica\Module; namespace Friendica\Module;
use Friendica\App;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\Session; use Friendica\Core\L10n;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Network\HTTPException\BadRequestException; use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Util; use Friendica\Util;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class ParseUrl extends BaseModule 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 = []) protected function rawContent(array $request = [])
{ {
if (!Session::isAuthenticated()) { if (!$this->userSession->isAuthenticated()) {
throw new \Friendica\Network\HTTPException\ForbiddenException(); throw new \Friendica\Network\HTTPException\ForbiddenException();
} }

View File

@ -24,7 +24,7 @@ namespace Friendica\Module\Security;
use Friendica\App; use Friendica\App;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Response; use Friendica\Module\Response;
@ -36,12 +36,15 @@ class PasswordTooLong extends \Friendica\BaseModule
{ {
/** @var SystemMessages */ /** @var SystemMessages */
private $sysmsg; 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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->sysmsg = $sysmsg; $this->sysmsg = $sysmsg;
$this->userSession = $userSession;
} }
protected function post(array $request = []) 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 // 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) { if (strlen($request['password_current']) <= 72) {
throw new \Exception($this->l10n->t('Password does not need changing.')); 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)) { if (!DBA::isResult($result)) {
throw new \Exception($this->l10n->t('Password update failed. Please try again.')); throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
} }

View File

@ -26,8 +26,8 @@ use Friendica\BaseModule;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Module\Response; use Friendica\Module\Response;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use PragmaRX\Google2FA\Google2FA; use PragmaRX\Google2FA\Google2FA;
@ -47,18 +47,21 @@ class Verify extends BaseModule
protected $session; protected $session;
/** @var IManagePersonalConfigValues */ /** @var IManagePersonalConfigValues */
protected $pConfig; 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); parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->session = $session; $this->session = $session;
$this->pConfig = $pConfig; $this->pConfig = $pConfig;
$this->userSession = $userSession;
} }
protected function post(array $request = []) protected function post(array $request = [])
{ {
if (!Session::getLocalUser()) { if (!$this->userSession->getLocalUserId()) {
return; return;
} }
@ -67,7 +70,7 @@ class Verify extends BaseModule
$code = $request['verify_code'] ?? ''; $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 // The same code can't be used twice even if it's valid
if ($valid && $this->session->get('2fa') !== $code) { if ($valid && $this->session->get('2fa') !== $code) {
@ -82,7 +85,7 @@ class Verify extends BaseModule
protected function content(array $request = []): string protected function content(array $request = []): string
{ {
if (!Session::getLocalUser()) { if (!$this->userSession->getLocalUserId()) {
$this->baseUrl->redirect(); $this->baseUrl->redirect();
} }