Unable to import if user uid 1 does not exist.

Created on 14 June 2020, over 4 years ago
Updated 30 October 2023, over 1 year ago

Unable to import if user uid 1 does not exist.

Facing following error:
TypeError: Argument 1 passed to Drupal\Core\Session\AccountSwitcher::switchTo() must implement interface Drupal\Core\Session\AccountInterface, null given, called in modules/contrib/default_content_deploy/src/Importer.php on line 244

🐛 Bug report
Status

Needs work

Component

Code

Created by

🇮🇳India mukesh.dev

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy gigiabba

    I've created a new patch that leverages users roles, but it needs to be reworked onto the new development branch.

    • mkalkbrenner committed 70dafc39 on 2.1.x
      Issue #3151523 by mukesh.dev, gigiabba, mkalkbrenner: Unable to import...
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • 🇳🇴Norway eiriksm Norway

    This patch breaks my setup with 2 assumptions:

    - That a role called administrator exist. It's pretty common of course, but far from required. In fact, you could have a role called administrator that only had access to "access content" should you want to.
    - That any users with any roles even exist when trying to import content. I import content right after a reinstall in CI, at which point only user 1 exist

    One solution to my problem would be to add back the old behavior as a fallback, making it at least a bit more backwards compatible. Would you be open to that?

  • Status changed to Fixed 3 months ago
  • 🇩🇪Germany stefan.korn Jossgrund

    I was also hit by this change, in that we are not using the assumed role name "administrator" for the admin role.

    @eiriksm pointed out, a role called administrator does not necessarily exist nor need it to be an admin role.

    Proposing to change the check for an admin user like this:

    diff --git a/src/AdministratorTrait.php b/src/AdministratorTrait.php
    index 60821e8..5f5128a 100644
    --- a/src/AdministratorTrait.php
    +++ b/src/AdministratorTrait.php
    @@ -25,9 +25,18 @@ trait AdministratorTrait {
         static $root_user = NULL;
     
         if (!$root_user) {
    +      $admin_roles = $this->entityTypeManager()->getStorage('user_role')->loadByProperties(
    +        [
    +          'is_admin' => TRUE
    +        ]
    +      );
    +      $admin_role = reset($admin_roles);
    +      if (!$admin_role) {
    +        throw new \RuntimeException('No admin role found');
    +      }
           $query = $this->entityTypeManager()->getStorage('user')->getQuery();
           $ids = $query->condition('status', 1)
    -        ->condition('roles', 'administrator')
    +        ->condition('roles', $admin_role->id())
             ->accessCheck(FALSE)
             ->execute();
           $users = User::loadMultiple($ids);

    Would you consider to change this? I can open a new issue with MR in that case.

    Proposal by eiriksm to change to previous behavior and load user 1 if no admin user is found, might also be an option.

  • 🇳🇴Norway eiriksm Norway

    Also additionally wanted to point out that it seems the assumption here is that user 1 does not have special powers. That is a setting in Drupal core, but it still defaults to the old behavior. User 1 is special unless you have specifically disabled it. Which would make more sense to check for first, instead of just assuming a role with that name exists and is even an admin role

Production build 0.71.5 2024