Skip to content

Commit

Permalink
Merge pull request TomAnthony#1 from drinks/master
Browse files Browse the repository at this point in the history
Add GOOGLEAUTH_GROUPS setting
  • Loading branch information
jcarbaugh committed Mar 12, 2013
2 parents a7de429 + 97292b7 commit a5e2c5f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions googleauth/backends.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.models import User, Group

IS_STAFF = getattr(settings, 'GOOGLEAUTH_IS_STAFF', True)
USER_GROUPS = getattr(settings, 'GOOGLEAUTH_GROUPS', tuple())
DOMAIN = getattr(settings, 'GOOGLEAUTH_DOMAIN', None)


class GoogleAuthBackend(object):

def authenticate(self, identifier=None, attributes=None):

email = attributes.get('email', None)
(username, domain) = email.split('@')

if DOMAIN and DOMAIN != domain:
return None

try:

user = User.objects.get(email=email)

except User.DoesNotExist:

user = User.objects.create(username=username, email=email)
user.first_name = attributes.get('firstname', None)
user.last_name = attributes.get('lastname', None)
user.is_staff = IS_STAFF
user.set_unusable_password()

for group in USER_GROUPS:
try:
grp = Group.objects.get(name=group)
user.groups.add(grp)
except:
pass

user.save()

return user

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
pass
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="django-googleauth",
version='0.2',
version='0.3.0',
py_modules=["googleauth"],
description="OpenID authentication Google and Google Apps accounts",
author="Jeremy Carbaugh",
Expand Down

0 comments on commit a5e2c5f

Please sign in to comment.