Utilities

django-guardian helper functions.

Functions defined within this module should be considered as django-guardian’s internal functionality. They are not guaranteed to be stable - which means they actual input parameters/output type may change in future releases.

get_anonymous_user

guardian.utils.get_anonymous_user()

Returns User instance (not AnonymousUser) depending on ANONYMOUS_USER_NAME configuration.

get_identity

guardian.utils.get_identity(identity)

Returns (user_obj, None) or (None, group_obj) tuple depending on what is given. Also accepts AnonymousUser instance but would return User instead - it is convenient and needed for authorization backend to support anonymous users.

Parameters:identity – either User or Group instance
Raises:NotUserNorGroup – if cannot return proper identity instance

Examples:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create(username='joe')
>>> get_identity(user)
(<User: joe>, None)

>>> group = Group.objects.create(name='users')
>>> get_identity(group)
(None, <Group: users>)

>>> anon = AnonymousUser()
>>> get_identity(anon)
(<User: AnonymousUser>, None)

>>> get_identity("not instance")
...
NotUserNorGroup: User/AnonymousUser or Group instance is required (got )

clean_orphan_obj_perms

guardian.utils.clean_orphan_obj_perms()

Seeks and removes all object permissions entries pointing at non-existing targets.

Returns number of removed objects.