def test_is_authenticated(self): """Test that the 'is_authenticated' compatibility function is working. Bit of explanation: the `is_authenticated` property on request.user is *always* set to 'False' for AnonymousUser, and it is *always* set to 'True' for *any* other (i.e. identified/authenticated) user. So, the logic of this test is to ensure that compat.is_authenticated() returns the correct value based on whether or not the User is an anonymous user (simulating what goes on in the real request.user). """ # Test compat.is_authenticated for anonymous users self.user = auth.get_user(self.client) if django.VERSION < (1, 10): assert self.user.is_anonymous() else: assert self.user.is_anonymous assert not compat.is_authenticated(self.user) # Setup some other user, which is *not* anonymous, and check # compat.is_authenticated self.user = User.objects.create(username="******", email="*****@*****.**", password="******") if django.VERSION < (1, 10): assert not self.user.is_anonymous() else: assert not self.user.is_anonymous assert compat.is_authenticated(self.user)
def test_is_authenticated(self): """Test that the 'is_authenticated' compatibility function is working. Bit of explanation: the `is_authenticated` property on request.user is *always* set to 'False' for AnonymousUser, and it is *always* set to 'True' for *any* other (i.e. identified/authenticated) user. So, the logic of this test is to ensure that compat.is_authenticated() returns the correct value based on whether or not the User is an anonymous user (simulating what goes on in the real request.user). """ # Test compat.is_authenticated for anonymous users self.user = auth.get_user(self.client) if django.VERSION < (1, 10): assert self.user.is_anonymous() else: assert self.user.is_anonymous assert not compat.is_authenticated(self.user) # Setup some other user, which is *not* anonymous, and check # compat.is_authenticated self.user = User.objects.create( username="******", email="*****@*****.**", password="******" ) if django.VERSION < (1, 10): assert not self.user.is_anonymous() else: assert not self.user.is_anonymous assert compat.is_authenticated(self.user)
def process_request(self, request): """ Gets the current user from the request and prepares and connects a signal receiver with the user already attached to it. """ # Initialize thread local storage threadlocal.auditlog = { 'signal_duid': (self.__class__, time.time()), 'remote_addr': request.META.get('REMOTE_ADDR'), } # In case of proxy, set 'original' address forward_for_addr = request.META.get('HTTP_X_FORWARDED_FOR') if forward_for_addr: threadlocal.auditlog['remote_addr'] = forward_for_addr.split(',')[0] # Connect signal for automatic logging if hasattr(request, 'user') and is_authenticated(request.user): threadlocal_duid = threadlocal.auditlog['signal_duid'] set_actor = partial( self.set_actor, user=request.user, signal_duid=threadlocal_duid, ) pre_save.connect(set_actor, sender=LogEntry, dispatch_uid=threadlocal_duid, weak=False)
def process_request(self, request): if request.META.get('HTTP_X_FORWARDED_FOR'): audit_ip = request.META.get('HTTP_X_FORWARDED_FOR').split(',')[0] else: audit_ip = request.META.get('REMOTE_ADDR') set_audit_info( audit_ip, request.user if ( (hasattr(request, 'user') and is_authenticated(request.user)) ) else None )
def process_request(self, request): """ Gets the current user from the request and prepares and connects a signal receiver with the user already attached to it. """ # Initialize thread local storage threadlocal.auditlog = { 'signal_duid': (self.__class__, time.time()), 'remote_addr': request.META.get('REMOTE_ADDR'), } # In case of proxy, set 'original' address if request.META.get('HTTP_X_FORWARDED_FOR'): threadlocal.auditlog['remote_addr'] = request.META.get('HTTP_X_FORWARDED_FOR').split(',')[0] # Connect signal for automatic logging if hasattr(request, 'user') and is_authenticated(request.user): set_actor = curry(self.set_actor, user=request.user, signal_duid=threadlocal.auditlog['signal_duid']) pre_save.connect(set_actor, sender=LogEntry, dispatch_uid=threadlocal.auditlog['signal_duid'], weak=False)
def process_request(self, request): """ Gets the current user from the request and prepares and connects a signal receiver with the user already attached to it. """ # Initialize thread local storage threadlocal.auditlog = { 'signal_duid': (self.__class__, time.time()), 'remote_addr': request.META.get('REMOTE_ADDR'), 'actor_pk': None, 'actor_name': None, } # In case of proxy, set 'original' address if request.META.get('HTTP_X_FORWARDED_FOR'): threadlocal.auditlog['remote_addr'] = request.META.get('HTTP_X_FORWARDED_FOR').split(',')[0] # Connect signal and add actor details for automatic logging if hasattr(request, 'user') and is_authenticated(request.user): threadlocal.auditlog['actor_pk'] = request.user.pk threadlocal.auditlog['actor_name'] = smart_text(request.user) set_actor = curry(self.set_actor, user=request.user, signal_duid=threadlocal.auditlog['signal_duid']) pre_save.connect(set_actor, sender=LogEntry, dispatch_uid=threadlocal.auditlog['signal_duid'], weak=False)