wip - add batch create users
This commit is contained in:
parent
420c85cf8d
commit
33ffc5a895
3 changed files with 59 additions and 2 deletions
|
@ -2,6 +2,9 @@ from database import db
|
|||
from areas.apps.models import App, AppRole
|
||||
from helpers import KratosApi
|
||||
|
||||
from flask import current_app
|
||||
|
||||
|
||||
class UserService:
|
||||
@staticmethod
|
||||
def get_users():
|
||||
|
@ -52,7 +55,8 @@ class UserService:
|
|||
app_roles = data["app_roles"]
|
||||
for ar in app_roles:
|
||||
app = App.query.filter_by(slug=ar["name"]).first()
|
||||
app_role = AppRole.query.filter_by(user_id=id, app_id=app.id).first()
|
||||
app_role = AppRole.query.filter_by(
|
||||
user_id=id, app_id=app.id).first()
|
||||
|
||||
if app_role:
|
||||
app_role.role_id = ar["role_id"] if "role_id" in ar else None
|
||||
|
@ -75,6 +79,41 @@ class UserService:
|
|||
db.session.delete(ar)
|
||||
db.session.commit()
|
||||
|
||||
@staticmethod
|
||||
def post_multiple_users(data):
|
||||
# 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
|
||||
|
||||
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()
|
||||
except Exception:
|
||||
current_app.logger.error(
|
||||
"Exception calling Kratos %s\n on creating user %s, %s\n",
|
||||
Exception, user_email, user_name)
|
||||
|
||||
return UserService.get_user(res["id"])
|
||||
|
||||
@staticmethod
|
||||
def __insertAppRoleToUser(userId, userRes):
|
||||
apps = App.query.all()
|
||||
|
|
|
@ -7,7 +7,7 @@ from areas import api_v1
|
|||
from helpers import KratosApi
|
||||
from helpers.auth_guard import admin_required
|
||||
|
||||
from .validation import schema
|
||||
from .validation import schema, schema_multiple
|
||||
from .user_service import UserService
|
||||
|
||||
|
||||
|
@ -59,3 +59,14 @@ def delete_user(id):
|
|||
UserService.delete_user(id)
|
||||
return jsonify(), res.status_code
|
||||
return jsonify(res.json()), res.status_code
|
||||
|
||||
|
||||
@api_v1.route("/users-batch", methods=["POST"])
|
||||
@jwt_required()
|
||||
@cross_origin()
|
||||
@expects_json(schema_multiple)
|
||||
@admin_required()
|
||||
def post_multiple_users():
|
||||
data = request.get_json()
|
||||
res = UserService.post_multiple_users(data)
|
||||
return jsonify(res)
|
||||
|
|
|
@ -31,3 +31,10 @@ schema = {
|
|||
},
|
||||
"required": ["email", "app_roles"],
|
||||
}
|
||||
|
||||
schema_multiple = {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": schema
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue