django-permissions-1.0 / docs / usage / additional_groups.rst

Additional groups

Warning

django-permissions is in alpha state. Please consider the API as supposed to be changed until it reaches beta state.

This will demonstrate how one can create special groups (per convention) and check permissions against them even if the user has not been assigned to these groups explicitly.

Create new permissions

from permissions.utils import register_permission
permission = register_permission("View", "view")
permission = register_permission("Edit", "edit")

Create new groups

from permissions.utils import register_group
anonymous = register_group("Anonymous")
owner = register_group("Owner")

This will create default Django groups.

Create a content object

from django.contrib.flatpages.models import FlatPage
content = FlatPage.objects.create(title="Example", url="example")

Grant permissions

from permission.utils import grant_permission
grant_permission("view", anonymous, content)
grant_permission("edit", owner, content)

Now all users which are member of the special group "Anonymous" have the permission to view the object "content". And all users which are member of the special group "Owner" have the permission to edit the content.

Check permission

from permission.utils import has_permission
from permission.utils import get_group

# Every user is automatically within the Anonymous group.
groups = [get_group("Anonymous")]

# The creator of the page is also within the Owner group.
# Note: FlatPages actually don't have a creator attribute.
if request.user == content.creator:
    groups.append(get_group("Owner"))

# Passing the additional groups to has_permission
result = has_permission("edit", request.user, content, groups)

if result == False:
    print "Alert!"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.