Esempio n. 1
0
def social_auth_user(strategy, uid, user=None, *args, **kwargs):
    """
    Allows user to create a new account and associate a social account,
    even if that social account is already connected to a different
    user. It effectively 'steals' the social association from the
    existing user. This can be a useful option during the testing phase
    of a project.

    Return UserSocialAuth account for backend/uid pair or None if it
    doesn't exist.

    Delete UserSocialAuth if UserSocialAuth entry belongs to another
    user.
    """
    social = UserSocialAuth.get_social_auth(kwargs['backend'].name, uid)
    if social:
        if user and social.user != user:
            # Delete UserSocialAuth pairing so this account can now connect
            social.delete()
            social = None
        elif not user:
            user = social.user
    return {'social': social,
            'user': user,
            'is_new': user is None,
            'new_association': False}
Esempio n. 2
0
def social_auth_user(strategy, uid, user=None, *args, **kwargs):
    """
    Allows user to create a new account and associate a social account,
    even if that social account is already connected to a different
    user. It effectively 'steals' the social association from the
    existing user. This can be a useful option during the testing phase
    of a project.

    Return UserSocialAuth account for backend/uid pair or None if it
    doesn't exist.

    Delete UserSocialAuth if UserSocialAuth entry belongs to another
    user.
    """
    social = UserSocialAuth.get_social_auth(kwargs['backend'].name, uid)
    if social:
        if user and social.user != user:
            # Delete UserSocialAuth pairing so this account can now connect
            social.delete()
            social = None
        elif not user:
            user = social.user
    return {
        'social': social,
        'user': user,
        'is_new': user is None,
        'new_association': False
    }
Esempio n. 3
0
def get_user_id(auth_data):
    provider = auth_data['provider']
    uid = auth_data['uid']
    logger.debug('uid = {}'.format(uid))
    user_social_auth = UserSocialAuth.get_social_auth(provider, uid)
    if user_social_auth:
        user = YMUser.objects.get(id=user_social_auth.user_id)
    else:
        user = YMUser()
    if 'email' in auth_data:
        user.email = auth_data['email']
    if 'username' in auth_data:
        user.username = auth_data['username']
    if 'first_name' in auth_data:
        user.first_name = auth_data['first_name']
    if 'last_name' in auth_data:
        user.last_name = auth_data['last_name']
    if 'picture' in auth_data:
        user.icon_url = auth_data['picture']
    if 'locale' in auth_data:
        user.locale = auth_data['locale']
    # logger.debug('user save called...')
    user.save()
    if user_social_auth:
        return (user.id, False)
    user_social_auth = UserSocialAuth(user_id=user.id, provider=provider,
                                      uid=uid, extra_data=auth_data)
    user_social_auth.save()
    return (user.id, True)
Esempio n. 4
0
def get_user_id(auth_data):
    provider = auth_data['provider']
    uid = auth_data['uid']
    logger.debug('uid = {}'.format(uid))
    user_social_auth = UserSocialAuth.get_social_auth(provider, uid)
    if user_social_auth:
        user = YMUser.objects.get(id=user_social_auth.user_id)
    else:
        user = YMUser()
    if 'email' in auth_data:
        user.email = auth_data['email']
    if 'username' in auth_data:
        user.username = auth_data['username']
    if 'first_name' in auth_data:
        user.first_name = auth_data['first_name']
    if 'last_name' in auth_data:
        user.last_name = auth_data['last_name']
    if 'picture' in auth_data:
        user.icon_url = auth_data['picture']
    if 'locale' in auth_data:
        user.locale = auth_data['locale']
    # logger.debug('user save called...')
    user.save()
    if user_social_auth:
        return (user.id, False)
    user_social_auth = UserSocialAuth(user_id=user.id,
                                      provider=provider,
                                      uid=uid,
                                      extra_data=auth_data)
    user_social_auth.save()
    return (user.id, True)
Esempio n. 5
0
def load_extra_data(backend,
                    details,
                    response,
                    uid,
                    user,
                    social_user=None,
                    *args,
                    **kwargs):
    """
    Load extra data from provider and store it on current UserSocialAuth extra_data field.
    """
    social_user = social_user or UserSocialAuth.get_social_auth(
        backend.name, uid)
    # create verified email address
    if kwargs['is_new'] and EMAIL_CONFIRMATION:
        from ..models import EmailAddress
        # check if email exist before creating it
        # we might be associating an exisiting user
        if EmailAddress.objects.filter(email=user.email).count() < 1:
            EmailAddress.objects.create(user=user,
                                        email=user.email,
                                        verified=True,
                                        primary=True)

    if social_user:
        extra_data = backend.extra_data(user, uid, response, details)
        if kwargs.get('original_email') and 'email' not in extra_data:
            extra_data['email'] = kwargs.get('original_email')
        # update extra data if anything has changed
        if extra_data and social_user.extra_data != extra_data:
            if social_user.extra_data:
                social_user.extra_data.update(extra_data)
            else:
                social_user.extra_data = extra_data
            social_user.save()
        # fetch additional data from facebook on creation
        if backend.name == 'facebook' and kwargs['is_new']:
            response = json.loads(
                requests.get(
                    'https://graph.facebook.com/%s?access_token=%s' %
                    (extra_data['id'], extra_data['access_token'])).content)
            try:
                user.city, user.country = response.get('hometown').get(
                    'name').split(', ')
            except (AttributeError, TypeError):
                pass
            try:
                user.birth_date = datetime.strptime(response.get('birthday'),
                                                    '%m/%d/%Y').date()
            except (AttributeError, TypeError):
                pass
            user.save()
        return {'social_user': social_user}
