most role permissions lost during migrate
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #1760982. -->
Reported by: [Nephele](https://www.drupal.org/user/1035068)
>>>
<p>When I upgrade/migrate my site from og-7.x-1.x to og-7.x-2.x, most of the permissions assigned to the og roles are lost. Administrator members, for example, lose nearly all their permissions. Debugging reveals that the source of the problem is that most of the site's group content types are lost during the update; although og_migrate recovers those group content types, it's too late in the process to be used by og_7200_roles.</p>
<h3>The site setup</h3>
<p>The original site setup is: there are multiple custom group content types (e.g, aircraft, aircraft_activity, calendar_event, etc.). Also, the site has og_field_access enabled. Therefore there most of the og permissions are associated with those content types and the fields used by those content types (e.g., "update any aircraft content", "view field_datetime field", "edit field_datetime_field"). In total, there are 225 og permissions. 78 of them are set to true for authenticated members and 159 set to true for admin members.</p>
<h3>Debugging the problem</h3>
<p>og_7200_roles creates new bundle-specific copies of the authenticated member and admin member roles, but only a handful of the 225 permissions are copied from the original global roles to the bundle-specific roles. After the migration, pages such as admin/config/group/permissions/node/aircraft_group list all of the 225 permissions, but they're no longer selected. The og_role_permission database table only contains 28 entries for each of the new admin member roles.</p>
<p>The problem is that when og_role_grant_permissions() gets asked to copy the permissions to the new role, the permissions first get effectively get filtered according to the current list of available permissions (as generated by og_get_permissions()). But at this point in the migration, og_get_permissions() doesn't include any of the permissions related to the group content types. Tracking the problem even further, og_is_group_content_type() doesn't think that they're group content types, because they don't have an OG_AUDIENCE_FIELD field.</p>
<p>So what happened to OG_AUDIENCE_FIELD? During the update (specifically og_update_7200) the old version of the field (group_audience) gets deleted -- and deletion removes the instances of the field attached to each bundle. og_7200_og_membership creates instances of the new og_group_ref field during the migration, but those instances are created AFTER og_7200_role. So at the point in the update/migration process where og_7200_role has to do its work, technically none of the group content types actually exist.</p>
<h3>Fixing the problem</h3>
<p>As a first stab at just demonstrating that the problem can be fixed, I've created a patch that copies the field instance information during og_update_7200 -- allowing the old group_audience field instances to be used directly to determine which instances are needed. With this patch og_migrate can successfully copy all the permissions.</p>
<p>However, I know my patch isn't really the correct fix. It isn't bothering to figure out the correct value of $group_type. The code is all duplicated in og_7200_og_permissions, and probably shouldn't be. Most importantly, I'm guessing that the preferred place for this type of processing is in og_migrate.</p>
<p>On the other hand, I don't like the idea of reconstructing the fields based solely on og_membership -- what if someone wants to migrate a site where certain content types are set up as possible group content types, but it just so happens that none of the nodes have been assigned a group yet (i.e., there are no corresponding entries in og_membership)? (Which may even be the case for one or two of my group content types). But if the processing is done in og_migrate, og_membership is all that's available.</p>
<p>Which in turn makes me wonder: should the original group_audience field be deleted within update, or should the deletion be handled during og_migrate? I've realized that another problem I'm having (<span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/og/-/work_items/1754540" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/og/-/work_items/1754540</a></span>) can't be fixed unless the group_audience field exists during og_migrate.</p>
<p>So.... Any thoughts on where/how to best handle migrating the group content types? Or how to improve migration of old group_audience data?</p>
>>> [!note] Pre-migration files
- [og\_migrate\_loses\_role\_permissions.patch](https://www.drupal.org/files/og_migrate_loses_role_permissions.patch)
>>>
issue
GitLab AI Context
Project: project/og
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/og/-/raw/2.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/og
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD