Awesome Open Source
Awesome Open Source

Django REST - Access Policy

Package version Python versions

This project brings a declaritive, organized approach to managing access control in Django REST Framework projects. Each ViewSet or function-based view can be assigned an explicit policy for the exposed resource(s). No more digging through views or seralizers to understand access logic -- it's all in one place in a format that less technical stakeholders can understand. If you're familiar with other declaritive access models, such as AWS' IAM, the syntax will be familiar.

In short, you can start expressing your access rules like this:

class ArticleAccessPolicy(AccessPolicy):
    statements = [
            "action": ["list", "retrieve"],
            "principal": "*",
            "effect": "allow"
            "action": ["publish", "unpublish"],
            "principal": ["group:editor"],
            "effect": "allow"            

This project has complete test coverage and the base AccessPolicy class is only ~150 lines of code: there's no magic here.


Source Code:


0.9.0 (April 2021)

  • Adds special admin and staff principal keys to match users with is_superuser and is_staff set to True. Thanks @BarnabasSzabolcs!

0.8.7 (February 2021)

  • Fixed bug preventing argument being passed to custom condition method if "*" character used.

0.8.6 (January 2021)

  • Adds missing requirement to Thanks @daviddavis!

0.8.5 (January 2021)

  • Adds support for boolean expressions in condition statement elements. Thanks @tanonl!

0.8.1 (October 2020)

0.8.0 (September 2020)

0.7.0 (August 2020)

  • Allows using HTTP method placeholders in action element of statements to match request.
    • For example, "action": ["<method:post>"] will match all POST requests.

0.6.2 (July 2020)

  • Uses instead of in user principal check, for compatibility with non-id primary keys.
  • Fixes to documentation. Thanks @oguzhancelikarslan!

0.6.1 (June 2020)

  • Replaces references to "delete" action with "destroy" in docs/tests, to be consistent with DRF's ViewSet actions. Thanks @greenled!

0.6.0 (May 2020)

  • Only call database-hitting get_user_group_values if needed in private method. Thanks KillianMeersman!
  • Use prefetch_related_objects to ensure that user's groups aren't fetched more than once. Thanks filwaline!

0.5.1 (December 2019)

  • Tox config updates and typo fixes in docs.

0.5.0 (September 2019)

  • Add option to define re-usable custom conditions/permissions in a module that can be referenced by multiple policies.

0.4.2 (June 2019)

  • Fixes readme format for Pypy display.

0.4.0 (June 2019)

  • Allow passing arguments to condition methods, via condition values formatted as {method_name}:{arg_value}.

0.3.0 (May 2019)

  • Adds special <safe_methods> action key that matches when the current request is an HTTP read-only method: HEAD, GET, OPTIONS.

0.2.0 (May 2019)

  • Adds special authenticated and anonymous principal keys to match any authenticated user and any non-authenticated user, respectively. Thanks @bogdandm for discussion/advice!

0.1.0 (May 2019)

  • Initial release


Tests are found in a simplified Django project in the /tests folder. Install the project requirements and do ./ test to run them.


See License.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (53,198
django (1,009
authorization (214
django-rest-framework (137
permissions (78
declarative (77
access-control (65
iam (47