codereview/dashboard/auth.py

22 lines
744 B
Python
Raw Permalink Normal View History

2010-12-08 21:16:35 +00:00
import pam
from django.contrib.auth.models import User, Group
class PAMBackend:
def authenticate(self, username=None, password=None):
if pam.authenticate(username, password, service='login'):
try:
return User.objects.get(username=username)
except User.DoesNotExist:
# Create new django user
user = User(username=username)
user.set_password(password)
user.save()
user.groups.add(Group.objects.get(name='Users'))
return user
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None