Improve keycloak user export further

This commit is contained in:
Paul Bienkowski 2021-11-17 19:56:40 +01:00
parent fbc0e26912
commit 53e8d3ea45
3 changed files with 37 additions and 17 deletions

View file

@ -93,7 +93,14 @@ async def login_redirect(req):
).scalar()
if user:
log.info(
"Re-matched existing user %s (sub: %s) based on email and username (%s)",
user.id,
user.sub,
preferred_username,
)
user.match_by_username_email = False
user.sub = sub
if user is None:
log.info(
@ -104,7 +111,7 @@ async def login_redirect(req):
user = User(sub=sub, username=preferred_username, email=email)
req.ctx.db.add(user)
else:
log.info("Logged in known user (id: %s).", user.id)
log.info("Logged in known user (id: %s, sub: %s).", user.id, user.sub)
if email != user.email:
log.debug("Updating user (id: %s) email from auth system.", user.id)

View file

@ -4,6 +4,7 @@ import sys
import os
import argparse
import asyncio
import logging
from obs.api.app import app
from obs.api.db import connect_db
@ -11,6 +12,12 @@ from obs.api.db import connect_db
def main():
debug = app.config.DEBUG
logging.basicConfig(
level=logging.DEBUG if debug else logging.INFO,
format="%(levelname)s: %(message)s",
)
app.run(
host=app.config.HOST,
port=app.config.PORT,

View file

@ -61,7 +61,6 @@ async def import_users(mongo, session, keycloak_users_file):
new_user = User(
sub=str(uuid4()),
match_by_username_email=True,
email=user["email"],
username=user["username"],
bio=user.get("bio"),
@ -70,22 +69,24 @@ async def import_users(mongo, session, keycloak_users_file):
api_key=str(user["_id"]),
created_at=user.get("createdAt") or datetime.utcnow(),
updated_at=user.get("updatedAt") or datetime.utcnow(),
match_by_username_email=True,
)
needs_email_verification = user.get("needsEmailValidation", True)
required_actions = ["UPDATE_PASSWORD"]
if needs_email_verification:
required_actions.append("VERIFY_EMAIL")
if keycloak_users_file:
needs_email_verification = user.get("needsEmailValidation", True)
required_actions = ["UPDATE_PASSWORD"]
if needs_email_verification:
required_actions.append("VERIFY_EMAIL")
keycloak_users.append(
{
"username": user["username"],
"email": user["email"],
"enabled": True,
"requiredActions": required_actions,
"emailVerified": not needs_email_verification,
}
)
keycloak_users.append(
{
"username": new_user.username,
"email": new_user.email,
"enabled": True,
"requiredActions": required_actions,
"emailVerified": not needs_email_verification,
}
)
session.add(new_user)
log.info("Creating user %s", new_user.username)
@ -98,7 +99,7 @@ async def import_users(mongo, session, keycloak_users_file):
id_map[old_id_by_email[user.email]] = user.id
if keycloak_users_file:
json.dump(keycloak_users, keycloak_users_file, indent=4)
json.dump({"users": keycloak_users}, keycloak_users_file, indent=4)
log.info("Wrote keycloak users file to %s.", keycloak_users_file.name)
return id_map
@ -111,6 +112,8 @@ def parse_datetime(s):
async def import_tracks(mongo, session, user_id_map):
track_count = 0
async for track in mongo.tracks.find({}):
stats = track.get("statistics") or {}
new_track = Track(
@ -139,7 +142,6 @@ async def import_tracks(mongo, session, user_id_map):
)
session.add(new_track)
log.info("Creating track %s", new_track.slug)
comment_ids = track.get("comments") or []
if comment_ids:
@ -155,6 +157,10 @@ async def import_tracks(mongo, session, user_id_map):
new_track.comments.append(new_comment)
session.add(new_comment)
track_count += 1
log.info("Created %s tracks", track_count)
await session.commit()