Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 809131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:40:37+00:00 2026-05-15T00:40:37+00:00

I have been looking at the sqlalchemy recipes on their wiki, but don’t know

  • 0

I have been looking at the sqlalchemy recipes on their wiki, but don’t know which one is best to implement what I am trying to do.

Every row on in my tables have an user_id associated with it. Right now, for every query, I queried by the id of the user that’s currently logged in, then query by the criteria I am interested in. My concern is that the developers might forget to add this filter to the query (a huge security risk). Therefore, I would like to set a global filter based on the current user’s admin rights to filter what the logged in user could see.

Appreciate your help. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:40:37+00:00Added an answer on May 15, 2026 at 12:40 am

    Below is simplified redefined query constructor to filter all model queries (including relations). You can pass it to as query_cls parameter to sessionmaker. User ID parameter don’t need to be global as far as session is constructed when it’s already available.

    class HackedQuery(Query):
    
        def get(self, ident):
            # Use default implementation when there is no condition
            if not self._criterion:
                return Query.get(self, ident)
            # Copied from Query implementation with some changes.
            if hasattr(ident, '__composite_values__'):
                ident = ident.__composite_values__()
            mapper = self._only_mapper_zero(
                        "get() can only be used against a single mapped class.")
            key = mapper.identity_key_from_primary_key(ident)
            if ident is None:
                if key is not None:
                    ident = key[1]
            else:
                from sqlalchemy import util
                ident = util.to_list(ident)
            if ident is not None:
                columns = list(mapper.primary_key)
                if len(columns)!=len(ident):
                    raise TypeError("Number of values doen't match number "
                                    'of columns in primary key')
                params = {}
                for column, value in zip(columns, ident):
                    params[column.key] = value
                return self.filter_by(**params).first()
    
    
    def QueryPublic(entities, session=None):
        # It's not directly related to the problem, but is useful too.
        query = HackedQuery(entities, session).with_polymorphic('*')
        # Version for several entities needs thorough testing, so we 
        # don't use it yet.
        assert len(entities)==1, entities
        cls = _class_to_mapper(entities[0]).class_
        public_condition = getattr(cls, 'public_condition', None)
        if public_condition is not None:
            query = query.filter(public_condition)
        return query
    

    It works for single model queries only, and there is a lot of work to make it suitable for other cases. I’d like to see an elaborated version since it’s MUST HAVE functionality for most web applications. It uses fixed condition stored in each model class, so you have to modify it to your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking at several deployment strategies for SSIS and wondering which one
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been looking in to doing some test driven development for one of
I have been looking everywhere on how to turn my Nexus One LED On/Off
I have been looking over the web but have not come across an answer
I have been looking around the internet for examples of an AJAX function which
I have been looking for solutions and found the following piece of code. But
I have been looking for the error for a long time, but cannot find
I have been looking around online to try to find a complete solution but
I have been looking over the web for this but I can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.