Fixes #1432 Refactors the invite + registration system to be simpler and more consistent with regards to existing user invites. Previously, per-user invites are stored in the user.invites dict instead of in the invites collection, which creates a few issues: - Existing user do not show up in Org Invites list: #1432 - Existing user invites also do not expire, unlike new user invites, creating potential security issue. Instead, existing user invites should be treated like new user invites. This PR moves them into the same collection, adding a `userid` field to InvitePending to match with an existing user. If a user already exists, it will be matched by userid, instead of by email. This allows for user to update their email while still being invited. Note that the email of the invited existing user will not change in the invite email. This is also by design: an admin of one org should not be given any hint that an invited user already has an account, such as by having their email automatically update. For an org admin, the invite to a new or existing user should be indistinguishable. The sha256 of invite token is stored instead of actual token for better security. The registration system has also been refactored with the following changes: - Auto-creation of new orgs for new users has been removed - User.create_user() replaces the old User._create() and just creates the user with additional complex logic around org auto-add - Users are added to org in org add_user_to_org() - Users are added to org through invites with add_user_with_invite() Tests: - Additional tests include verifying that existing and new pending invites appear in the pending invites list - Tests for `/users/invite/<token>?email=` and `/users/me/invite/<token>` endpoints - Deleting pending invites - Additional tests added for user self-registration, including existing user self-registration to default org of existing user (in nightly tests)
53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
"""
|
|
Migration 0030 - Move user invites from user.invites to invites collection
|
|
"""
|
|
|
|
from btrixcloud.migrations import BaseMigration
|
|
from btrixcloud.models import InvitePending
|
|
from btrixcloud.invites import get_hash
|
|
|
|
MIGRATION_VERSION = "0030"
|
|
|
|
|
|
class Migration(BaseMigration):
|
|
"""Migration class."""
|
|
|
|
# pylint: disable=unused-argument
|
|
def __init__(self, mdb, **kwargs):
|
|
super().__init__(mdb, migration_version=MIGRATION_VERSION)
|
|
|
|
async def migrate_up(self):
|
|
"""Perform migration up.
|
|
|
|
Iterate over all users that have invites to other orgs
|
|
Add the invites to the invites collection instead
|
|
"""
|
|
users_db = self.mdb["users"]
|
|
invites_db = self.mdb["invites"]
|
|
|
|
# flatten user invites
|
|
async for user in users_db.find({"invites": {"$nin": [None, {}]}}):
|
|
for user_invite in user["invites"].values():
|
|
user_invite["email"] = user["email"]
|
|
print("Migrating existing user invite", user_invite)
|
|
invite = InvitePending(
|
|
userid=user["id"],
|
|
tokenHash=get_hash(user_invite["id"]),
|
|
**user_invite
|
|
)
|
|
await invites_db.insert_one(invite.to_dict())
|
|
|
|
await users_db.find_one_and_update(
|
|
{"_id": user["_id"]}, {"$unset": {"invites": 1}}
|
|
)
|
|
|
|
# add tokenHash to existing invites without it
|
|
# note that tokenHash is of the existing _id
|
|
# for new invites, the tokenHash will be of a separate uuid that is not stored
|
|
async for invite_data in invites_db.find({"tokenHash": {"$eq": None}}):
|
|
print("Migrating new user invite", invite_data)
|
|
await invites_db.find_one_and_update(
|
|
{"_id": invite_data["_id"]},
|
|
{"$set": {"tokenHash": get_hash(invite_data["_id"])}},
|
|
)
|