2014-05-22 12:14:10 +02:00
|
|
|
import logging
|
|
|
|
|
2014-03-07 17:45:48 +01:00
|
|
|
from allauth.socialaccount import providers
|
|
|
|
from allauth.socialaccount.providers.base import ProviderAccount
|
|
|
|
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
|
|
|
|
from allauth.account.models import EmailAddress
|
|
|
|
|
2014-04-07 11:02:07 +02:00
|
|
|
from . import app_settings
|
|
|
|
|
2014-05-22 12:14:10 +02:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-03-07 17:45:48 +01:00
|
|
|
class Authentic2Account(ProviderAccount):
|
|
|
|
def to_str(self):
|
|
|
|
return self.account.uid
|
|
|
|
|
|
|
|
|
|
|
|
class Authentic2Provider(OAuth2Provider):
|
|
|
|
id = 'authentic2'
|
|
|
|
name = 'Authentic2'
|
|
|
|
package = 'portail_citoyen2.allauth_authentic2'
|
|
|
|
account_class = Authentic2Account
|
|
|
|
|
|
|
|
def extract_uid(self, data):
|
|
|
|
return str(data['username'])
|
|
|
|
|
|
|
|
def extract_common_fields(self, data):
|
|
|
|
return dict(email=data.get('email'),
|
|
|
|
username=data.get('username'),
|
2014-05-27 10:32:47 +02:00
|
|
|
first_name=data.get('first_name'),
|
|
|
|
last_name=data.get('last_name'))
|
2014-03-07 17:45:48 +01:00
|
|
|
|
|
|
|
def extract_email_addresses(self, data):
|
|
|
|
ret = [EmailAddress(email=data['email'],
|
|
|
|
verified=True,
|
|
|
|
primary=True)]
|
|
|
|
return ret
|
|
|
|
|
2014-04-07 11:02:07 +02:00
|
|
|
def sociallogin_from_response(self, request, response):
|
2014-05-22 19:20:09 +02:00
|
|
|
sociallogin = super(Authentic2Provider, self).sociallogin_from_response(
|
2014-04-07 11:02:07 +02:00
|
|
|
request, response)
|
|
|
|
if app_settings.ADMIN_ROLE in response.get('role', []):
|
2014-05-22 12:14:10 +02:00
|
|
|
log.debug('giving admin role to user')
|
2014-04-07 11:02:07 +02:00
|
|
|
sociallogin.account.user.is_superuser = True
|
|
|
|
sociallogin.account.user.is_staff = True
|
|
|
|
return sociallogin
|
|
|
|
|
|
|
|
|
2014-03-07 17:45:48 +01:00
|
|
|
providers.registry.register(Authentic2Provider)
|