Problem/Motivation

According to documentation, User::getLastAccessedTime returns 0 if the user has never accessed the site:

A value of 0 means the user has never accessed the site

But in fact, the returned value in such case is '0' (string). And for users that accessed the site, the returned timestamp is also a string, when the expected returned value type is integer:

Return value
int Timestamp of the last access.

This is not accurate and makes it fail in some scenarios, like strict comparisons.

Steps to reproduce

After logging into a site as user 1, execute this Drush command: drush eval "var_dump(\Drupal\user\Entity\User::load(1)->getLastAccessedTime());". The dumped value is a string, not an int.

Proposed resolution

  • Cast the returned type to integer.
  • Change the \Drupal\Core\Session\UserSession::$access type to int.

Remaining tasks

Review

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-2954725

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

manuel.adan created an issue. See original summary.

manuel.adan’s picture

Assigned: manuel.adan » Unassigned
Status: Active » Needs review
StatusFileSize
new436 bytes

Status: Needs review » Needs work

The last submitted patch, 2: 2954725-2.patch, failed testing. View results

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new442 bytes

I have done patch on Drupal 8.8.x-dev

Status: Needs review » Needs work

The last submitted patch, 6: 2954725-6.patch, failed testing. View results

dpi’s picture

Title: User::getLastAccessedTime() returns string instead of integer » AccountInterface::getLastAccessedTime() implementors return incorrect data type
Issue tags: +DX

Change looks good, but needs work.

I think we should go ahead and fix the documentation for \Drupal\Core\Session\UserSession::$access, which claims the value is a string, and check for any created User/UserSession objects initialised with 'access' => string

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

Looks like this still needs fixing, but most likely needs a reroll, definitely needs tests, and we should see if the other methods have corresponding issues.

akashkumar07’s picture

StatusFileSize
new442 bytes

Adding a reroll of #6.

vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new1.28 KB
new729 bytes

Fixing the failed test case, Please have a look.

acbramley’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

Looks good, but we should also have tests around this (core is notorious for setting string values when updating int fields via the UI).

Also need to action #8

quietone’s picture

Changing to the DX special tag defined on Issue tags -- special tags.

dcam made their first commit to this issue’s fork.

dcam’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests

I converted #19 to an MR and added a Kernel test.

dcam’s picture

smustgrave’s picture

Status: Needs review » Needs work

Believe #8 still needs to be addressed.

dcam’s picture

Issue summary: View changes
Status: Needs work » Needs review

I changed the UserSession->access type and updated the proposed resolution accordingly.

I searched for instantiations of User and UserSession. I didn't find any User instantiations. None of the UserSession instantiations set the access value. No action was taken.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback appears to be addressed.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new1.06 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Post-bot-rebellion rebase

dcam’s picture

There was a merge conflict in core/modules/user/tests/src/Kernel/UserEntityTest.php. I rebased the MR.

dcam’s picture

Had to rebase due to a deprecation test removal in core/modules/user/tests/src/Kernel/UserEntityTest.php.

dcam’s picture

This needed a rebase due to the removal of migrate_drupal.

godotislate’s picture

Status: Reviewed & tested by the community » Needs work

AccountProxy and UserSession implement getLastAccessedTime() as well, so presumably they need int casts on the return value?

acbramley’s picture

Confirming at least UserSession needs the cast - \Drupal\user\Authentication\Provider\Cookie::getUserFromSession creates a UserSesssion with values directly from the database. All of the timestamp fields are strings at this point. The UserSession constructor blindly sets these values on its properties.

I think AccountProxy is fine since it just does $this->getAccount()->getLastAccessedTime() so the other casts will be hit there.

acbramley’s picture

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Needs work

The line being changed in the User entity class was changed today by #3574012: Add a getFieldValue() method to bypass typed data overhead for specific use cases. The bug probably needs to be re-verified and the code has to be updated if it still exists.

acbramley’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Believe feedback for this one has been addressed.

Thanks!

  • godotislate committed c62d3799 on main
    fix: #2954725 AccountInterface::getLastAccessedTime() implementors...

  • godotislate committed dd785268 on 11.x
    fix: #2954725 AccountInterface::getLastAccessedTime() implementors...
godotislate’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed c62d379 to main.

There were merge conflicts against 11.x in the test classes, because of deprecation test methods that were removed in main. I manually resolved and committed dd78526 and pushed to 11.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

godotislate’s picture

Status: Fixed » Patch (to be ported)

There was this test failure on 11.x

Drupal\Tests\user\Kernel\Migrate\d6\MigrateUserTest::testUser
Failed asserting that 1543973668 is identical to '1543973668'.

core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserTest.php:108

so I reverted (commit 34e8746cc05c1404dcb1939cf7d80f98cbc42e3)

Set to Patch to be ported, will need an 11.x MR.

  • godotislate committed 34e8746c on 11.x
    Revert "fix: #2954725 AccountInterface::getLastAccessedTime()...