UserSession class [5] - Refactor src/Module/ files with DI

This commit is contained in:
Philipp 2022-10-20 22:59:12 +02:00
parent a729f3255d
commit eecc456e0c
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
78 changed files with 455 additions and 530 deletions

View file

@ -25,7 +25,6 @@ use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Module\Response;
use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
@ -52,11 +51,11 @@ class AppSpecific extends BaseSettings
$this->pConfig = $pConfig;
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
$verified = $this->pConfig->get(Session::getLocalUser(), '2fa', 'verified');
$verified = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
if (!$verified) {
$this->baseUrl->redirect('settings/2fa');
@ -70,7 +69,7 @@ class AppSpecific extends BaseSettings
protected function post(array $request = [])
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
@ -83,17 +82,17 @@ class AppSpecific extends BaseSettings
if (empty($description)) {
DI::sysmsg()->addNotice($this->t('App-specific password generation failed: The description is empty.'));
$this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
} elseif (AppSpecificPassword::checkDuplicateForUser(Session::getLocalUser(), $description)) {
} elseif (AppSpecificPassword::checkDuplicateForUser(DI::userSession()->getLocalUserId(), $description)) {
DI::sysmsg()->addNotice($this->t('App-specific password generation failed: This description already exists.'));
$this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
} else {
$this->appSpecificPassword = AppSpecificPassword::generateForUser(Session::getLocalUser(), $_POST['description'] ?? '');
$this->appSpecificPassword = AppSpecificPassword::generateForUser(DI::userSession()->getLocalUserId(), $_POST['description'] ?? '');
DI::sysmsg()->addInfo($this->t('New app-specific password generated.'));
}
break;
case 'revoke_all' :
AppSpecificPassword::deleteAllForUser(Session::getLocalUser());
AppSpecificPassword::deleteAllForUser(DI::userSession()->getLocalUserId());
DI::sysmsg()->addInfo($this->t('App-specific passwords successfully revoked.'));
$this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
break;
@ -103,7 +102,7 @@ class AppSpecific extends BaseSettings
if (!empty($_POST['revoke_id'])) {
self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
if (AppSpecificPassword::deleteForUser(Session::getLocalUser(), $_POST['revoke_id'])) {
if (AppSpecificPassword::deleteForUser(DI::userSession()->getLocalUserId(), $_POST['revoke_id'])) {
DI::sysmsg()->addInfo($this->t('App-specific password successfully revoked.'));
}
@ -113,13 +112,13 @@ class AppSpecific extends BaseSettings
protected function content(array $request = []): string
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return Login::form('settings/2fa/app_specific');
}
parent::content();
$appSpecificPasswords = AppSpecificPassword::getListForUser(Session::getLocalUser());
$appSpecificPasswords = AppSpecificPassword::getListForUser(DI::userSession()->getLocalUserId());
return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/app_specific.tpl'), [
'$form_security_token' => self::getFormSecurityToken('settings_2fa_app_specific'),

View file

@ -22,7 +22,6 @@
namespace Friendica\Module\Settings\TwoFactor;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Network\HTTPException\FoundException;
use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
@ -36,24 +35,24 @@ class Index extends BaseSettings
{
protected function post(array $request = [])
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
self::checkFormSecurityTokenRedirectOnError('settings/2fa', 'settings_2fa');
try {
User::getIdFromPasswordAuthentication(Session::getLocalUser(), $_POST['password'] ?? '');
User::getIdFromPasswordAuthentication(DI::userSession()->getLocalUserId(), $_POST['password'] ?? '');
$has_secret = (bool)DI::pConfig()->get(Session::getLocalUser(), '2fa', 'secret');
$verified = DI::pConfig()->get(Session::getLocalUser(), '2fa', 'verified');
$has_secret = (bool)DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
$verified = DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
switch ($_POST['action'] ?? '') {
case 'enable':
if (!$has_secret && !$verified) {
$Google2FA = new Google2FA();
DI::pConfig()->set(Session::getLocalUser(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), '2fa', 'secret', $Google2FA->generateSecretKey(32));
DI::baseUrl()
->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
@ -61,9 +60,9 @@ class Index extends BaseSettings
break;
case 'disable':
if ($has_secret) {
RecoveryCode::deleteForUser(Session::getLocalUser());
DI::pConfig()->delete(Session::getLocalUser(), '2fa', 'secret');
DI::pConfig()->delete(Session::getLocalUser(), '2fa', 'verified');
RecoveryCode::deleteForUser(DI::userSession()->getLocalUserId());
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), '2fa', 'secret');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), '2fa', 'verified');
DI::session()->remove('2fa');
DI::sysmsg()->addInfo(DI::l10n()->t('Two-factor authentication successfully disabled.'));
@ -104,14 +103,14 @@ class Index extends BaseSettings
protected function content(array $request = []): string
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return Login::form('settings/2fa');
}
parent::content();
$has_secret = (bool) DI::pConfig()->get(Session::getLocalUser(), '2fa', 'secret');
$verified = DI::pConfig()->get(Session::getLocalUser(), '2fa', 'verified');
$has_secret = (bool) DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
$verified = DI::pConfig()->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/index.tpl'), [
'$form_security_token' => self::getFormSecurityToken('settings_2fa'),
@ -129,12 +128,12 @@ class Index extends BaseSettings
'$recovery_codes_title' => DI::l10n()->t('Recovery codes'),
'$recovery_codes_remaining' => DI::l10n()->t('Remaining valid codes'),
'$recovery_codes_count' => RecoveryCode::countValidForUser(Session::getLocalUser()),
'$recovery_codes_count' => RecoveryCode::countValidForUser(DI::userSession()->getLocalUserId()),
'$recovery_codes_message' => DI::l10n()->t('<p>These one-use codes can replace an authenticator app code in case you have lost access to it.</p>'),
'$app_specific_passwords_title' => DI::l10n()->t('App-specific passwords'),
'$app_specific_passwords_remaining' => DI::l10n()->t('Generated app-specific passwords'),
'$app_specific_passwords_count' => AppSpecificPassword::countForUser(Session::getLocalUser()),
'$app_specific_passwords_count' => AppSpecificPassword::countForUser(DI::userSession()->getLocalUserId()),
'$app_specific_passwords_message' => DI::l10n()->t('<p>These randomly generated passwords allow you to authenticate on apps not supporting two-factor authentication.</p>'),
'$action_title' => DI::l10n()->t('Actions'),

View file

@ -25,7 +25,6 @@ use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Module\Response;
use Friendica\Security\TwoFactor\Model\RecoveryCode;
@ -50,11 +49,11 @@ class Recovery extends BaseSettings
$this->pConfig = $pConfig;
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
$secret = $this->pConfig->get(Session::getLocalUser(), '2fa', 'secret');
$secret = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
if (!$secret) {
$this->baseUrl->redirect('settings/2fa');
@ -68,7 +67,7 @@ class Recovery extends BaseSettings
protected function post(array $request = [])
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
@ -76,7 +75,7 @@ class Recovery extends BaseSettings
self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
if ($_POST['action'] == 'regenerate') {
RecoveryCode::regenerateForUser(Session::getLocalUser());
RecoveryCode::regenerateForUser(DI::userSession()->getLocalUserId());
DI::sysmsg()->addInfo($this->t('New recovery codes successfully generated.'));
$this->baseUrl->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
}
@ -85,19 +84,19 @@ class Recovery extends BaseSettings
protected function content(array $request = []): string
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return Login::form('settings/2fa/recovery');
}
parent::content();
if (!RecoveryCode::countValidForUser(Session::getLocalUser())) {
RecoveryCode::generateForUser(Session::getLocalUser());
if (!RecoveryCode::countValidForUser(DI::userSession()->getLocalUserId())) {
RecoveryCode::generateForUser(DI::userSession()->getLocalUserId());
}
$recoveryCodes = RecoveryCode::getListForUser(Session::getLocalUser());
$recoveryCodes = RecoveryCode::getListForUser(DI::userSession()->getLocalUserId());
$verified = $this->pConfig->get(Session::getLocalUser(), '2fa', 'verified');
$verified = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
'$form_security_token' => self::getFormSecurityToken('settings_2fa_recovery'),

View file

@ -25,7 +25,6 @@ use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Module\BaseSettings;
use Friendica\Module\Response;
@ -53,11 +52,11 @@ class Trusted extends BaseSettings
$this->pConfig = $pConfig;
$this->trustedBrowserRepo = $trustedBrowserRepo;
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
$verified = $this->pConfig->get(Session::getLocalUser(), '2fa', 'verified');
$verified = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
if (!$verified) {
$this->baseUrl->redirect('settings/2fa');
@ -71,7 +70,7 @@ class Trusted extends BaseSettings
protected function post(array $request = [])
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
@ -80,7 +79,7 @@ class Trusted extends BaseSettings
switch ($_POST['action']) {
case 'remove_all':
$this->trustedBrowserRepo->removeAllForUser(Session::getLocalUser());
$this->trustedBrowserRepo->removeAllForUser(DI::userSession()->getLocalUserId());
DI::sysmsg()->addInfo($this->t('Trusted browsers successfully removed.'));
$this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
break;
@ -90,7 +89,7 @@ class Trusted extends BaseSettings
if (!empty($_POST['remove_id'])) {
self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
if ($this->trustedBrowserRepo->removeForUser(Session::getLocalUser(), $_POST['remove_id'])) {
if ($this->trustedBrowserRepo->removeForUser(DI::userSession()->getLocalUserId(), $_POST['remove_id'])) {
DI::sysmsg()->addInfo($this->t('Trusted browser successfully removed.'));
}
@ -103,7 +102,7 @@ class Trusted extends BaseSettings
{
parent::content();
$trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(Session::getLocalUser());
$trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(DI::userSession()->getLocalUserId());
$parser = Parser::create();

View file

@ -29,7 +29,6 @@ use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Module\BaseSettings;
use Friendica\Module\Response;
@ -54,12 +53,12 @@ class Verify extends BaseSettings
$this->pConfig = $pConfig;
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
$secret = $this->pConfig->get(Session::getLocalUser(), '2fa', 'secret');
$verified = $this->pConfig->get(Session::getLocalUser(), '2fa', 'verified');
$secret = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
$verified = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
if ($secret && $verified) {
$this->baseUrl->redirect('settings/2fa');
@ -73,7 +72,7 @@ class Verify extends BaseSettings
protected function post(array $request = [])
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return;
}
@ -82,10 +81,10 @@ class Verify extends BaseSettings
$google2fa = new Google2FA();
$valid = $google2fa->verifyKey($this->pConfig->get(Session::getLocalUser(), '2fa', 'secret'), $_POST['verify_code'] ?? '');
$valid = $google2fa->verifyKey($this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'secret'), $_POST['verify_code'] ?? '');
if ($valid) {
$this->pConfig->set(Session::getLocalUser(), '2fa', 'verified', true);
$this->pConfig->set(DI::userSession()->getLocalUserId(), '2fa', 'verified', true);
DI::session()->set('2fa', true);
DI::sysmsg()->addInfo($this->t('Two-factor authentication successfully activated.'));
@ -99,7 +98,7 @@ class Verify extends BaseSettings
protected function content(array $request = []): string
{
if (!Session::getLocalUser()) {
if (!DI::userSession()->getLocalUserId()) {
return Login::form('settings/2fa/verify');
}
@ -107,7 +106,7 @@ class Verify extends BaseSettings
$company = 'Friendica';
$holder = DI::session()->get('my_address');
$secret = $this->pConfig->get(Session::getLocalUser(), '2fa', 'secret');
$secret = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'secret');
$otpauthUrl = (new Google2FA())->getQRCodeUrl($company, $holder, $secret);