Extended, flexible and powerful object-level and rule-driven permissions for Django & Django REST framework
Project description
permissible is a module to make it easier to configure object-level permissions,
and to help unify the different places performing permissions checks (including DRF
and Django admin) to create a full permissions check that can work without any
further architectural pondering.
It is built on top of django-guardian but can be easily configured for other object-level libraries.
Introduction
This module allows us to define permission requirements in our Models (similarly to how django-rules does it in Model.Meta). Given that different view engines (e.g. DRF vs Django's admin) have different implementations for checking permissions, this allows us to centralize the permissions configuration and keep the code clear and simple. This approach also allows us to unify permissions checks across both Django admin and DRF (and indeed any other place you use PermissibleMixin).
Installation
-
Install the package (use of
django-guardianis optional but needed for most Features below):pip install permissible # Django, djangorestframework pip install permissible[guardian] # Same + django-guardian, djangorestframework-guardian
-
If using
django-guardian, make sure to add theObjectPermissionsBackendto yourAUTHENTICATION_BACKENDS(otherwise enable object permissions in your own desired way):AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', # default 'guardian.backends.ObjectPermissionBackend', )
Features
Feature 1: Consistent, policy-based permissions configuration
In its simplest form, permissible can be used just for its permissions
configuration. This has no impact on your database, and does not rely on any
particular object-level permissions library. (It does require one; we prefer
django-guardian.)
Here, we add the PermissibleMixin to each model we want to protect, and
define policies for ach that define what permissions are needed for each action
that is taken on an object in the model (e.g. a "retrieve" action on a "survey").
Policies are defined in a policies.py, in a dict called ACTION_POLICIES, eg:
ACTION_POLICIES = {
"surveys.Survey": {
"global": {
"create": NO_RESTRICTION,
"retrieve": NO_RESTRICTION,
...
},
"object": {
"create": NO_RESTRICTION,
"retrieve": p(["view"]),
...
},
}
}
With the permissions configured, now we can force different views to use them:
- If you would like the permissions to work for API views (via
django-rest-framework): AddPermissiblePermsto thepermission_classesfor the viewsets for our models - If you would like the permissions to work in the Django admin: Add
PermissibleAdminMixinto the admin classes for our models
That's it. Actions are now protected by permissions checks. But there is no easy way to create the permissions in the first place. That's where the next two features come in.
Feature 2: Simple, role-based permissions assignment using "domain" models (RBAC)
The permissible library can also help automatically assign permissions based on
certain "domain" models. The domain model is the model we should check permissions
against. For instance, the domain model for a "project file" might be a "project",
in which case having certain permissions on the "project" would confer other
permissions for the "project files", even though no specific permission exists
for the "project file". Loosely speaking, a domain "owns" other models. The concept of
"domains" is fairly consistent in RBAC.
Of course, it's easy to link a "project" to a "project file" through a foreign key.
But permissible solves the problem of tying this to the Django Group model,
which is what we use for permissions, according to roles.
Each resulting Group (managed on the backend) corresponds to a single role.
To accomplish this, permissible provides 3 base model classes that you should use:
PermDomain: Make the domain model (e.g.Team) derive fromPermDomainPermDomainRole: Create a new model that derives fromPermDomainRoleand has aForeignKeyto the domain model - and definesROLE_DEFINITIONSPermDomainMember: Create a new model that derives fromPermDomainMemberand has aForeignKeyto the domain model (this model automatically adds and removes records when a user is a member of the appropriatePermDomainRole)
Then, set up the ACTION_POLICIES appropriately. For instance, for a model class
"surveys.Survey" owned by its Survey.project.team, we might have the following:
ACTION_POLICIES = {
"surveys.Survey": {
"global": {
"create": NO_RESTRICTION,
"retrieve": NO_RESTRICTION,
...
},
"object": {
"create": p(["add_on"], "project.team"),
"retrieve": p(["view_on"], "project.team"),
...
},
}
}
You can adjust ACTION_POLICIES to incorporate checking of the domain model for
permissions. See the documentation for PermDef and
PermissibleMixin.has_object_permissions for info and examples.
Data paths (request data lookup)
If your object-level checks need to validate permissions based on values
coming from the request body (for example when creating objects), you can
specify a data_paths mapping in ACTION_POLICIES. This lets the
permission system extract nested keys from request.data and build dummy
objects for object-level permission checks. PermissiblePerms will use the
data_paths entry for the current action when a non-detail action (e.g.
create) includes request data.
Example:
ACTION_POLICIES = {
"surveys.Survey": {
"data_paths": {
# For the create action, use request.data['survey'] as the object data
"create": "survey",
# Or to read a nested value: request.data['payload']['survey']
"batch_create": "payload.survey",
},
"object": {
"create": p(["add_on"], "project.team"),
"retrieve": p(["view_on"], "project.team"),
...
},
},
}
Behavior:
- When
PermissiblePerms.has_permissionsees a non-detail action withrequest.data, it will look updata_paths[action]. If present it will pull that nested portion of the payload and pass it toModel.make_objs_from_data(...)to build dummy model instances. Those instances are then checked using the object-levelPermDefrules (same as for detail actions).
If no data_paths entry exists for the action, the entire request.data is
used as input to make_objs_from_data, which is the previous/default
behavior.
Remember: PermDomain is the core model on which roles are defined (eg Project or
Team) and PermDomainRole is the model that represents a single role (and
therefore a single Django auth.Group) for a single PermDomain - eg Team Admins.
The PermDomainRole.ROLE_DEFINITIONS defines what object permissions will be
given to each role/group for every PermDomain.
You can also use PermDomainAdminMixin to help you manage the PermDomain records
and the subsequent role-based access control:
Feature 3: Assignment on record creation
permissible can automatically assign object permissions on object creation,
through use of 3 view-related mixins:
admin.PermissibleObjectAssignMixin(for admin classes - give creating user all permissions)serializers.PermissibleObjectAssignMixin(for serializers - give creating user all permissions)serializers.PermDomainObjectAssignMixin(for serializers for domain models like "Team" or "Project - add creating user to all domain model's Groups)
NOTE: this feature is dependent on django-guardian, as it uses the assign_perm
shortcut. Also, admin.PermissibleObjectAssignMixin extends the
ObjectPermissionsAssignmentMixin mixin from djangorestframework-guardian.
Core concepts
PermissibleMixin:
- Add
PermissibleMixinto any model you want to protect - Define
ACTION_POLICIESinpolicies.pyfor each app, where each key is the full model label (egaccounts.User), see example above- Remember that (just like Django's permission checking normally) both global and object permissions must pass
- Both
"global"and"object"keys use the same format: a map of actions to a list ofPermDefobjects - Actions are the same as those defined by DRF (for convenience):
create,retrieve,update,partial_update,destroy, and any others you want to define and check later (listusesretrievepermissions by default but can also define its own if needed)
- See below for
PermDefexplanation
PermDef
- A simple data structure to hold permissions configuration.
- Each
PermDefis defined with the following:short_perm_codes: A list of short permission codes, e.g. ["view", "change"]obj_path: An optional string path (e.g. "project.team") from the original object to a potentially different object on whom we will actually check permissions. (For instance if you want to check a related parent object to determine whether the user has access to the child object. This is critical for PermDomain behavior.)global_condition_checker: An ADDITIONAL check, on top of the usual permissions-checking (user.has_perms). Is passed the user instance and the context object (by default, the values inside therequestobject)obj_filter: A tuple of(attr, operator, needed_value)to further check the object - e.g.("is_public", "==", True). ForPermissibleFilter, this is also used to filter the queryset down to permitted objects.model_label: A fully qualified model label to use a different model class for checks. This is only used if theobj_pathactually points to the context (e.g.obj_path="_context.team_id") to perform additional permission checks based on the context (i.e. what is inside the request object)
- PermDef objects can be combined with each other, either with
|or&- e.g.PermDef(["view"]) | PermDef(["custom_perm"])
Example flow
- The application has the following models:
User(inherits Django's base abstract user model)Group(Django's model)Team(inheritsPermDomain)TeamGroup(inheritsPermDomainRole)TeamUser(inheritsPermDomainMember)TeamInfo(contains a foreign key toTeam)
Create a team
- A new team is created (via Django admin), which triggers the creation of appropriate
groups and assignment of permissions:
Team.save()creates severalTeamGrouprecords, one for each possible role (e.g. member, owner)- For each
TeamGroup, thesave()method triggers the creation of a newGroup, and assigns permissions to each of these groups, in accordance withPermDomainRole.role_definitions:TeamGroupwith "Member" role is given no permissionsTeamGroupwith "Viewer" role is given "view_team" permissionTeamGroupwith "Contributor" role is given "contribute_to_team" and "view_team" permissionsTeamGroupwith "Admin" role is given "change_team", "contribute_to_team" and "view_team" permissionsTeamGroupwith "Owner" role is given "delete", "change_team", "contribute_to_team" and "view_team" permissions- (NOTE: this behavior can be customized)
- Note that no one is given permission to create
Teamto begin with - it must have been created by a superuser or someone who was manually given such permission in the admin
Create a user
- A new user is created (via Django admin), and added to the relevant groups (e.g. members, admins)
- A
TeamUserrecord is added automatically when this user joins those groups. Note that if the user is removed from ALL of those groups for thisTeam, they will automatically have theirTeamUserrecord removed.
Edit a team-related record
- The user tries to edit a
TeamInforecord, either via API (django-rest-framework) or Django admin, triggering the following checks:- View/viewset checks global permissions
- View/viewset checks object permissions:
- Checking object permission directly FAILS (as this user was not given any permission for this object in particular)
- Checking permission for domain object (i.e. team) SUCCEEDS if the user was added to the correct groups
Create a team-related record
- The user tries to create a
TeamInforecord, either via API (django-rest-framework) or Django admin, triggering the following checks:- View/viewset checks global permissions
- View/viewset checks creation permissions:
- Checking object permission directly FAILS as this object doesn't have an ID yet, so can't have any permissions associated with it
- Checking permission for domain object (i.e. team) SUCCEEDS if the user was added to the correct groups
- View/viewset does not check object permission (this is out of our control, and makes sense as there is no object)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file permissible-0.7.9.tar.gz.
File metadata
- Download URL: permissible-0.7.9.tar.gz
- Upload date:
- Size: 73.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d375b195ffd89c85e7f73bc0b74a00934687d635046e8b461590916a57b5aac5
|
|
| MD5 |
7576b030feedf596a76ef9ef40a3a9dd
|
|
| BLAKE2b-256 |
b3b1ebc1eff701b3b3221b906f90ee2a800d60b27682c1e4e3e18b27277c5665
|
Provenance
The following attestation bundles were made for permissible-0.7.9.tar.gz:
Publisher:
build.yml on gaussian/permissible
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
permissible-0.7.9.tar.gz -
Subject digest:
d375b195ffd89c85e7f73bc0b74a00934687d635046e8b461590916a57b5aac5 - Sigstore transparency entry: 707076799
- Sigstore integration time:
-
Permalink:
gaussian/permissible@0575769a9b611654ddd6aca8d3ceb9901bdaf69a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/gaussian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@0575769a9b611654ddd6aca8d3ceb9901bdaf69a -
Trigger Event:
pull_request_target
-
Statement type:
File details
Details for the file permissible-0.7.9-py3-none-any.whl.
File metadata
- Download URL: permissible-0.7.9-py3-none-any.whl
- Upload date:
- Size: 59.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff75ce4514acdcf6e35e902e43e2dbc775e1f419ddd60d35d3ca31115ec574f6
|
|
| MD5 |
719d79478febb7f13f48c53ab8140f22
|
|
| BLAKE2b-256 |
e2d9db841f359b80a4d0a493f5175a89a462726b2c76939e40b811144c0b2c00
|
Provenance
The following attestation bundles were made for permissible-0.7.9-py3-none-any.whl:
Publisher:
build.yml on gaussian/permissible
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
permissible-0.7.9-py3-none-any.whl -
Subject digest:
ff75ce4514acdcf6e35e902e43e2dbc775e1f419ddd60d35d3ca31115ec574f6 - Sigstore transparency entry: 707076813
- Sigstore integration time:
-
Permalink:
gaussian/permissible@0575769a9b611654ddd6aca8d3ceb9901bdaf69a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/gaussian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@0575769a9b611654ddd6aca8d3ceb9901bdaf69a -
Trigger Event:
pull_request_target
-
Statement type: