Skip to content

Documentation: Replace signal with hooks #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -510,25 +510,17 @@ https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-cu

Sometimes you need to use special logic to update the user object
depending on the SAML2 attributes and the mapping described above
is simply not enough. For these cases djangosaml2 provides a Django
signal that you can listen to. In order to do so you can add the
following code to your app::
is simply not enough. For these cases djangosaml2 provides hooks_
that can be overriden with custom functionality. For example::

from djangosaml2.signals import pre_user_save
from djangosaml2.backends import Saml2Backend

def custom_update_user(sender=User, instance, attributes, user_modified, **kargs)
...
return True # I modified the user object


Your handler will receive the user object, the list of SAML attributes
and a flag telling you if the user is already modified and need
to be saved after your handler is executed. If your handler
modifies the user object it should return True. Otherwise it should
return False. This way djangosaml2 will know if it should save
the user object so you don't need to do it and no more calls to
the save method are issued.
class MySaml2Backend(Saml2Backend):
def save_user(self, user, *args, **kwargs):
# Add custom logic here
return super().save_user(user, *args, **kwargs)

.. _hooks: https://github.com/knaperek/djangosaml2/blob/master/djangosaml2/backends.py#L181

IdP setup
=========
Expand Down