예제 #1
0
def _accommodate_legacy_pre_V1_1_0_users(access_token, amazon_user_email, amazon_user_id):

    # Check if there is a UserProfile (old workflow) for this amazon id
    if not UserProfile.objects.filter(amazon_id=amazon_user_id):
        return

    # If there was a user profile then we need to convert and scrub the user
    UserProfile.objects.get(amazon_id=amazon_user_id).delete()

    try:
        # This is a legacy condition covering users that were created before V1.1.0
        user = User.objects.get(username=amazon_user_email)
        student = Student.objects.get(user=user).id

        role_controller = RoleController(user)
        user_role = role_controller.role_by_entity_type_and_entity_id(
            RoleController.ENTITY_STUDENT,
            student,
            RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_STUDENT))
        create_linked_authentication_service(
            user_role,
            LinkedServiceController.SERVICE_AMAZON,
            amazon_user_id,
            {
                'amazon_user_email': amazon_user_email,
                'amazon_user_id': amazon_user_id,
                'amazon_access_token': access_token
            })
    except ObjectDoesNotExist:
        # It is possible that previously accommodation went wrong and not all objects are currently available
        # If that situation occurs - just leave it as that, it should happen only for sponsors
        pass
예제 #2
0
def _accommodate_legacy_pre_V1_1_0_users(access_token, amazon_user_email,
                                         amazon_user_id):

    # Check if there is a UserProfile (old workflow) for this amazon id
    if not UserProfile.objects.filter(amazon_id=amazon_user_id):
        return

    # If there was a user profile then we need to convert and scrub the user
    UserProfile.objects.get(amazon_id=amazon_user_id).delete()

    try:
        # This is a legacy condition covering users that were created before V1.1.0
        user = User.objects.get(username=amazon_user_email)
        student = Student.objects.get(user=user).id

        role_controller = RoleController(user)
        user_role = role_controller.role_by_entity_type_and_entity_id(
            RoleController.ENTITY_STUDENT, student,
            RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_STUDENT))
        create_linked_authentication_service(
            user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id,
            {
                'amazon_user_email': amazon_user_email,
                'amazon_user_id': amazon_user_id,
                'amazon_access_token': access_token
            })
    except ObjectDoesNotExist:
        # It is possible that previously accommodation went wrong and not all objects are currently available
        # If that situation occurs - just leave it as that, it should happen only for sponsors
        pass
예제 #3
0
def _process_amazon_login(access_token, amazon_user_email, amazon_user_id, request, amazon_user_name=None):

    # Check to see if we need to accomodate pre V1.1.0 accounts
    _accommodate_legacy_pre_V1_1_0_users(access_token, amazon_user_email, amazon_user_id)

    # Check to see if these credential are tied to a user role via an authentication service
    user_role = select_role_by_authentication_service(LinkedServiceController.SERVICE_AMAZON, amazon_user_id)

    next_url = None

    # If there is a role then get the user, else get the currently authenticate user else create a new user
    if user_role and user_role.user:
        # Case - Amazon Login to existing account
        user = user_role.user

        # Get the role controller for this user
        role_controller = RoleController(user)
    else:
        # First get the user or create one
        if request.user.is_authenticated():
            # Case - Add new amazon login based role to existing account
            user = request.user

            # Make a note on the spudder users that as there are multiple roles a password needs to be set for this user
            user.spudder_user.mark_as_password_required()

        else:
            # Case - first time registration
            user = User.objects.create_user(amazon_user_email, amazon_user_email, amazon_user_id)
            user.save()

        # If there is a school id then this is a student registration
        school_id = request.GET.get('school_id', None)
        if school_id:
            # Create the student
            student = create_student(user, school_id, request.GET.get('referrer', None))

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_STUDENT,
                student.id,
                RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_STUDENT))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role,
                LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id,
                {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })

        # If the account_type is sponsor, create a SponsorPage
        account_type = get_request_param(request, 'account_type')
        if account_type == 'sponsor':
            # Create sponsor
            sponsor = SponsorPage(sponsor=user)
            sponsor.save()

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_SPONSOR,
                sponsor.id,
                RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_SPONSOR))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role,
                LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id,
                {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })
            
        # If the account_type is fan, create a FanPage
        if account_type == 'fan':
            # Create fan
            fan, _ = FanPage.objects.get_or_create(fan=user)
            fan.save()

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_FAN,
                fan.id,
                RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role,
                LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id,
                {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })

        # If the account_type is club, create a Club entity in first stage (before registration as recipient)
        if account_type == 'club':
            inv = None
            if request.session['invitation_id']:
                inv = Invitation.objects.get(id=request.session['invitation_id'])
                if str(amazon_user_name) != str(TempClub.objects.get(id=inv.target_entity_id).name):
                    return HttpResponseRedirect('/spudderaffiliates/invitation/%s/incorrect_name' % inv.id)

            club, _ = Club.objects.get_or_create(
                name=amazon_user_name,
                amazon_email=amazon_user_email,
                amazon_id=amazon_user_id
            )
            if inv:
                club.affiliate = Affiliate.objects.get(name=inv.extras['affiliate_name'])
            club.save()

            club_admin, _ = ClubAdministrator.objects.get_or_create(admin=user, club=club)
            club_admin.save()

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_CLUB_ADMIN,
                club_admin.id,
                RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_CLUB_ADMIN))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role,
                LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id,
                {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })

            next_url = '/club/register/recipient'

    # Get and update the amazon auth record with the latest access token
    # Note that we don't use this at the moment but will in future - MG:20140708
    amazon_auth = get_authentication_wrapper(user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id)
    amazon_auth.update_amazon_access_token(access_token)

    if not next_url:
        next_url = request.GET.get('next', None) or user_role.home_page_path

    if not request.user.is_authenticated():
        if user.spudder_user.has_set_password:
            return redirect('/users/account/signin/%s?next=%s' % (user.id, next_url))
        user = authenticate(
            username=user_role.user.username,
            password=amazon_auth.user_password)
        if user:
            django.contrib.auth.login(request, user)
        else:
            raise Http404
    logging.info('%s?next=%s',change_role_url(user_role),next_url)

    return redirect('%s?next=%s' % (
        change_role_url(user_role),
        next_url))
