prepare endpoint for batch create users
This commit is contained in:
parent
33ffc5a895
commit
8eacdc8db9
3 changed files with 22 additions and 25 deletions
|
@ -1,2 +1,3 @@
|
|||
from .apps import *
|
||||
from .apps_service import *
|
||||
from .models import *
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue