Fix code quality: Add accessCheck to entity queries and inject service

Created on 20 May 2024, 9 months ago
Updated 19 September 2024, 5 months ago

Problem/Motivation

Major - Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.

in src/Form/OpenIDConnectClientFormBase.php:133

Major - \Drupal calls should be avoided in classes, use dependency injection instead

in src/Plugin/OpenIDConnectClientCollection.php:61

Copied from MR !108

Proposed resolution

  1. Add a call to accessCheck() with the correct parameter.
  2. Inject the service, rather than calling \Drupal

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Needs review

Version

3.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @solideogloria
  • First commit to issue fork.
  • Pipeline finished with Failed
    9 months ago
    Total: 142s
    #180791
  • Status changed to Needs review 9 months ago
  • Status changed to Needs work 9 months ago
  • The code changes do not follow Drupal coding standards.

  • 🇵🇹Portugal jcnventura

    Also NW for the removal of a valid comment with a pointless "Use the injected module handler service to check module existence", which basically describes the inserted line, but doesn't really say WHY the line is there.

    The other funny part is that the patch does a lot to remove a call to \Drupal calls, but proceeds to then replace that with a \Drupal call in another class.

  • Status changed to Needs review 8 months ago
  • 🇮🇳India atul_ghate

    Hi @jcnventura,

    Thank you for your feedback. I have fixed all the PHPCS issues and updated the code as per your suggestions. Please review the changes and let me know if any further modifications are required.

  • Status changed to Needs work 6 months ago
  • 🇮🇳India Tirupati_Singh

    Hi @atul_ghate, I tried applying the provided MR as a patch and getting errors while applying the patch.

    /openid_connect$ curl https://git.drupalcode.org/project/openid_connect/-/merge_requests/109.patch | git apply -v
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 18250    0 18250    0     0  37404      0 --:--:-- --:--:-- --:--:-- 37397
    Checking patch src/Entity/OpenIDConnectClientEntity.php...
    Checking patch src/Form/OpenIDConnectClientFormBase.php...
    Checking patch src/Plugin/OpenIDConnectClientCollection.php...
    Checking patch src/Controller/OpenIDConnectRedirectController.php...
    error: while searching for:
    use Drupal\Core\StringTranslation\StringTranslationTrait;
    use Drupal\Core\Url;
    use Drupal\externalauth\AuthmapInterface;
    use Drupal\openid_connect\OpenIDConnectClaims;
    use Drupal\openid_connect\OpenIDConnect;
    use Drupal\openid_connect\OpenIDConnectClientEntityInterface;
    use Drupal\openid_connect\OpenIDConnectSessionInterface;
    use Drupal\openid_connect\OpenIDConnectStateTokenInterface;
    use Drupal\openid_connect\Plugin\OpenIDConnectClientInterface;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\HttpFoundation\RequestStack;
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    
    /**
    
    error: patch failed: src/Controller/OpenIDConnectRedirectController.php:17
    error: src/Controller/OpenIDConnectRedirectController.php: patch does not apply
    Checking patch src/Entity/OpenIDConnectClientEntity.php...
    error: while searching for:
    
    use Drupal\Core\Config\Entity\ConfigEntityBase;
    use Drupal\openid_connect\OpenIDConnectClientEntityInterface;
    use Drupal\openid_connect\Plugin\OpenIDConnectClientInterface;
    use Drupal\openid_connect\Plugin\OpenIDConnectClientCollection;
    
    /**
     * Defines the OpenID Connect client entity.
    
    error: patch failed: src/Entity/OpenIDConnectClientEntity.php:4
    error: src/Entity/OpenIDConnectClientEntity.php: patch does not apply
    Checking patch src/Form/OpenIDConnectClientFormBase.php...
    Checking patch src/OpenIDConnect.php...
    error: while searching for:
        LoggerChannelFactoryInterface $logger,
        FileSystemInterface $fileSystem,
        OpenIDConnectSessionInterface $session,
        FileRepositoryInterface $fileRepository
      ) {
        $this->configFactory = $config_factory;
        $this->authmap = $authmap;
    
    error: patch failed: src/OpenIDConnect.php:174
    error: src/OpenIDConnect.php: patch does not apply
    Checking patch src/OpenIDConnectAutoDiscover.php...
    error: while searching for:
    
    namespace Drupal\openid_connect;
    
    use Drupal\Core\Http\ClientFactory;
    use Drupal\Core\Logger\LoggerChannelFactoryInterface;
    use GuzzleHttp\Exception\RequestException;
    use Drupal\Component\Serialization\Json;
    
    /**
     * OpenID Connect well-known URI discovery service.
    
    error: patch failed: src/OpenIDConnectAutoDiscover.php:2
    error: src/OpenIDConnectAutoDiscover.php: patch does not apply
    Checking patch src/Plugin/OpenIDConnectClientBase.php...
    error: while searching for:
    
    namespace Drupal\openid_connect\Plugin;
    
    use Drupal\Component\Serialization\Json;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\GeneratedUrl;
    
    error: patch failed: src/Plugin/OpenIDConnectClientBase.php:2
    error: src/Plugin/OpenIDConnectClientBase.php: patch does not apply
    Checking patch src/Plugin/OpenIDConnectClientCollection.php...
    Checking patch tests/src/Unit/Entity/OpenIDConnectClientEntityTest.php...
    error: while searching for:
    <?php
    
    declare(strict_types = 1);
    
    namespace Drupal\Tests\openid_connect\Unit\Entity;
    
    
    error: patch failed: tests/src/Unit/Entity/OpenIDConnectClientEntityTest.php:1
    error: tests/src/Unit/Entity/OpenIDConnectClientEntityTest.php: patch does not apply
    Checking patch tests/src/Unit/OpenIDConnectStateTokenTest.php...
    error: while searching for:
    <?php
    
    declare(strict_types = 1);
    
    namespace Drupal\Tests\openid_connect\Unit;
    
    use Drupal\openid_connect\OpenIDConnectSessionInterface;
    use Drupal\Tests\UnitTestCase;
    use Drupal\openid_connect\OpenIDConnectStateToken;
    
    /**
     * Test the OpenIDConnectStateToken class.
    
    error: patch failed: tests/src/Unit/OpenIDConnectStateTokenTest.php:1
    error: tests/src/Unit/OpenIDConnectStateTokenTest.php: patch does not apply
    Checking patch tests/src/Unit/OpenIDConnectTest.php...
    error: while searching for:
    <?php
    
    declare(strict_types = 1);
    
    namespace Drupal\Tests\openid_connect\Unit;
    
    use Drupal\Component\Serialization\Json;
    use Drupal\Core\Config\ConfigFactoryInterface;
    use Drupal\Core\Entity\EntityFieldManagerInterface;
    use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\Core\Extension\ModuleHandler;
    use Drupal\Core\Logger\LoggerChannelFactoryInterface;
    use Drupal\Core\Messenger\MessengerInterface;
    use Drupal\Core\Session\AccountProxyInterface;
    use Drupal\externalauth\AuthmapInterface;
    use Drupal\externalauth\ExternalAuthInterface;
    use Drupal\file\FileRepositoryInterface;
    use Drupal\openid_connect\OpenIDConnectClientEntityInterface;
    use Drupal\openid_connect\OpenIDConnectSessionInterface;
    use Drupal\openid_connect\Plugin\OpenIDConnectClientInterface;
    use Drupal\Tests\UnitTestCase;
    use Drupal\user\Entity\User;
    use Drupal\user\UserDataInterface;
    use Drupal\openid_connect\OpenIDConnect;
    use Drupal\Core\Entity\EntityStorageInterface;
    use Drupal\user\UserInterface;
    use Drupal\Core\Config\ImmutableConfig;
    use Drupal\Core\Logger\LoggerChannelInterface;
    use Drupal\Core\DependencyInjection\ContainerBuilder;
    use Drupal\Core\Field\FieldDefinitionInterface;
    use Drupal\Core\Entity\EntityTypeRepositoryInterface;
    use Drupal\file\Entity\File;
    use Drupal\Core\Field\FieldItemListInterface;
    use Drupal\Core\File\FileSystemInterface;
    use Psr\Log\InvalidArgumentException;
    
    /**
    
    error: patch failed: tests/src/Unit/OpenIDConnectTest.php:1
    error: tests/src/Unit/OpenIDConnectTest.php: patch does not apply
    Checking patch tests/src/Unit/OpenIdConnectClaimsTest.php...
    error: while searching for:
    <?php
    
    declare(strict_types = 1);
    
    namespace Drupal\Tests\openid_connect\Unit;
    
    
    error: patch failed: tests/src/Unit/OpenIdConnectClaimsTest.php:1
    error: tests/src/Unit/OpenIdConnectClaimsTest.php: patch does not apply
    Checking patch tests/src/Unit/OpenIdConnectSessionTest.php...
    error: while searching for:
    <?php
    
    declare(strict_types = 1);
    
    namespace Drupal\Tests\openid_connect\Unit;
    
    
    error: patch failed: tests/src/Unit/OpenIdConnectSessionTest.php:1
    error: tests/src/Unit/OpenIdConnectSessionTest.php: patch does not apply

    Hence, moving issue to Needs work.

  • Status changed to Needs review 5 months ago
  • 🇮🇳India atul_ghate

    Hi @tirupati_singh,
    I was able to apply this patch cleanly, and it resolved all the PHPCS issues. (See the attached screenshot.)

Production build 0.71.5 2024