muccg / rdrf

The Rare Disease Registry Framework (RDRF) is an open source tool for the creation of web-based patient registries.
GNU Affero General Public License v3.0
15 stars 8 forks source link

Anonymous user error ( traceback) #1254

Closed id2359 closed 4 years ago

id2359 commented 4 years ago

Walked away from computer for a while ( I was on the proms page of cicclinical on stag


Request Method: | GET
-- | --
https://rdrf.ccgapps.com.au/cicclinical/ICHOMCRC/12/clinicalproms
2.1.15
AttributeError
'AnonymousUser' object has no attribute 'can_view'
/env/lib/python3.7/site-packages/rdrf/helpers/utils.py in <listcomp>, line 279
/env/bin/uwsgi
3.7.6
['.',  '/app',  '/usr/local/lib/python37.zip',  '/usr/local/lib/python3.7',  '/usr/local/lib/python3.7/lib-dynload',  '/env/lib/python3.7/site-packages']
Thu, 12 Mar 2020 14:00:57 +0800
gmaloneccg commented 4 years ago

There seems to be a period of time in which this error can be caught, but if the page is left for even longer, it refreshes in the background and lands on the router (which I think is the correct behaviour), whereupon logging in continues from when it timed out. This behaviour is not always true, however, and the AnonymousUser error can be intercepted (as seen above).

I have noticed this behaviour for some time but I have not reported it previously, I didn't know if it was a "real" error, as it were. Going back one page redirects to the router, and logging in picks up from when it timed out, but the attempted action that caused the traceback is not completed, and wipes unsaved data on the page.

jithendralal commented 4 years ago

Similar error on: http://localhost:8000/ICHOMCRC/patient/1/edit?just_created=True

TypeError TypeError: 'AnonymousUser' object is not iterable

File "/app/rdrf/rdrf/views/patient_view.py", line 704, in post registry_model=registry_model) File "/app/rdrf/registry/patients/admin_forms.py", line 224, in init if self._is_parent_editing_child(instance): File "/app/rdrf/registry/patients/admin_forms.py", line 275, in _is_parent_editing_child parent_guardian = ParentGuardian.objects.get(user=self.user)

gmaloneccg commented 4 years ago

Replicated:

Environment:

Request Method: GET
Request URL: https://rdrf.ccgapps.com.au/cicclinical/ICHOMCRC/47/clinicalproms

Django Version: 2.1.15
Python Version: 3.7.6
Installed Applications:
['django.contrib.contenttypes',
 'django.contrib.auth',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.staticfiles',
 'django.contrib.messages',
 'django_extensions',
 'django.contrib.admin',
 'messages_ui',
 'ajax_select',
 'explorer',
 'useraudit',
 'templatetag_handlebars',
 'iprestrict',
 'rest_framework',
 'anymail',
 'rdrf',
 'registry.groups',
 'registry.patients',
 'registry.common',
 'registry.genetic',
 'registration',
 'reversion',
 'storages',
 'django_otp',
 'django_otp.plugins.otp_static',
 'django_otp.plugins.otp_totp',
 'two_factor',
 'django_user_agents',
 'formtools',
 'session_security']
Installed Middleware:
('useraudit.middleware.RequestToThreadLocalMiddleware',
 'django.middleware.common.CommonMiddleware',
 'iprestrict.middleware.IPRestrictMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django_otp.middleware.OTPMiddleware',
 'registry.common.middleware.EnforceTwoFactorAuthMiddleware',
 'session_security.middleware.SessionSecurityMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django_user_agents.middleware.UserAgentMiddleware')

Traceback:

File "/env/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/env/lib/python3.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/env/lib/python3.7/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/env/lib/python3.7/site-packages/rdrf/views/proms_views.py" in get
  165.                                       patient_model)

File "/env/lib/python3.7/site-packages/rdrf/views/proms_views.py" in _build_context
  182.             "context_launcher": context_launcher.html,

File "/env/lib/python3.7/site-packages/rdrf/forms/components.py" in html
  57.         return self._fill_template()

File "/env/lib/python3.7/site-packages/rdrf/forms/components.py" in _fill_template
  64.             data = self._get_template_data()

File "/env/lib/python3.7/site-packages/rdrf/forms/components.py" in _get_template_data
  130.             "fixed_contexts": self._get_fixed_contexts(),

File "/env/lib/python3.7/site-packages/rdrf/forms/components.py" in _get_fixed_contexts
  303.                         fixed_context_group, rdrf_context):

File "/env/lib/python3.7/site-packages/rdrf/forms/components.py" in _get_visible_form_links
  325.                               self.current_form_name)

File "/env/lib/python3.7/site-packages/rdrf/helpers/utils.py" in get_form_links
  278.                 context_model=context_model) for form in container_model.forms

File "/env/lib/python3.7/site-packages/rdrf/helpers/utils.py" in <listcomp>
  279.             if not form.is_questionnaire and user.can_view(form) and form.applicable_to(patient_model)]

Exception Type: AttributeError at /ICHOMCRC/47/clinicalproms
Exception Value: 'AnonymousUser' object has no attribute 'can_view'
id2359 commented 4 years ago

That's good the bug is in a helper function in utils

jithendralal commented 4 years ago

The new decorator added causes a redirect to login page, if the request.user is AnonymousUser (which was the reported issue).

This was applied to form views and proms views.

gmaloneccg commented 4 years ago

Previous methods to force the event no longer work, and unable to recreate the bug locally or on staging, therefore marking as a pass.