Question

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 ?

Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top