summaryrefslogtreecommitdiff
path: root/postgresqleu/auth.py
blob: 66356b712ac2026bbc451e27b21d088821e923cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend
from django.conf import settings
import psycopg2

class AuthBackend(ModelBackend):
	def authenticate(self, username=None, password=None):
		conn = psycopg2.connect(settings.AUTH_CONNECTION_STRING)
		try:
			conn.set_client_encoding('UNICODE')
			cur = conn.cursor()
			cur.execute('SELECT * FROM community_login(%s,%s)', (username, password))
			row  = cur.fetchall()[0]
		finally:
			conn.close()

		if row[1] == 1:
			try:
				user = User.objects.get(username=username)
			except User.DoesNotExist:
				# User doesn't exist yet
				user = User(username=username, password='setmanually', email=row[3], first_name=row[2])
				user.save()
			return user
		return None

	def get_user(self, user_id):
		try:
			return User.objects.get(pk=user_id)
		except User.DoesNotExist:
			return None