Commit d3038b12 authored by Lukas Burgey's avatar Lukas Burgey

Refactor update_userinfo

parent b6a5affc
......@@ -215,40 +215,49 @@ class User(AbstractUser):
dep.user_remove()
def update_userinfo(self, userinfo):
if 'sub' not in userinfo:
raise Exception('Missing attribute in userinfo: sub')
self.userinfo = userinfo
self.save()
groups = userinfo.get('groups', [])
# check if groups were removed
for group in self.groups.all():
if group.name not in groups:
LOGGER.info(self.msg('User left the group %s'), group)
self.groups.remove(group)
# remove group from user and deactivate deployments
for dep in self.deployments.filter(group=group):
LOGGER.info(dep.msg('Deactivating, as user left the group'), group)
dep.deactivate()
self.userinfo = userinfo
self.save()
if 'sub' not in userinfo:
raise Exception('Missing attribute in userinfo: sub')
# check if groups were added
for group_name in groups:
group = None
try:
group = Group.objects.get(name=group_name)
# check if user needs to be in this group
if not self.groups.filter(name=group_name).exists():
LOGGER.info(self.msg('User is now in the group %s'), group)
self.groups.add(group)
for dep in self.deployments.filter(group=group):
dep.activate()
except Group.DoesNotExist:
LOGGER.info('Adding group %s', group_name)
LOGGER.info('New group from IdP: %s', group_name)
group = Group(name=group_name)
group.save()
self.groups.add(group)
for dep in self.deployments.filter(group=group):
LOGGER.info(dep.msg('Reactivating, as user is back in the group'), group)
dep.activate()
self.groups.add(group)
from . import NewDeployment
dep = NewDeployment.get_deployment(self, group=group)
dep.save()
# include the ssh key from unity
unity_key_value = userinfo.get('ssh_key', '')
unity_key_name = 'unity_key'
......
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