diff --git a/README.md b/README.md index 477e71d..1ff18e0 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,10 @@ PLUGINS_CONFIG = { # Groups must be created beforehand in NetBox. 'GROUP_MAPPINGS': { 'saml-group3': 'netbox-group' - } + }, + # Regex pattern to match groups for sync. Optional. + # Groups must be created beforehand in NetBox. + 'GROUP_REGEX_PATTERN': r'^Netbox-.*' } } } diff --git a/django3_saml2_nbplugin/backends.py b/django3_saml2_nbplugin/backends.py index de0eed8..c16c8dd 100644 --- a/django3_saml2_nbplugin/backends.py +++ b/django3_saml2_nbplugin/backends.py @@ -182,6 +182,17 @@ def configure_user(self, request: WSGIRequest, user: User) -> User: if saml_group in ident_groups: user_groups.append(Group.objects.get(name=django_group)) user.groups.set(user_groups) + if "GROUP_REGEX_PATTERN" in be_settings and "GROUP_ATTR" in be_settings: + group_regex_pattern = be_settings["GROUP_REGEX_PATTERN"] + user_groups = [] + for group_name in ident_groups: + if re.match(group_regex_pattern, group_name): + try: + user_groups.append(Group.objects.get(name=group_name)) + except Group.DoesNotExist: + # Group does not exist in Netbox, skip it + continue + user.groups.set(user_groups) user.save() # call Netbox superclass for further processing of REMOTE_AUTH_xxx variables.