예제 #4
0
def _process_amazon_login(access_token,
                          amazon_user_email,
                          amazon_user_id,
                          request,
                          amazon_user_name=None):

    # Check to see if we need to accomodate pre V1.1.0 accounts
    _accommodate_legacy_pre_V1_1_0_users(access_token, amazon_user_email,
                                         amazon_user_id)

    # Check to see if these credential are tied to a user role via an authentication service
    user_role = select_role_by_authentication_service(
        LinkedServiceController.SERVICE_AMAZON, amazon_user_id)

    next_url = None

    # If there is a role then get the user, else get the currently authenticate user else create a new user
    if user_role and user_role.user:
        # Case - Amazon Login to existing account
        user = user_role.user

        # Get the role controller for this user
        role_controller = RoleController(user)
    else:
        # First get the user or create one
        if request.user.is_authenticated():
            # Case - Add new amazon login based role to existing account
            user = request.user

            # Make a note on the spudder users that as there are multiple roles a password needs to be set for this user
            user.spudder_user.mark_as_password_required()

        else:
            # Case - first time registration
            user = User.objects.create_user(amazon_user_email,
                                            amazon_user_email, amazon_user_id)
            user.save()

        # If there is a school id then this is a student registration
        school_id = request.GET.get('school_id', None)
        if school_id:
            # Create the student
            student = create_student(user, school_id,
                                     request.GET.get('referrer', None))

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_STUDENT, student.id,
                RoleBase.RoleWrapperByEntityType(
                    RoleController.ENTITY_STUDENT))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role, LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id, {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })

        # If the account_type is sponsor, create a SponsorPage
        account_type = get_request_param(request, 'account_type')
        if account_type == 'sponsor':
            # Create sponsor
            sponsor = SponsorPage(sponsor=user)
            sponsor.save()

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_SPONSOR, sponsor.id,
                RoleBase.RoleWrapperByEntityType(
                    RoleController.ENTITY_SPONSOR))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role, LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id, {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })

        # If the account_type is fan, create a FanPage
        if account_type == 'fan':
            # Create fan
            fan, _ = FanPage.objects.get_or_create(fan=user)
            fan.save()

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_FAN, fan.id,
                RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role, LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id, {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })

        # If the account_type is club, create a Club entity in first stage (before registration as recipient)
        if account_type == 'club':
            inv = None
            if request.session['invitation_id']:
                inv = Invitation.objects.get(
                    id=request.session['invitation_id'])
                if str(amazon_user_name) != str(
                        TempClub.objects.get(id=inv.target_entity_id).name):
                    return HttpResponseRedirect(
                        '/spudderaffiliates/invitation/%s/incorrect_name' %
                        inv.id)

            club, _ = Club.objects.get_or_create(
                name=amazon_user_name,
                amazon_email=amazon_user_email,
                amazon_id=amazon_user_id)
            if inv:
                club.affiliate = Affiliate.objects.get(
                    name=inv.extras['affiliate_name'])
            club.save()

            club_admin, _ = ClubAdministrator.objects.get_or_create(admin=user,
                                                                    club=club)
            club_admin.save()

            # Get the Role controller for the current user
            role_controller = RoleController(user)

            # Get the user_role for this user/student combo
            user_role = role_controller.role_by_entity_type_and_entity_id(
                RoleController.ENTITY_CLUB_ADMIN, club_admin.id,
                RoleBase.RoleWrapperByEntityType(
                    RoleController.ENTITY_CLUB_ADMIN))

            # Store the amazon linked authentication service details
            create_linked_authentication_service(
                user_role, LinkedServiceController.SERVICE_AMAZON,
                amazon_user_id, {
                    'amazon_user_email': amazon_user_email,
                    'amazon_user_id': amazon_user_id,
                    'amazon_access_token': access_token
                })

            next_url = '/club/register/recipient'

    # Get and update the amazon auth record with the latest access token
    # Note that we don't use this at the moment but will in future - MG:20140708
    amazon_auth = get_authentication_wrapper(
        user_role, LinkedServiceController.SERVICE_AMAZON, amazon_user_id)
    amazon_auth.update_amazon_access_token(access_token)

    if not next_url:
        next_url = request.GET.get('next', None) or user_role.home_page_path

    if not request.user.is_authenticated():
        if user.spudder_user.has_set_password:
            return redirect('/users/account/signin/%s?next=%s' %
                            (user.id, next_url))
        user = authenticate(username=user_role.user.username,
                            password=amazon_auth.user_password)
        if user:
            django.contrib.auth.login(request, user)
        else:
            raise Http404
    logging.info('%s?next=%s', change_role_url(user_role), next_url)

    return redirect('%s?next=%s' % (change_role_url(user_role), next_url))