Initial commit – AufmaßCreater v2.35
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
from app.extensions import db
|
||||
from app.models.license import License, LicenseModule
|
||||
from app.models.module import Module
|
||||
|
||||
|
||||
def check_company_module_access(company_id, module_name):
|
||||
"""Prüft, ob eine Firma ein Modul nutzen darf (License + CompanyModule)."""
|
||||
module = Module.query.filter_by(name=module_name).first()
|
||||
if not module:
|
||||
return False
|
||||
if module.standard:
|
||||
return True
|
||||
|
||||
license = License.query.filter_by(company_id=company_id, aktiv=True).first()
|
||||
if license:
|
||||
if LicenseModule.query.filter_by(
|
||||
license_id=license.id, module_id=module.id, aktiv=True
|
||||
).first():
|
||||
return True
|
||||
|
||||
from app.models.company_module import CompanyModule
|
||||
if CompanyModule.query.filter_by(company_id=company_id, module_id=module.id, aktiv=True).first():
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def check_module_access(company_id, module_name):
|
||||
"""Prüft, ob eine Firma ein Modul nutzen darf (ohne User-Check)."""
|
||||
return check_company_module_access(company_id, module_name)
|
||||
|
||||
|
||||
def check_user_module_access(user, module_name):
|
||||
"""Prüft, ob ein bestimmter Benutzer ein Modul nutzen darf."""
|
||||
if user.is_superadmin():
|
||||
return True
|
||||
|
||||
module = Module.query.filter_by(name=module_name).first()
|
||||
if not module:
|
||||
return False
|
||||
if module.standard:
|
||||
return True
|
||||
|
||||
if not check_company_module_access(user.company_id, module_name):
|
||||
return False
|
||||
|
||||
if user.is_firmadmin():
|
||||
return True
|
||||
|
||||
from app.models.user_module import UserModulePermission
|
||||
if UserModulePermission.query.filter_by(user_id=user.id, module_id=module.id, aktiv=True).first():
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def get_aktive_module(company_id, user=None):
|
||||
"""Liefert alle Module, die die Firma (bzw. der Benutzer) nutzen darf."""
|
||||
all_modules = Module.query.order_by(Module.sortierung).all()
|
||||
|
||||
license = License.query.filter_by(company_id=company_id, aktiv=True).first()
|
||||
aktiv_module_ids = set()
|
||||
if license:
|
||||
for lm in LicenseModule.query.filter_by(license_id=license.id, aktiv=True).all():
|
||||
aktiv_module_ids.add(lm.module_id)
|
||||
|
||||
from app.models.company_module import CompanyModule
|
||||
company_module_ids = {
|
||||
cm.module_id for cm in CompanyModule.query.filter_by(company_id=company_id, aktiv=True).all()
|
||||
}
|
||||
|
||||
from app.models.user_module import UserModulePermission
|
||||
user_module_ids = set()
|
||||
if user and not user.is_firmadmin():
|
||||
user_module_ids = {
|
||||
um.module_id for um in UserModulePermission.query.filter_by(user_id=user.id, aktiv=True).all()
|
||||
}
|
||||
|
||||
result = []
|
||||
for m in all_modules:
|
||||
if m.standard:
|
||||
result.append(m)
|
||||
elif m.id in aktiv_module_ids:
|
||||
result.append(m)
|
||||
elif m.id in company_module_ids:
|
||||
if user is None or user.is_firmadmin():
|
||||
result.append(m)
|
||||
elif m.id in user_module_ids:
|
||||
result.append(m)
|
||||
return result
|
||||
Reference in New Issue
Block a user