Updated: Comment #N

Problem/Motivation

user_authenticate could do with an OO revamp. This is also not easy (a.k.a not possible) to switch out if/when needed.

Proposed resolution

Add a UserAuth service that can be used in services instead.

Remaining tasks

Reviews. RTBC

User interface changes

None

API changes

Deprecate user_authenticate(), and remove in the future.

Comments

damiankloip’s picture

Component: base system » user.module
damiankloip’s picture

Needs tests, probably a conversion for BasicAuth, amongst other things.

Status: Needs review » Needs work

The last submitted patch, userAuth.patch, failed testing.

The last submitted patch, userAuth.patch, failed testing.

dawehner’s picture

+1 for the idea!

  1. +++ b/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php
    @@ -165,7 +165,7 @@ public function validateAuthentication(array &$form, array &$form_state) {
    -      $form_state['uid'] = user_authenticate($form_state['values']['name'], $password);
    +      $form_state['uid'] = \Drupal::service('user.auth')->authenticate($form_state['values']['name'], $password);
    

    Seriously we should inject it, so other people also follow us.

  2. +++ b/core/modules/user/lib/Drupal/user/UserAuth.php
    @@ -0,0 +1,75 @@
    +/**
    + * Class UserAuth.
    + */
    +class UserAuth {
    

    Shit, really?

  3. +++ b/core/modules/user/lib/Drupal/user/UserAuth.php
    @@ -0,0 +1,75 @@
    +   * @param $name
    +   *   User name to authenticate.
    +   * @param $password
    +   *   A plain-text password, such as trimmed text from form values.
    +   * @return int|bool
    +   *   The user's uid on success, or FALSE on failure to authenticate.
    +   */
    +  public function authenticate($name, $password) {
    

    A bit more types would be helpful.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new5.34 KB
new4.53 KB

Good Points, thanks! That initial patch was sloppy...

Also added a UserAuthInterface.

Status: Needs review » Needs work

The last submitted patch, 6: 2206687-6.patch, failed testing.

damiankloip’s picture

Sorry, that patch totally doesn't work. New patch coming shortly. Going to add a unit test while I'm there.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new11.47 KB
new7.21 KB

Fixed the 'arguements' typo in user.services.yml and the loadByProperty() call. Also added unit tests - I would say that's pretty thorough coverage.

klausi’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/user/lib/Drupal/user/UserAuth.php
    @@ -0,0 +1,70 @@
    +  /**
    +   * The password service.
    +   *
    +   * @var \Drupal\Core\Password\PasswordInterface
    +   */
    +  protected $password;
    

    That's a bad name. Since we also have $password in the authenticate() method this can get confusing. Better use $passwordService or $passwordChecker or $passwordHasher.

  2. +++ b/core/modules/user/lib/Drupal/user/UserAuth.php
    @@ -0,0 +1,70 @@
    +   * @param PasswordInterface $password
    

    Missing @param docs.

The PHPUnit test looks superb, great work!

You should also convert BasicAuth to use this service and then remove user_authenticate() (or mark it @deprecated and call the service?). UserLoginForm and BasicAuth are the only two instances that use it.

damiankloip’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new15.15 KB
new5.35 KB

Thanks klausi! Much appreciated.

Also converted the actual user_authenticate function to use the new service too.

Here are those changes, think it takes care of everything now...

damiankloip’s picture

damiankloip’s picture

StatusFileSize
new15.15 KB
new521 bytes

Ugh, wrong service name.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/user/tests/Drupal/user/Tests/UserAuthTest.php
@@ -0,0 +1,215 @@
+class UserAuthTest extends UnitTestCase {

<3

damiankloip’s picture

Title: Replace user_authenticate with a UserAuth service... (or something) » Replace user_authenticate with a UserAuth service

We can remove this I think :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

We need a change record for this - once one exists can set back to rtbc.

damiankloip’s picture

Status: Needs work » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 8bab667 and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.