|
| 1 | +.. _access_control: |
| 2 | + |
| 3 | +View decoration |
| 4 | +=============== |
| 5 | + |
| 6 | +The ``django-plotly-dash`` views, as served by Django, can be wrapped with |
| 7 | +an arbitrary decoration function. This allows the use |
| 8 | +of the Django `login_required <https://docs.djangoproject.com/en/2.1/topics/auth/default/#the-login-required-decorator>`_ view decorator |
| 9 | +as well as enabling more specialised and fine-grained control. |
| 10 | + |
| 11 | +The ``login_required`` decorator |
| 12 | +---------------------------- |
| 13 | + |
| 14 | +The ``login_required`` decorator from the Django authentication system can be used as a view decorator. A wrapper function is provided |
| 15 | +in ``django_plotly_dash.access``. |
| 16 | + |
| 17 | +.. code-block:: python |
| 18 | +
|
| 19 | + PLOTLY_DASH = { |
| 20 | +
|
| 21 | + ... |
| 22 | + # Name of view wrapping function |
| 23 | + "view_decorator": "django_plotly_dash.access.login_required", |
| 24 | + ... |
| 25 | + } |
| 26 | +
|
| 27 | +Note that the view wrapping is on all of the ``django-plotly-dash`` views. |
| 28 | + |
| 29 | +Fine-grained control |
| 30 | +-------------------- |
| 31 | + |
| 32 | +The view decoration function is called for each variant exposed in |
| 33 | +the ``django_plotly_dash.urls`` file. As well as the |
| 34 | +underlying view function, each call to the decorator is given the name of the |
| 35 | +route, as used by ``django.urls.reverse``, the specific url fragment for the view, and a name |
| 36 | +describing the type of view. |
| 37 | + |
| 38 | +From this information, it is possible to implement view-specific wrapping of the view functions, and |
| 39 | +in turn the wrapper functions can then use the request content, along with other information, to control |
| 40 | +access to the underlying view function. |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + from django.views.decorators.csrf import csrf_exempt |
| 45 | +
|
| 46 | + def check_access_permitted(request, **kwargs): |
| 47 | + # See if access is allowed; if so return True |
| 48 | + # This function is called on each request |
| 49 | +
|
| 50 | + ... |
| 51 | +
|
| 52 | + return True |
| 53 | +
|
| 54 | + def user_app_control(view_function, name=None, **kwargs): |
| 55 | + # This function is called on the registration of each django-plotly-dash view |
| 56 | + # name is one of main component-suites routes layout dependencies update-component |
| 57 | +
|
| 58 | + def wrapped_view(request, *args, **kwargs): |
| 59 | + is_permitted = check_access_permitted(request, **kwargs) |
| 60 | + if not is_permitted: |
| 61 | + # Access not permitted, so raise error or generate an appropriate response |
| 62 | +
|
| 63 | + ... |
| 64 | +
|
| 65 | + else: |
| 66 | + return view_function(request, *args, **kwargs) |
| 67 | +
|
| 68 | + if getattr(view_function,"csrf_exempt",False): |
| 69 | + return csrf_exempt(wrapped_view) |
| 70 | +
|
| 71 | + return wrapped_view |
| 72 | +
|
| 73 | +The above sketch highlights how access can be controlled based on each request. Note that the |
| 74 | +``csrf_exempt`` property of any wrapped view is preserved by the decoration function and this |
| 75 | +approach needs to be extended to other properties if needed. Also, this sketch only passes |
| 76 | +``kwargs`` to the permission function. |
| 77 | + |
| 78 | + |
0 commit comments