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,14 +29,18 @@ class UserService:
} }
res = KratosApi.post("/admin/identities", kratos_data).json() res = KratosApi.post("/admin/identities", kratos_data).json()
appRole = AppRole( if data["app_roles"]:
user_id=res["id"], app_roles = data["app_roles"]
role_id=data["role_id"] if "role_id" in data else None, for ar in app_roles:
app_id=1, 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(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,24 +52,48 @@ 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"]:
if app_role: app_roles = data["app_roles"]
app_role.role_id = data["role_id"] if "role_id" in data else None for ar in app_roles:
db.session.commit() app = App.query.filter_by(slug=ar["name"]).first()
else: app_role = AppRole.query.filter_by(user_id=id, app_id=app.id).first()
appRole = AppRole(
user_id=id, if 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
app_id=1, db.session.commit()
) else:
db.session.add(appRole) appRole = AppRole(
db.session.commit() 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) 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,
}, },
"role_id": { "app_roles": {
"type": "integer", "type": "array",
"description": "Role of the user", "items": {
"minimum": 1, "type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of the app",
"minLenght": 1,
},
"role_id": {
"type": ["integer", "null"],
"description": "Role of the user",
"minimum": 1,
},
},
"required": ["name", "role_id"],
},
}, },
}, },
"required": ["email"], "required": ["email", "app_roles"],
} }