Skip to content

Commit

Permalink
Added permission chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 19, 2018
1 parent bb790e7 commit 1a21637
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
Binary file added docs/access_no_perms.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/access_one_perm.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions docs/restrict_parts.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
How to restrict access to parts of Django admin?
=================================================

You can enable and restrict access to specific parts of Django admin using the permission system.
When a model is added, by default, Django creates three permissions. :code:`add, change and delete`


Admin uses these permissions to decide access for users. For a user with :code:`is_superuser=False`, and no permissions, the admin looks like this

.. image:: access_no_perms.png

If you add a permission :code:`user.user_permissions.add(Permission.objects.get(codename="add_hero"))`, the admin starts looking like this

.. image:: access_one_perm.png

You can add more complex logic to restrict access by changing these methods::

def has_add_permission(self, request):
...

def has_change_permission(self, request, obj=None):
...

def has_delete_permission(self, request, obj=None):
...

def has_module_permission(self, request):
...
1 change: 1 addition & 0 deletions docs/specific_users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ To disable a user from being able to access the admin, you should set :code:`is_

This holds true even if the user is a superuser. :code:`is_superuser=True`. If a non-staff tries to access the admin, they see a message like this.

.. image:: access_no_is_staff.png
13 changes: 13 additions & 0 deletions heroes_and_monsters/entities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ def get_actions(self, request):
def is_very_benevolent(self, obj):
return obj.benevolence_factor > 75

# def has_add_permission(self, request):
# return has_hero_access(request.user)

# def has_change_permission(self, request, obj=None):
# return has_hero_access(request.user)

# def has_delete_permission(self, request, obj=None):
# return has_hero_access(request.user)

# def has_module_permission(self, request):
# return has_hero_access(request.user)


is_very_benevolent.boolean = True


Expand Down

0 comments on commit 1a21637

Please sign in to comment.