Ejemplo n.º 1
0
class SubsequentOverridesView(PermissionsTemplateView):

    template_name = 'tests/passed.html'
    permissions = Permissions(
        P(user__is_authenticated=True, if_false=Login2View.as_view())
        | P(user__is_superuser=True,
            if_false=TemplateView.as_view(template_name='tests/welcome.html')))
Ejemplo n.º 2
0
 def test_request_arguments(self):
     request = self.get_request()
     request.user = self.user
     request.user2 = self.admin
     self.assertTrue(
         Permissions(P(user__user_is_user=Arg('user'))).check(request))
     self.assertFalse(
         Permissions(P(user__user_is_user=Arg('user2'))).check(request))
 def get_rules(self, request=None, **kwargs):
     try:
         request.article = Article.objects.get(slug=kwargs.get('slug'))
     except Article.DoesNotExist:
         request.article = None
     return editor_or_administrator | P(
         P(user__is_author=True) & P(article__is_published=False)
         & P(article__author=request.user))
Ejemplo n.º 4
0
class OverridesIfTrueView(PermissionsTemplateView):

    template_name = 'tests/passed.html'
    permissions = Permissions(
        P(user__is_authenticated=True) | P(
            P(user__is_authenticated=True)
            & P(user__is_superuser=True,
                if_true=TemplateView.as_view(
                    template_name='tests/welcome.html'))))
Ejemplo n.º 5
0
class TrueFalseRedirects25View(PermissionsTemplateView):

    template_name = 'tests/passed.html'
    permissions = Permissions(
        P(user__is_authenticated=True)
        & P(P(user__is_superuser=True) | P(object__owner=Cmp('user')),
            if_false=AccessDeniedView.as_view()))

    def dispatch(self, request, *args, **kwargs):
        request.object = TestObject.objects.get(title='Test!')
        return super(TrueFalseRedirects25View,
                     self).dispatch(request, *args, **kwargs)
Ejemplo n.º 6
0
        class SomeBasicObjectPermissions(Permissions):

            rules = P(some_object1__title='Some Object 1')

            def get_rules(self, request, **kwargs):
                request.some_object1 = TestObject(title='Some Object 1')
                request.some_object2 = TestObject(title='Some Object 2')
                request.some_object3 = TestObject(title='Some Object 3')
                return P(some_object2__title='Some Object 2')
Ejemplo n.º 7
0
class AuthenticatedPermissions(Permissions):

    rules = P(user__is_authenticated=True)
Ejemplo n.º 8
0
 def get_rules(self, request, **kwargs):
     request.some_object = TestObject(title='Some Object')
     return P()
Ejemplo n.º 9
0
        class TestView(DjangoViewMixin, SimpleGetView):

            permissions = SomeObjectPermissions(
                P(user_is_staff | user_is_superuser))
class AdministratorPermissions(Permissions):

    rules = P(user__is_administrator=True)
Ejemplo n.º 11
0
class NestedPermissions(Permissions):

    rules = P(
        P(user__is_authenticated=True) & P(
            P(user__is_staff=True)
            & P(P(user__is_superuser=True) & P(user__username='******'))))
Ejemplo n.º 12
0
class NestedNegatedPermissions(Permissions):

    rules = P(
        ~P(user__is_authenticated=False)
        & P(P(user__is_authenticated=True) | ~P(user__is_authenticated=False)))
class UserPermissions(Permissions):

    rules = P(user__is_authenticated=True,
              if_false=SubscriptionRequiredRedirectView.as_view())
Ejemplo n.º 14
0
class NegatePermissions(Permissions):

    rules = ~P(user__is_public=False) & ~P(user__is_authenticated=False)
Ejemplo n.º 15
0
 def test_children(self):
     self.assertEqual(
         '(&(&(~(&{\'user\': 1})),(|{\'user\': 2},(~(&{\'user\': 3})))))',
         str(P(~P(user=1) & P(P(user=2) | ~P(user=3)))))
Ejemplo n.º 16
0
class NegatedOverrideIfTrueFalsePermissions(Permissions):

    rules = ~P(user__is_authenticated=True,
               if_true=if_true_override,
               if_false=if_false_override)
