Add system settings, audit logging, user management, and dark mode fixes

System Settings:
- Add SystemSettings.vue with Zabbix integration, SMTP/email config, SAML SSO settings
- Add Setting model with key-value storage and typed values
- Add settings API with caching

Audit Logging:
- Add AuditLog model tracking user, IP, action, entity changes
- Add comprehensive audit logging to all CRUD operations:
  - Machines, Computers, Equipment, Network devices, VLANs, Subnets
  - Printers, USB devices (including checkout/checkin)
  - Applications, Settings, Users/Roles
- Track old/new values for all field changes
- Mask sensitive values (passwords, tokens) in logs

User Management:
- Add UsersList.vue with full user CRUD
- Add Role management with granular permissions
- Add 41 predefined permissions across 10 categories
- Add users API with roles and permissions endpoints

Reports:
- Add TonerReport.vue for printer supply monitoring

Dark Mode Fixes:
- Fix map position section in PCForm, PrinterForm
- Fix alert-warning in KnowledgeBaseDetail
- All components now use CSS variables for theming

CLI Commands:
- Add flask seed permissions
- Add flask seed settings

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
cproudlock
2026-02-04 22:16:56 -05:00
parent 9efdb5f52d
commit e18c7c2d87
40 changed files with 4221 additions and 39 deletions

View File

@@ -1,9 +1,11 @@
"""Printers API routes - new Asset-based architecture."""
import logging
from flask import Blueprint, request
from flask_jwt_extended import jwt_required
from shopdb.extensions import db
from shopdb.extensions import db, cache
from shopdb.core.models import Asset, AssetType, Vendor, Model, Communication, CommunicationType
from shopdb.utils.responses import (
success_response,
@@ -16,6 +18,8 @@ from shopdb.utils.pagination import get_pagination_params, paginate_query
from ..models import Printer, PrinterType
from ..services import ZabbixService
logger = logging.getLogger(__name__)
printers_asset_bp = Blueprint('printers_asset', __name__)
@@ -421,8 +425,12 @@ def get_printer_supplies(printer_id: int):
return error_response(ErrorCodes.VALIDATION_ERROR, 'Printer has no IP address')
service = ZabbixService()
if not service.isconfigured:
return error_response(ErrorCodes.BAD_REQUEST, 'Zabbix not configured')
if not service.isconfigured or not service.isreachable:
# Return empty supplies if Zabbix not available (fail gracefully)
return success_response({
'ipaddress': comm.ipaddress,
'supplies': []
})
supplies = service.getsuppliesbyip(comm.ipaddress)
@@ -432,6 +440,117 @@ def get_printer_supplies(printer_id: int):
})
# =============================================================================
# Low Supplies
# =============================================================================
def _get_low_supplies_data():
"""Build low supplies data (cached for 10 minutes)."""
cached = cache.get('printers_low_supplies')
if cached is not None:
return cached
service = ZabbixService()
if not service.isconfigured or not service.isreachable:
return {'printers': [], 'summary': {'total_checked': 0, 'low': 0, 'critical': 0}}
# All active printers with an IP address
printers = (
db.session.query(Printer, Asset, Communication)
.join(Asset, Asset.assetid == Printer.assetid)
.join(Communication, Communication.assetid == Asset.assetid)
.filter(Asset.isactive == True)
.filter(Communication.ipaddress.isnot(None))
.filter(Communication.ipaddress != '')
.all()
)
# Dedupe by printer id (may have multiple comms)
seen = set()
unique_printers = []
for printer, asset, comm in printers:
if printer.printerid not in seen:
seen.add(printer.printerid)
unique_printers.append((printer, asset, comm))
results = []
total_checked = 0
for printer, asset, comm in unique_printers:
supplies = service.getsuppliesbyip_cached(comm.ipaddress)
if supplies is None:
continue
total_checked += 1
# Annotate each supply with status
annotated = []
has_low = False
for s in supplies:
level = s.get('level', 0)
if level <= 5:
status = 'critical'
has_low = True
elif level <= 10:
status = 'low'
has_low = True
else:
status = 'ok'
annotated.append({
'name': s.get('name', 'Unknown'),
'level': level,
'status': status
})
if has_low:
# Get location name
location_name = None
if asset.locationid:
from shopdb.core.models import Location
loc = Location.query.get(asset.locationid)
if loc:
location_name = loc.location
results.append({
'printerid': printer.printerid,
'printername': asset.name or printer.hostname or '',
'assetnumber': asset.assetnumber or '',
'ipaddress': comm.ipaddress,
'location': location_name,
'supplies': annotated
})
low_count = 0
critical_count = 0
for p in results:
has_critical = any(s['status'] == 'critical' for s in p['supplies'])
has_low = any(s['status'] == 'low' for s in p['supplies'])
if has_critical:
critical_count += 1
elif has_low:
low_count += 1
data = {
'printers': results,
'summary': {
'total_checked': total_checked,
'low': low_count,
'critical': critical_count
}
}
cache.set('printers_low_supplies', data, timeout=600)
return data
@printers_asset_bp.route('/lowsupplies', methods=['GET'])
@jwt_required(optional=True)
def low_supplies():
"""Get printers with low or critical supply levels."""
data = _get_low_supplies_data()
return success_response(data)
# =============================================================================
# Dashboard
# =============================================================================
@@ -465,12 +584,24 @@ def dashboard_summary():
).group_by(Vendor.vendor
).all()
# Get real low/critical supply counts (skip if Zabbix not reachable)
low_count = 0
critical_count = 0
service = ZabbixService()
if service.isconfigured and service.isreachable:
try:
supply_data = _get_low_supplies_data()
low_count = supply_data['summary']['low']
critical_count = supply_data['summary']['critical']
except Exception as e:
logger.warning(f"Could not fetch supply data for dashboard: {e}")
return success_response({
'total': total,
'totalprinters': total, # For dashboard compatibility
'online': total, # Placeholder - would need monitoring integration
'lowsupplies': 0, # Placeholder - would need Zabbix integration
'criticalsupplies': 0, # Placeholder - would need Zabbix integration
'totalprinters': total,
'online': total,
'lowsupplies': low_count,
'criticalsupplies': critical_count,
'bytype': [{'type': t, 'count': c} for t, c in by_type],
'byvendor': [{'vendor': v, 'count': c} for v, c in by_vendor],
})

