Skip to content

Instantly share code, notes, and snippets.

@markhepburn
Created January 13, 2010 23:02

Revisions

  1. Mark Hepburn revised this gist Jan 13, 2010. 2 changed files with 2 additions and 9 deletions.
    8 changes: 1 addition & 7 deletions auth.py
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,7 @@
    class OracleBackend(object):
    """Authenticate by attempting to establish an oracle connection.
    Uses the database from the settings. The django user module is
    still used for management, just not authentication. A random
    password is used to make sure they don't authenticate against the
    model backend by accident."""
    still used for management, just not authentication."""
    def authenticate(self, username=None, password=None):
    try:
    dsn = cx_Oracle.makedsn(settings.DATABASE_HOST, settings.DATABASE_PORT, settings.DATABASE_NAME)
    @@ -22,10 +20,6 @@ def authenticate(self, username=None, password=None):
    user.is_staff = True
    user.is_superuser = False

    user.save()
    from ecobaseentry.ecobase.admin import usergroup
    user.groups.add(usergroup)

    user.save()
    return user
    except: # probably DatabaseError, but I'll catch everything to be safe
    3 changes: 1 addition & 2 deletions models.py
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@ def set_schema(sender, **kwargs):
    from django.db import connection
    from django.conf import settings
    cursor = connection.cursor()
    cursor.execute("alter session set current_schema=%s" % settings.DEFAULT_SCHEMA)
    cursor.execute('alter session set current_schema=%s' % settings.DEFAULT_SCHEMA)
    if set_schema not in connection_created.receivers:
    print 'adding set_schema to signals'
    connection_created.connect(set_schema)
  2. markhepburn created this gist Jan 13, 2010.
    32 changes: 32 additions & 0 deletions auth.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    from django.conf import settings
    from django.contrib.auth.models import User
    import cx_Oracle

    class OracleBackend(object):
    """Authenticate by attempting to establish an oracle connection.
    Uses the database from the settings. The django user module is
    still used for management, just not authentication. A random
    password is used to make sure they don't authenticate against the
    model backend by accident."""
    def authenticate(self, username=None, password=None):
    try:
    dsn = cx_Oracle.makedsn(settings.DATABASE_HOST, settings.DATABASE_PORT, settings.DATABASE_NAME)
    connection = cx_Oracle.connect(str('%s/%s@%s' % (username, password, dsn)))
    connection.close()
    # valid, so log them in:
    try:
    user = User.objects.get(username=username)
    except User.DoesNotExist:
    user = User(username=username, password='')
    user.set_unusable_password()
    user.is_staff = True
    user.is_superuser = False

    user.save()
    from ecobaseentry.ecobase.admin import usergroup
    user.groups.add(usergroup)

    user.save()
    return user
    except: # probably DatabaseError, but I'll catch everything to be safe
    return None
    14 changes: 14 additions & 0 deletions middleware.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    class UserDBConnection(object):
    """If the user is logged in (assumed if their password is in the
    session!), the global connection object is updated to use their
    connection credentials instead of the default Django ones."""
    def process_request(self, request):
    if 'password' in request.session:
    from django import db
    from django.conf import settings
    username = request.user.username
    password = request.session['password']

    db.connection.settings_dict['DATABASE_USER'] = username
    db.connection.settings_dict['DATABASE_PASSWORD'] = password
    return None
    8 changes: 8 additions & 0 deletions models.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    def set_schema(sender, **kwargs):
    from django.db import connection
    from django.conf import settings
    cursor = connection.cursor()
    cursor.execute("alter session set current_schema=%s" % settings.DEFAULT_SCHEMA)
    if set_schema not in connection_created.receivers:
    print 'adding set_schema to signals'
    connection_created.connect(set_schema)