mirror of
https://github.com/correl/codereview.git
synced 2024-12-28 03:00:03 +00:00
21 lines
744 B
Python
21 lines
744 B
Python
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
|