grafana/django-saml2-auth

Create group if not in DB

santigandolfo opened this issue · 6 comments

Currently if a group from the user_identity and a Group.DoesNotExist is thrown, then the group is skipped.

        groups = []

        for group_name in user["user_identity"][group_attribute]:
            # Group names can optionally be mapped to different names in Django
            if group_map and group_name in group_map:
                group_name_django = group_map[group_name]
            else:
                group_name_django = group_name

            try:
                groups.append(Group.objects.get(name=group_name_django))
            except Group.DoesNotExist:
                pass

It would be nice to have a setting 'CREATE_GROUPS': True, (similar to CREATE_USER) that instead would let us create the group and add it to the list:

        groups = []

        for group_name in user["user_identity"][group_attribute]:
            # Group names can optionally be mapped to different names in Django
            if group_map and group_name in group_map:
                group_name_django = group_map[group_name]
            else:
                group_name_django = group_name

            try:
                groups.append(Group.objects.get(name=group_name_django))
            except Group.DoesNotExist:
                should_create_new_groups = dictor(saml2_auth_settings, "CREATE_GROUPS", False)
                if should_create_new_groups:
                    groups.append(Group.objects.create(name=group_name_django))

Hey @santigandolfo,

Would you create a PR for this and include some tests?

Hi @mostafa, done!
#191

@santigandolfo

Thanks for your contribution! I merged the PR after fixing the tests. It'll be released in the next release, v3.11.0.

Hi @mostafa , do you have any ETA on when we will have release 3.11.0?

Hey @santigandolfo,

I was hoping I can add tests with mock IdP (#187), yet it seems to be a bigger task than I initially anticipated. So, I'll release the new version without the test today.

@santigandolfo

I just released v3.11.0.