Authorization library for Django
Project description
django-cancango
django-cancango
is an authorization library for Django. It works on top of default Django permissions and allows to restrict the resources (models and objects) a given user can access.
This library is inspiered by cancancan
for Ruby on Rails.
Quick start
- Add
cancango
to yourINSTALLED_APPS
setting like this:
INSTALLED_APPS = [
...,
'cancango',
]
- Create a function that define user abilites. For example, in
abilities.py
:
def declare_abilities(user, ability):
if not user.is_authenticated:
# Allow anonymous users to view published articles
return ability.can('view', Article, published=True)
if user.has_perm('article.view_own_article'):
# Allow logged in user to change his articles
return ability.can('change', Article, author=user)
if user.is_superuser:
# Allow superuser change all articles
return ability.can('change', Article)
- Configure
cancango
by addingCANCANGO
section insettings.py
:
CANCANGO = {
'ABILITIES': 'myapp.abilities.declare_abilities'
}
Next, add cancango
middleware after AuthenticationMiddleware
:
MIDDLEWARE = [
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
'cancango.middleware.CanCanGoMiddleware',
...
]
Adding the middleware adds request.user.can(...)
function that you can use
to check for model or object permissions.
- Check abilities in a view:
class ArticleDetailView(PermissionRequiredMixin, DetailView):
queryset = TodoItem.objects.all()
def has_permission(self):
article = self.get_object()
return self.request.user.can('view', article)
Testing
Run ./manage.py test
to run all test for the testapp
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
django-cancan-0.0.1.tar.gz
(8.1 kB
view hashes)
Built Distribution
Close
Hashes for django_cancan-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bba476f35d202cb27a964578c5010d7c8d22dc169f182a4dcd2a2ef8e85dda07 |
|
MD5 | 3f6c51a5615ea06e4e7460fe1e4935dc |
|
BLAKE2b-256 | 5b3f9cab579e44d62707b3becf55ac0cb8569b4c93e84ad329da2cea79d49a94 |