def connect_user(request, access_token=None, facebook_graph=None): ''' Given a request either - (if authenticated) connect the user - login - register ''' user = None graph = facebook_graph or get_facebook_graph(request, access_token) converter = get_instance_for('user_conversion', graph) assert converter.is_authenticated() facebook_data = converter.facebook_profile_data() force_registration = request.REQUEST.get('force_registration') or\ request.REQUEST.get('force_registration_hard') connect_facebook = to_bool(request.REQUEST.get('connect_facebook')) logger.debug('force registration is set to %s', force_registration) if connect_facebook and request.user.is_authenticated() and not force_registration: # we should only allow connect if users indicate they really want to connect # only when the request.CONNECT_FACEBOOK = 1 # if this isn't present we just do a login action = CONNECT_ACTIONS.CONNECT user = _connect_user(request, converter) else: email = facebook_data.get('email', False) email_verified = facebook_data.get('verified', False) kwargs = {} if email and email_verified: kwargs = {'facebook_email': email} auth_user = authenticate(facebook_id=facebook_data['id'], **kwargs) if auth_user and not force_registration: action = CONNECT_ACTIONS.LOGIN # Has the user registered without Facebook, using the verified FB # email address? # It is after all quite common to use email addresses for usernames update = getattr(auth_user, 'fb_update_required', False) if not auth_user.get_profile().facebook_id: update = True # login the user user = _login_user(request, converter, auth_user, update=update) else: action = CONNECT_ACTIONS.REGISTER # when force registration is active we should remove the old profile try: user = _register_user(request, converter, remove_old_connections=force_registration) except facebook_exceptions.AlreadyRegistered, e: # in Multithreaded environments it's possible someone beats us to # the punch, in that case just login logger.info('parallel register encountered, slower thread is doing a login') auth_user = authenticate( facebook_id=facebook_data['id'], **kwargs) action = CONNECT_ACTIONS.LOGIN user = _login_user(request, converter, auth_user, update=False)
def _connect(request, graph): ''' Handles the view logic around connect user - (if authenticated) connect the user - login - register We are already covered by the facebook_required_lazy decorator So we know we either have a graph and permissions, or the user denied the oAuth dialog ''' backend = get_registration_backend() context = RequestContext(request) connect_facebook = to_bool(request.REQUEST.get('connect_facebook')) logger.info('trying to connect using Facebook') if graph: logger.info('found a graph object') converter = get_instance_for('user_conversion', graph) authenticated = converter.is_authenticated() # Defensive programming :) if not authenticated: raise ValueError('didnt expect this flow') logger.info('Facebook is authenticated') facebook_data = converter.facebook_profile_data() # either, login register or connect the user try: action, user = connect_user( request, connect_facebook=connect_facebook) logger.info('Django facebook performed action: %s', action) except facebook_exceptions.IncompleteProfileError, e: # show them a registration form to add additional data warning_format = u'Incomplete profile data encountered with error %s' warn_message = warning_format % unicode(e) send_warning(warn_message, e=e, facebook_data=facebook_data) context['facebook_mode'] = True context['form'] = e.form #MJA modification to auto-register #backend.register(request, None, username='******', eamil='*****@*****.**', password1='Blahblah3') #return next_redirect(request) return render_to_response( facebook_settings.FACEBOOK_REGISTRATION_TEMPLATE, context_instance=context, ) except facebook_exceptions.AlreadyConnectedError, e: user_ids = [u.get_user_id() for u in e.users] ids_string = ','.join(map(str, user_ids)) additional_params = dict(already_connected=ids_string) return backend.post_error(request, additional_params)
def update_connection(request, graph): ''' A special purpose view for updating the connection with an existing user - updates the access token (already done in get_graph) - sets the facebook_id if nothing is specified - stores friends and likes if possible ''' converter = get_instance_for('user_conversion', graph) user = _connect_user(request, converter, overwrite=False) _update_likes_and_friends(request, user, converter) _update_access_token(user, graph) return user
def user_home(request, graph): context = RequestContext(request) backend = get_registration_backend() converter = get_instance_for("user_conversion", graph) if not graph: raise ValueError("No graph") else: x = graph.get("me/friends") f = x["data"] # return HttpResponse("Friends %s" % f) # graph.set('me/feed', message = 'Salesior is coming soon..', url = 'http://www.facebook.com') # send_mail('Successfull login in salesior', 'Congrats..', '*****@*****.**', ['*****@*****.**'], fail_silently = False) return render_to_response("patron/patron_homepage.html", {"friendlist": f}, context)
def connect_user(request, access_token=None, facebook_graph=None, connect_facebook=False): ''' Given a request either - (if authenticated) connect the user - login - register ''' user = None graph = facebook_graph or get_facebook_graph(request, access_token) converter = get_instance_for('user_conversion', graph) assert converter.is_authenticated() facebook_data = converter.facebook_profile_data() force_registration = request.POST.get('force_registration') or \ request.GET.get('force_registration') or \ request.POST.get('force_registration_hard') or \ request.GET.get('force_registration_hard') logger.debug('force registration is set to %s', force_registration) if connect_facebook and request.user.is_authenticated( ) and not force_registration: # we should only allow connect if users indicate they really want to connect # only when the request.CONNECT_FACEBOOK = 1 # if this isn't present we just do a login action = CONNECT_ACTIONS.CONNECT # default behaviour is not to overwrite old data user = _connect_user(request, converter, overwrite=True) else: email = facebook_data.get('email', False) email_verified = facebook_data.get('verified', False) kwargs = {} if email and email_verified: kwargs = {'facebook_email': email} auth_user = authenticate(facebook_id=facebook_data['id'], **kwargs) if auth_user and not force_registration: action = CONNECT_ACTIONS.LOGIN # Has the user registered without Facebook, using the verified FB # email address? # It is after all quite common to use email addresses for usernames update = getattr(auth_user, 'fb_update_required', False) profile = try_get_profile(auth_user) current_facebook_id = get_user_attribute(auth_user, profile, 'facebook_id') if not current_facebook_id: update = True # login the user user = _login_user(request, converter, auth_user, update=update) else: action = CONNECT_ACTIONS.REGISTER # when force registration is active we should remove the old # profile try: user = _register_user( request, converter, remove_old_connections=force_registration) except facebook_exceptions.AlreadyRegistered as e: # in Multithreaded environments it's possible someone beats us to # the punch, in that case just login logger.info( 'parallel register encountered, slower thread is doing a login' ) auth_user = authenticate(facebook_id=facebook_data['id'], **kwargs) if not auth_user: # We don't have a valid user so raise raise e action = CONNECT_ACTIONS.LOGIN user = _login_user(request, converter, auth_user, update=False) _update_likes_and_friends(request, user, converter) _update_access_token(user, graph) logger.info('connect finished with action %s', action) return action, user
def _connect(request, graph): ''' Handles the view logic around connect user - (if authenticated) connect the user - login - register We are already covered by the facebook_required_lazy decorator So we know we either have a graph and permissions, or the user denied the oAuth dialog ''' backend = get_registration_backend() context = RequestContext(request) connect_facebook = to_bool(request.REQUEST.get('connect_facebook')) logger.info('trying to connect using Facebook') if graph: logger.info('found a graph object') converter = get_instance_for('user_conversion', graph) authenticated = converter.is_authenticated() # Defensive programming :) if not authenticated: raise ValueError('didnt expect this flow') logger.info('Facebook is authenticated') facebook_data = converter.facebook_profile_data() # either, login register or connect the user try: action, user = connect_user(request, connect_facebook=connect_facebook) logger.info('Django facebook performed action: %s', action) except facebook_exceptions.IncompleteProfileError as e: # show them a registration form to add additional data warning_format = 'Incomplete profile data encountered with error %s' warn_message = warning_format % str(e) send_warning(warn_message, e=e, facebook_data=facebook_data) context['facebook_mode'] = True context['form'] = e.form return render_to_response( facebook_settings.FACEBOOK_REGISTRATION_TEMPLATE, context_instance=context, ) except facebook_exceptions.AlreadyConnectedError as e: user_ids = [u.get_user_id() for u in e.users] ids_string = ','.join(map(str, user_ids)) additional_params = dict(already_connected=ids_string) return backend.post_error(request, additional_params) response = backend.post_connect(request, user, action) if action is CONNECT_ACTIONS.LOGIN: pass elif action is CONNECT_ACTIONS.CONNECT: # connect means an existing account was attached to facebook messages.info( request, _("You have connected your account " "to %s's facebook profile") % facebook_data['name']) elif action is CONNECT_ACTIONS.REGISTER: # hook for tying in specific post registration functionality response.set_cookie('fresh_registration', user.id) else: # the user denied the request additional_params = dict(fb_error_or_cancel='1') response = backend.post_error(request, additional_params) return response
def _connect(request, graph): ''' Handles the view logic around connect user - (if authenticated) connect the user - login - register We are already covered by the facebook_required_lazy decorator So we know we either have a graph and permissions, or the user denied the oAuth dialog ''' backend = get_registration_backend() context = RequestContext(request) connect_facebook = to_bool(request.REQUEST.get('connect_facebook')) logger.info('C01: trying to connect using Facebook') if graph: logger.info('C02: found a graph object') converter = get_instance_for('user_conversion', graph) authenticated = converter.is_authenticated() # Defensive programming :) if not authenticated: logger.info('C04: not authenticated') raise ValueError('didnt expect this flow') logger.info('C05: Facebook is authenticated') facebook_data = converter.facebook_profile_data() # either, login register or connect the user try: action, user = connect_user( request, connect_facebook=connect_facebook) logger.info('Django facebook performed action: %s', action) # If client or subclient were detected redirect them to client login page if action == CONNECT_ACTIONS.CLIENT_REDIRECT: return redirect("facebook_client_login") except facebook_exceptions.IncompleteProfileError, e: # show them a registration form to add additional data warning_format = u'Incomplete profile data encountered with error %s' warn_message = warning_format % unicode(e) logger.info(warn_message) send_warning(warn_message, e=e, facebook_data=facebook_data) context['facebook_mode'] = True context['blocked_email'] = e.form.data['email'] e.form.data['email'] = u'' context['form'] = e.form # Style register page layout = get_layout(request=request) logger.info('C06: Got layout %s' % layout) context['layout'] = layout return render_to_response( backend.get_registration_template(), context_instance=context, ) except facebook_exceptions.AlreadyConnectedError, e: logger.info('Already connected error') user_ids = [u.get_user_id() for u in e.users] ids_string = ','.join(map(str, user_ids)) additional_params = dict(already_connected=ids_string) return backend.post_error(request, additional_params)
def _connect(request, graph): ''' Handles the view logic around connect user - (if authenticated) connect the user - login - register We are already covered by the facebook_required_lazy decorator So we know we either have a graph and permissions, or the user denied the oAuth dialog ''' backend = get_registration_backend() context = RequestContext(request) connect_facebook = to_bool(request.REQUEST.get('connect_facebook')) logger.info('trying to connect using Facebook') if graph: logger.info('found a graph object') converter = get_instance_for('user_conversion', graph) authenticated = converter.is_authenticated() # Defensive programming :) if not authenticated: raise ValueError('didnt expect this flow') logger.info('Facebook is authenticated') facebook_data = converter.facebook_profile_data() # either, login register or connect the user try: action, user = connect_user( request, connect_facebook=connect_facebook) logger.info('Django facebook performed action: %s', action) except facebook_exceptions.IncompleteProfileError as e: # show them a registration form to add additional data warning_format = u'Incomplete profile data encountered with error %s' warn_message = warning_format % unicode(e) send_warning(warn_message, e=e, facebook_data=facebook_data) context['facebook_mode'] = True context['form'] = e.form return render_to_response( backend.get_registration_template(), context_instance=context, ) except facebook_exceptions.AlreadyConnectedError as e: user_ids = [u.get_user_id() for u in e.users] ids_string = ','.join(map(str, user_ids)) additional_params = dict(already_connected=ids_string) return backend.post_error(request, additional_params) response = backend.post_connect(request, user, action) if action is CONNECT_ACTIONS.LOGIN: pass elif action is CONNECT_ACTIONS.CONNECT: # connect means an existing account was attached to facebook messages.info(request, _("You have connected your account " "to %s's facebook profile") % facebook_data['name']) elif action is CONNECT_ACTIONS.REGISTER: # hook for tying in specific post registration functionality response.set_cookie('fresh_registration', user.id) else: # the user denied the request additional_params = dict(fb_error_or_cancel='1') response = backend.post_error(request, additional_params) return response
def _connect(request, facebook_login, graph): ''' Handles the view logic around connect user - (if authenticated) connect the user - login - register We are already covered by the facebook_required_lazy decorator So we know we either have a graph and permissions, or the user denied the oAuth dialog ''' backend = get_registration_backend() context = RequestContext(request) if facebook_login: logger.info('trying to connect using Facebook') if graph: logger.info('found a graph object') converter = get_instance_for('user_conversion', graph) authenticated = converter.is_authenticated() # Defensive programming :) if not authenticated: raise ValueError('didnt expect this flow') logger.info('Facebook is authenticated') facebook_data = converter.facebook_profile_data() # either, login register or connect the user try: action, user = connect_user(request) logger.info('Django facebook performed action: %s', action) except facebook_exceptions.IncompleteProfileError, e: # show them a registration form to add additional data warning_format = u'Incomplete profile data encountered with error %s' warn_message = warning_format % e.message send_warning(warn_message, e=e, facebook_data=facebook_data) context['facebook_mode'] = True context['form'] = e.form return render_to_response( facebook_settings.FACEBOOK_REGISTRATION_TEMPLATE, context_instance=context, ) except facebook_exceptions.AlreadyConnectedError, e: user_ids = [u.user_id for u in e.users] ids_string = ','.join(map(str, user_ids)) return error_next_redirect( request, additional_params=dict(already_connected=ids_string)) if action is CONNECT_ACTIONS.CONNECT: # connect means an existing account was attached to facebook messages.info(request, _("You have connected your account " "to %s's facebook profile") % facebook_data['name']) elif action is CONNECT_ACTIONS.REGISTER: # hook for tying in specific post registration functionality response = backend.post_registration_redirect( request, user) # compatibility for Django registration backends which return redirect tuples instead of a response if not isinstance(response, HttpResponse): to, args, kwargs = response response = redirect(to, *args, **kwargs) return response
def connect_user(request, access_token=None, facebook_graph=None, connect_facebook=False): """ Given a request either - (if authenticated) connect the user - login - register """ user = None graph = facebook_graph or get_facebook_graph(request, access_token) converter = get_instance_for("user_conversion", graph) assert converter.is_authenticated() facebook_data = converter.facebook_profile_data() force_registration = request.REQUEST.get("force_registration") or request.REQUEST.get("force_registration_hard") logger.debug("force registration is set to %s", force_registration) if connect_facebook and request.user.is_authenticated() and not force_registration: # we should only allow connect if users indicate they really want to connect # only when the request.CONNECT_FACEBOOK = 1 # if this isn't present we just do a login action = CONNECT_ACTIONS.CONNECT # default behaviour is not to overwrite old data user = _connect_user(request, converter, overwrite=True) else: email = facebook_data.get("email", False) email_verified = facebook_data.get("verified", False) kwargs = {} if email and email_verified: kwargs = {"facebook_email": email} auth_user = authenticate(facebook_id=facebook_data["id"], **kwargs) if auth_user and not force_registration: action = CONNECT_ACTIONS.LOGIN # Has the user registered without Facebook, using the verified FB # email address? # It is after all quite common to use email addresses for usernames update = getattr(auth_user, "fb_update_required", False) profile = try_get_profile(auth_user) current_facebook_id = get_user_attribute(auth_user, profile, "facebook_id") if not current_facebook_id: update = True # login the user user = _login_user(request, converter, auth_user, update=update) else: action = CONNECT_ACTIONS.REGISTER # when force registration is active we should remove the old # profile try: user = _register_user(request, converter, remove_old_connections=force_registration) except facebook_exceptions.AlreadyRegistered, e: # in Multithreaded environments it's possible someone beats us to # the punch, in that case just login logger.info("parallel register encountered, slower thread is doing a login") auth_user = authenticate(facebook_id=facebook_data["id"], **kwargs) if not auth_user: # We don't have a valid user so raise raise e action = CONNECT_ACTIONS.LOGIN user = _login_user(request, converter, auth_user, update=False)
def connect_user(request, access_token=None, facebook_graph=None, connect_facebook=False): ''' Given a request either - (if authenticated) connect the user - login - register ''' logger.info('CU01: Start data access_token %s' % access_token) logger.info('CU02: Start data facebook_graph %s' % facebook_graph) logger.info('CU03: Connect facebook %s' % connect_facebook) user = None graph = facebook_graph or get_facebook_graph(request, access_token) converter = get_instance_for('user_conversion', graph) assert converter.is_authenticated() facebook_data = converter.facebook_profile_data() force_registration = request.REQUEST.get('force_registration') or\ request.REQUEST.get('force_registration_hard') logger.info('CU04: Force registration %s' % force_registration) if connect_facebook and request.user.is_authenticated() and not force_registration: logger.info('CU05: User is authenticated %s' % request.user.is_authenticated()) # we should only allow connect if users indicate they really want to connect # only when the request.CONNECT_FACEBOOK = 1 # if this isn't present we just do a login action = CONNECT_ACTIONS.CONNECT logger.info('CU06: Action %s' % action) # default behaviour is not to overwrite old data user = _connect_user(request, converter, overwrite=True) else: logger.info('CU07: Bad day') email = facebook_data.get('email', False) logger.info('CU08: Email %s' % email) email_verified = facebook_data.get('verified', False) kwargs = {} if email and email_verified: logger.info('CU09: Facebook email added to kwargs') kwargs = {'facebook_email': email} # social-auth support # trying to find social user with given email logger.info('CU10: Social auth support') social_user = get_user_model().objects.filter(email=email) logger.info('CU11: Social users found %s' % social_user) social_user = social_user[0] if social_user else None logger.info('CU12: Social user %s' % social_user) if social_user and UserSocialAuth.objects.filter(user__id=social_user.id).exists(): logger.info('CU13: Social user exists') try: current_user_profile = try_get_profile(social_user) logger.info('CU14: Got profile for social user %s' % current_user_profile) except: logger.info('CU15: No profile') profile_model = get_profile_model() logger.info('CU16: Profile model %s' % profile_model) profile_model.objects.create(user=social_user) logger.info('CU17: Profile object created') auth_user = authenticate(facebook_id=facebook_data['id'], **kwargs) logger.info('CU19: Aunthenticated user %s ' % auth_user) if auth_user and not force_registration: referer_url = request.META.get('HTTP_REFERER', None) if referer_url: urlparsed = urlparse(referer_url) is_facebook = urlparsed.netloc.endswith('facebook.com') else: is_facebook = False logger.info('CU32 is_facebook: %s, referer_url: %s'%(is_facebook, referer_url)) if auth_user.is_client() or auth_user.is_subclient() and not is_facebook: action = CONNECT_ACTIONS.CLIENT_REDIRECT logger.info('CU31: Client or subclient detected. Action: CLIENT_REDIRECT') return action, auth_user action = CONNECT_ACTIONS.LOGIN logger.info('CU20: Action %s' % action) # Has the user registered without Facebook, using the verified FB # email address? # It is after all quite common to use email addresses for usernames update = getattr(auth_user, 'fb_update_required', False) logger.info('CU21: Update %s' % update) profile = try_get_profile(auth_user) logger.info('CU22: Got profile %s' % profile) current_facebook_id = get_user_attribute( auth_user, profile, 'facebook_id') logger.info('CU23: Current facebook_id %s' % current_facebook_id) if not current_facebook_id: update = True # login the user user = _login_user(request, converter, auth_user, update=update) else: action = CONNECT_ACTIONS.REGISTER logger.info('CU24: Action %s' % action) # when force registration is active we should remove the old # profile try: user = _register_user(request, converter, remove_old_connections=force_registration) logger.info('CU25: User registered %s' % user) except facebook_exceptions.AlreadyRegistered, e: # in Multithreaded environments it's possible someone beats us to # the punch, in that case just login logger.info( 'CU26: parallel register encountered, slower thread is doing a login') auth_user = authenticate( facebook_id=facebook_data['id'], **kwargs) logger.info('CU27: Auth user %s' % auth_user) if not auth_user: logger.info('CU28: No auth user') # We don't have a valid user so raise raise e logger.info('CU29: Login user') action = CONNECT_ACTIONS.LOGIN user = _login_user(request, converter, auth_user, update=False)
def connect_user(request, access_token=None, facebook_graph=None, connect_facebook=False): ''' Given a request either - (if authenticated) connect the user - login - register ''' user = None graph = facebook_graph or get_facebook_graph(request, access_token) converter = get_instance_for('user_conversion', graph) assert converter.is_authenticated() facebook_data = converter.facebook_profile_data() force_registration = request.REQUEST.get('force_registration') or\ request.REQUEST.get('force_registration_hard') logger.debug('force registration is set to %s', force_registration) if connect_facebook and request.user.is_authenticated() and not force_registration: # we should only allow connect if users indicate they really want to connect # only when the request.CONNECT_FACEBOOK = 1 # if this isn't present we just do a login action = CONNECT_ACTIONS.CONNECT # default behaviour is not to overwrite old data user = _connect_user(request, converter, overwrite=True) else: email = facebook_data.get('email', False) email_verified = facebook_data.get('verified', False) kwargs = {} if email and email_verified: # if email: kwargs = {'facebook_email': email} auth_user = authenticate(facebook_id=facebook_data['id'], **kwargs) print "AUTH USER",auth_user # if auth_user and not force_registration: if auth_user: print "Got the auth user" action = CONNECT_ACTIONS.LOGIN # Has the user registered without Facebook, using the verified FB # email address? # It is after all quite common to use email addresses for usernames update = getattr(auth_user, 'fb_update_required', False) profile = try_get_profile(auth_user) current_facebook_id = get_user_attribute( auth_user, profile, 'facebook_id') if not current_facebook_id: update = True # login the user user = _login_user(request, converter, auth_user, update=update) else: print "In register" action = CONNECT_ACTIONS.REGISTER # when force registration is active we should remove the old # profile try: user = _register_user(request, converter, remove_old_connections=force_registration,graph = graph) print user except facebook_exceptions.AlreadyRegistered as e: # in Multithreaded environments it's possible someone beats us to # the punch, in that case just login print "Got exception" logger.info( 'parallel register encountered, slower thread is doing a login') auth_user = authenticate( facebook_id=facebook_data['id'], **kwargs) if not auth_user: # We don't have a valid user so raise raise e action = CONNECT_ACTIONS.LOGIN user = _login_user(request, converter, auth_user, update=False) _update_likes_and_friends(request, user, converter) _update_access_token(user, graph) logger.info('connect finished with action %s', action) return action, user