Skip to content

Add ModifiedSaml2Backend & _update_user example to perform authorization #303

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
Jun 28, 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
16 changes: 16 additions & 0 deletions docs/source/contents/setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,22 @@ For example::
user.groups.add(user_group)
return super().save_user(user, *args, **kwargs)

Keep in mind save_user is only called when there was a reason to save the User model (ie. first login), and it has no access to SAML attributes for authorization. If this is required, it can be achieved by overriding the _update_user::

from djangosaml2.backends import Saml2Backend

class ModifiedSaml2Backend(Saml2Backend):
def _update_user(self, user, attributes: dict, attribute_mapping: dict, force_save: bool = False):
if 'eduPersonEntitlement' in attributes:
if 'some-entitlement' in attributes['eduPersonEntitlement']:
user.is_staff = True
force_save = True
else:
user.is_staff = False
force_save = True
return super()._update_user(user, attributes, attribute_mapping, force_save)


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


Expand Down