Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Django permissions without defining a content type or model?

I'd like to use a permissions based system to restrict certain actions within my Django application. These actions need not be related to a particular model (e.g. access to sections in the application, searching...), so I can't use the stock permissions framework directly, because the Permission model requires a reference to an installed content type.

I could write my own permission model but then I'd have to rewrite all the goodies included with the Django permissions, such as:

  • The possibility to assign permissions to users and groups.
  • The permission_required decorator.
  • User.has_perm and related user methods.
  • The perms template variable.
  • ...

I've checked some apps like django-authority and django-guardian, but they seem to provide permissions even more coupled to the model system, by allowing per-object permissions.

Is there a way to reuse this framework without having defined any model (besides User and Group) for the project?

like image 484
Chewie Avatar asked Dec 18 '12 12:12

Chewie


People also ask

How do I set permissions in Django?

With Django, you can create groups to class users and assign permissions to each group so when creating users, you can just assign the user to a group and, in turn, the user has all the permissions from that group. To create a group, you need the Group model from django. contrib. auth.

How do permissions work in Django?

By default, Django automatically gives add, change, and delete permissions to all models, which allow users with the permissions to perform the associated actions via the admin site. You can define your own permissions to models and grant them to specific users.

How do I give permission to admin in Django?

Test the 'view' permission is added to all modelsUsing #3 for Django 1.7 only creates the permission objects if the model doesn't already exist. Is there a way to create a migration (or something else) to create the permission objects for existing models?

How do I restrict permissions in Django access?

Restrict access to unauthenticated users in Django Views. To simply restrict access to a view based on if the user is authenticated (logged in) or not does not require you to dive deep into the permission system at all, you can simply do it with Decorators, Mixins or the user is_authenticated property.


1 Answers

For those of you, who are still searching:

You can create an auxiliary model with no database table. That model can bring to your project any permission you need. There is no need to deal with ContentType or create Permission objects explicitly.

from django.db import models          class RightsSupport(models.Model):                  class Meta:                  managed = False  # No database table creation or deletion  \                          # operations will be performed for this model.                           default_permissions = () # disable "add", "change", "delete"                                  # and "view" default permissions          permissions = (              ('customer_rights', 'Global customer rights'),               ('vendor_rights', 'Global vendor rights'),              ('any_rights', 'Global any rights'),          ) 

Right after manage.py makemigrations and manage.py migrate you can use these permissions like any other.

# Decorator  @permission_required('app.customer_rights') def my_search_view(request):     …  # Inside a view  def my_search_view(request):     request.user.has_perm('app.customer_rights')  # In a template # The currently logged-in user’s permissions are stored in the template variable {{ perms }}  {% if perms.app.customer_rights %}     <p>You can do any customer stuff</p> {% endif %} 
like image 172
Dmitry Avatar answered Oct 07 '22 06:10

Dmitry