Repository: apache/superset
File: superset/security/manager.py (Python)

Code before:
        :return: A list of filters
        """
        if guest_user := self.get_current_guest_user_if_guest():
            return [
                rule
                for rule in guest_user.rls
                if not rule.get("dataset")
                or str(rule.get("dataset")) == str(dataset.data["id"])
            ]
        return []

    def get_rls_filters(self, table: "BaseDatasource | Explorable") -> list[SqlaQuery]:
        """
        Retrieves the appropriate row level security filters for the current user and
        the passed table.

        :param table: The table to check against
        :returns: A list of filters
        """

        if not (hasattr(g, "user") and g.user is not None):
            return []

        # Check request-scoped cache keyed by (user_identifier, table_id).
        # Uses user_id for regular users and username for guest users.
        cache: dict[tuple[int | str, int | str], list[SqlaQuery]] = getattr(
            g, "_rls_filter_cache", {}
        )
        user_key: int | str | None = get_user_id() or getattr(g.user, "username", None)
        if user_key is not None:
            cache_key = (user_key, table.id)
            if cache_key in cache:
                return cache[cache_key]

        # pylint: disable=import-outside-toplevel
        from superset.connectors.sqla.models import (
            RLSFilterRoles,
            RLSFilterTables,
            RowLevelSecurityFilter,
        )

        user_roles = [role.id for role in self.get_user_roles(g.user)]
        regular_filter_roles = (
            self.session.query(RLSFilterRoles.c.rls_filter_id)
            .join(RowLevelSecurityFilter)
            .filter(
                RowLevelSecurityFilter.filter_type == RowLevelSecurityFilterType.REGULAR
            )
            .filter(RLSFilterRoles.c.role_id.in_(user_roles))
        )
        base_filter_roles = (

Review comment (suggestion):
The dict type is a bit of a monster; can we add a type alias to describe what these mean? Something like this?
```python
RequestUser = int | str
RequestTable = int | str  # isn't this in fact just int?
dict[tuple[RequestUser, RequestTable], list[SqlaQuery]]
```

Code after:
            public_role = get_conf().get("AUTH_ROLE_PUBLIC")
            return [self.get_public_role()] if public_role else []
        return super().get_user_roles(user)

    def get_guest_rls_filters(
        self, dataset: "BaseDatasource | Explorable"
    ) -> list[GuestTokenRlsRule]:
        """
        Retrieves the row level security filters for the current user and the dataset,
        if the user is authenticated with a guest token.
        :param dataset: The dataset to check against
        :return: A list of filters
        """
        if guest_user := self.get_current_guest_user_if_guest():
            return [
                rule
                for rule in guest_user.rls
                if not rule.get("dataset")
                or str(rule.get("dataset")) == str(dataset.data["id"])
            ]
        return []

    def get_rls_filters(self, table: "BaseDatasource | Explorable") -> list[SqlaQuery]:
        """
        Retrieves the appropriate row level security filters for the current user and
        the passed table.

        :param table: The table to check against
        :returns: A list of filters
        """

        if not (hasattr(g, "user") and g.user is not None):
            return []

        # Check request-scoped cache. Username is included in the key to stay
        # safe if override_user() is called with different users in one request.
        cache: _RLSCache = getattr(g, "_rls_filter_cache", {})
        username = get_username()
        if username is not None:
            cache_key: _RLSCacheKey = (username, table.id)
            if cache_key in cache:
                return cache[cache_key]

        # pylint: disable=import-outside-toplevel
        from superset.connectors.sqla.models import (
            RLSFilterRoles,
            RLSFilterTables,
            RowLevelSecurityFilter,
        )

        user_roles = [role.id for role in self.get_user_roles(g.user)]