Esempio n. 6
0
def get_user_id(auth_data):
    provider = auth_data['provider']
    uid = auth_data['uid']
    user = UserSocialAuth.get_social_auth(provider, uid)
    if user: return user.id
    user = YMUser()
    if 'email' in auth_data: user.email = auth_data['email']
    if 'username' in auth_data: user.username = auth_data['username']
    if 'first_name' in auth_data: user.first_name = auth_data['first_name']
    if 'last_name' in auth_data: user.last_name = auth_data['last_name']
    user.save()
    user_social_auth = UserSocialAuth(user_id = user.id, provider = provider,
                                    uid = uid, extra_data = auth_data)
    user_social_auth.save()
    return user.id
Esempio n. 7
0
def social_user(backend, uid, user=None, *args, **kwargs):
    """Return UserSocialAuth account for backend/uid pair or None if it
    doesn't exists.

    CHANGE: Raise AuthAlreadyAssociated if UserSocialAuth entry belongs to another
    user.
    INSTEAD: Set new UserSocialAuth to user if associated before.
    """
    social_user = UserSocialAuth.get_social_auth(backend.name, uid)
    if social_user:
        if user and social_user.user != user:
            social_user.user = user
            social_user.save()
        elif not user:
            user = social_user.user
    return {'social_user': social_user, 'user': user}
def social_auth_user(strategy, uid, user=None, *args, **kwargs):
    """Return UserSocialAuth account for backend/uid pair or None if it
    doesn't exists.

    Delete UserSocialAuth if UserSocialAuth entry belongs to another
    user.
    """
    social = UserSocialAuth.get_social_auth(strategy.backend.name, uid)
    if social:
        if user and social.user != user:
            # Delete UserSocialAuth pairing so this account can now connect
            social.delete()
            social = None
        elif not user:
            user = social.user
    return {'social': social,
            'user': user,
            'is_new': user is None,
            'new_association': False}
def social_auth_user(strategy, uid, user=None, *args, **kwargs):
    """Return UserSocialAuth account for backend/uid pair or None if it
    doesn't exists.

    Delete UserSocialAuth if UserSocialAuth entry belongs to another
    user.
    """
    social = UserSocialAuth.get_social_auth(strategy.backend.name, uid)
    if social:
        if user and social.user != user:
            # Delete UserSocialAuth pairing so this account can now connect
            social.delete()
            social = None
        elif not user:
            user = social.user
    return {
        'social': social,
        'user': user,
        'is_new': user is None,
        'new_association': False
    }
Esempio n. 10
0
def load_extra_data(backend, details, response, uid, user, social_user=None, *args, **kwargs):
    """
    Load extra data from provider and store it on current UserSocialAuth extra_data field.
    """
    social_user = social_user or UserSocialAuth.get_social_auth(backend.name, uid)
    # create verified email address
    if kwargs['is_new'] and EMAIL_CONFIRMATION:
        from ..models import EmailAddress
        # check if email exist before creating it
        # we might be associating an exisiting user
        if EmailAddress.objects.filter(email=user.email).count() < 1:
            EmailAddress.objects.create(user=user,
                                        email=user.email,
                                        verified=True,
                                        primary=True)

    if social_user:
        extra_data = backend.extra_data(user, uid, response, details)
        if kwargs.get('original_email') and 'email' not in extra_data:
            extra_data['email'] = kwargs.get('original_email')
        # update extra data if anything has changed
        if extra_data and social_user.extra_data != extra_data:
            if social_user.extra_data:
                social_user.extra_data.update(extra_data)
            else:
                social_user.extra_data = extra_data
            social_user.save()
        # fetch additional data from facebook on creation
        if backend.name == 'facebook' and kwargs['is_new']:
            response = json.loads(requests.get('https://graph.facebook.com/%s?access_token=%s' % (extra_data['id'], extra_data['access_token'])).content)
            try:
                user.city, user.country = response.get('hometown').get('name').split(', ')
            except AttributeError:
                pass
            try:
                user.birth_date = datetime.strptime(response.get('birthday'), '%m/%d/%Y').date()
            except AttributeError:
                pass
            user.save()
        return {'social_user': social_user}
Esempio n. 11
0
def youtube_embed_url(user):
    try:
        return UserSocialAuth.get_social_auth(
            'google-oauth2', user.email).extra_data['image']['url']
    except:
        return '/static/global/images/placeholder/user.png'
Esempio n. 12
0
def youtube_embed_url(user):
    try:
        return UserSocialAuth.get_social_auth('google-oauth2',user.email).extra_data['image']['url']
    except:
        return '/static/global/images/placeholder/user.png'