dashboard/areas/users/user_service.py

130 lines
4.1 KiB
Python
Raw Normal View History

from database import db
from areas.apps import App, AppRole, AppsService
from helpers import KratosApi
2022-07-11 15:11:18 +02:00
from flask import current_app
class UserService:
no_access_role_id = 3
@staticmethod
def get_users():
res = KratosApi.get("/admin/identities").json()
userList = []
for r in res:
userList.append(UserService.__insertAppRoleToUser(r["id"], r))
return userList
@staticmethod
def get_user(id):
res = KratosApi.get("/admin/identities/{}".format(id)).json()
return UserService.__insertAppRoleToUser(id, res)
@staticmethod
def post_user(data):
kratos_data = {
"schema_id": "default",
"traits": {"email": data["email"], "name": data["name"]},
}
res = KratosApi.post("/admin/identities", kratos_data).json()
2022-05-16 13:44:15 +02:00
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 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,
2022-05-16 13:44:15 +02:00
app_id=app.id,
)
2022-05-16 13:44:15 +02:00
db.session.add(app_role)
db.session.commit()
return UserService.get_user(res["id"])
@staticmethod
def put_user(id, data):
kratos_data = {
"schema_id": "default",
"traits": {"email": data["email"], "name": data["name"]},
}
KratosApi.put("/admin/identities/{}".format(id), kratos_data)
2022-05-16 13:44:15 +02:00
if data["app_roles"]:
app_roles = data["app_roles"]
for ar in app_roles:
app = App.query.filter_by(slug=ar["name"]).first()
2022-07-11 15:11:18 +02:00
app_role = AppRole.query.filter_by(
user_id=id, app_id=app.id).first()
2022-05-16 13:44:15 +02:00
if app_role:
app_role.role_id = ar["role_id"] if "role_id" in ar else None
db.session.commit()
else:
appRole = AppRole(
user_id=id,
role_id=ar["role_id"] if "role_id" in ar else None,
app_id=app.id,
)
db.session.add(appRole)
db.session.commit()
return UserService.get_user(id)
2022-05-16 13:44:15 +02:00
@staticmethod
def delete_user(id):
app_role = AppRole.query.filter_by(user_id=id).all()
2022-05-19 19:01:26 +02:00
for ar in app_role:
db.session.delete(ar)
db.session.commit()
2022-05-16 13:44:15 +02:00
2022-07-11 15:11:18 +02:00
@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
created_users = []
2022-07-11 15:11:18 +02:00
for user_data in data:
try:
user = UserService.post_user(user)
created_users.append(user)
2022-07-11 15:11:18 +02:00
except Exception:
current_app.logger.error(
"Exception calling Kratos %s\n on creating user %s, %s\n",
Exception, user_data["email"], user_data["name"])
2022-07-11 15:11:18 +02:00
return created_users
2022-07-11 15:11:18 +02:00
@staticmethod
def __insertAppRoleToUser(userId, userRes):
2022-05-16 13:44:15 +02:00
apps = App.query.all()
app_roles = []
for app in apps:
2022-05-16 14:01:56 +02:00
tmp_app_role = AppRole.query.filter_by(
user_id=userId, app_id=app.id
).first()
2022-05-16 13:44:15 +02:00
app_roles.append(
{
"name": app.slug,
"role_id": tmp_app_role.role_id if tmp_app_role else None,
}
)
2022-05-16 13:44:15 +02:00
userRes["traits"]["app_roles"] = app_roles
return userRes