'Returns a query to select the records owned by one of the entities.
@param table: the table
@param entities: list of entities
@return: a web2py Query instance, or None if no query can be
constructed'

def realm_query(self, table, entities):

    OENT = 'realm_entity'
    query = None
    if (entities and ('ANY' not in entities) and (OENT in table.fields)):
        public = (table[OENT] == None)
        if (len(entities) == 1):
            query = ((table[OENT] == entities[0]) | public)
        else:
            query = (table[OENT].belongs(entities) | public)
    return query
