Commit cf5c582c authored by dpi's avatar dpi Committed by Joao Ventura
Browse files

Issue #3318453 by dpi, jcnventura: Access to TFA page is denied depending on...

Issue #3318453 by dpi, jcnventura: Access to TFA page is denied depending on entity/database internals
parent b1204651
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -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)) {
+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;
  }

}
+5 −0
Original line number Diff line number Diff line
name: TFA Test User
type: module
package: Testing
dependencies:
  - drupal:user
+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;
}
+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');
  }

}