Commit 03e91116 authored by Lukas Burgey's avatar Lukas Burgey

Clean up some code prior to refactoring

parent 7c6be6a2
......@@ -276,17 +276,25 @@ class NewDeployment(models.Model):
for item in self.state_items.all():
if item.state != NOT_DEPLOYED:
item.user_remove()
self._set_target('not_deployed')
self._set_target(NOT_DEPLOYED)
self.publish_to_client()
# each state item publishes its state to the user
def user_credential_removed(self, key):
# TODO implement
# some parts of the user object / the credentials have changed
# we have to update them at the clients
def user_credentials_changed(self):
LOGGER.debug('User object of %s changed', self.user)
pass
def user_credential_added(self, key):
# TODO implement
pass
# Currently out of scope
# def user_credential_added(self, key):
# raise Exception('user_credentialis currently out of scope and therefore not implemented')
# Currently out of scope
# def user_credential_removed(self, key):
# # TODO implement
# raise Exception('user_credential_removed is currently out of scope and therefore not implemented')
def service_added(self, service):
LOGGER.debug(self.msg('Adding service {}'.format(service)))
......@@ -320,10 +328,12 @@ class NewDeployment(models.Model):
)
else:
LOGGER.error('Deployment has neither a group or a service')
raise ValueError('Deployment has neither a group or a service')
# update the state of the remote webpage
def publish_to_user(self):
if self.user is not None:
# mitigating circular dependencies here
from ..views.webpage import user_state
msg = dumps({
'user_state': user_state(self.user),
......@@ -338,7 +348,7 @@ class NewDeployment(models.Model):
def _set_target(self, target):
self.state_target = target
LOGGER.debug(self.msg('target: '+target))
LOGGER.debug(self.msg('target is now: '+target))
self.save()
def __str__(self):
......@@ -417,7 +427,7 @@ class NewDeploymentStateItem(models.Model):
site=site,
service=service,
)
LOGGER.debug('get_state_item hit')
LOGGER.debug('get_state_item: item already exists')
return item
except cls.DoesNotExist:
......@@ -430,26 +440,29 @@ class NewDeploymentStateItem(models.Model):
LOGGER.debug(item.msg('created'))
return item
# STATE TRANSITIONS
# STATE transitions
# user: deployment requested
def user_deploy(self):
if self.state == 'removal_pending':
self._set_state('deployed')
if self.state == REMOVAL_PENDING:
self._set_state(DEPLOYED)
return
if self.state == 'deployed':
if self.state == DEPLOYED:
LOGGER.info(self.msg('ignoring invalid state transition user_deploy'))
return
self._set_state(
'deployment_pending',
publish=False, # the post response already contains the update
DEPLOYMENT_PENDING,
publish=False, # the post response already contains the update
)
# user: removal requested
def user_remove(self):
if self.parent.state_target == DEPLOYED and (self.state == FAILED or self.state == REJECTED):
if (
self.parent.state_target == DEPLOYED
and (self.state == FAILED or self.state == REJECTED)
):
self._reset()
self._set_state(NOT_DEPLOYED, publish=False)
return
......@@ -467,8 +480,8 @@ class NewDeploymentStateItem(models.Model):
return
self._set_state(
'removal_pending',
publish=False, # the post response already contains the update
REMOVAL_PENDING,
publish=False, # the post response already contains the update
)
# user: questionnaire answered
......@@ -479,36 +492,36 @@ class NewDeploymentStateItem(models.Model):
# returns None on success, or a string describing an error
def client_response(self, output):
state = output.get('state', 'undefined')
state = output.get('state', None)
self.message = output.get('message', '')
self.save()
if state != 'undefined':
# update values
if state == 'deployed':
if self.parent.state_target == NOT_DEPLOYED:
self.user_remove()
else:
self.credentials = output.get('credentials', {})
self.save()
elif state == 'not_deployed':
# reset credentials and questi
self._reset()
self.save()
elif state == 'questionnaire':
self.questionnaire = output.get('questionnaire', {})
self.save()
elif state == 'rejected':
pass
elif state == 'failed':
pass
else:
return 'unknown state \''+state+'\''
if state is None:
return 'missing state in output'
self._set_state(state)
return None
return 'missing state in output'
# update values
if state == DEPLOYED:
if self.parent.state_target == NOT_DEPLOYED:
self.user_remove()
else:
self.credentials = output.get('credentials', {})
self.save()
elif state == NOT_DEPLOYED:
# reset credentials and questionnaire
self._reset()
self.save()
elif state == QUESTIONNAIRE:
self.questionnaire = output.get('questionnaire', {})
self.save()
elif state == REJECTED:
pass
elif state == FAILED:
pass
else:
return 'unknown state \''+state+'\''
self._set_state(state)
return None
# resets all client sent values
def _reset(self):
......
......@@ -18,7 +18,7 @@ def user_info_default():
class User(AbstractUser):
USER_ALREADY_EXISTS = Exception("The user does already exist. This usually implies that the IdP changed the sub. Only possible fix: delete the old user")
USER_ALREADY_EXISTS = Exception('The user does already exist. This usually implies that the IdP changed the sub. Only possible fix: delete the old user')
TYPE_CHOICES = (
('apiclient', 'API-Client'),
......@@ -96,7 +96,6 @@ class User(AbstractUser):
]
}
# returns the user as identified by userinfo and idp
# if the user does not exists
@classmethod
......@@ -104,7 +103,6 @@ class User(AbstractUser):
if 'sub' not in userinfo:
raise ValueError('get_user needs a userinfo which contains the users subject')
try:
user = cls.objects.get(
sub=userinfo['sub'],
......@@ -124,7 +122,6 @@ class User(AbstractUser):
return "{}@{}".format(userinfo['sub'], idp.id)
@classmethod
def construct_from_userinfo(cls, userinfo, idp):
if 'sub' not in userinfo:
......@@ -235,19 +232,7 @@ class User(AbstractUser):
for dep in self.deployments.all():
dep.user_remove()
def update_userinfo(self, userinfo):
if 'email' in userinfo:
self.email = userinfo['email']
if 'family_name' in userinfo:
self.last_name = userinfo['family_name']
if 'given_name' in userinfo:
self.first_name = userinfo['given_name']
self.userinfo = userinfo
self.save()
def update_userinfo_groups(self, userinfo):
groups = userinfo.get('groups', [])
# check if groups were removed
......@@ -284,8 +269,7 @@ class User(AbstractUser):
LOGGER.info(dep.msg('Reactivating, as user is back in the group'), group)
dep.activate()
# include the ssh key from unity
def update_userinfo_ssh_key(self, userinfo):
unity_key_value = userinfo.get('ssh_key', '')
unity_key_name = 'unity_key'
......@@ -303,6 +287,27 @@ class User(AbstractUser):
)
key.save()
def update_userinfo(self, userinfo):
if 'email' in userinfo:
self.email = userinfo['email']
if 'family_name' in userinfo:
self.last_name = userinfo['family_name']
if 'given_name' in userinfo:
self.first_name = userinfo['given_name']
self.userinfo = userinfo
self.save()
self.update_userinfo_groups(userinfo)
self.update_userinfo_ssh_key(userinfo)
# TODO implement
# The user object was changed.
def user_changed(self):
pass
class SSHPublicKey(models.Model):
name = models.CharField(
......@@ -371,6 +376,7 @@ def deactivate_user(sender, instance=None, created=False, **kwargs):
if not instance.is_active and instance.is_active_at_clients:
instance.deactivate()
@receiver(post_save, sender=User)
def activate_user(sender, instance=None, created=False, **kwargs):
if created:
......
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