Exemplo n.º 1
0
def _get_permissions_query(obj=None):
    query = Query(tables=[get_permission_owners_query()])
    if obj is None:
        query.conditions.append(NULL_OBJECT_CONDITION)
    else:
        query.params.update({
            'object_pk':
            obj.pk,
            'ctype_pk':
            ContentType.objects.get_for_model(obj).pk,
            'null_object_condition':
            NULL_OBJECT_CONDITION,
            'null_object_id':
            NULL_OWNER_TO_PERMISSION_OBJECT_ID
        })
        query.conditions.append("""
                ({null_object_condition!s})
                OR (op.content_type_id = {ctype_pk!s} AND
                    (op.object_id = {object_pk!s} OR op.object_id = {null_object_id!s})
                )
                OR (
                    gl.content_type_id = gug.group_content_type_id AND
                    gug.group_id = {object_pk!s} AND gug.group_content_type_id = {ctype_pk!s}
                )
            """)
    return query
Exemplo n.º 2
0
def _get_permissions_query(obj=None):
    query = Query(
        tables=[get_permission_owners_query()]
    )
    if obj is None:
        query.conditions.append(NULL_OBJECT_CONDITION)
    else:
        query.params.update({
            'object_pk': obj.pk, 'ctype_pk': ContentType.objects.get_for_model(obj).pk,
            'null_object_condition': NULL_OBJECT_CONDITION,
            'null_object_id': NULL_OWNER_TO_PERMISSION_OBJECT_ID
        })
        query.conditions.append(
            """
                ({null_object_condition!s})
                OR (op.content_type_id = {ctype_pk!s} AND
                    (op.object_id = {object_pk!s} OR op.object_id = {null_object_id!s})
                )
                OR (
                    gl.content_type_id = gug.group_content_type_id AND
                    gug.group_id = {object_pk!s} AND gug.group_content_type_id = {ctype_pk!s}
                )
            """
        )
    return query
Exemplo n.º 3
0
def filter_object_id_list(object_list, user_id, permission_id):
    # object_list list is a list of tuples (ctype_id, object_id)
    query = """
        SELECT ctype_id, object_id FROM ({ids_query}) AS ids
        WHERE EXISTS (SELECT op.id FROM {permission_owners} WHERE {filter_condition})
    """
    query = query.format(ids_query=generate_obj_list_query(object_list),
                         permission_owners=get_permission_owners_query(),
                         filter_condition=_generate_filter_condition(
                             user_id, permission_id, 'ids.ctype_id',
                             'ids.object_id'))
    cursor = connection.cursor()
    cursor.execute(query)
    return [(row[0], row[1]) for row in cursor.fetchall()]
Exemplo n.º 4
0
def filter_object_id_list(object_list, user_id, permission_id):
    # object_list list is a list of tuples (ctype_id, object_id)
    query = """
        SELECT ctype_id, object_id FROM ({ids_query}) AS ids
        WHERE EXISTS (SELECT op.id FROM {permission_owners} WHERE {filter_condition})
    """
    query = query.format(
        ids_query=generate_obj_list_query(object_list),
        permission_owners=get_permission_owners_query(),
        filter_condition=_generate_filter_condition(
            user_id, permission_id, 'ids.ctype_id', 'ids.object_id'
        )
    )
    cursor = connection.cursor()
    cursor.execute(query)
    return [(row[0], row[1]) for row in cursor.fetchall()]