Commit 8847bdc6 authored by Sharad Heft's avatar Sharad Heft
Browse files

Refactor the group creation process.

parent 8cda1db4
......@@ -9,7 +9,6 @@ UserModel = get_user_model()
class TESTOAuth2Backend(ModelBackend):
@transaction.atomic
def authenticate(self, id=None, username=None, **profile):
if id is None or username is None:
return None
......@@ -19,21 +18,8 @@ class TESTOAuth2Backend(ModelBackend):
username=username
)
user.groups.clear()
if getattr(settings, 'TEST_AUTH_CREATE_GROUPS', False):
for g in profile.get('groups', []):
group, _ = Group.objects.get_or_create(
id=g['id'], name=g['name']
)
user.groups.add(group)
# Adding carveout for Server Admin group
if g['name'] == "Server Admin":
user.is_staff = True
user.save()
if getattr(settings, 'TEST_OAUTH_CREATE_GROUPS', False):
self._do_create_groups(user, profile.get('groups', []))
return user
......@@ -42,3 +28,20 @@ class TESTOAuth2Backend(ModelBackend):
return UserModel.objects.get(pk=user_id)
except UserModel.DoesNotExist:
return None
@transaction.atomic
def _do_create_groups(self, user, groups):
user.groups.clear()
for g in groups:
group, _ = Group.objects.get_or_create(
id=g['id'], name=g['name']
)
user.groups.add(group)
# Adding carveout for Server Admin group
if g['name'] == "Server Admin":
user.is_staff = True
user.save()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment