from django.contrib.auth.models import User
from django.conf import settings
import cx_Oracle
# set in config or here depending on your taste
try:
ORACLE_CONNECT = settings.ORACLE_CONNECT
except:
ORACLE_CONNECT = None
# when using runserver I turn debugging on, you can
# set to false of remove.
try:
DEBUG = settings.DEBUG
except:
DEBUG = False
class OracleAuthBackend:
"""
This class is used to authenticate against an Oracle database and
adds a user if authentication is successful. It uppercases the
username since oracle usernames are not currently case sensitive.
Additionally I append _ORACLE to the username to help make it
easier to identify what auth source was used and to avoid conflicts
with ldap authenticated sessions.
"""
def authenticate(self, username=None, password=None):
if DEBUG:
print "Attempting to log in as %s" % (username)
if ORACLE_CONNECT == None:
constr = '%s/%s' % (username, password)
else:
constr = '%s/%s@%s' % (username, password, ORACLE_CONNECT)
try:
auth_con = cx_Oracle.connect(constr.encode('ascii','ignore'))
auth_con.close()
except Exception, e:
if DEBUG:
print e
return None
oracle_user = username.upper() + '_ORACLE'
try:
if DEBUG:
print 'Looking up: %s' % oracle_user
user = User.objects.get(username=oracle_user)
except:
if DEBUG:
print 'Adding user: %s' % oracle_user
user = User(username=oracle_user)
user.set_unusable_password()
try:
user.save()
except:
if DEBUG:
print "ERROR: adding %s" % oracle_user
return None
return user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Comments