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


Create a new user

>>> from django.contrib.auth.models import User
>>> user = User.objects.create(username="doe")

Create a new permission

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

Create a new role

>>> from permissions.utils import register_role
>>> editor = register_role("Editor")

Assign user to role

>>> editor.add_principal(user)

Create a content object

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

Grant permission

>>> from permissions.utils import grant_permission
>>> grant_permission(content, editor, "view")

Now all users which are member of the role "Editor" have the permission to view object "content".

Check permission

>>> from permissions.utils import has_permission
>>> has_permission(content, user, "view")

This will check whether the current user has the permission "View" for the FlatPage "content".

More information

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
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.