Repository: django/django
File: django/contrib/auth/management/__init__.py (Python)

Code before:
        )
        permissions = Permission.objects.filter(
            content_type_id__in=[ct.id for ct in ctypes]
        )
        for permission in permissions:
            prefix = permission.codename.split("_")[0]
            new_codename = f"{prefix}_{new_model}"
            new_name = f"Can {prefix} {new_model}"
            if permission.codename != new_codename or permission.name != new_name:
                permission.codename = new_codename
                permission.name = new_name
                try:
                    with transaction.atomic(using=db):
                        permission.save(update_fields={"name", "codename"})
                except IntegrityError:
                    # Skip conflicting permissions.
                    pass

    def rename_forward(self, apps, schema_editor):
        self._rename(apps, schema_editor, self.old_model, self.new_model)

    def rename_backward(self, apps, schema_editor):
        self._rename(apps, schema_editor, self.new_model, self.old_model)


def inject_permission_rename_operations(
    plan,
    verbosity=2,
    interactive=True,
    using=DEFAULT_DB_ALIAS,
    apps=global_apps,
    **kwargs,
):
    """
    Insert a `RenamePermissionType` operation after every planned `RenameModel`
    operation.
    """
    try:
        Permission = apps.get_model("auth", "Permission")
    except LookupError:
        available = False
    else:
        if not router.allow_migrate_model(using, Permission):
            return
        available = True

    if not available:
        return

    for migration, backward in plan:


Review comment (security):
Why not to keep the same naming pattern? `inject_*_operations` sounds unnecessary
```suggestion
def rename_permissions(
```

Code after:
        )
        for permission in Permission.objects.filter(
            content_type_id__in=ctypes.values("id")
        ):
            prefix = permission.codename.split("_")[0]
            default_verbose_name = camel_case_to_spaces(new_model)

            new_codename = f"{prefix}_{new_model.lower()}"
            new_name = f"Can {prefix} {default_verbose_name}"

            if permission.codename != new_codename or permission.name != new_name:
                permission.codename = new_codename
                permission.name = new_name
                try:
                    with transaction.atomic(using=db):
                        permission.save(update_fields={"name", "codename"})
                except IntegrityError:
                    pass

    def rename_forward(self, apps, schema_editor):
        self._rename(apps, schema_editor, self.old_model, self.new_model)

    def rename_backward(self, apps, schema_editor):
        self._rename(apps, schema_editor, self.new_model, self.old_model)


def rename_permissions(
    plan,
    verbosity=2,
    interactive=True,
    using=DEFAULT_DB_ALIAS,
    apps=global_apps,
    **kwargs,
):
    """
    Insert a `RenamePermissionType` operation after every planned `RenameModel`
    operation.
    """
    try:
        Permission = apps.get_model("auth", "Permission")
    except LookupError:
        return
    else:
        if not router.allow_migrate_model(using, Permission):
            return

    for migration, backward in plan:
        inserts = []
        for index, operation in enumerate(migration.operations):
            if isinstance(operation, migrations.RenameModel):
                operation = RenamePermission(