Skip to content

Instantly share code, notes, and snippets.

@markhepburn
Created January 13, 2010 23:02
Show Gist options
  • Save markhepburn/276655 to your computer and use it in GitHub Desktop.
Save markhepburn/276655 to your computer and use it in GitHub Desktop.
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."""
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()
return user
except: # probably DatabaseError, but I'll catch everything to be safe
return None
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
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:
connection_created.connect(set_schema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment