Skip to content

Commit 9d975f7

Browse files
committed
Merge branch 'settings' into dev
2 parents a7e6e64 + 7e2671b commit 9d975f7

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

djangosaml2/views.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,20 @@ class LoginView(SPConfigMixin, View):
110110
will be rendered.
111111
"""
112112

113-
wayf_template = 'djangosaml2/wayf.html'
114-
authorization_error_template = 'djangosaml2/auth_error.html'
115-
post_binding_form_template = 'djangosaml2/post_binding_form.html'
113+
wayf_template = getattr(
114+
settings,
115+
'SAML2_CUSTOM_WAYF_TEMPLATE','djangosaml2/wayf.html'
116+
)
117+
authorization_error_template = getattr(
118+
settings,
119+
'SAML2_CUSTOM_AUTHORIZATION_ERROR_TEMPLATE',
120+
'djangosaml2/auth_error.html'
121+
)
122+
post_binding_form_template = getattr(
123+
settings,
124+
'SAML2_CUSTOM_POST_BINDING_FORM_TEMPLATE',
125+
'djangosaml2/post_binding_form.html'
126+
)
116127

117128
def get_next_path(self, request: HttpRequest) -> str:
118129
''' Returns the path to put in the RelayState to redirect the user to after having logged in.

docs/source/contents/setup.rst

+16
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,29 @@ For example::
400400

401401
from djangosaml2.backends import Saml2Backend
402402

403+
403404
class ModifiedSaml2Backend(Saml2Backend):
404405
def save_user(self, user, *args, **kwargs):
405406
user.save()
406407
user_group = Group.objects.get(name='Default')
407408
user.groups.add(user_group)
408409
return super().save_user(user, *args, **kwargs)
409410

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

412428

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def read(*rnames):
2424

2525
setup(
2626
name='djangosaml2',
27-
version='1.3.2',
27+
version='1.3.3',
2828
description='pysaml2 integration for Django',
2929
long_description=read('README.md'),
3030
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)