Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed some Page.has_view_permission and added tests for that

  • Loading branch information...
commit 3739c105bcfadc9f0833e22368f43b49fb3382f1 1 parent b8b54ba
@ojii authored
Showing with 204 additions and 29 deletions.
  1. +18 −19 cms/models/pagemodel.py
  2. +6 −6 cms/tests/menu.py
  3. +180 −4 cms/tests/permmod.py
View
37 cms/models/pagemodel.py
@@ -1,8 +1,14 @@
# -*- coding: utf-8 -*-
-import copy
+from cms.exceptions import NoHomeFound
+from cms.models.managers import PageManager, PagePermissionsPermissionManager
+from cms.models.metaclasses import PageMetaClass
+from cms.models.placeholdermodel import Placeholder
+from cms.models.pluginmodel import CMSPlugin
+from cms.publisher.errors import MpttPublisherCantPublish
+from cms.utils import i18n, urlutils, page as page_utils
+from cms.utils.copy_plugins import copy_plugins_to
+from cms.utils.helpers import reversion_register
from datetime import datetime
-from os.path import join
-
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.exceptions import ObjectDoesNotExist
@@ -12,20 +18,14 @@
from django.db.models.fields.related import OneToOneRel
from django.shortcuts import get_object_or_404
from django.utils.translation import get_language, ugettext_lazy as _
+from menus.menu_pool import menu_pool
+from mptt.models import MPTTModel
+from os.path import join
+import copy
+
-from cms.exceptions import NoHomeFound
-from cms.models.managers import PageManager, PagePermissionsPermissionManager
-from cms.models.metaclasses import PageMetaClass
-from cms.models.placeholdermodel import Placeholder
-from cms.models.pluginmodel import CMSPlugin
-from cms.publisher.errors import MpttPublisherCantPublish
-from cms.utils.copy_plugins import copy_plugins_to
-from cms.utils.helpers import reversion_register
-from cms.utils import i18n, urlutils, page as page_utils
-from menus.menu_pool import menu_pool
-from mptt.models import MPTTModel
class Page(MPTTModel):
@@ -713,15 +713,14 @@ def has_view_permission(self, request):
if request.user.is_authenticated():
site = current_site(request)
+ global_perms_q = Q(can_view=True) & Q(
+ Q(sites__in=[site]) | Q(sites__isnull=True)
+ )
global_view_perms = GlobalPagePermission.objects.with_user(
- request.user).filter(can_view=True, sites__in=[site]).exists()
+ request.user).filter(global_perms_q).exists()
# a global permission was given to the request's user
if global_view_perms:
return True
- # authenticated user, no restriction and public for all fallback
- if (not is_restricted and not global_view_perms and
- not settings.CMS_PUBLIC_FOR == 'all'):
- return False
# authenticated user, no restriction and public for all
if (not is_restricted and not global_view_perms and
settings.CMS_PUBLIC_FOR == 'all'):
View
12 cms/tests/menu.py
@@ -874,7 +874,7 @@ def test_public_for_all_num_queries(self):
pages = [page]
with self.assertNumQueries(2):
"""
- The two queries are:
+ The queries are:
PagePermission query for affected pages
GlobalpagePermission query for user
"""
@@ -935,7 +935,7 @@ def test_authed_basic_perm_num_queries(self):
pages = [page]
with self.assertNumQueries(4):
"""
- The two queries are:
+ The queries are:
PagePermission query for affected pages
GlobalpagePermission query for user
Generic django permission lookup
@@ -968,7 +968,7 @@ def test_authed_no_access_num_queries(self):
pages = [page]
with self.assertNumQueries(4):
"""
- The two queries are:
+ The queries are:
PagePermission query for affected pages
GlobalpagePermission query for user
Generic django permission lookup
@@ -1018,7 +1018,7 @@ def test_page_permissions_num_queries(self):
pages = [page]
with self.assertNumQueries(2):
"""
- The two queries are:
+ The queries are:
PagePermission query for affected pages
GlobalpagePermission query for user
"""
@@ -1047,7 +1047,7 @@ def test_page_permissions_view_groups_num_queries(self):
pages = [page]
with self.assertNumQueries(3):
"""
- The two queries are:
+ The queries are:
PagePermission query for affected pages
GlobalpagePermission query for user
Group query via PagePermission
@@ -1082,7 +1082,7 @@ def test_global_permission_num_queries(self):
pages = [page]
with self.assertNumQueries(2):
"""
- The two queries are:
+ The queries are:
PagePermission query for affected pages
GlobalpagePermission query for user
"""
View
184 cms/tests/permmod.py
@@ -1,19 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
-from cms.api import create_page, publish_page, approve_page, add_plugin, \
- create_page_user, assign_user_to_page
+from cms.api import (create_page, publish_page, approve_page, add_plugin,
+ create_page_user, assign_user_to_page)
from cms.models import Page, CMSPlugin
from cms.models.moderatormodels import (ACCESS_DESCENDANTS,
ACCESS_PAGE_AND_DESCENDANTS)
+from cms.models.permissionmodels import PagePermission, GlobalPagePermission
from cms.test_utils.testcases import (URL_CMS_PAGE_ADD, URL_CMS_PLUGIN_REMOVE,
SettingsOverrideTestCase, URL_CMS_PLUGIN_ADD, CMSTestCase)
-
from cms.test_utils.util.context_managers import SettingsOverride
from cms.utils.page_resolver import get_page_from_path
from cms.utils.permissions import has_generic_permission
-from django.contrib.auth.models import User, Permission
+from django.contrib.auth.models import User, Permission, AnonymousUser, Group
+from django.contrib.sites.models import Site
from django.core.management import call_command
+
class PermissionModeratorTests(SettingsOverrideTestCase):
"""Permissions and moderator together
@@ -806,3 +808,177 @@ def test_switch_moderator_off(self):
with SettingsOverride(CMS_MODERATOR=False):
page2 = get_page_from_path(page1.get_absolute_url().strip('/'))
self.assertEqual(page1.get_absolute_url(), page2.get_absolute_url())
+
+
+class ViewPermissionTests(SettingsOverrideTestCase):
+ settings_overrides = {
+ 'CMS_MODERATOR': False,
+ 'CMS_PERMISSION': True,
+ 'CMS_PUBLIC_FOR': 'all',
+ }
+
+
+ def get_request(self, user=None):
+ attrs = {
+ 'user': user or AnonymousUser(),
+ 'REQUEST': {},
+ 'session': {},
+ }
+ return type('Request', (object,), attrs)
+
+ def test_public_for_all_staff(self):
+ request = self.get_request()
+ request.user.is_staff = True
+ page = Page()
+ page.pk = 1
+ self.assertTrue(page.has_view_permission(request))
+
+ def test_public_for_all_staff_assert_num_queries(self):
+ request = self.get_request()
+ request.user.is_staff = True
+ page = Page()
+ page.pk = 1
+ with self.assertNumQueries(0):
+ page.has_view_permission(request)
+
+ def test_public_for_all(self):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ request = self.get_request(user)
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ self.assertTrue(page.has_view_permission(request))
+
+ def test_public_for_all_num_queries(self):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ request = self.get_request(user)
+ site = Site()
+ site.pk = 1
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ with self.assertNumQueries(3):
+ """
+ The queries are:
+ The current Site
+ PagePermission query for affected pages
+ GlobalpagePermission query for user
+ """
+ page.has_view_permission(request)
+
+ def test_unauthed(self):
+ request = self.get_request()
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ self.assertTrue(page.has_view_permission(request))
+
+ def test_unauthed_num_queries(self):
+ request = self.get_request()
+ site = Site()
+ site.pk = 1
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ with self.assertNumQueries(1):
+ """
+ The query is:
+ PagePermission query for affected pages
+ """
+ page.has_view_permission(request)
+
+ def test_authed_basic_perm(self):
+ with SettingsOverride(CMS_PUBLIC_FOR='staff'):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ user.user_permissions.add(Permission.objects.get(codename='view_page'))
+ request = self.get_request(user)
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ self.assertTrue(page.has_view_permission(request))
+
+ def test_authed_basic_perm_num_queries(self):
+ site = Site()
+ site.pk = 1
+ with SettingsOverride(CMS_PUBLIC_FOR='staff'):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ user.user_permissions.add(Permission.objects.get(codename='view_page'))
+ request = self.get_request(user)
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ with self.assertNumQueries(5):
+ """
+ The queries are:
+ The site
+ PagePermission query for affected pages
+ GlobalpagePermission query for user
+ Generic django permission lookup
+ content type lookup by permission lookup
+ """
+ page.has_view_permission(request)
+
+ def test_authed_no_access(self):
+ with SettingsOverride(CMS_PUBLIC_FOR='staff'):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ request = self.get_request(user)
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ self.assertFalse(page.has_view_permission(request))
+
+ def test_unauthed_no_access(self):
+ with SettingsOverride(CMS_PUBLIC_FOR='staff'):
+ request = self.get_request()
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ self.assertFalse(page.has_view_permission(request))
+
+ def test_unauthed_no_access_num_queries(self):
+ site = Site()
+ site.pk = 1
+ request = self.get_request()
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ with self.assertNumQueries(1):
+ page.has_view_permission(request)
+
+ def test_page_permissions(self):
+ with SettingsOverride(CMS_PUBLIC_FOR='staff'):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ request = self.get_request(user)
+ page = create_page('A', 'nav_playground.html', 'en')
+ PagePermission.objects.create(can_view=True, user=user, page=page)
+ self.assertTrue(page.has_view_permission(request))
+
+ def test_page_permissions_view_groups(self):
+ with SettingsOverride(CMS_PUBLIC_FOR='staff'):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ group = Group.objects.create(name='testgroup')
+ group.user_set.add(user)
+ request = self.get_request(user)
+ page = create_page('A', 'nav_playground.html', 'en')
+ PagePermission.objects.create(can_view=True, group=group, page=page)
+ self.assertTrue(page.has_view_permission(request))
+
+ def test_global_permission(self):
+ with SettingsOverride(CMS_PUBLIC_FOR='staff'):
+ user = User.objects.create_user('user', 'user@domain.com', 'user')
+ GlobalPagePermission.objects.create(can_view=True, user=user)
+ request = self.get_request(user)
+ page = Page()
+ page.pk = 1
+ page.level = 0
+ page.tree_id = 1
+ self.assertTrue(page.has_view_permission(request))
Please sign in to comment.
Something went wrong with that request. Please try again.