Skip to content

Commit f1507e4

Browse files
authored
Add ModifiedSaml2Backend & _update_user example to perform authorization (#303)
1 parent c9ac2e7 commit f1507e4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

docs/source/contents/setup.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,22 @@ For example::
407407
user.groups.add(user_group)
408408
return super().save_user(user, *args, **kwargs)
409409

410+
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::
411+
412+
from djangosaml2.backends import Saml2Backend
413+
414+
class ModifiedSaml2Backend(Saml2Backend):
415+
def _update_user(self, user, attributes: dict, attribute_mapping: dict, force_save: bool = False):
416+
if 'eduPersonEntitlement' in attributes:
417+
if 'some-entitlement' in attributes['eduPersonEntitlement']:
418+
user.is_staff = True
419+
force_save = True
420+
else:
421+
user.is_staff = False
422+
force_save = True
423+
return super()._update_user(user, attributes, attribute_mapping, force_save)
424+
425+
410426
.. _hooks: https://github.com/identitypython/djangosaml2/blob/master/djangosaml2/backends.py#L181
411427

412428

0 commit comments

Comments
 (0)