Skip to content
Snippets Groups Projects
Commit 8eacdc8d authored by Davor's avatar Davor
Browse files

prepare endpoint for batch create users

parent 33ffc5a8
No related branches found
No related tags found
1 merge request!55Resolve "Merge dashboard and dashboard-backend repos"
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]
......
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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment