From 691e45521d30a9e92be6dea7d8d0d73577af5666 Mon Sep 17 00:00:00 2001 From: Serge Ohl Date: Wed, 19 Sep 2018 16:13:24 -0400 Subject: [PATCH 1/2] Add multi default org --- sentry_ldap_auth/backend.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/sentry_ldap_auth/backend.py b/sentry_ldap_auth/backend.py index f854361..e7e7cff 100644 --- a/sentry_ldap_auth/backend.py +++ b/sentry_ldap_auth/backend.py @@ -55,23 +55,15 @@ def get_or_create_user(self, username, ldap_user): if orgs != None and len(orgs) > 0: return model - # Find the default organization - organizations = Organization.objects.filter(name=settings.AUTH_LDAP_DEFAULT_SENTRY_ORGANIZATION) - - if not organizations or len(organizations) < 1: - return model - member_role = getattr(settings, 'AUTH_LDAP_SENTRY_ORGANIZATION_ROLE_TYPE', 'member') has_global_access = getattr(settings, 'AUTH_LDAP_SENTRY_ORGANIZATION_GLOBAL_ACCESS', False) - # Add the user to the organization with global access - OrganizationMember.objects.create( - organization=organizations[0], - user=user, - role=member_role, - has_global_access=has_global_access, - flags=getattr(OrganizationMember.flags, 'sso:linked'), - ) + # Add user in each organization in AUTH_LDAP_DEFAULT_SENTRY_ORGANIZATION + for org in settings.AUTH_LDAP_DEFAULT_SENTRY_ORGANIZATION.split(","): + organizations = Organization.objects.filter(name=org) + if not organizations or len(organizations) < 1: + continue + self._add_user_in_organization(organizations[0], user, member_role, has_global_access) if not getattr(settings, 'AUTH_LDAP_SENTRY_SUBSCRIBE_BY_DEFAULT', True): UserOption.objects.set_value( @@ -82,3 +74,13 @@ def get_or_create_user(self, username, ldap_user): ) return model + + def _add_user_in_organization(self, organizations, user, member_role, has_global_access): + # Add the user to the organization with global access + OrganizationMember.objects.create( + organization=organizations, + user=user, + role=member_role, + has_global_access=has_global_access, + flags=getattr(OrganizationMember.flags, 'sso:linked'), + ) From 4732e37e0be8d0b8ac4b9def1c45bbf947fa9b36 Mon Sep 17 00:00:00 2001 From: Serge Ohl Date: Wed, 19 Sep 2018 16:15:45 -0400 Subject: [PATCH 2/2] add doc for multi org --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9168853..998c383 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,11 @@ Then, add any applicable configuration options. Depending on your environment, a ```Python AUTH_LDAP_DEFAULT_SENTRY_ORGANIZATION = u'My Organization Name' ``` -Auto adds created user to the specified organization (matched by name) if it exists. +Auto adds created user to the specified organization (matched by name) if it exists. It can be a list of organizations like: + +```Python +AUTH_LDAP_DEFAULT_SENTRY_ORGANIZATION = u'My Organization Name,sentry' +``` ```Python AUTH_LDAP_SENTRY_ORGANIZATION_ROLE_TYPE = 'member'