Skip to content
This repository has been archived by the owner on Sep 24, 2023. It is now read-only.

Add multi default orgs #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
30 changes: 16 additions & 14 deletions sentry_ldap_auth/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be

Suggested change
self._add_user_in_organization(organizations[0], user, member_role, has_global_access)
self._add_user_in_organization(org, user, member_role, has_global_access)


if not getattr(settings, 'AUTH_LDAP_SENTRY_SUBSCRIBE_BY_DEFAULT', True):
UserOption.objects.set_value(
Expand All @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit.. rename organizations to org

Suggested change
def _add_user_in_organization(self, organizations, user, member_role, has_global_access):
def _add_user_in_organization(self, org, user, member_role, has_global_access):

# Add the user to the organization with global access
OrganizationMember.objects.create(
organization=organizations,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
organization=organizations,
organization=org,

user=user,
role=member_role,
has_global_access=has_global_access,
flags=getattr(OrganizationMember.flags, 'sso:linked'),
)