Commit cd2f0d08 authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

Store additional provider info

store useful account details for third party authentication providers
that provide such info.
parent 1b6bdd92
......@@ -154,6 +154,9 @@ class ShibbolethAuthProvider(AuthProvider):
description = _('Allows you to login to your account using your academic '
'credentials')
add_prompt = _('Add academic credentials to your account.')
details_tpl = _('Shibboleth account %(identifier)s is connected with your '
' account.')
user_title = _('Academic credentials (%(identifier)s)')
@property
def add_url(self):
......@@ -169,6 +172,8 @@ class TwitterAuthProvider(AuthProvider):
description = _('Allows you to login to your account using your twitter '
'account')
add_prompt = _('Connect with your Twitter account.')
details_tpl = _('Twitter screen name: %(info_screen_name)s')
user_title = _('Twitter (%(info_screen_name)s)')
@property
def add_url(self):
......
......@@ -34,6 +34,7 @@
import hashlib
import uuid
import logging
import json
from time import asctime
from datetime import datetime, timedelta
......@@ -623,8 +624,14 @@ class AstakosUser(User):
**kwargs).count())
def add_auth_provider(self, provider, **kwargs):
info_data = ''
if 'provider_info' in kwargs:
info_data = json.dumps(kwargs.pop('provider_info'))
if self.can_add_auth_provider(provider, **kwargs):
self.auth_providers.create(module=provider, active=True, **kwargs)
self.auth_providers.create(module=provider, active=True,
info_data=info_data,
**kwargs)
else:
raise Exception('Cannot add provider')
......@@ -711,19 +718,40 @@ class AstakosUserAuthProvider(models.Model):
active = models.BooleanField(default=True)
auth_backend = models.CharField('Backend', max_length=255, blank=False,
default='astakos')
info_data = models.TextField(default="", null=True, blank=True)
objects = AstakosUserAuthProviderManager()
class Meta:
unique_together = (('identifier', 'module', 'user'), )
def __init__(self, *args, **kwargs):
super(AstakosUserAuthProvider, self).__init__(*args, **kwargs)
try:
self.info = json.loads(self.info_data)
except:
self.info = {}
for key,value in self.info.iteritems():
setattr(self, 'info_%s' % key, value)
@property
def settings(self):
return auth_providers.get_provider(self.module)
@property
def details_display(self):
return self.settings.details_tpl % self.__dict__
return self.settings.get_details_tpl_display % self.__dict__
@property
def title_display(self):
title_tpl = self.settings.get_title_display
try:
if self.settings.get_user_title_display:
title_tpl = self.settings.get_user_title_display
except Exception, e:
pass
return title_tpl % self.__dict__
def can_remove(self):
return self.user.can_remove_auth_provider(self.module)
......@@ -745,6 +773,9 @@ class AstakosUserAuthProvider(models.Model):
return "%s:%s" % (self.module, self.auth_backend)
return self.module
def save(self, *args, **kwargs):
self.info_data = json.dumps(self.info)
return super(AstakosUserAuthProvider, self).save(*args, **kwargs)
class Membership(models.Model):
......
......@@ -112,6 +112,7 @@ def authenticated(
access_token = dict(cgi.parse_qsl(content))
userid = access_token['user_id']
username = access_token.get('screen_name', userid)
# an existing user accessed the view
if request.user.is_authenticated():
......@@ -125,7 +126,8 @@ def authenticated(
messages.error(request, 'Account already exists.')
return HttpResponseRedirect(reverse('edit_profile'))
user.add_auth_provider('twitter', identifier=userid)
user.add_auth_provider('twitter', identifier=userid,
provider_info={'screen_name': username})
return HttpResponseRedirect(reverse('edit_profile'))
try:
......
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