Loading src/Controller/TfaLoginController.php +4 −1 Original line number Diff line number Diff line Loading @@ -113,7 +113,10 @@ class TfaLoginController { return $access->andIf(AccessResult::forbidden('User is not logged in.')); } $is_self = $account->id() === $target_user->id(); // ID may be numeric string depending on entity class/storage, despite docs // for both AccountInterface::id() and UserInterface::id() claiming strict // integer. $is_self = (int) $account->id() === (int) $target_user->id(); if (!$is_self) { $method = $route->getParameter('method'); if (!empty($method)) { Loading tests/modules/tfa_test_user/src/Entity/TfaTestUser.php 0 → 100644 +23 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\tfa_test_user\Entity; use Drupal\user\Entity\User; use Drupal\user\UserInterface; /** * Test user for TFA. */ final class TfaTestUser extends User implements UserInterface { /** * {@inheritdoc} */ public function id(): ?int { $id = parent::id(); return $id !== NULL ? (int) $id : NULL; } } tests/modules/tfa_test_user/tfa_test_user.info.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line name: TFA Test User type: module package: Testing dependencies: - drupal:user tests/modules/tfa_test_user/tfa_test_user.module 0 → 100644 +17 −0 Original line number Diff line number Diff line <?php /** * @file * Hooks for tfa_test_user.module. */ declare(strict_types=1); use Drupal\tfa_test_user\Entity\TfaTestUser; /** * Implements hook_entity_bundle_info(). */ function tfa_test_user_entity_bundle_info_alter(array &$bundles): void { $bundles['user']['user']['class'] = TfaTestUser::class; } tests/src/Functional/TfaLoginControllerTest.php 0 → 100644 +48 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\tfa\Functional; use Drupal\Core\Url; use Drupal\tfa_test_user\Entity\TfaTestUser; /** * Tests login controller output. * * @group tfa * @coversDefaultClass \Drupal\tfa\Controller\TfaLoginController */ final class TfaLoginControllerTest extends TfaTestBase { /** * {@inheritdoc} */ protected static $modules = [ 'tfa_test_user', ]; /** * Test the most basic login controller output. * * Tests with custom user entity bundle to ensure loadable with strict types. */ public function testBasic(): void { $this->config('tfa.settings') ->set('enabled', TRUE) ->set('required_roles', ['authenticated' => 'authenticated']) ->save(); $user = $this->createUser([ 'setup own tfa', ]); $this->assertInstanceOf(TfaTestUser::class, $user); $this->drupalLogin($user); $this->drupalGet(Url::fromRoute('tfa.overview', ['user' => $user->id()])); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->responseContains('<h1>TFA</h1>'); $this->assertSession()->pageTextContains('Number of times validation skipped: 0 of 3'); } } Loading
src/Controller/TfaLoginController.php +4 −1 Original line number Diff line number Diff line Loading @@ -113,7 +113,10 @@ class TfaLoginController { return $access->andIf(AccessResult::forbidden('User is not logged in.')); } $is_self = $account->id() === $target_user->id(); // ID may be numeric string depending on entity class/storage, despite docs // for both AccountInterface::id() and UserInterface::id() claiming strict // integer. $is_self = (int) $account->id() === (int) $target_user->id(); if (!$is_self) { $method = $route->getParameter('method'); if (!empty($method)) { Loading
tests/modules/tfa_test_user/src/Entity/TfaTestUser.php 0 → 100644 +23 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\tfa_test_user\Entity; use Drupal\user\Entity\User; use Drupal\user\UserInterface; /** * Test user for TFA. */ final class TfaTestUser extends User implements UserInterface { /** * {@inheritdoc} */ public function id(): ?int { $id = parent::id(); return $id !== NULL ? (int) $id : NULL; } }
tests/modules/tfa_test_user/tfa_test_user.info.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line name: TFA Test User type: module package: Testing dependencies: - drupal:user
tests/modules/tfa_test_user/tfa_test_user.module 0 → 100644 +17 −0 Original line number Diff line number Diff line <?php /** * @file * Hooks for tfa_test_user.module. */ declare(strict_types=1); use Drupal\tfa_test_user\Entity\TfaTestUser; /** * Implements hook_entity_bundle_info(). */ function tfa_test_user_entity_bundle_info_alter(array &$bundles): void { $bundles['user']['user']['class'] = TfaTestUser::class; }
tests/src/Functional/TfaLoginControllerTest.php 0 → 100644 +48 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\tfa\Functional; use Drupal\Core\Url; use Drupal\tfa_test_user\Entity\TfaTestUser; /** * Tests login controller output. * * @group tfa * @coversDefaultClass \Drupal\tfa\Controller\TfaLoginController */ final class TfaLoginControllerTest extends TfaTestBase { /** * {@inheritdoc} */ protected static $modules = [ 'tfa_test_user', ]; /** * Test the most basic login controller output. * * Tests with custom user entity bundle to ensure loadable with strict types. */ public function testBasic(): void { $this->config('tfa.settings') ->set('enabled', TRUE) ->set('required_roles', ['authenticated' => 'authenticated']) ->save(); $user = $this->createUser([ 'setup own tfa', ]); $this->assertInstanceOf(TfaTestUser::class, $user); $this->drupalLogin($user); $this->drupalGet(Url::fromRoute('tfa.overview', ['user' => $user->id()])); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->responseContains('<h1>TFA</h1>'); $this->assertSession()->pageTextContains('Number of times validation skipped: 0 of 3'); } }