Ejemplo n.º 17
0
 def test_oneliners(self):
     request = self.get_request()
     self.assertFalse(
         Permissions(
             P(user__is_authenticated=True)
             & P(user__username=request.user.username)).check(request))
     request.user = self.user
     self.assertTrue(
         Permissions(
             P(user__is_authenticated=True)
             | P(user__username='******')).check(request))
     Permissions(user_is_authenticated
                 & P(user__is_superuser=True, if_false=if_true_override)
                 ).check(request)
     self.assertEqual(OVERRIDE_TRUE,
                      request.permissionsx_return_overrides())
     Permissions(user_is_superuser
                 | P(user__is_staff=True, if_false=if_false_override)
                 ).check(request)
     self.assertEqual(OVERRIDE_FALSE,
                      request.permissionsx_return_overrides())
     permissions_tested = Permissions(
         P(user__is_authenticated=True)
         & P(user__is_staff=True, if_false=if_false_override)
         & P(user__is_superuser=False))
     permissions_tested.check(request)
     self.assertEqual(OVERRIDE_FALSE,
                      request.permissionsx_return_overrides())
     request.user = self.staff
     permissions_tested = Permissions(
         P(user__is_authenticated=True)
         & P(user__is_staff=True, if_true=if_true_override)
         & P(user__is_superuser=False))
     permissions_tested.check(request)
     self.assertEqual(OVERRIDE_TRUE,
                      request.permissionsx_return_overrides())
Ejemplo n.º 18
0
class OverrideIfTruePermissions(Permissions):

    rules = P(user__is_authenticated=True, if_true=if_true_override)
Ejemplo n.º 19
0
class OverrideIfFalsePermissions(Permissions):

    rules = P(user__is_authenticated=True, if_false=if_false_override)
Ejemplo n.º 20
0
 def get_rules(self, request=None):
     return ~P(user__is_authenticated=False) & P(
         user__username=request.user.username)
Ejemplo n.º 21
0
class OverridesIfFalseView(PermissionsTemplateView):

    template_name = 'tests/passed.html'
    permissions = Permissions(
        P(user__is_authenticated=True)
        & P(user__is_superuser=True, if_false=Login2View.as_view()))
Ejemplo n.º 22
0
class NestedNegatedOverridePermissions(Permissions):

    rules = P(
        P(user__is_authenticated=False) & ~P(user__is_authenticated=True,
                                             if_true=if_true_override,
                                             if_false=if_false_override))
Ejemplo n.º 23
0
class UserAttributesDependentPermissions(Permissions):

    rules = P(user__username='******')
Ejemplo n.º 24
0
 def get_rules(self, request, **kwargs):
     request.some_object1 = TestObject(title='Some Object 1')
     request.some_object2 = TestObject(title='Some Object 2')
     request.some_object3 = TestObject(title='Some Object 3')
     return P(some_object2__title='Some Object 2')
from django.core.urlresolvers import reverse_lazy
from django.contrib import messages

from permissionsx.models import P
from permissionsx.models import Permissions
from permissionsx.contrib.django.views import MessageRedirectView

from example.content.models import Article

editor_or_administrator = P(user__is_editor=True) | P(
    user__is_administrator=True)


class SubscriptionRequiredRedirectView(MessageRedirectView):

    message = (messages.warning, 'You must be signed up to access content!')
    redirect_url = reverse_lazy('account_signup')


class UserPermissions(Permissions):

    rules = P(user__is_authenticated=True,
              if_false=SubscriptionRequiredRedirectView.as_view())


class AuthorPermissions(Permissions):

    rules = P(user__is_author=True) | editor_or_administrator


class StaffPermissions(Permissions):
Ejemplo n.º 26
0
 def get_rules(self, request, **kwargs):
     rules = super(SomeObjectPermissions,
                   self).get_rules(request, **kwargs)
     request.some_object4 = TestObject(title='Some Object 4')
     return rules & P(some_object3__title='Some Object 3')
class AuthorPermissions(Permissions):

    rules = P(user__is_author=True) | editor_or_administrator
Ejemplo n.º 28
0
        class TestView(DjangoViewMixin, SimpleGetView):

            permissions = SomeObjectPermissions(
                P(some_object4__title='Some Object 4'))
Ejemplo n.º 29
0
"""PermissionsX - Authorization for Django.

:copyright: Copyright (c) 2013-2014 by Robert Pogorzelski.
:license:   BSD, see LICENSE for more details.

"""
from __future__ import absolute_import

from permissionsx.models import P
from permissionsx.models import Permissions

user_is_authenticated = P(user__is_authenticated=True)
user_is_staff = P(user__is_staff=True)
user_is_superuser = P(user__is_superuser=True)

OVERRIDE_TRUE = 'Override returns True'
OVERRIDE_FALSE = 'Override returns False'


def if_true_override():
    return OVERRIDE_TRUE


def if_false_override():
    return OVERRIDE_FALSE


class AuthenticatedPermissions(Permissions):

    rules = P(user__is_authenticated=True)
Ejemplo n.º 30
0
class IsPublicPermissions(Permissions):

    rules = P(user__is_public=True)