Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow configuring designated registration org to which new users can register #1735

Merged
merged 2 commits into from
Apr 23, 2024
Merged
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
3 changes: 3 additions & 0 deletions backend/btrixcloud/orgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ async def get_org_for_user_by_id(
async def get_org_by_id(self, oid: UUID):
"""Get an org by id"""
res = await self.orgs.find_one({"_id": oid})
if not res:
raise HTTPException(status_code=400, detail="invalid_org_id")

return Organization.from_dict(res)

async def get_default_org(self):
Expand Down
19 changes: 13 additions & 6 deletions backend/btrixcloud/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(self, mdb, email, invites):
self.email_collation = Collation("en", strength=2)

self.registration_enabled = is_bool(os.environ.get("REGISTRATION_ENABLED"))
self.register_to_org_id = os.environ.get("REGISTER_TO_ORG_ID")

# pylint: disable=attribute-defined-outside-init
def set_ops(self, org_ops, crawl_config_ops, base_crawl_ops):
Expand Down Expand Up @@ -335,6 +336,7 @@ async def _create(
self, create: UserCreateIn, request: Optional[Request] = None
) -> User:
"""create new user in db"""
# pylint: disable=too-many-branches
await self.validate_password(create.password)

hashed_password = get_password_hash(create.password)
Expand Down Expand Up @@ -362,7 +364,7 @@ async def _create(
except DuplicateKeyError:
raise HTTPException(status_code=400, detail="user_already_exists")

add_to_default_org = False
add_to_org = False

if create.inviteToken:
new_user_invite = None
Expand All @@ -374,19 +376,24 @@ async def _create(
print(exc)

if new_user_invite and not new_user_invite.oid:
add_to_default_org = True
add_to_org = True

else:
add_to_default_org = True
add_to_org = True
if not is_verified:
asyncio.create_task(self.request_verify(user, request))

# org to auto-add user to, if any
auto_add_org: Optional[Organization] = None

# if add to default, then get default org
if add_to_default_org:
auto_add_org = await self.org_ops.get_default_org()
if add_to_org:
if self.register_to_org_id:
auto_add_org = await self.org_ops.get_org_by_id(
UUID(self.register_to_org_id)
)
else:
auto_add_org = await self.org_ops.get_default_org()

# if creating new org, create here
elif create.newOrg is True:
Expand All @@ -401,7 +408,7 @@ async def _create(

# if org set, add user to org
if auto_add_org:
await self.org_ops.add_user_to_org(auto_add_org, user.id)
await self.org_ops.add_user_to_org(auto_add_org, user.id, UserRole.CRAWLER)

return user

Expand Down
2 changes: 2 additions & 0 deletions chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ data:

REGISTRATION_ENABLED: "{{ .Values.registration_enabled | default 0 }}"

REGISTER_TO_ORG_ID: "{{ .Values.registration_org_id }}"

ALLOW_DUPE_INVITES: "{{ .Values.allow_dupe_invites | default 0 }}"

JWT_TOKEN_LIFETIME_MINUTES: "{{ .Values.jwt_token_lifetime_minutes | default 60 }}"
Expand Down
4 changes: 4 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ volume_storage_class:
# crawler_node_type:

registration_enabled: "0"

# if set, along with 'registration_enabled', will add registrated users to this org
# registration_org_id: ""

jwt_token_lifetime_minutes: 1440

# if set to "1", allow inviting same user to same org multiple times
Expand Down
Loading