View File

@@ -8,6 +8,7 @@ from shopdb.utils.responses import success_response, error_response, paginated_r
from shopdb.utils.pagination import get_pagination_params, paginate_query
from shopdb.core.models.machine import Machine, MachineType
from shopdb.core.models.communication import Communication, CommunicationType
from shopdb.core.models import AuditLog
from ..models import PrinterData
from ..services import ZabbixService
@@ -132,10 +133,21 @@ def update_printer_data(machine_id: int):
pd = PrinterData(machineid=machine_id)
db.session.add(pd)
# Track changes for audit log
changes = {}
for key in ['windowsname', 'sharename', 'iscsf', 'installpath', 'pin']:
if key in data:
old_val = getattr(pd, key, None)
new_val = data[key]
if old_val != new_val:
changes[key] = {'old': old_val, 'new': new_val}
setattr(pd, key, data[key])
# Audit log if there were changes
if changes:
AuditLog.log('updated', 'Printer', entityid=machine_id,
entityname=machine.machinenumber or machine.hostname, changes=changes)
db.session.commit()
return success_response({
@@ -176,14 +188,28 @@ def update_printer_communication(machine_id: int):
comm = Communication(machineid=machine_id, comtypeid=ip_comtype.comtypeid)
db.session.add(comm)
# Track changes for audit log
changes = {}
# Update fields
if 'ipaddress' in data:
if comm.ipaddress != data['ipaddress']:
changes['ipaddress'] = {'old': comm.ipaddress, 'new': data['ipaddress']}
comm.ipaddress = data['ipaddress']
if 'isprimary' in data:
if comm.isprimary != data['isprimary']:
changes['isprimary'] = {'old': comm.isprimary, 'new': data['isprimary']}
comm.isprimary = data['isprimary']
if 'macaddress' in data:
if comm.macaddress != data['macaddress']:
changes['macaddress'] = {'old': comm.macaddress, 'new': data['macaddress']}
comm.macaddress = data['macaddress']
# Audit log if there were changes
if changes:
AuditLog.log('updated', 'Printer', entityid=machine_id,
entityname=machine.machinenumber or machine.hostname, changes=changes)
db.session.commit()
return success_response({
@@ -211,8 +237,12 @@ def get_printer_supplies(machine_id: int):
return error_response(ErrorCodes.VALIDATION_ERROR, 'Printer has no IP address')
service = ZabbixService()
if not service.isconfigured:
return error_response(ErrorCodes.SERVICE_UNAVAILABLE, 'Zabbix not configured')
if not service.isconfigured or not service.isreachable:
# Return empty supplies if Zabbix not available (fail gracefully)
return success_response({
'ipaddress': primary_comm.ipaddress,
'supplies': []
})
supplies = service.getsuppliesbyip(primary_comm.ipaddress)