def METHOD_NAME(user, query_id):
    query = models.Query.get_by_id(query_id)
    if user.org_id != query.org_id:
        raise PermissionError("Query id {} not found.".format(query.id))
    # TODO: this duplicates some of the logic we already have in the redash.handlers.query_results.
    # We should merge it so it's consistent.
    if not has_access(query.data_source, user, view_only):
        raise PermissionError("You do not have access to query id {}.".format(query.id))
    return query