Added new role management

This commit is contained in:
Luka Radenovic 2022-05-16 13:44:15 +02:00 committed by Maarten de Waard
parent efbc1b21c9
commit 61e512c208
3 changed files with 71 additions and 27 deletions

View file

@ -1,4 +1,5 @@
import copy import copy
from areas.apps.models import App
from database import db from database import db
from areas.apps import AppRole from areas.apps import AppRole
@ -28,13 +29,17 @@ class UserService:
} }
res = KratosApi.post("/admin/identities", kratos_data).json() res = KratosApi.post("/admin/identities", kratos_data).json()
appRole = AppRole( 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"], user_id=res["id"],
role_id=data["role_id"] if "role_id" in data else None, role_id=ar["role_id"] if "role_id" in ar else None,
app_id=1, app_id=app.id,
) )
db.session.add(appRole) db.session.add(app_role)
db.session.commit() db.session.commit()
return UserService.get_user(res["id"]) return UserService.get_user(res["id"])
@ -47,15 +52,20 @@ class UserService:
} }
KratosApi.put("/admin/identities/{}".format(id), kratos_data) KratosApi.put("/admin/identities/{}".format(id), kratos_data)
app_role = AppRole.query.filter_by(user_id=id).first() 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.query.filter_by(user_id=id, app_id=app.id).first()
if app_role: if app_role:
app_role.role_id = data["role_id"] if "role_id" in data else None app_role.role_id = ar["role_id"] if "role_id" in ar else None
db.session.commit() db.session.commit()
else: else:
appRole = AppRole( appRole = AppRole(
user_id=id, user_id=id,
role_id=data["role_id"] if "role_id" in data else None, role_id=ar["role_id"] if "role_id" in ar else None,
app_id=1, app_id=app.id,
) )
db.session.add(appRole) db.session.add(appRole)
db.session.commit() db.session.commit()
@ -63,8 +73,27 @@ class UserService:
return UserService.get_user(id) return UserService.get_user(id)
@staticmethod @staticmethod
def __insertAppRoleToUser(userId, userRes): def delete_user(id):
app_role = AppRole.query.filter_by(user_id=userId).first() app_role = AppRole.query.filter_by(user_id=id).all()
userRes["traits"]["role_id"] = app_role.role_id if app_role else None for ar in app_role:
db.session.delete(ar)
db.session.commit()
@staticmethod
def __insertAppRoleToUser(userId, userRes):
app_role = AppRole.query.filter_by(user_id=userId)
apps = App.query.all()
app_roles = []
for app in apps:
tmp_app_role = app_role.filter_by(app_id=app.id).first()
app_roles.append(
{
"name": app.slug,
"role_id": tmp_app_role.role_id if tmp_app_role else None,
}
)
userRes["traits"]["app_roles"] = app_roles
return userRes return userRes

View file

@ -51,6 +51,7 @@ def put_user(id):
@cross_origin() @cross_origin()
def delete_user(id): def delete_user(id):
res = KratosApi.delete("/identities/{}".format(id)) res = KratosApi.delete("/identities/{}".format(id))
UserService.delete_user(id)
if res.status_code == 204: if res.status_code == 204:
return jsonify(), res.status_code return jsonify(), res.status_code
return jsonify(res.json()), res.status_code return jsonify(res.json()), res.status_code

View file

@ -9,11 +9,25 @@ schema = {
"pattern": r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])", "pattern": r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])",
"minLength": 1, "minLength": 1,
}, },
"app_roles": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of the app",
"minLenght": 1,
},
"role_id": { "role_id": {
"type": "integer", "type": ["integer", "null"],
"description": "Role of the user", "description": "Role of the user",
"minimum": 1, "minimum": 1,
}, },
}, },
"required": ["email"], "required": ["name", "role_id"],
},
},
},
"required": ["email", "app_roles"],
} }