Sachin Rajput
Sachin Rajput

Reputation: 248

Listing user associated permission in django 2.2.5

I want to list all the permissions that my user has be it a custom permission or default permission

what I have tried till now

  1. request.user.user_permissions
  2. request.user.get_all_permissions
  3. permissions = Permission.objects.filter(user=request.user)

But none of them returned any permissions though the user was a superuser, staff and active

Any lead and help will be great

Upvotes: 0

Views: 36

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

An admin user normally has no permissions assigned to them, the .has_perm(…) [Django-doc] will always return True, or as the documentation says:

Returns True if the user has the specified permission, where perm is in the format "<app label>.<permission codename>". (see documentation on permissions). If the user is inactive, this method will always return False. For an active superuser, this method will always return True.

So the admin user is not assigned all permissions (this would also be "error-prone" since later new permissions can pop up, and it would thus require extra logic to add these to the superusers that already exist.

Upvotes: 1

Related Questions