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

Code before:
        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:

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

                inserts.append((index + 1, operation))
        for inserted, (index, operation) in enumerate(inserts):
            migration.operations.insert(inserted + index, operation)


def get_system_username():
    """
    Return the current system user's username, or an empty string if the
    username could not be determined.
    """
    try:
        result = getpass.getuser()
    except (ImportError, KeyError, OSError):

Review comment (security):
Do we need the `available` temporary variable?
```suggestion
    try:
        Permission = apps.get_model("auth", "Permission")
    except LookupError:
        return
    else:
        if not router.allow_migrate_model(using, Permission):
            return

```

Code after:
    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(
                    migration.app_label,
                    operation.old_name,
                    operation.new_name,
                )
                inserts.append((index + 1, operation))
        for inserted, (index, operation) in enumerate(inserts):
            migration.operations.insert(inserted + index, operation)


def get_system_username():
    """
    Return the current system user's username, or an empty string if the
    username could not be determined.
    """
    try:
        result = getpass.getuser()
    except (ImportError, KeyError, OSError):
        # TODO: Drop ImportError and KeyError when dropping support for PY312.
        # KeyError (Python <3.13) or OSError (Python 3.13+) will be raised by
        # os.getpwuid() (called by getuser()) if there is no corresponding
        # entry in the /etc/passwd file (for example, in a very restricted
        # chroot environment).