Mapping roles to users while migrating Following [this][1], I have  done migration for users from Drupal 7 to Drupal 8, but I got stuck with assigning roles to the specified users.

In user.php file, my code for mapping roles to assigned users is the following.

public function prepareRow(Row $row) {
    $uid = $row-&gt;getSourceProperty('uid');
    // user_role

    $result = $this-&gt;getDatabase()-&gt;query('
      SELECT
        r.rid, r.name, ur.uid
      FROM
        {role} r, {users_roles} ur
      WHERE
        r.rid = ur.rid
      AND
        ur.uid = :uid
    ', array(':uid' =&gt; $uid));
    foreach ($result as $record) {
      $row-&gt;setSourceProperty('roles', $record-&gt;name );
    }

    // local_market field of type entity_reference
    $result = $this-&gt;getDatabase()-&gt;query('
      SELECT
        fld.field_local_market_target_id
      FROM
        {field_data_field_local_market} fld
      WHERE
        fld.entity_id = :uid
    ', array(':uid' =&gt; $uid));
    foreach ($result as $record) {
      if (!is_null($record-&gt;tids)) {
        $row-&gt;setSourceProperty('local_market', explode(',', $record-&gt;tids) );
      }
    }



    return parent::prepareRow($row);
  }


Except roles and custom field, field_local_market, all users migrated from Drupal 7 to Drupal 8.

destination:
  plugin: entity:user

process:
  uid: uid
  name: name
  pass: pass
  mail: mail
  status: status
  created: created
  changed: changed
  access: access
  login: login
  timezone: timezone
  langcode: language
  preferred_langcode: language
  preferred_admin_langcode: language
  init: init
  field_local_market: local_market 
  field_user_role: user_role


What's the right way to assign roles to the users?

Migration of roles is done from Drupal 7 to Drupal 8 by using drush migrate_upgrade; the only task left is assigning/mapping these roles to certain UIDs or users.
You can migrate users with roles without migrating roles using migrate static_map

In migrate set 

  process:
  uid: null
  name: name
  pass: pass
  mail: mail
  status: status
  created: created
  access: access
  login: login
  roles:
    plugin: static_map
    bypass: true
    source: roles
    map:
      5: new_role_name
      3: editor


and in the source plugin you have to set the roles in prepareRow

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $uid = $row-&gt;getSourceProperty('uid');

    $query = $this-&gt;select('users_roles', 'r');
    $query-&gt;fields('r', ['rid']);
    $query-&gt;condition('r.uid', $uid, '=');
    $record = $query-&gt;execute()-&gt;fetchAllKeyed();
    $row-&gt;setSourceProperty('roles', array_keys($record));

    return parent::prepareRow($row);
  }