Commit edeba838 authored by Lukas Burgey's avatar Lukas Burgey

Add change detection to update_userinfo

parent 27adaffe
......@@ -233,6 +233,7 @@ class User(AbstractUser):
dep.user_remove()
def update_userinfo_groups(self, userinfo):
changed = False
groups = userinfo.get('groups', [])
# check if groups were removed
......@@ -246,6 +247,9 @@ class User(AbstractUser):
LOGGER.info(dep.msg('Deactivating, as user left the group'), group)
dep.deactivate()
if not changed:
changed = True
# check if groups were added
for group_name in groups:
group = None
......@@ -259,6 +263,9 @@ class User(AbstractUser):
for dep in self.deployments.filter(group=group):
dep.activate()
if not changed:
changed = True
except Group.DoesNotExist:
LOGGER.info('New group from IdP: %s', group_name)
group = Group(name=group_name)
......@@ -269,6 +276,11 @@ class User(AbstractUser):
LOGGER.info(dep.msg('Reactivating, as user is back in the group'), group)
dep.activate()
if not changed:
changed = True
return changed
def update_userinfo_ssh_key(self, userinfo):
unity_key_value = userinfo.get('ssh_key', '')
unity_key_name = 'unity_key'
......@@ -276,8 +288,19 @@ class User(AbstractUser):
try:
key = self._ssh_keys.get(name=unity_key_name)
if key.key != unity_key_value:
LOGGER.debug('unity_key of user %s changed', self)
key.delete_key()
raise SSHPublicKey.DoesNotExist()
key = SSHPublicKey(
name=unity_key_name,
key=unity_key_value,
user=self,
)
key.save()
# changed
return True
# not changed
return False
except SSHPublicKey.DoesNotExist:
key = SSHPublicKey(
......@@ -287,7 +310,11 @@ class User(AbstractUser):
)
key.save()
# changed
return True
def update_userinfo(self, userinfo):
if 'email' in userinfo:
self.email = userinfo['email']
......@@ -299,13 +326,22 @@ class User(AbstractUser):
self.userinfo = userinfo
self.save()
changed = False
if self.update_userinfo_groups(userinfo):
changed = True
self.update_userinfo_groups(userinfo)
self.update_userinfo_ssh_key(userinfo)
if self.update_userinfo_ssh_key(userinfo):
changed = True
if changed:
LOGGER.debug('update_userinfo caused changes to the user')
self.user_changed()
# TODO implement
# The user object was changed.
# Call if the user or its keys are changed
def user_changed(self):
LOGGER.debug('User changed. Propagating changes')
pass
......@@ -334,30 +370,13 @@ class SSHPublicKey(models.Model):
# somewhere
# the receiver 'delete_removen_ssh_key' does the actual deletion
def delete_key(self):
LOGGER.info(self.msg('Deletion of key started'))
self.deleted = True
self.save()
# LOGGER.info(self.msg('Deletion of key started'))
# self.deleted = True
# self.save()
LOGGER.info('Direct deletion of SSH key')
self.delete()
# delete implies removeing the key from all clients
# TODO dep don't have remove_key anymore
#for dep in self.user.deployments.all():
# dep.remove_key(self)
# when a key is removed by a client we try to finally delete it
def try_final_deletion(self):
if self.deleted:
if not self.deployed_anywhere:
LOGGER.info(self.msg('All clients have removen this key. Final deletion'))
self._final_deletion()
def _final_deletion(self):
_self = self
for dep in self.deployments.all():
#for item in state.state_items.all():
# item.delete()
dep.delete()
_self.delete()
# we do not update the user object from here: update_userinfo does that anyway
def __str__(self):
if self.deleted:
......
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