Hi, we recently noticed that there are some duplicated users showing on our user listing when we try to filter the users by role. After looking into the issue closer, we noticed that there are some duplicated entries in the "user__roles" table and it's only happening on those users that we previously tried to update their roles using this module.
We found that the issue comes from line 80 of the `Feeds/Target/UserRole.php` file. It tries to merge and save the user roles from both updating entity and the "original" entity without checking if there are any duplicated roles in the array. There could be duplicated roles when trying to import a user that already exist on the site with the importing role on it.

I guess most people only use this module to import instead of updating like us and thus not noticing this issue.
Here's the reproducible steps:
1. Create a drupal site with `feeds` enabled. (I use "https://simplytest.me/")
2. Create a feed type for user, with following configuration

3. Add "name" and "role" to the mapping.

4. Create a user manually and add the role "editor". (or any user role that exists on the site)
5. Create a csv file with the name of the user created above.

6. Import the file.
7. Go to user listing page and filter by the role.
8. See duplicated users

| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 3264354-3-import-existing-users-leaves-duplicated-roles-in-database.patch | 2.86 KB | sker101 |
| iShot2022-02-14 11.13.41.png | 74.02 KB | sker101 | |
| iShot2022-02-14 11.13.58.png | 13.8 KB | sker101 | |
| iShot2022-02-14 11.13.24.png | 156.67 KB | sker101 | |
| iShot2022-02-14 11.09.45.png | 90.04 KB | sker101 |
Comments
Comment #2
sker101 commentedComment #3
sker101 commentedI created a patch which creates a helper function to merge and uniquify the role array before adding it to the target entity.
Comment #4
megachrizThanks for finding a fixing this bug! I somehow missed this issue report earlier.
Committed #3 with small changes to the code comments.