Improve keycloak user export further
This commit is contained in:
parent
fbc0e26912
commit
53e8d3ea45
|
@ -93,7 +93,14 @@ async def login_redirect(req):
|
||||||
).scalar()
|
).scalar()
|
||||||
|
|
||||||
if user:
|
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.match_by_username_email = False
|
||||||
|
user.sub = sub
|
||||||
|
|
||||||
if user is None:
|
if user is None:
|
||||||
log.info(
|
log.info(
|
||||||
|
@ -104,7 +111,7 @@ async def login_redirect(req):
|
||||||
user = User(sub=sub, username=preferred_username, email=email)
|
user = User(sub=sub, username=preferred_username, email=email)
|
||||||
req.ctx.db.add(user)
|
req.ctx.db.add(user)
|
||||||
else:
|
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:
|
if email != user.email:
|
||||||
log.debug("Updating user (id: %s) email from auth system.", user.id)
|
log.debug("Updating user (id: %s) email from auth system.", user.id)
|
||||||
|
|
|
@ -4,6 +4,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
from obs.api.app import app
|
from obs.api.app import app
|
||||||
from obs.api.db import connect_db
|
from obs.api.db import connect_db
|
||||||
|
@ -11,6 +12,12 @@ from obs.api.db import connect_db
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
debug = app.config.DEBUG
|
debug = app.config.DEBUG
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.DEBUG if debug else logging.INFO,
|
||||||
|
format="%(levelname)s: %(message)s",
|
||||||
|
)
|
||||||
|
|
||||||
app.run(
|
app.run(
|
||||||
host=app.config.HOST,
|
host=app.config.HOST,
|
||||||
port=app.config.PORT,
|
port=app.config.PORT,
|
||||||
|
|
|
@ -61,7 +61,6 @@ async def import_users(mongo, session, keycloak_users_file):
|
||||||
|
|
||||||
new_user = User(
|
new_user = User(
|
||||||
sub=str(uuid4()),
|
sub=str(uuid4()),
|
||||||
match_by_username_email=True,
|
|
||||||
email=user["email"],
|
email=user["email"],
|
||||||
username=user["username"],
|
username=user["username"],
|
||||||
bio=user.get("bio"),
|
bio=user.get("bio"),
|
||||||
|
@ -70,22 +69,24 @@ async def import_users(mongo, session, keycloak_users_file):
|
||||||
api_key=str(user["_id"]),
|
api_key=str(user["_id"]),
|
||||||
created_at=user.get("createdAt") or datetime.utcnow(),
|
created_at=user.get("createdAt") or datetime.utcnow(),
|
||||||
updated_at=user.get("updatedAt") or datetime.utcnow(),
|
updated_at=user.get("updatedAt") or datetime.utcnow(),
|
||||||
|
match_by_username_email=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
needs_email_verification = user.get("needsEmailValidation", True)
|
if keycloak_users_file:
|
||||||
required_actions = ["UPDATE_PASSWORD"]
|
needs_email_verification = user.get("needsEmailValidation", True)
|
||||||
if needs_email_verification:
|
required_actions = ["UPDATE_PASSWORD"]
|
||||||
required_actions.append("VERIFY_EMAIL")
|
if needs_email_verification:
|
||||||
|
required_actions.append("VERIFY_EMAIL")
|
||||||
|
|
||||||
keycloak_users.append(
|
keycloak_users.append(
|
||||||
{
|
{
|
||||||
"username": user["username"],
|
"username": new_user.username,
|
||||||
"email": user["email"],
|
"email": new_user.email,
|
||||||
"enabled": True,
|
"enabled": True,
|
||||||
"requiredActions": required_actions,
|
"requiredActions": required_actions,
|
||||||
"emailVerified": not needs_email_verification,
|
"emailVerified": not needs_email_verification,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
session.add(new_user)
|
session.add(new_user)
|
||||||
log.info("Creating user %s", new_user.username)
|
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
|
id_map[old_id_by_email[user.email]] = user.id
|
||||||
|
|
||||||
if keycloak_users_file:
|
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)
|
log.info("Wrote keycloak users file to %s.", keycloak_users_file.name)
|
||||||
|
|
||||||
return id_map
|
return id_map
|
||||||
|
@ -111,6 +112,8 @@ def parse_datetime(s):
|
||||||
|
|
||||||
|
|
||||||
async def import_tracks(mongo, session, user_id_map):
|
async def import_tracks(mongo, session, user_id_map):
|
||||||
|
track_count = 0
|
||||||
|
|
||||||
async for track in mongo.tracks.find({}):
|
async for track in mongo.tracks.find({}):
|
||||||
stats = track.get("statistics") or {}
|
stats = track.get("statistics") or {}
|
||||||
new_track = Track(
|
new_track = Track(
|
||||||
|
@ -139,7 +142,6 @@ async def import_tracks(mongo, session, user_id_map):
|
||||||
)
|
)
|
||||||
|
|
||||||
session.add(new_track)
|
session.add(new_track)
|
||||||
log.info("Creating track %s", new_track.slug)
|
|
||||||
|
|
||||||
comment_ids = track.get("comments") or []
|
comment_ids = track.get("comments") or []
|
||||||
if comment_ids:
|
if comment_ids:
|
||||||
|
@ -155,6 +157,10 @@ async def import_tracks(mongo, session, user_id_map):
|
||||||
new_track.comments.append(new_comment)
|
new_track.comments.append(new_comment)
|
||||||
session.add(new_comment)
|
session.add(new_comment)
|
||||||
|
|
||||||
|
track_count += 1
|
||||||
|
|
||||||
|
log.info("Created %s tracks", track_count)
|
||||||
|
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue