73 lines
3.2 KiB
Python
73 lines
3.2 KiB
Python
from . import APP_ADMIN, SYS_ADMIN, User, Userapp, dbauth, Application, Role, apps_roles, userapps_roles
|
|
from .errors import AutorisationError, ApplicationError
|
|
from flask_login import current_user
|
|
from sqlalchemy import delete
|
|
|
|
session = dbauth.session
|
|
|
|
def get_applications():
|
|
apps = []
|
|
for app in Application.query:
|
|
# Die Application wird nur zurückgegeben, wenn der current-User SysAdmin ist oder wenn
|
|
# der current_user in der zurückzugebenden Application AppAdmin ist
|
|
if current_user.is_SysAdmin() or current_user.has_permission(app.appid, [APP_ADMIN]):
|
|
if not current_user.is_SysAdmin():
|
|
for role in list(app.roles):
|
|
if role.roleid == SYS_ADMIN:
|
|
app.roles.remove(role)
|
|
apps.append(app)
|
|
return apps
|
|
|
|
def updateapplication(data):
|
|
if not current_user.is_SysAdmin():
|
|
raise AutorisationError(allowed_roles=[SYS_ADMIN], user_roles=current_user.get_userapproles(data["appid"]))
|
|
currentapp = None
|
|
if data["id"] == -1:
|
|
currentapp = Application()
|
|
currentapp.id = None
|
|
session.add(currentapp)
|
|
else:
|
|
currentapp = Application.query.get(data["id"])
|
|
if currentapp is None:
|
|
raise Exception ("404")
|
|
currentapp.name = data["name"]
|
|
currentapp.appid = data["appid"]
|
|
currentapp.minversion = data["minversion"]
|
|
currentapp.beschreibung = data["beschreibung"]
|
|
session.commit()
|
|
|
|
def deleteapplications(appids):
|
|
if not current_user.is_SysAdmin():
|
|
raise AutorisationError(allowed_roles=[SYS_ADMIN], user_roles=None)
|
|
for appid in appids:
|
|
app: Application = Application.query.filter(Application.appid == appid).one()
|
|
if app is not None:
|
|
# Die Datensätze in "apps_roles" müssen gelöscht werden
|
|
app.roles.clear()
|
|
# UserApps löschen
|
|
q_userapps = dbauth.select(Userapp). where(Userapp.appid == app.id)
|
|
for userapp in dbauth.session.scalars(q_userapps):
|
|
userapp.userapp_roles.clear()
|
|
session.delete(userapp)
|
|
session.delete(app)
|
|
session.commit()
|
|
|
|
def updateapplicationroles(approles):
|
|
# Änderungen an Applications darf grundsätzlich nur der SysAdmin machen
|
|
if not current_user.is_SysAdmin():
|
|
raise AutorisationError(allowed_roles=[SYS_ADMIN], user_roles=None)
|
|
|
|
for appidinfo in approles:
|
|
app: Application = Application.query.filter(Application.appid == appidinfo["appid"]).one()
|
|
if app is None:
|
|
raise ApplicationError(f"Die Application mit der Appid {appidinfo['appid']} existiert nicht", )
|
|
for roleidinfo in appidinfo["roles"]:
|
|
role: Role = Role.query.filter(Role.roleid == roleidinfo["roleid"]).one()
|
|
if role in app.roles:
|
|
if not roleidinfo['isChecked']:
|
|
app.roles.remove(role) # hier kann das remove einfach so verwendet werden, da nicht über app.roles iteriert wird
|
|
# TODO: die Rolle muss auch aus allen userapps für alle User gelöscht werden
|
|
else:
|
|
if roleidinfo['isChecked']:
|
|
app.roles.append(role)
|
|
session.commit() |