When creating a new link or changing an existing link, delete existing ones to the same AGC

This commit is contained in:
Benjamin Dauvergne 2014-11-20 15:03:04 +01:00
parent c7dd01e007
commit 2e387d0a94
1 changed files with 7 additions and 0 deletions

View File

@ -462,10 +462,17 @@ class LoginOrLinkView(PopupViewMixin, MspOAuthSessionViewMixin, View):
self.logger.info('user %r logged in using msp', unicode(user))
return self.redirect(request)
elif request.user.is_authenticated():
# Old link are deleted
msp_account, created = models.MspAccount.objects.get_or_create(defaults={
'agc': agc,
'token': json.dumps(self.token),
}, user=request.user)
for old_msp_account in models.MspAccount.objects \
.exclude(pk=msp_account.pk) \
.filter(agc=agc):
old_msp_account.delete()
self.logger.info('msp link deleted for user %r',
unicode(request.user))
if not created:
# Previous links is replaced
# XXX: we could add support for multi-link by changing the