Role is missing on user import

Created on 17 April 2024, 7 months ago

Problem/Motivation

Hi, I've used feeds to import users for a while, so I feel like I've got the process down pretty well, but today I tried to import users with roles in a CSV file and the feed is returning the error:
The role role_machine_name cannot be assigned because it does not exist.

Except, the role does exist. I copied the machine name of the role (I also tried the user-friendly name) and pasted it into the CSV column directly to make sure I wasn't misspelling it or anything. Under Mapping, I made sure the role I want is selected for the import process.
I'm not dealing with multiple roles per user, so I didn't setup a tamper.

Steps to reproduce

Create a CSV with username, email, roles, and other fields you want
Create a feed
- Processor: Import new users, Update existing users
Map the fields to the user roles
Attempt import
Note warning errors that the role doesn't exist.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States rex.barkdoll

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

Merge Requests

Comments & Activities

  • Issue created by @rex.barkdoll
  • πŸ‡―πŸ‡΅Japan ptmkenny

    I am having the same issue. I will attempt to find the bad commit, but when I edit the mappings for Role, I can no longer select "id", only "UUID" or "User role weight".

  • πŸ‡―πŸ‡΅Japan ptmkenny

    In my exported config:

         map:
           target_id: roles
         settings:
    -      reference_by: id
    +      reference_by: uuid
    

    So it looks like the id reference got removed, but when? I also confirmed this behavior in the latest dev; it is no longer possible to reference roles by machine_name, but that is the most convenient way to do so.

  • Merge request !165fix user role import β†’ (Merged) created by ptmkenny
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 7 months ago
    717 pass
  • Status changed to Needs review 7 months ago
  • πŸ‡―πŸ‡΅Japan ptmkenny

    Ok, I found the cause of the bug. ConfigEntityReference.php is not checking for id if there are mapping definitions, only UUID.

    I think everything in Drupal has an id and a UUID, so I modified the code to assume everything does and simplify the logic. But, I don't know if this is actually a safe assumption. Let's see what the tests say, and please review.

  • Pipeline finished with Success
    7 months ago
    Total: 1837s
    #169813
  • πŸ‡³πŸ‡±Netherlands megachriz

    The issue is actually that Drupal 10 apparently added some new data types for config schema. Data types 'machine_name' and 'required_label' are new.

    Config schema for user role in Drupal 9:

    user.role.*:
      type: config_entity
      label: 'User role settings'
      mapping:
        id:
          type: string
          label: 'ID'
        label:
          type: label
          label: 'Label'
        weight:
          type: integer
          label: 'User role weight'
        is_admin:
          type: boolean
          label: 'User is admin'
        permissions:
          type: sequence
          label: 'Permissions'
          orderby: value
          sequence:
            type: string
            label: 'Permission'

    And in Drupal 10:

    user.role.*:
      type: config_entity
      label: 'User role settings'
      mapping:
        id:
          type: machine_name
          label: 'ID'
        label:
          type: required_label
          label: 'Label'
        weight:
          type: integer
          label: 'User role weight'
        is_admin:
          type: boolean
          label: 'User is admin'
        permissions:
          type: sequence
          label: 'Permissions'
          orderby: value
          sequence:
            type: string
            label: 'Permission'

    So changes in Drupal core broke this in Feeds.

  • πŸ‡³πŸ‡±Netherlands megachriz

    From https://www.drupal.org/node/3349638: β†’

    Note: There's no disruption for config schemas switching from type: label to type: required_label, because config schema validation is not yet being used.

    Hm, that's not true for Feeds!

    Besides machine_name, other newly introduced types don't seem to be significant for Feeds: https://www.drupal.org/list-changes/drupal/published?keywords_descriptio... β†’

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 6 months ago
    720 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 6 months ago
    720 pass
  • πŸ‡³πŸ‡±Netherlands megachriz

    @ptmkenny
    A config entity doesn't always have an 'id' property, but it does have an 'uuid' property:

    config_entity:
      type: mapping
      mapping:
        uuid:
          type: uuid
          label: 'UUID'
        langcode:
          type: langcode
        status:
          type: boolean
          label: 'Status'
        dependencies:
          type: config_dependencies
          label: 'Dependencies'
        third_party_settings:
          type: sequence
          label: 'Third party settings'
          sequence:
            type: '[%parent.%parent.%type].third_party.[%key]'
        _core:
          type: _core_config_info

    So I reverted your changes in the MR.

  • Status changed to RTBC 6 months ago
  • πŸ‡―πŸ‡΅Japan ptmkenny

    Thank you so much for the explanation and better solution.

    I have a site that can be initialized and all of the content is imported from feeds, so I tested the updated MR there. All the roles were imported correctly, so marking RTBC.

  • Pipeline finished with Skipped
    6 months ago
    #177398
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 6 months ago
    720 pass
  • Status changed to Fixed 6 months ago
  • πŸ‡³πŸ‡±Netherlands megachriz

    Cool. Thanks for testing! I merged the code.

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

  • πŸ‡ΊπŸ‡ΈUnited States rraney

    Hello - I think I have the same issue. I updated the module. I tried using the machine name and mapping to ID. In Devel, the id is the same as the machine name. That didn't work. I am now going to try mapping the UUID to the UUID. Is that the proposed solution once the module is updated?

  • πŸ‡³πŸ‡±Netherlands megachriz

    @rraney
    When mapping to user roles, it makes the most sense to reference roles by ID or by label. But you could use UUID as well. When on Drupal 10, you need Feeds 8.x-2.0-beta5 or later to configure the role target in the UI.

Production build 0.71.5 2024