Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StoreIdViewMixin and add samesite to storeid cookie #1012

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions rdmo/core/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import logging

from django.conf import settings
Expand Down Expand Up @@ -83,6 +84,18 @@ def get(self, request, *args, **kwargs):
return super().get(self, request, *args, **kwargs)


class StoreIdViewMixin(View):

def render_to_response(self, context, **response_kwargs):
response = super().render_to_response(context, **response_kwargs)
response.set_cookie('storeid', self.get_store_id(), samesite='Lax')
return response

def get_store_id(self):
session_key = self.request.session.session_key or 'anonymous'
return hashlib.sha256(session_key.encode()).hexdigest()


class RedirectViewMixin(View):

def post(self, request, *args, **kwargs):
Expand Down
12 changes: 2 additions & 10 deletions rdmo/management/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hashlib
import logging

from django.contrib.auth.mixins import LoginRequiredMixin
Expand All @@ -7,22 +6,15 @@
from rules import test_rule
from rules.contrib.views import PermissionRequiredMixin as RulesPermissionRequiredMixin

from rdmo.core.views import CSRFViewMixin, PermissionRedirectMixin
from rdmo.core.views import CSRFViewMixin, PermissionRedirectMixin, StoreIdViewMixin

logger = logging.getLogger(__name__)


class ManagementView(LoginRequiredMixin, PermissionRedirectMixin, RulesPermissionRequiredMixin,
CSRFViewMixin, TemplateView):
CSRFViewMixin, StoreIdViewMixin, TemplateView):
template_name = 'management/management.html'

def has_permission(self):
# Use test_rule from rules for permissions check
return test_rule('management.can_view_management', self.request.user, self.request.site)

def render_to_response(self, context, **response_kwargs):
storeid = hashlib.sha256(self.request.session.session_key.encode()).hexdigest()

response = super().render_to_response(context, **response_kwargs)
response.set_cookie('storeid', storeid)
return response
15 changes: 3 additions & 12 deletions rdmo/projects/views/project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hashlib
import logging

from django.conf import settings
Expand All @@ -15,7 +14,7 @@
from django.views.generic.edit import FormMixin

from rdmo.core.plugins import get_plugin, get_plugins
from rdmo.core.views import CSRFViewMixin, ObjectPermissionMixin, RedirectViewMixin
from rdmo.core.views import CSRFViewMixin, ObjectPermissionMixin, RedirectViewMixin, StoreIdViewMixin
from rdmo.questions.models import Catalog
from rdmo.questions.utils import get_widgets
from rdmo.tasks.models import Task
Expand All @@ -26,18 +25,10 @@

logger = logging.getLogger(__name__)

class ProjectsView(LoginRequiredMixin, CSRFViewMixin, TemplateView):
template_name = 'projects/projects.html'

# def has_permission(self):
# # Use test_rule from rules for permissions check
# return test_rule('projects.can_view_all_projects', self.request.user, self.request.site)
def render_to_response(self, context, **response_kwargs):
storeid = hashlib.sha256(self.request.session.session_key.encode()).hexdigest()
class ProjectsView(LoginRequiredMixin, CSRFViewMixin, StoreIdViewMixin, TemplateView):
template_name = 'projects/projects.html'

response = super().render_to_response(context, **response_kwargs)
response.set_cookie('storeid', storeid)
return response

class ProjectDetailView(ObjectPermissionMixin, DetailView):
model = Project
Expand Down
Loading