-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from rsinger86/typed-statements
Typed statements
- Loading branch information
Showing
9 changed files
with
160 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Statement Dataclass | ||
|
||
A `Statement` dataclass can be used instead of dictionaries to define policy statements. | ||
|
||
For example, the following policies are equivalent: | ||
|
||
```python | ||
from rest_access_policy import Statement | ||
|
||
|
||
class ArticleAccessPolicy(AccessPolicy): | ||
statements = [ | ||
Statement( | ||
action="destroy", | ||
principal=["*"], | ||
effect="allow", | ||
condition="is_author" | ||
) | ||
] | ||
|
||
def is_author(self, request, view, action) -> bool: | ||
article = view.get_object() | ||
return request.user == article.author | ||
|
||
|
||
|
||
class ArticleAccessPolicy(AccessPolicy): | ||
statements = [ | ||
{ | ||
"action": ["destroy"], | ||
"principal": ["*"], | ||
"effect": "allow", | ||
"condition": "is_author" | ||
} | ||
] | ||
|
||
def is_author(self, request, view, action) -> bool: | ||
article = view.get_object() | ||
return request.user == article.author | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .exceptions import AccessPolicyException | ||
from .access_policy import AccessPolicy | ||
from .access_policy import AccessPolicy, Statement | ||
from .access_view_set_mixin import AccessViewSetMixin | ||
from .field_access_mixin import FieldAccessMixin | ||
from .fields import PermittedPkRelatedField, PermittedSlugRelatedField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from dataclasses import asdict | ||
from rest_framework.test import APITestCase | ||
|
||
from rest_access_policy import AccessViewSetMixin, AccessPolicy, Statement | ||
from rest_framework.viewsets import ViewSet | ||
from rest_framework.permissions import AllowAny | ||
|
||
|
||
class StatementTestCase(APITestCase): | ||
def test_should_raise_error_if_invalid_effect(self): | ||
with self.assertRaises(Exception) as context: | ||
Statement(principal="*", action="build", effect="veto") | ||
|
||
self.assertTrue("effect must be one of" in str(context.exception)) | ||
|
||
def test_to_dict(self): | ||
statement = Statement( | ||
principal="*", | ||
action="build", | ||
effect="allow", | ||
condition_expression=["method1"], | ||
) | ||
|
||
self.assertEqual( | ||
asdict(statement), | ||
{ | ||
"principal": "*", | ||
"action": "build", | ||
"effect": "allow", | ||
"condition": [], | ||
"condition_expression": ["method1"], | ||
}, | ||
) |