Skip to content

Commit

Permalink
fix: access level control on handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Dec 31, 2024
1 parent 5c31acb commit 14507b3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/cms/contexts/handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from django.conf import settings
from django.core.exceptions import PermissionDenied

from . models import WebPath


def _get_site_from_host(request):
requested_site = re.match(r'^[a-zA-Z0-9\.\-\_]*',
request.get_host()).group()

website = get_object_or_404(WebSite,
domain=requested_site,
is_active=True)
return website


class BaseContentHandler(object):
template = "default_template.html"

Expand Down Expand Up @@ -59,4 +71,16 @@ def as_view(self): # pragma: no cover
open
returns a rendered page
"""
raise NotImplementedError()
# access level
website = _get_site_from_host(self.request)
access_level = webpath.get_access_level()
if access_level == '0':
pass
elif not request.user.is_authenticated:
return redirect(f"//{settings.MAIN_DOMAIN}{settings.LOGIN_URL}?next=//{website.domain}{webpath.get_full_path()}")
elif access_level == '2' or request.user.is_superuser:
pass
elif getattr(request.user, access_level, None):
pass
else:
raise PermissionDenied
6 changes: 3 additions & 3 deletions src/cms/contexts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
# from django.contrib.admin.models import LogEntry, CHANGE
from django.contrib.admin.models import CHANGE
from django.contrib.contenttypes.models import ContentType
from django.template.loader import get_template, render_to_string
from django.template.exceptions import (TemplateDoesNotExist,
TemplateSyntaxError)
from django.utils import translation
from django.utils.module_loading import import_string
from django.utils.translation import gettext as _
from django.utils.safestring import mark_safe
from django.template.loader import get_template, render_to_string
from django.template.exceptions import (TemplateDoesNotExist,
TemplateSyntaxError)

from cms.templates.models import Log

Expand Down
1 change: 1 addition & 0 deletions src/cms/contexts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
app_settings.SITEMAP_WEBPATHS_PRIORITY)
ROBOTS_SETTINGS = getattr(settings, 'ROBOTS_SETTINGS', app_settings.ROBOTS_SETTINGS)


def _get_site_from_host(request):
requested_site = re.match(r'^[a-zA-Z0-9\.\-\_]*',
request.get_host()).group()
Expand Down
6 changes: 6 additions & 0 deletions src/cms/publications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def __init__(self, **kwargs):
self.webpath = self.pub_context.webpath

def as_view(self):
super(PublicationViewHandler, self).as_view()

if not self.pub_context: return Http404()

# i18n
Expand Down Expand Up @@ -90,6 +92,8 @@ def breadcrumbs(self):
return (leaf,)

def as_view(self):
super(PublicationListHandler, self).as_view()

category = None
category_name = self.request.GET.get('category_name')
if category_name:
Expand Down Expand Up @@ -194,6 +198,8 @@ def item_extra_kwargs(self, item):
return {'content_encoded': self.item_content_encoded(item)}

def as_view(self):
super(PublicationRssHandler, self).as_view()

match_dict = self.match.groupdict()
self.page = Page.objects.filter(is_active=True,
webpath__site=self.website,
Expand Down

0 comments on commit 14507b3

Please sign in to comment.