From 8eacdc8db9038980c4b353de5cddd7f93efdacf4 Mon Sep 17 00:00:00 2001 From: Davor Date: Tue, 12 Jul 2022 00:12:33 +0200 Subject: [PATCH] prepare endpoint for batch create users --- areas/apps/__init__.py | 1 + areas/apps/apps_service.py | 2 +- areas/users/user_service.py | 44 +++++++++++++++++-------------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/areas/apps/__init__.py b/areas/apps/__init__.py index 937a88c..c798e15 100644 --- a/areas/apps/__init__.py +++ b/areas/apps/__init__.py @@ -1,2 +1,3 @@ from .apps import * +from .apps_service import * from .models import * diff --git a/areas/apps/apps_service.py b/areas/apps/apps_service.py index e48d588..3ab57c1 100644 --- a/areas/apps/apps_service.py +++ b/areas/apps/apps_service.py @@ -2,7 +2,7 @@ from .models import App, AppRole class AppsService: @staticmethod - def get_apps(): + def get_all_apps(): apps = App.query.all() return [{"id": app.id, "name": app.name, "slug": app.slug} for app in apps] diff --git a/areas/users/user_service.py b/areas/users/user_service.py index d46eb8f..d6062ec 100644 --- a/areas/users/user_service.py +++ b/areas/users/user_service.py @@ -1,11 +1,13 @@ from database import db -from areas.apps.models import App, AppRole +from areas.apps import App, AppRole, AppsService from helpers import KratosApi from flask import current_app class UserService: + no_access_role_id = 3 + @staticmethod def get_users(): res = KratosApi.get("/admin/identities").json() @@ -34,7 +36,18 @@ class UserService: app = App.query.filter_by(slug=ar["name"]).first() app_role = AppRole( user_id=res["id"], - role_id=ar["role_id"] if "role_id" in ar else None, + role_id=ar["role_id"] if "role_id" in ar else UserService.no_access_role_id, + app_id=app.id, + ) + + db.session.add(app_role) + db.session.commit() + else: + all_apps = AppsService.get_all_apps() + for app in all_apps: + app_role = AppRole( + user_id=res["id"], + role_id=UserService.no_access_role_id, app_id=app.id, ) @@ -84,35 +97,18 @@ class UserService: # check if data is array # for every item in array call Kratos - check if there can be batch create on Kratos # - if yes, what happens with the batch if there is at least one existing email + created_users = [] for user_data in data: - user_email = user_data["email"] - user_name = user_data["name"] try: - kratos_data = { - "schema_id": "default", - "traits": {"email": user_email, "name": user_name}, - } - res = KratosApi.post("/admin/identities", kratos_data).json() - - if data["app_roles"]: - app_roles = data["app_roles"] - for ar in app_roles: - app = App.query.filter_by(slug=ar["name"]).first() - app_role = AppRole( - user_id=res["id"], - role_id=ar["role_id"] if "role_id" in ar else None, - app_id=app.id, - ) - - db.session.add(app_role) - db.session.commit() + user = UserService.post_user(user) + created_users.append(user) except Exception: current_app.logger.error( "Exception calling Kratos %s\n on creating user %s, %s\n", - Exception, user_email, user_name) + Exception, user_data["email"], user_data["name"]) - return UserService.get_user(res["id"]) + return created_users @staticmethod def __insertAppRoleToUser(userId, userRes):