Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rules/permissions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from asgiref.sync import sync_to_async

from .rulesets import RuleSet

permissions = RuleSet()
Expand Down Expand Up @@ -32,3 +34,9 @@ def has_perm(self, user, perm, *args, **kwargs):

def has_module_perms(self, user, app_label):
return has_perm(app_label, user)

async def ahas_perm(self, user, perm, *args, **kwargs):
return await sync_to_async(has_perm)(user, perm, *args, **kwargs)

async def ahas_module_perms(self, user, app_label):
return await sync_to_async(has_perm)(app_label, user)
16 changes: 16 additions & 0 deletions tests/testsuite/test_permissions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest import TestCase

from asgiref.sync import sync_to_async

from rules.permissions import (
ObjectPermissionBackend,
add_perm,
Expand Down Expand Up @@ -50,3 +52,17 @@ def test_backend(self):
assert not backend.has_perm(None, "can_edit_book")
remove_perm("can_edit_book")
assert not perm_exists("can_edit_book")

async def test_backend_async(self):
backend = ObjectPermissionBackend()

await sync_to_async(add_perm)("can_edit_book", always_true)
assert "can_edit_book" in permissions
assert await backend.ahas_perm(None, "can_edit_book")
assert await backend.ahas_module_perms(None, "can_edit_book")
with self.assertRaises(KeyError):
await sync_to_async(add_perm)("can_edit_book", always_true)
await sync_to_async(set_perm)("can_edit_book", always_false)
assert not await backend.ahas_perm(None, "can_edit_book")
await sync_to_async(remove_perm)("can_edit_book")
assert not await sync_to_async(perm_exists)("can_edit_book")
Loading