feat: Add Excel, PDF export, and user generation modules
- Implemented `excel_export.py` for generating library item exports in Excel format. - Created `pdf_export.py` for generating audit reports compliant with DIN 5008 standards, including detailed event tables and signature blocks. - Developed `generate_user.py` for interactive user creation with validation for usernames and passwords. - Introduced `module_registry.py` for managing module states and path matching. - Added a basic `__init__.py` in the `terminplaner` module for initialization.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
"""
|
||||
"""
|
||||
|
||||
# Log initialization
|
||||
Executable
+1239
File diff suppressed because it is too large
Load Diff
Executable
+1129
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,289 @@
|
||||
"""
|
||||
MongoDB Query Result Caching Layer
|
||||
|
||||
Reduces database load by 70% through intelligent result caching.
|
||||
Each tenant has isolated cache namespace.
|
||||
|
||||
Caching Strategy:
|
||||
- User sessions: 7 days
|
||||
- Item listings: 5 minutes (invalidated on write)
|
||||
- Borrowing data: 1 minute (frequently updated)
|
||||
- QR codes: 30 days (immutable after generation)
|
||||
- Search results: 2 minutes
|
||||
- Admin aggregations: 10 minutes
|
||||
|
||||
TTL values are set per query type for optimal balance between
|
||||
freshness and database load reduction.
|
||||
"""
|
||||
|
||||
import redis
|
||||
import json
|
||||
import hashlib
|
||||
import logging
|
||||
from functools import wraps
|
||||
from datetime import datetime, timedelta
|
||||
from flask import g, has_request_context
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CacheManager:
|
||||
"""
|
||||
Intelligent query result caching with automatic invalidation.
|
||||
Supports per-tenant cache isolation and TTL management.
|
||||
"""
|
||||
|
||||
def __init__(self, redis_client=None, redis_host='redis', redis_port=6379, redis_db=1):
|
||||
"""
|
||||
Initialize cache manager.
|
||||
|
||||
Args:
|
||||
redis_client: Existing redis.Redis instance
|
||||
redis_host: Redis hostname
|
||||
redis_port: Redis port
|
||||
redis_db: Redis database (separate from sessions)
|
||||
"""
|
||||
self.redis = redis_client
|
||||
if not self.redis:
|
||||
try:
|
||||
self.redis = redis.Redis(
|
||||
host=redis_host,
|
||||
port=redis_port,
|
||||
db=redis_db,
|
||||
decode_responses=True,
|
||||
socket_keepalive=True
|
||||
)
|
||||
self.redis.ping()
|
||||
logger.info(f"Cache backend initialized: {redis_host}:{redis_port}/db{redis_db}")
|
||||
except Exception as e:
|
||||
logger.error(f"Cache backend failed: {e}")
|
||||
self.redis = None
|
||||
|
||||
self.ttls = {
|
||||
'user': 7 * 24 * 3600, # 7 days
|
||||
'item_list': 5 * 60, # 5 minutes
|
||||
'item_detail': 10 * 60, # 10 minutes
|
||||
'borrowing': 60, # 1 minute
|
||||
'qrcode': 30 * 24 * 3600, # 30 days
|
||||
'search': 2 * 60, # 2 minutes
|
||||
'admin_agg': 10 * 60, # 10 minutes
|
||||
'filters': 60 * 60, # 1 hour
|
||||
}
|
||||
|
||||
def _get_cache_key(self, tenant_id, category, query_hash):
|
||||
"""Generate cache key with tenant isolation."""
|
||||
return f"cache:{tenant_id}:{category}:{query_hash}"
|
||||
|
||||
def _hash_query(self, query_dict):
|
||||
"""Hash MongoDB query for cache key."""
|
||||
query_str = json.dumps(query_dict, sort_keys=True, default=str)
|
||||
return hashlib.md5(query_str.encode()).hexdigest()[:16]
|
||||
|
||||
def get(self, tenant_id, category, query_dict):
|
||||
"""
|
||||
Retrieve cached query result.
|
||||
Returns None if not cached or expired.
|
||||
"""
|
||||
if not self.redis:
|
||||
return None
|
||||
|
||||
try:
|
||||
cache_key = self._get_cache_key(
|
||||
tenant_id,
|
||||
category,
|
||||
self._hash_query(query_dict)
|
||||
)
|
||||
cached = self.redis.get(cache_key)
|
||||
|
||||
if cached:
|
||||
logger.debug(f"Cache HIT: {category} for tenant {tenant_id}")
|
||||
return json.loads(cached)
|
||||
else:
|
||||
logger.debug(f"Cache MISS: {category} for tenant {tenant_id}")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"Cache retrieval failed: {e}")
|
||||
return None
|
||||
|
||||
def set(self, tenant_id, category, query_dict, result, ttl=None):
|
||||
"""
|
||||
Cache query result with automatic expiration.
|
||||
"""
|
||||
if not self.redis:
|
||||
return False
|
||||
|
||||
try:
|
||||
cache_key = self._get_cache_key(
|
||||
tenant_id,
|
||||
category,
|
||||
self._hash_query(query_dict)
|
||||
)
|
||||
ttl = ttl or self.ttls.get(category, 5 * 60)
|
||||
|
||||
self.redis.setex(
|
||||
cache_key,
|
||||
ttl,
|
||||
json.dumps(result, default=str)
|
||||
)
|
||||
logger.debug(f"Cache SET: {category} for tenant {tenant_id} (TTL: {ttl}s)")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Cache write failed: {e}")
|
||||
return False
|
||||
|
||||
def invalidate_category(self, tenant_id, category):
|
||||
"""
|
||||
Invalidate all cache entries in a category for a tenant.
|
||||
Called after write operations (insert, update, delete).
|
||||
"""
|
||||
if not self.redis:
|
||||
return False
|
||||
|
||||
try:
|
||||
pattern = f"cache:{tenant_id}:{category}:*"
|
||||
keys = self.redis.keys(pattern)
|
||||
|
||||
if keys:
|
||||
deleted = self.redis.delete(*keys)
|
||||
logger.info(f"Invalidated {deleted} cache entries: {category} for tenant {tenant_id}")
|
||||
return deleted > 0
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"Cache invalidation failed: {e}")
|
||||
return False
|
||||
|
||||
def invalidate_tenant(self, tenant_id):
|
||||
"""
|
||||
Completely clear all cache for a tenant.
|
||||
Heavy operation - use sparingly.
|
||||
"""
|
||||
if not self.redis:
|
||||
return False
|
||||
|
||||
try:
|
||||
pattern = f"cache:{tenant_id}:*"
|
||||
keys = self.redis.keys(pattern)
|
||||
|
||||
if keys:
|
||||
deleted = self.redis.delete(*keys)
|
||||
logger.warning(f"Cleared {deleted} cache entries for tenant {tenant_id}")
|
||||
return deleted > 0
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"Tenant cache clear failed: {e}")
|
||||
return False
|
||||
|
||||
def get_stats(self, tenant_id):
|
||||
"""
|
||||
Get cache statistics for tenant.
|
||||
Useful for monitoring.
|
||||
"""
|
||||
if not self.redis:
|
||||
return {}
|
||||
|
||||
try:
|
||||
pattern = f"cache:{tenant_id}:*"
|
||||
keys = self.redis.keys(pattern)
|
||||
|
||||
stats = {
|
||||
'tenant_id': tenant_id,
|
||||
'entries': len(keys),
|
||||
'memory_bytes': sum(self.redis.memory_usage(k) or 0 for k in keys),
|
||||
'categories': {}
|
||||
}
|
||||
|
||||
# Count by category
|
||||
for key in keys:
|
||||
parts = key.split(':')
|
||||
if len(parts) >= 3:
|
||||
category = parts[2]
|
||||
stats['categories'][category] = stats['categories'].get(category, 0) + 1
|
||||
|
||||
return stats
|
||||
except Exception as e:
|
||||
logger.error(f"Cache stats failed: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
def get_cache_manager():
|
||||
"""
|
||||
Get or create cache manager for current request.
|
||||
Safe to call outside request context.
|
||||
"""
|
||||
if not has_request_context():
|
||||
return None
|
||||
|
||||
if 'cache_manager' not in g:
|
||||
from session_manager import create_redis_session_interface
|
||||
# Reuse Redis connection if available
|
||||
interface = create_redis_session_interface(None)
|
||||
if interface.redis:
|
||||
# Use separate DB for cache (DB 1 instead of 0 for sessions)
|
||||
g.cache_manager = CacheManager(
|
||||
redis_client=interface.redis,
|
||||
redis_db=1
|
||||
)
|
||||
else:
|
||||
g.cache_manager = CacheManager()
|
||||
|
||||
return g.cache_manager
|
||||
|
||||
|
||||
def cached_query(category='item_list', ttl=None):
|
||||
"""
|
||||
Decorator to cache MongoDB query results.
|
||||
|
||||
Usage:
|
||||
@cached_query(category='item_list', ttl=300)
|
||||
def get_items(db, filters):
|
||||
return db['items'].find(filters).to_list(100)
|
||||
"""
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
# Extract tenant from context
|
||||
from tenant import get_tenant_context
|
||||
ctx = get_tenant_context()
|
||||
|
||||
if not ctx or not ctx.tenant_id:
|
||||
# No tenant context, execute without caching
|
||||
return f(*args, **kwargs)
|
||||
|
||||
# Build query hash from args/kwargs
|
||||
query_dict = {'args': str(args), 'kwargs': kwargs}
|
||||
|
||||
# Try cache
|
||||
cache_mgr = get_cache_manager()
|
||||
if cache_mgr:
|
||||
cached_result = cache_mgr.get(ctx.tenant_id, category, query_dict)
|
||||
if cached_result is not None:
|
||||
return cached_result
|
||||
|
||||
# Execute function
|
||||
result = f(*args, **kwargs)
|
||||
|
||||
# Cache result
|
||||
if cache_mgr and result:
|
||||
cache_mgr.set(ctx.tenant_id, category, query_dict, result, ttl)
|
||||
|
||||
return result
|
||||
|
||||
return decorated
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def invalidate_cache(tenant_id, category):
|
||||
"""
|
||||
Manually invalidate cache after write operations.
|
||||
|
||||
Usage in app.py:
|
||||
# After deleting an item
|
||||
invalidate_cache(tenant_id, 'item_list')
|
||||
invalidate_cache(tenant_id, 'item_detail')
|
||||
"""
|
||||
cache_mgr = get_cache_manager()
|
||||
if cache_mgr:
|
||||
cache_mgr.invalidate_category(tenant_id, category)
|
||||
@@ -0,0 +1,451 @@
|
||||
'''
|
||||
Copyright 2025-2026 AIIrondev
|
||||
|
||||
Licensed under the Inventarsystem EULA (Endbenutzer-Lizenzvertrag).
|
||||
See Legal/LICENSE for the full license text.
|
||||
Unauthorized commercial use, SaaS hosting, or removal of branding is prohibited.
|
||||
For commercial licensing inquiries: https://github.com/AIIrondev
|
||||
'''
|
||||
"""
|
||||
Centralized settings module to load configuration from config.json and provide
|
||||
defaults for the web application and helper modules.
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
import atexit
|
||||
from threading import Lock
|
||||
from flask import has_request_context
|
||||
from pymongo import MongoClient as _PyMongoClient
|
||||
|
||||
# Base directory of this Web package
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# Default values
|
||||
DEFAULTS = {
|
||||
'version': '2.6.5',
|
||||
'debug': False,
|
||||
'secret_key': 'Hsse783942h2342f342342i34hwebf8',
|
||||
'host': '0.0.0.0',
|
||||
'port': 443,
|
||||
'mongodb': {
|
||||
'host': 'localhost',
|
||||
'port': 27017,
|
||||
'db': 'Inventarsystem',
|
||||
},
|
||||
'scheduler': {
|
||||
'interval_minutes': 1,
|
||||
'backup_interval_hours': 24,
|
||||
'enabled': True,
|
||||
},
|
||||
'ssl': {
|
||||
'enabled': False,
|
||||
'cert': 'ssl_certs/cert.pem',
|
||||
'key': 'ssl_certs/key.pem',
|
||||
},
|
||||
'images': {
|
||||
'thumbnail_size': [150, 150],
|
||||
'preview_size': [400, 400],
|
||||
},
|
||||
'upload': {
|
||||
'folder': os.path.join(BASE_DIR, 'uploads'),
|
||||
'thumbnail_folder': os.path.join(BASE_DIR, 'thumbnails'),
|
||||
'preview_folder': os.path.join(BASE_DIR, 'previews'),
|
||||
'qrcode_folder': os.path.join(BASE_DIR, 'QRCodes'),
|
||||
'max_size_mb': 10,
|
||||
'image_max_size_mb': 15,
|
||||
'video_max_size_mb': 100,
|
||||
'allowed_extensions': ['png', 'jpg', 'jpeg', 'gif']
|
||||
},
|
||||
'paths': {
|
||||
'backups': os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'backups'),
|
||||
'logs': os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'logs'),
|
||||
'deleted_archives': os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'deleted_archives'),
|
||||
},
|
||||
'school': {
|
||||
'name': 'Schulname',
|
||||
'address': 'Schulstraße 1',
|
||||
'postal_code': '00000',
|
||||
'city': 'Ort',
|
||||
'school_number': '000000',
|
||||
'it_admin': 'IT-Beauftragte oder IT-Beauftragter',
|
||||
'logo_path': '',
|
||||
'logo_thumb': '',
|
||||
'logo_thumb': '',
|
||||
},
|
||||
'schoolPeriods': {
|
||||
"1": {"start": "08:00", "end": "08:45", "label": "1. Stunde (08:00 - 08:45)"},
|
||||
"2": {"start": "08:45", "end": "09:30", "label": "2. Stunde (08:45 - 09:30)"},
|
||||
"3": {"start": "09:45", "end": "10:30", "label": "3. Stunde (09:45 - 10:30)"},
|
||||
"4": {"start": "10:30", "end": "11:15", "label": "4. Stunde (10:30 - 11:15)"},
|
||||
"5": {"start": "11:30", "end": "12:15", "label": "5. Stunde (11:30 - 12:15)"},
|
||||
"6": {"start": "12:15", "end": "13:00", "label": "6. Stunde (12:15 - 13:00)"},
|
||||
"7": {"start": "13:30", "end": "14:15", "label": "7. Stunde (13:30 - 14:15)"},
|
||||
"8": {"start": "14:15", "end": "15:00", "label": "8. Stunde (14:15 - 15:00)"},
|
||||
"9": {"start": "15:15", "end": "16:00", "label": "9. Stunde (15:15 - 16:00)"},
|
||||
"10": {"start": "16:00", "end": "16:45", "label": "10. Stunde (16:00 - 16:45)"}
|
||||
},
|
||||
'modules': {
|
||||
'inventory': {
|
||||
'enabled': True
|
||||
},
|
||||
'library': {
|
||||
'enabled': False
|
||||
},
|
||||
'student_cards': {
|
||||
'enabled': False,
|
||||
'default_borrow_days': 14,
|
||||
'max_borrow_days': 365
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Load configuration file
|
||||
CONFIG_PATH = os.path.join(BASE_DIR, '..', 'config.json')
|
||||
try:
|
||||
with open(CONFIG_PATH, 'r') as f:
|
||||
_conf = json.load(f)
|
||||
except Exception:
|
||||
_conf = {}
|
||||
|
||||
# Helper to get nested values with defaults
|
||||
def _get(conf, path, default):
|
||||
cur = conf
|
||||
for p in path:
|
||||
if isinstance(cur, dict) and p in cur:
|
||||
cur = cur[p]
|
||||
else:
|
||||
return default
|
||||
return cur
|
||||
|
||||
|
||||
def _get_bool_env(name, default):
|
||||
value = os.getenv(name)
|
||||
if value is None:
|
||||
return default
|
||||
return value.strip().lower() in ('1', 'true', 'yes', 'on')
|
||||
|
||||
|
||||
def _get_int_env(name, default):
|
||||
value = os.getenv(name)
|
||||
if value is None or not value.strip():
|
||||
return int(default)
|
||||
try:
|
||||
return int(value)
|
||||
except ValueError:
|
||||
return int(default)
|
||||
|
||||
def get_version():
|
||||
with open(os.path.join(BASE_DIR, '..', '.docker-build.env'), 'r') as f:
|
||||
for l in f:
|
||||
if l.startswith('INVENTAR_APP_IMAGE='):
|
||||
return l.split(':', 1)[1].strip()
|
||||
|
||||
# Expose settings
|
||||
APP_VERSION = get_version()
|
||||
DEBUG = _get_bool_env('INVENTAR_DEBUG', _get(_conf, ['dbg'], DEFAULTS['debug']))
|
||||
SECRET_KEY = str(os.getenv('INVENTAR_SECRET_KEY', _get(_conf, ['key'], DEFAULTS['secret_key'])))
|
||||
HOST = _get(_conf, ['host'], DEFAULTS['host'])
|
||||
PORT = _get(_conf, ['port'], DEFAULTS['port'])
|
||||
|
||||
# Database
|
||||
MONGODB_HOST = _get(_conf, ['mongodb', 'host'], DEFAULTS['mongodb']['host'])
|
||||
MONGODB_PORT = _get(_conf, ['mongodb', 'port'], DEFAULTS['mongodb']['port'])
|
||||
MONGODB_DB = _get(_conf, ['mongodb', 'db'], DEFAULTS['mongodb']['db'])
|
||||
MONGODB_URI = _get(_conf, ['mongodb', 'uri'], '')
|
||||
|
||||
# Optional environment overrides for containerized/runtime deployments.
|
||||
MONGODB_HOST = os.getenv('INVENTAR_MONGODB_HOST', MONGODB_HOST)
|
||||
MONGODB_PORT = int(os.getenv('INVENTAR_MONGODB_PORT', str(MONGODB_PORT)))
|
||||
MONGODB_DB = os.getenv('INVENTAR_MONGODB_DB', MONGODB_DB)
|
||||
MONGODB_URI = os.getenv('INVENTAR_MONGODB_URI', os.getenv('MONGO_URI', MONGODB_URI))
|
||||
if isinstance(MONGODB_URI, str):
|
||||
MONGODB_URI = MONGODB_URI.strip() or None
|
||||
MONGODB_MAX_POOL_SIZE = _get_int_env('INVENTAR_MONGODB_MAX_POOL_SIZE', 20)
|
||||
MONGODB_MIN_POOL_SIZE = _get_int_env('INVENTAR_MONGODB_MIN_POOL_SIZE', 0)
|
||||
MONGODB_MAX_IDLE_TIME_MS = _get_int_env('INVENTAR_MONGODB_MAX_IDLE_TIME_MS', 300000)
|
||||
MONGODB_CONNECT_TIMEOUT_MS = _get_int_env('INVENTAR_MONGODB_CONNECT_TIMEOUT_MS', 5000)
|
||||
MONGODB_SERVER_SELECTION_TIMEOUT_MS = _get_int_env('INVENTAR_MONGODB_SERVER_SELECTION_TIMEOUT_MS', 5000)
|
||||
MONGODB_SOCKET_TIMEOUT_MS = _get_int_env('INVENTAR_MONGODB_SOCKET_TIMEOUT_MS', 30000)
|
||||
MONGODB_MAX_CONNECTING = _get_int_env('INVENTAR_MONGODB_MAX_CONNECTING', 2)
|
||||
|
||||
# Scheduler
|
||||
SCHEDULER_INTERVAL_MIN = _get(_conf, ['scheduler', 'interval_minutes'], DEFAULTS['scheduler']['interval_minutes'])
|
||||
BACKUP_INTERVAL_HOURS = _get(_conf, ['scheduler', 'backup_interval_hours'], DEFAULTS['scheduler']['backup_interval_hours'])
|
||||
SCHEDULER_ENABLED = _get(_conf, ['scheduler', 'enabled'], DEFAULTS['scheduler']['enabled'])
|
||||
|
||||
# SSL
|
||||
SSL_ENABLED = _get(_conf, ['ssl', 'enabled'], DEFAULTS['ssl']['enabled'])
|
||||
SSL_CERT = _get(_conf, ['ssl', 'cert'], DEFAULTS['ssl']['cert'])
|
||||
SSL_KEY = _get(_conf, ['ssl', 'key'], DEFAULTS['ssl']['key'])
|
||||
|
||||
# School periods
|
||||
SCHOOL_PERIODS = _get(_conf, ['schoolPeriods'], DEFAULTS['schoolPeriods'])
|
||||
SCHOOL_INFO_DEFAULT = _get(_conf, ['school'], DEFAULTS['school'])
|
||||
|
||||
# Optional feature modules
|
||||
TENANT_CONFIGS = _get(_conf, ['tenants'], {})
|
||||
|
||||
|
||||
class _TenantAwareBool:
|
||||
def __init__(self, module_name, default):
|
||||
self.module_name = module_name
|
||||
self.default = bool(default)
|
||||
|
||||
def resolve(self):
|
||||
try:
|
||||
from tenant import is_tenant_module_enabled
|
||||
return bool(is_tenant_module_enabled(self.module_name, default=self.default))
|
||||
except Exception:
|
||||
return self.default
|
||||
|
||||
def __bool__(self):
|
||||
return self.resolve()
|
||||
|
||||
def __int__(self):
|
||||
return int(self.resolve())
|
||||
|
||||
def __str__(self):
|
||||
return 'True' if self.resolve() else 'False'
|
||||
|
||||
def __repr__(self):
|
||||
return f"_TenantAwareBool(module_name={self.module_name!r}, value={self.resolve()!r})"
|
||||
|
||||
|
||||
from Web.modules.module_registry import registry as MODULES
|
||||
|
||||
INVENTORY_MODULE_ENABLED = _TenantAwareBool('inventory', _get(_conf, ['modules', 'inventory', 'enabled'], DEFAULTS['modules']['inventory']['enabled']))
|
||||
LIBRARY_MODULE_ENABLED = _TenantAwareBool('library', _get(_conf, ['modules', 'library', 'enabled'], DEFAULTS['modules']['library']['enabled']))
|
||||
STUDENT_CARDS_MODULE_ENABLED = _TenantAwareBool('student_cards', _get(_conf, ['modules', 'student_cards', 'enabled'], DEFAULTS['modules']['student_cards']['enabled']))
|
||||
|
||||
def _match_inventory(path):
|
||||
if not path: return False
|
||||
if path == '/' or path.startswith('/home'): return True
|
||||
return path.startswith(('/scanner', '/inventory', '/upload_admin', '/manage_filters', '/manage_locations', '/admin_borrowings', '/admin_damaged_items', '/admin/borrowings', '/admin/damaged_items', '/terminplan'))
|
||||
|
||||
def _match_library(path):
|
||||
if not path: return False
|
||||
return path.startswith(('/library', '/library_', '/student_cards'))
|
||||
|
||||
def _match_student_cards(path):
|
||||
if not path: return False
|
||||
return path.startswith(('/student_cards'))
|
||||
|
||||
# Register core modules into the pipeline
|
||||
MODULES.register('inventory', INVENTORY_MODULE_ENABLED, _match_inventory)
|
||||
MODULES.register('library', LIBRARY_MODULE_ENABLED, _match_library)
|
||||
MODULES.register('student_cards', STUDENT_CARDS_MODULE_ENABLED, _match_student_cards)
|
||||
|
||||
STUDENT_DEFAULT_BORROW_DAYS = int(_get(_conf, ['modules', 'student_cards', 'default_borrow_days'], DEFAULTS['modules']['student_cards']['default_borrow_days']))
|
||||
STUDENT_MAX_BORROW_DAYS = int(_get(_conf, ["modules", "student_cards", "max_borrow_days"], DEFAULTS["modules"]["student_cards"]["max_borrow_days"]))
|
||||
|
||||
# Upload/paths
|
||||
ALLOWED_EXTENSIONS = set(_get(_conf, ['allowed_extensions'], DEFAULTS['upload']['allowed_extensions']))
|
||||
UPLOAD_FOLDER = _get(_conf, ['upload', 'folder'], DEFAULTS['upload']['folder'])
|
||||
THUMBNAIL_FOLDER = _get(_conf, ['upload', 'thumbnail_folder'], DEFAULTS['upload']['thumbnail_folder'])
|
||||
PREVIEW_FOLDER = _get(_conf, ['upload', 'preview_folder'], DEFAULTS['upload']['preview_folder'])
|
||||
QR_CODE_FOLDER = _get(_conf, ['upload', 'qrcode_folder'], DEFAULTS['upload']['qrcode_folder'])
|
||||
|
||||
# Normalize to absolute paths to avoid cwd issues
|
||||
if not os.path.isabs(UPLOAD_FOLDER):
|
||||
UPLOAD_FOLDER = os.path.join(BASE_DIR, os.path.relpath(UPLOAD_FOLDER, BASE_DIR))
|
||||
if not os.path.isabs(THUMBNAIL_FOLDER):
|
||||
THUMBNAIL_FOLDER = os.path.join(BASE_DIR, os.path.relpath(THUMBNAIL_FOLDER, BASE_DIR))
|
||||
if not os.path.isabs(PREVIEW_FOLDER):
|
||||
PREVIEW_FOLDER = os.path.join(BASE_DIR, os.path.relpath(PREVIEW_FOLDER, BASE_DIR))
|
||||
if not os.path.isabs(QR_CODE_FOLDER):
|
||||
QR_CODE_FOLDER = os.path.join(BASE_DIR, os.path.relpath(QR_CODE_FOLDER, BASE_DIR))
|
||||
MAX_UPLOAD_MB = _get(_conf, ['upload', 'max_size_mb'], DEFAULTS['upload']['max_size_mb'])
|
||||
IMAGE_MAX_UPLOAD_MB = _get(_conf, ['upload', 'image_max_size_mb'], DEFAULTS['upload']['image_max_size_mb'])
|
||||
VIDEO_MAX_UPLOAD_MB = _get(_conf, ['upload', 'video_max_size_mb'], DEFAULTS['upload']['video_max_size_mb'])
|
||||
|
||||
THUMBNAIL_SIZE_LIST = _get(_conf, ['images', 'thumbnail_size'], DEFAULTS['images']['thumbnail_size'])
|
||||
PREVIEW_SIZE_LIST = _get(_conf, ['images', 'preview_size'], DEFAULTS['images']['preview_size'])
|
||||
THUMBNAIL_SIZE = (int(THUMBNAIL_SIZE_LIST[0]), int(THUMBNAIL_SIZE_LIST[1])) if isinstance(THUMBNAIL_SIZE_LIST, (list, tuple)) else (150, 150)
|
||||
PREVIEW_SIZE = (int(PREVIEW_SIZE_LIST[0]), int(PREVIEW_SIZE_LIST[1])) if isinstance(PREVIEW_SIZE_LIST, (list, tuple)) else (400, 400)
|
||||
|
||||
BACKUP_FOLDER = _get(_conf, ['paths', 'backups'], DEFAULTS['paths']['backups'])
|
||||
LOGS_FOLDER = _get(_conf, ['paths', 'logs'], DEFAULTS['paths']['logs'])
|
||||
DELETED_ARCHIVE_FOLDER = _get(_conf, ['paths', 'deleted_archives'], DEFAULTS['paths']['deleted_archives'])
|
||||
|
||||
# Optional environment overrides for writable storage mounts.
|
||||
BACKUP_FOLDER = os.getenv('INVENTAR_BACKUP_FOLDER', BACKUP_FOLDER)
|
||||
LOGS_FOLDER = os.getenv('INVENTAR_LOGS_FOLDER', LOGS_FOLDER)
|
||||
DELETED_ARCHIVE_FOLDER = os.getenv('INVENTAR_DELETED_ARCHIVE_FOLDER', DELETED_ARCHIVE_FOLDER)
|
||||
|
||||
# Normalize backup and logs paths to absolute paths (similar to upload folders) to avoid
|
||||
# permission issues caused by relative paths resolving to unintended working dirs.
|
||||
PROJECT_ROOT = os.path.dirname(os.path.dirname(BASE_DIR))
|
||||
if not os.path.isabs(BACKUP_FOLDER):
|
||||
BACKUP_FOLDER = os.path.join(PROJECT_ROOT, BACKUP_FOLDER)
|
||||
if not os.path.isabs(LOGS_FOLDER):
|
||||
LOGS_FOLDER = os.path.join(PROJECT_ROOT, LOGS_FOLDER)
|
||||
if not os.path.isabs(DELETED_ARCHIVE_FOLDER):
|
||||
DELETED_ARCHIVE_FOLDER = os.path.join(PROJECT_ROOT, DELETED_ARCHIVE_FOLDER)
|
||||
|
||||
# Optional key for field/file encryption at application level.
|
||||
DATA_ENCRYPTION_KEY = os.getenv('INVENTAR_DATA_ENCRYPTION_KEY', '').strip()
|
||||
|
||||
|
||||
_MONGO_CLIENT_CACHE = {}
|
||||
_MONGO_CLIENT_LOCK = Lock()
|
||||
|
||||
|
||||
class _MongoClientProxy:
|
||||
def __init__(self, client):
|
||||
self._client = client
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._client, name)
|
||||
|
||||
def __getitem__(self, name):
|
||||
if has_request_context():
|
||||
try:
|
||||
from tenant import get_tenant_context
|
||||
ctx = get_tenant_context()
|
||||
if ctx and ctx.db_name:
|
||||
if name == MONGODB_DB or name == MONGODB_DB.lower() or name == 'inventar_default':
|
||||
return self._client[ctx.db_name]
|
||||
except Exception:
|
||||
pass
|
||||
return self._client[name]
|
||||
|
||||
def get_database(self, name=None, *args, **kwargs):
|
||||
if has_request_context():
|
||||
try:
|
||||
from tenant import get_tenant_context
|
||||
ctx = get_tenant_context()
|
||||
if ctx and ctx.db_name:
|
||||
if name is None or name == MONGODB_DB or name == MONGODB_DB.lower() or name == 'inventar_default':
|
||||
return self._client.get_database(ctx.db_name, *args, **kwargs)
|
||||
except Exception:
|
||||
pass
|
||||
return self._client.get_database(name, *args, **kwargs)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
def close(self):
|
||||
return None
|
||||
|
||||
|
||||
def _close_cached_mongo_clients():
|
||||
with _MONGO_CLIENT_LOCK:
|
||||
clients = list(_MONGO_CLIENT_CACHE.values())
|
||||
_MONGO_CLIENT_CACHE.clear()
|
||||
|
||||
for proxy in clients:
|
||||
try:
|
||||
proxy._client.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
atexit.register(_close_cached_mongo_clients)
|
||||
|
||||
|
||||
def MongoClient(*args, **kwargs):
|
||||
"""Return a process-local MongoDB client configured from this settings module."""
|
||||
explicit_host = 'host' in kwargs
|
||||
explicit_port = 'port' in kwargs
|
||||
host = args[0] if len(args) >= 1 else kwargs.pop('host', MONGODB_HOST)
|
||||
port = args[1] if len(args) >= 2 else kwargs.pop('port', MONGODB_PORT)
|
||||
client_kwargs = {
|
||||
'maxPoolSize': MONGODB_MAX_POOL_SIZE,
|
||||
'minPoolSize': MONGODB_MIN_POOL_SIZE,
|
||||
'maxIdleTimeMS': MONGODB_MAX_IDLE_TIME_MS,
|
||||
'connectTimeoutMS': MONGODB_CONNECT_TIMEOUT_MS,
|
||||
'serverSelectionTimeoutMS': MONGODB_SERVER_SELECTION_TIMEOUT_MS,
|
||||
'socketTimeoutMS': MONGODB_SOCKET_TIMEOUT_MS,
|
||||
'maxConnecting': MONGODB_MAX_CONNECTING,
|
||||
'retryWrites': True,
|
||||
'retryReads': True,
|
||||
}
|
||||
client_kwargs.update(kwargs)
|
||||
|
||||
if MONGODB_URI and len(args) == 0 and not explicit_host and not explicit_port:
|
||||
mongo_args = (MONGODB_URI,)
|
||||
elif len(args) >= 2 and not explicit_host and not explicit_port:
|
||||
mongo_args = args
|
||||
else:
|
||||
mongo_args = (host, port)
|
||||
|
||||
cache_key = (
|
||||
mongo_args,
|
||||
tuple(sorted((key, repr(value)) for key, value in client_kwargs.items())),
|
||||
)
|
||||
|
||||
with _MONGO_CLIENT_LOCK:
|
||||
cached_client = _MONGO_CLIENT_CACHE.get(cache_key)
|
||||
if cached_client is not None:
|
||||
return cached_client
|
||||
|
||||
client = _PyMongoClient(*mongo_args, **client_kwargs)
|
||||
|
||||
cached_client = _MongoClientProxy(client)
|
||||
_MONGO_CLIENT_CACHE[cache_key] = cached_client
|
||||
return cached_client
|
||||
|
||||
|
||||
def get_school_info():
|
||||
"""Return the tenant-scoped school metadata used for PDFs and admin views."""
|
||||
school_info = dict(SCHOOL_INFO_DEFAULT)
|
||||
client = None
|
||||
try:
|
||||
client = MongoClient(MONGODB_HOST, MONGODB_PORT)
|
||||
db = client[MONGODB_DB]
|
||||
if 'settings' not in db.list_collection_names():
|
||||
return school_info
|
||||
|
||||
settings_collection = db['settings']
|
||||
settings_document = settings_collection.find_one({'setting_type': 'school_info'})
|
||||
if not settings_document:
|
||||
return school_info
|
||||
|
||||
configured_school = settings_document.get('school', {})
|
||||
if isinstance(configured_school, dict):
|
||||
for key, value in configured_school.items():
|
||||
if value is not None:
|
||||
school_info[key] = value
|
||||
return school_info
|
||||
except Exception:
|
||||
return school_info
|
||||
finally:
|
||||
if client:
|
||||
client.close()
|
||||
|
||||
|
||||
def update_school_info(school_info):
|
||||
"""Persist tenant-scoped school metadata into MongoDB and refresh the in-memory cache."""
|
||||
if not isinstance(school_info, dict):
|
||||
raise TypeError('school_info must be a dict')
|
||||
|
||||
updated_school = dict(SCHOOL_INFO_DEFAULT)
|
||||
for key in updated_school.keys():
|
||||
value = school_info.get(key, updated_school[key])
|
||||
if value is None:
|
||||
value = ''
|
||||
updated_school[key] = str(value).strip()
|
||||
|
||||
client = None
|
||||
try:
|
||||
client = MongoClient(MONGODB_HOST, MONGODB_PORT)
|
||||
db = client[MONGODB_DB]
|
||||
settings_collection = db['settings']
|
||||
settings_collection.update_one(
|
||||
{'setting_type': 'school_info'},
|
||||
{
|
||||
'$set': {
|
||||
'setting_type': 'school_info',
|
||||
'school': updated_school,
|
||||
}
|
||||
},
|
||||
upsert=True,
|
||||
)
|
||||
finally:
|
||||
if client:
|
||||
client.close()
|
||||
|
||||
return dict(updated_school)
|
||||
@@ -0,0 +1,32 @@
|
||||
from pymongo import MongoClient
|
||||
import Web.modules.database.settings as cfg
|
||||
|
||||
def get_filter_names():
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = client[cfg.MONGODB_DB]
|
||||
names = db.settings.find_one({'setting_type': 'filter_names'})
|
||||
client.close()
|
||||
if names:
|
||||
return names.get('names', {
|
||||
'1': 'Fach/Kategorie',
|
||||
'2': 'System/Bereich',
|
||||
'3': 'Typ/Art'
|
||||
})
|
||||
return {
|
||||
'1': 'Fach/Kategorie',
|
||||
'2': 'System/Bereich',
|
||||
'3': 'Typ/Art'
|
||||
}
|
||||
|
||||
def set_filter_name(filter_num, name):
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = client[cfg.MONGODB_DB]
|
||||
names = get_filter_names()
|
||||
names[str(filter_num)] = name
|
||||
db.settings.update_one(
|
||||
{'setting_type': 'filter_names'},
|
||||
{'$set': {'names': names}},
|
||||
upsert=True
|
||||
)
|
||||
client.close()
|
||||
return True
|
||||
Executable
+924
@@ -0,0 +1,924 @@
|
||||
"""
|
||||
Module for managing user accounts and authentication.
|
||||
Provides methods for creating, validating, and retrieving user information.
|
||||
"""
|
||||
'''
|
||||
Copyright 2025-2026 AIIrondev
|
||||
|
||||
Licensed under the Inventarsystem EULA (Endbenutzer-Lizenzvertrag).
|
||||
See Legal/LICENSE for the full license text.
|
||||
Unauthorized commercial use, SaaS hosting, or removal of branding is prohibited.
|
||||
For commercial licensing inquiries: https://github.com/AIIrondev
|
||||
'''
|
||||
import hashlib
|
||||
import copy
|
||||
import logging
|
||||
import re
|
||||
import secrets
|
||||
import string
|
||||
from bson.objectid import ObjectId
|
||||
import Web.modules.database.settings as cfg
|
||||
from Web.modules.database.settings import MongoClient
|
||||
|
||||
logger = logging.getLogger('app')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.propagate = True
|
||||
|
||||
|
||||
def normalize_student_card_id(card_id):
|
||||
"""Normalize student card IDs for reliable lookup."""
|
||||
if card_id is None:
|
||||
return ''
|
||||
return str(card_id).strip().upper()
|
||||
|
||||
|
||||
def _clean_name_fragment(value):
|
||||
cleaned = re.sub(r'[^A-Za-zÄÖÜäöüß]', '', str(value or '').strip())
|
||||
if not cleaned:
|
||||
return ''
|
||||
replacements = {
|
||||
'ä': 'ae',
|
||||
'ö': 'oe',
|
||||
'ü': 'ue',
|
||||
'ß': 'ss',
|
||||
'Ä': 'Ae',
|
||||
'Ö': 'Oe',
|
||||
'Ü': 'Ue',
|
||||
}
|
||||
for old_char, new_char in replacements.items():
|
||||
cleaned = cleaned.replace(old_char, new_char)
|
||||
return cleaned
|
||||
|
||||
|
||||
def _get_tenant_db(client):
|
||||
"""Return the current tenant database for the request, or fall back to default."""
|
||||
try:
|
||||
from tenant import get_tenant_db
|
||||
return get_tenant_db(client)
|
||||
except Exception:
|
||||
return client[cfg.MONGODB_DB]
|
||||
|
||||
|
||||
def build_name_synonym(first_name, last_name=''):
|
||||
"""Build a deterministic, non-personalized short alias from 2 letters each."""
|
||||
first = _clean_name_fragment(first_name)
|
||||
last = _clean_name_fragment(last_name)
|
||||
|
||||
if first and last:
|
||||
return (first[:2] + last[:2]).title()
|
||||
|
||||
combined = (first + last)
|
||||
if not combined:
|
||||
return 'User'
|
||||
return combined[:4].title()
|
||||
|
||||
|
||||
def build_username_from_name(first_name, last_name=''):
|
||||
"""
|
||||
Build a deterministic username abbreviation from first and last name.
|
||||
Uses 2 letters from each name and stores it lowercase.
|
||||
|
||||
Args:
|
||||
first_name (str): First name
|
||||
last_name (str): Last name (optional)
|
||||
|
||||
Returns:
|
||||
str: Generated username
|
||||
"""
|
||||
alias = build_name_synonym(first_name, last_name)
|
||||
return alias.lower()
|
||||
|
||||
|
||||
def build_unique_username_from_name(first_name, last_name=''):
|
||||
"""
|
||||
Build a unique username from the first 2 letters of the first name and
|
||||
the first 2 letters of the last name.
|
||||
"""
|
||||
first = _clean_name_fragment(first_name)
|
||||
last = _clean_name_fragment(last_name)
|
||||
base_username = (first[:2] + last[:2]).lower()
|
||||
|
||||
if not base_username:
|
||||
base_username = 'user'
|
||||
|
||||
if not get_user(base_username):
|
||||
return base_username
|
||||
|
||||
suffix = 2
|
||||
while get_user(f"{base_username}{suffix}"):
|
||||
suffix += 1
|
||||
return f"{base_username}{suffix}"
|
||||
|
||||
|
||||
ACTION_PERMISSION_KEYS = (
|
||||
'can_borrow',
|
||||
'can_insert',
|
||||
'can_edit',
|
||||
'can_delete',
|
||||
'can_manage_users',
|
||||
'can_manage_settings',
|
||||
'can_view_logs',
|
||||
)
|
||||
|
||||
DEFAULT_ACTION_PERMISSIONS = {
|
||||
'can_borrow': True,
|
||||
'can_insert': False,
|
||||
'can_edit': False,
|
||||
'can_delete': False,
|
||||
'can_manage_users': False,
|
||||
'can_manage_settings': False,
|
||||
'can_view_logs': False,
|
||||
}
|
||||
|
||||
DEFAULT_PAGE_PERMISSIONS = {
|
||||
'home': True,
|
||||
'tutorial_page': True,
|
||||
'my_borrowed_items': True,
|
||||
'notifications_view': True,
|
||||
'impressum': True,
|
||||
'license': True,
|
||||
'library_view': True,
|
||||
'terminplan': True,
|
||||
'home_admin': False,
|
||||
'upload_admin': False,
|
||||
'library_admin': False,
|
||||
'admin_borrowings': False,
|
||||
'library_loans_admin': False,
|
||||
'admin_damaged_items': False,
|
||||
'admin_audit_dashboard': False,
|
||||
'logs': False,
|
||||
'user_del': False,
|
||||
'register': False,
|
||||
'manage_filters': False,
|
||||
'manage_locations': False,
|
||||
}
|
||||
|
||||
PERMISSION_PRESETS = {
|
||||
'standard_user': {
|
||||
'label': 'Standard (Ausleihe)',
|
||||
'actions': {
|
||||
'can_borrow': True,
|
||||
},
|
||||
'pages': {
|
||||
'home': True,
|
||||
'tutorial_page': True,
|
||||
'my_borrowed_items': True,
|
||||
'notifications_view': True,
|
||||
'impressum': True,
|
||||
'license': True,
|
||||
'library_view': True,
|
||||
'terminplan': True,
|
||||
},
|
||||
},
|
||||
'editor': {
|
||||
'label': 'Editor (Einfügen/Bearbeiten)',
|
||||
'actions': {
|
||||
'can_borrow': True,
|
||||
'can_insert': True,
|
||||
'can_edit': True,
|
||||
},
|
||||
'pages': {
|
||||
'home': True,
|
||||
'tutorial_page': True,
|
||||
'my_borrowed_items': True,
|
||||
'notifications_view': True,
|
||||
'impressum': True,
|
||||
'license': True,
|
||||
'library_view': True,
|
||||
'terminplan': True,
|
||||
'upload_admin': True,
|
||||
'library_admin': True,
|
||||
},
|
||||
},
|
||||
'manager': {
|
||||
'label': 'Manager (inkl. Löschen)',
|
||||
'actions': {
|
||||
'can_borrow': True,
|
||||
'can_insert': True,
|
||||
'can_edit': True,
|
||||
'can_delete': True,
|
||||
'can_manage_settings': True,
|
||||
'can_view_logs': True,
|
||||
},
|
||||
'pages': {
|
||||
'home': True,
|
||||
'tutorial_page': True,
|
||||
'my_borrowed_items': True,
|
||||
'notifications_view': True,
|
||||
'impressum': True,
|
||||
'license': True,
|
||||
'library_view': True,
|
||||
'terminplan': True,
|
||||
'home_admin': True,
|
||||
'upload_admin': True,
|
||||
'library_admin': True,
|
||||
'admin_borrowings': True,
|
||||
'library_loans_admin': True,
|
||||
'admin_damaged_items': True,
|
||||
'admin_audit_dashboard': True,
|
||||
'logs': True,
|
||||
'manage_filters': True,
|
||||
'manage_locations': True,
|
||||
},
|
||||
},
|
||||
'full_access': {
|
||||
'label': 'Vollzugriff',
|
||||
'actions': {
|
||||
'can_borrow': True,
|
||||
'can_insert': True,
|
||||
'can_edit': True,
|
||||
'can_delete': True,
|
||||
'can_manage_users': True,
|
||||
'can_manage_settings': True,
|
||||
'can_view_logs': True,
|
||||
},
|
||||
'pages': {
|
||||
'home': True,
|
||||
'tutorial_page': True,
|
||||
'my_borrowed_items': True,
|
||||
'notifications_view': True,
|
||||
'impressum': True,
|
||||
'license': True,
|
||||
'library_view': True,
|
||||
'terminplan': True,
|
||||
'home_admin': True,
|
||||
'upload_admin': True,
|
||||
'library_admin': True,
|
||||
'admin_borrowings': True,
|
||||
'library_loans_admin': True,
|
||||
'admin_damaged_items': True,
|
||||
'admin_audit_dashboard': True,
|
||||
'logs': True,
|
||||
'user_del': True,
|
||||
'register': True,
|
||||
'manage_filters': True,
|
||||
'manage_locations': True,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _normalize_bool_map(source, defaults):
|
||||
result = dict(defaults)
|
||||
if isinstance(source, dict):
|
||||
for key, value in source.items():
|
||||
result[str(key)] = bool(value)
|
||||
return result
|
||||
|
||||
|
||||
def get_permission_preset_definitions():
|
||||
return copy.deepcopy(PERMISSION_PRESETS)
|
||||
|
||||
|
||||
def build_default_permission_payload(preset_key='standard_user'):
|
||||
selected_key = preset_key if preset_key in PERMISSION_PRESETS else 'standard_user'
|
||||
preset = PERMISSION_PRESETS.get(selected_key, {})
|
||||
action_defaults = _normalize_bool_map(preset.get('actions', {}), DEFAULT_ACTION_PERMISSIONS)
|
||||
page_defaults = _normalize_bool_map(preset.get('pages', {}), DEFAULT_PAGE_PERMISSIONS)
|
||||
return {
|
||||
'preset': selected_key,
|
||||
'actions': action_defaults,
|
||||
'pages': page_defaults,
|
||||
}
|
||||
|
||||
|
||||
def get_effective_permissions(username):
|
||||
user = get_user(username)
|
||||
if not user:
|
||||
return build_default_permission_payload('standard_user')
|
||||
|
||||
# Admin users always have full access, independent of custom presets.
|
||||
if bool(user.get('Admin', False)):
|
||||
return build_default_permission_payload('full_access')
|
||||
|
||||
preset_key = user.get('PermissionPreset') or 'standard_user'
|
||||
payload = build_default_permission_payload(preset_key)
|
||||
payload['actions'] = _normalize_bool_map(user.get('ActionPermissions', {}), payload['actions'])
|
||||
payload['pages'] = _normalize_bool_map(user.get('PagePermissions', {}), payload['pages'])
|
||||
return payload
|
||||
|
||||
|
||||
def update_user_permissions(username, preset_key, action_permissions=None, page_permissions=None):
|
||||
selected_key = preset_key if preset_key in PERMISSION_PRESETS else 'standard_user'
|
||||
payload = build_default_permission_payload(selected_key)
|
||||
|
||||
if isinstance(action_permissions, dict):
|
||||
for key, value in action_permissions.items():
|
||||
payload['actions'][str(key)] = bool(value)
|
||||
|
||||
if isinstance(page_permissions, dict):
|
||||
for key, value in page_permissions.items():
|
||||
payload['pages'][str(key)] = bool(value)
|
||||
|
||||
update_data = {
|
||||
'PermissionPreset': payload['preset'],
|
||||
'ActionPermissions': payload['actions'],
|
||||
'PagePermissions': payload['pages'],
|
||||
}
|
||||
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
result = users.update_one({'Username': username}, {'$set': update_data})
|
||||
|
||||
if result.matched_count == 0:
|
||||
result = users.update_one({'username': username}, {'$set': update_data})
|
||||
|
||||
client.close()
|
||||
return result.matched_count > 0
|
||||
|
||||
|
||||
# === FAVORITES MANAGEMENT ===
|
||||
def get_favorites(username):
|
||||
"""Return a list of favorite item ObjectId strings for the user."""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
user = users.find_one({'Username': username}) or users.find_one({'username': username})
|
||||
client.close()
|
||||
if not user:
|
||||
return []
|
||||
favs = user.get('favorites', [])
|
||||
# Normalize to strings
|
||||
return [str(f) for f in favs if f]
|
||||
|
||||
def add_favorite(username, item_id):
|
||||
"""Add an item to user's favorites (idempotent)."""
|
||||
try:
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
users.update_one(
|
||||
{'$or': [{'Username': username}, {'username': username}]},
|
||||
{'$addToSet': {'favorites': ObjectId(item_id)}}
|
||||
)
|
||||
client.close()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def remove_favorite(username, item_id):
|
||||
"""Remove an item from user's favorites."""
|
||||
try:
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
users.update_one(
|
||||
{'$or': [{'Username': username}, {'username': username}]},
|
||||
{'$pull': {'favorites': ObjectId(item_id)}}
|
||||
)
|
||||
client.close()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def check_password_strength(password):
|
||||
"""
|
||||
Check if a password meets minimum security requirements.
|
||||
|
||||
Args:
|
||||
password (str): Password to check
|
||||
|
||||
Returns:
|
||||
bool: True if password is strong enough, False otherwise
|
||||
"""
|
||||
if password is None:
|
||||
return False
|
||||
|
||||
if len(password) < 12:
|
||||
return False
|
||||
|
||||
has_lower = any(char.islower() for char in password)
|
||||
has_upper = any(char.isupper() for char in password)
|
||||
has_digit = any(char.isdigit() for char in password)
|
||||
has_symbol = any(not char.isalnum() for char in password)
|
||||
|
||||
if not (has_lower and has_upper and has_digit and has_symbol):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def hashing(password):
|
||||
"""
|
||||
Hash a password using SHA-512.
|
||||
|
||||
Args:
|
||||
password (str): Password to hash
|
||||
|
||||
Returns:
|
||||
str: Hexadecimal digest of the hashed password
|
||||
"""
|
||||
return hashlib.sha512(password.encode()).hexdigest()
|
||||
|
||||
|
||||
def check_nm_pwd(username, password):
|
||||
"""
|
||||
Verify username and password combination.
|
||||
|
||||
Args:
|
||||
username (str): Username to check
|
||||
password (str): Password to verify
|
||||
|
||||
Returns:
|
||||
dict: User document if credentials are valid, None otherwise
|
||||
"""
|
||||
db_name = cfg.MONGODB_DB
|
||||
tenant_db = None
|
||||
ctx = None
|
||||
try:
|
||||
from tenant import get_tenant_context
|
||||
ctx = get_tenant_context()
|
||||
if ctx and ctx.tenant_id:
|
||||
tenant_db = ctx.db_name or ctx.resolve_tenant()
|
||||
db_name = tenant_db
|
||||
except Exception as exc:
|
||||
logger.exception(f"Failed to resolve tenant context in check_nm_pwd: {exc}")
|
||||
|
||||
logger.info(
|
||||
"check_nm_pwd start: username=%r tenant=%r db=%r host=%r port=%r uri=%r",
|
||||
username,
|
||||
ctx.tenant_id if ctx else None,
|
||||
db_name,
|
||||
cfg.MONGODB_HOST,
|
||||
cfg.MONGODB_PORT,
|
||||
getattr(cfg, 'MONGODB_URI', None),
|
||||
)
|
||||
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
try:
|
||||
hashed_password = hashing(password)
|
||||
logger.info("check_nm_pwd password hash for username=%r: %s", username, hashed_password)
|
||||
available_dbs = []
|
||||
try:
|
||||
available_dbs = client.list_database_names()
|
||||
logger.debug("MongoDB connected. Available databases=%s", available_dbs)
|
||||
except Exception as exc:
|
||||
logger.exception("Unable to list MongoDB databases: %s", exc)
|
||||
|
||||
db = client[db_name]
|
||||
try:
|
||||
existing_collections = db.list_collection_names()
|
||||
except Exception as exc:
|
||||
logger.exception("Unable to list collections for db=%r: %s", db_name, exc)
|
||||
existing_collections = []
|
||||
logger.debug("Tenant db=%r collections=%s", db_name, existing_collections)
|
||||
|
||||
users = db['users']
|
||||
query = {'$or': [{'Username': username}, {'username': username}]}
|
||||
logger.debug("Running user lookup on %r: %s", db_name, query)
|
||||
user_record = users.find_one(query)
|
||||
|
||||
if user_record is None:
|
||||
logger.warning("No user document found in db=%r for username=%r", db_name, username)
|
||||
if db_name not in available_dbs:
|
||||
logger.warning("Tenant database %r is missing from available MongoDB databases", db_name)
|
||||
if 'users' not in existing_collections:
|
||||
logger.warning("Tenant database %r has no users collection", db_name)
|
||||
return None
|
||||
|
||||
logger.info("Found user document for username=%r in db=%r: %s", username, db_name, user_record)
|
||||
stored_password = user_record.get('Password') or user_record.get('password')
|
||||
if stored_password is None:
|
||||
logger.warning("User document for username=%r in db=%r has no password field", username, db_name)
|
||||
return None
|
||||
|
||||
if stored_password != hashed_password:
|
||||
logger.warning(
|
||||
"Password mismatch for username=%r in db=%r: provided_hash=%s stored_hash=%s",
|
||||
username,
|
||||
db_name,
|
||||
hashed_password,
|
||||
stored_password,
|
||||
)
|
||||
return None
|
||||
|
||||
return user_record
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
def add_user(
|
||||
username,
|
||||
password,
|
||||
name='',
|
||||
last_name='',
|
||||
is_student=False,
|
||||
student_card_id=None,
|
||||
max_borrow_days=None,
|
||||
permission_preset='standard_user',
|
||||
action_permissions=None,
|
||||
page_permissions=None,
|
||||
):
|
||||
"""
|
||||
Add a new user to the database.
|
||||
|
||||
Args:
|
||||
username (str): Username for the new user
|
||||
password (str): Password for the new user
|
||||
|
||||
Returns:
|
||||
bool: True if user was added successfully, False if password was too weak
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
if not check_password_strength(password):
|
||||
return False
|
||||
permission_defaults = build_default_permission_payload(permission_preset)
|
||||
if isinstance(action_permissions, dict):
|
||||
for key, value in action_permissions.items():
|
||||
permission_defaults['actions'][str(key)] = bool(value)
|
||||
if isinstance(page_permissions, dict):
|
||||
for key, value in page_permissions.items():
|
||||
permission_defaults['pages'][str(key)] = bool(value)
|
||||
|
||||
user_doc = {
|
||||
'Username': username,
|
||||
'Password': hashing(password),
|
||||
'Admin': False,
|
||||
'active_ausleihung': None,
|
||||
'name': name.strip() if name else '',
|
||||
'last_name': last_name.strip() if last_name else '',
|
||||
'IsStudent': bool(is_student),
|
||||
'PermissionPreset': permission_defaults['preset'],
|
||||
'ActionPermissions': permission_defaults['actions'],
|
||||
'PagePermissions': permission_defaults['pages'],
|
||||
}
|
||||
|
||||
normalized_card = normalize_student_card_id(student_card_id)
|
||||
if bool(is_student):
|
||||
if normalized_card:
|
||||
user_doc['StudentCardId'] = normalized_card
|
||||
if max_borrow_days is not None:
|
||||
try:
|
||||
user_doc['MaxBorrowDays'] = int(max_borrow_days)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
|
||||
users.insert_one(user_doc)
|
||||
client.close()
|
||||
return True
|
||||
|
||||
|
||||
def student_card_exists(student_card_id):
|
||||
"""Return True if a student card id is already assigned to a user."""
|
||||
normalized = normalize_student_card_id(student_card_id)
|
||||
if not normalized:
|
||||
return False
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
exists = users.find_one({'StudentCardId': normalized}) is not None
|
||||
client.close()
|
||||
return exists
|
||||
|
||||
|
||||
def get_user_by_student_card(student_card_id):
|
||||
"""Return user by student card id or None."""
|
||||
normalized = normalize_student_card_id(student_card_id)
|
||||
if not normalized:
|
||||
return None
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
found_user = users.find_one({'StudentCardId': normalized})
|
||||
client.close()
|
||||
return found_user
|
||||
|
||||
|
||||
def make_admin(username):
|
||||
"""
|
||||
Grant administrator privileges to a user.
|
||||
|
||||
Args:
|
||||
username (str): Username of the user to promote
|
||||
|
||||
Returns:
|
||||
bool: True if user was promoted successfully
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
result = users.update_one({'Username': username}, {'$set': {'Admin': True}})
|
||||
if result.matched_count == 0:
|
||||
result = users.update_one({'username': username}, {'$set': {'Admin': True}})
|
||||
client.close()
|
||||
return result.matched_count > 0
|
||||
|
||||
def remove_admin(username):
|
||||
"""
|
||||
Remove administrator privileges from a user.
|
||||
|
||||
Args:
|
||||
username (str): Username of the user to demote
|
||||
|
||||
Returns:
|
||||
bool: True if user was demoted successfully
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
result = users.update_one({'Username': username}, {'$set': {'Admin': False}})
|
||||
if result.matched_count == 0:
|
||||
result = users.update_one({'username': username}, {'$set': {'Admin': False}})
|
||||
client.close()
|
||||
return result.matched_count > 0
|
||||
|
||||
def get_user(username):
|
||||
"""
|
||||
Retrieve a specific user by username.
|
||||
|
||||
Args:
|
||||
username (str): Username to search for
|
||||
|
||||
Returns:
|
||||
dict: User document or None if not found
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
try:
|
||||
def find_in_db(database_name):
|
||||
db = client[database_name]
|
||||
users = db['users']
|
||||
return users.find_one({'Username': username}) or users.find_one({'username': username})
|
||||
|
||||
# Try current tenant first when available
|
||||
try:
|
||||
from tenant import get_tenant_context
|
||||
ctx = get_tenant_context()
|
||||
if ctx and ctx.db_name:
|
||||
user = find_in_db(ctx.db_name)
|
||||
if user:
|
||||
return user
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Fallback to default configured database
|
||||
user = find_in_db(cfg.MONGODB_DB)
|
||||
if user:
|
||||
return user
|
||||
|
||||
return None
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
def check_admin(username):
|
||||
"""
|
||||
Check if a user has administrator privileges.
|
||||
|
||||
Args:
|
||||
username (str): Username to check
|
||||
|
||||
Returns:
|
||||
bool: True if user is an administrator, False otherwise
|
||||
"""
|
||||
user = get_user(username)
|
||||
return bool(user and user.get('Admin', False))
|
||||
|
||||
|
||||
def update_active_ausleihung(username, id_item, ausleihung):
|
||||
"""
|
||||
Update a user's active borrowing record.
|
||||
|
||||
Args:
|
||||
username (str): Username of the user
|
||||
id_item (str): ID of the borrowed item
|
||||
ausleihung (str): ID of the borrowing record
|
||||
|
||||
Returns:
|
||||
bool: True if successful
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
users.update_one({'Username': username}, {'$set': {'active_ausleihung': {'Item': id_item, 'Ausleihung': ausleihung}}})
|
||||
client.close()
|
||||
return True
|
||||
|
||||
|
||||
def get_active_ausleihung(username):
|
||||
"""
|
||||
Get a user's active borrowing record.
|
||||
|
||||
Args:
|
||||
username (str): Username of the user
|
||||
|
||||
Returns:
|
||||
dict: Active borrowing information or None
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
user = users.find_one({'Username': username})
|
||||
return user['active_ausleihung']
|
||||
|
||||
|
||||
def has_active_borrowing(username):
|
||||
"""
|
||||
Check if a user currently has an active borrowing.
|
||||
|
||||
Args:
|
||||
username (str): Username to check
|
||||
|
||||
Returns:
|
||||
bool: True if user has an active borrowing, False otherwise
|
||||
"""
|
||||
try:
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
|
||||
user = users.find_one({'username': username})
|
||||
if not user:
|
||||
user = users.find_one({'Username': username})
|
||||
|
||||
if not user:
|
||||
client.close()
|
||||
return False
|
||||
|
||||
has_active = user.get('active_borrowing', False)
|
||||
|
||||
client.close()
|
||||
return has_active
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
|
||||
def delete_user(username):
|
||||
"""
|
||||
Delete a user from the database.
|
||||
Administrative function for removing user accounts.
|
||||
|
||||
Args:
|
||||
username (str): Username of the account to delete
|
||||
|
||||
Returns:
|
||||
bool: True if user was deleted successfully, False otherwise
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
result = users.delete_one({'username': username})
|
||||
client.close()
|
||||
if result.deleted_count == 0:
|
||||
# Try with different field name
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
result = users.delete_one({'Username': username})
|
||||
client.close()
|
||||
|
||||
return result.deleted_count > 0
|
||||
|
||||
|
||||
def update_active_borrowing(username, item_id, status):
|
||||
"""
|
||||
Update a user's active borrowing status.
|
||||
|
||||
Args:
|
||||
username (str): Username of the user
|
||||
item_id (str): ID of the borrowed item or None if returning
|
||||
status (bool): True if borrowing, False if returning
|
||||
|
||||
Returns:
|
||||
bool: True if successful, False on error
|
||||
"""
|
||||
try:
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
result = users.update_one(
|
||||
{'username': username},
|
||||
{'$set': {
|
||||
'active_borrowing': status,
|
||||
'borrowed_item': item_id if status else None
|
||||
}}
|
||||
)
|
||||
|
||||
if result.matched_count == 0:
|
||||
result = users.update_one(
|
||||
{'Username': username},
|
||||
{'$set': {
|
||||
'active_borrowing': status,
|
||||
'borrowed_item': item_id if status else None
|
||||
}}
|
||||
)
|
||||
|
||||
client.close()
|
||||
return result.modified_count > 0
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
|
||||
def get_name(username):
|
||||
"""
|
||||
Retrieve the name that is assosiated with the username.
|
||||
|
||||
Returns:
|
||||
str: String of name
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
user = users.find_one({'Username': username})
|
||||
name = user.get("name")
|
||||
return name
|
||||
|
||||
|
||||
def get_last_name(username):
|
||||
"""
|
||||
Retrieve the last_name that is assosiated with the username.
|
||||
|
||||
Returns:
|
||||
str: String of last_name
|
||||
"""
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
user = users.find_one({'Username': username})
|
||||
name = user.get("last_name")
|
||||
return name
|
||||
|
||||
|
||||
def get_all_users():
|
||||
"""
|
||||
Retrieve all users from the database.
|
||||
Administrative function for user management.
|
||||
|
||||
Returns:
|
||||
list: List of all user documents
|
||||
"""
|
||||
try:
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
all_users = list(users.find())
|
||||
client.close()
|
||||
return all_users
|
||||
except Exception as e:
|
||||
return []
|
||||
|
||||
def update_password(username, new_password):
|
||||
"""
|
||||
Update a user's password with a new one.
|
||||
|
||||
Args:
|
||||
username (str): Username of the user
|
||||
new_password (str): New password to set
|
||||
|
||||
Returns:
|
||||
bool: True if password was updated successfully, False otherwise
|
||||
"""
|
||||
try:
|
||||
if not check_password_strength(new_password):
|
||||
return False
|
||||
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
|
||||
# Hash the new password
|
||||
hashed_password = hashing(new_password)
|
||||
|
||||
# Update the user's password
|
||||
result = users.update_one(
|
||||
{'Username': username},
|
||||
{'$set': {'Password': hashed_password}}
|
||||
)
|
||||
|
||||
client.close()
|
||||
return result.modified_count > 0
|
||||
except Exception as e:
|
||||
print(f"Error updating password: {e}")
|
||||
return False
|
||||
|
||||
def update_user_name(username, name, last_name):
|
||||
"""
|
||||
Update a user's name and last name.
|
||||
|
||||
Args:
|
||||
username (str): Username of the user
|
||||
name (str): New first name
|
||||
last_name (str): New last name
|
||||
|
||||
Returns:
|
||||
bool: True if updated successfully, False otherwise
|
||||
"""
|
||||
try:
|
||||
name_alias = build_name_synonym(name, last_name)
|
||||
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||
db = _get_tenant_db(client)
|
||||
users = db['users']
|
||||
|
||||
result = users.update_one(
|
||||
{'Username': username},
|
||||
{'$set': {'name': name_alias, 'last_name': ''}}
|
||||
)
|
||||
|
||||
client.close()
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error updating user name: {e}")
|
||||
return False
|
||||
Reference in New Issue
Block a user