0
votes

Django == 1.5.4

Django-CMS == 2.4.2

Page flag "login_required" didn't work. It works only in main page, but in ALL inner pages it didn't. There is no any code modification in cms plugin.

"login_required" is checking in only one file : /site-packages/cms/views.py in line 136:

# permission checks
if page.login_required and not request.user.is_authenticated():
    return redirect_to_login(urlquote(request.get_full_path()), settings.LOGIN_URL)

I set

print "check"

above it and as was expected - it prints only in main page...

Any ideas what may be the problem ?

1
Works for me. Did you publish your changes to the child page(s)? - Brandon
Found what it was... All problems in cms app_hook. There is only one function, which checks "login_required" flag - detail in "/site-packages/cms/views.py", which calls from "/site-packages/cms/urls.py" But when we add an app_hook to page - django loads only app_hook urls.py and ignored cms urls.py... And as the result - function 'detail' won't be called and checks will be ignored.. Shame... try to create a bag report... Maybe somebody can advise something ??? - user2919162
I see. That does make sense, since control is handed from the CMS page to a page that is not under its control. Perhaps the CMS could pass an attribute to the app-hooked page to indicate that an ancestor page was login_required. - Brandon
I try to make something like this.... see my answer below. And create a bug report: github.com/divio/django-cms/issues/2480 Interesting, what developers of django-cms will answer - user2919162

1 Answers

0
votes

Cause of the problem was found in comments in question. I have solved it by writing custom decorator which checks "login required" flag of a cms-page:

views.py (of app_hook):

from django.shortcuts import render
from project.decorators import check_login_required_flag

@check_login_required_flag
def index(request):
    """ private page index """    
    return render(request, 'private_room/index.html', {})

decorators.py:

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from cms.utils.page_resolver import get_page_from_request

def check_login_required_flag(view_func):
    """
    Decorator checks 'login required' flag of a cms-page and redirect
    unauthorized user to login page
    """
    def _wrapped_view_func(request, *args, **kwargs):
        page = get_page_from_request(request)
        if page and page.login_required:
            if not request.user.is_authenticated():
                return HttpResponseRedirect(reverse('login')+'?next='+request.path)
        return view_func(request, *args, **kwargs)
    return _wrapped_view_func