A sweep of the URL map found 165 GET routes that answered anybody, with no token at all: the whole asset inventory, every machine, PC, printer and network device, the USB checkout history and who is holding which drive, the staff directory with its photographs, every core report. Guest access had been argued about one feature-flag at a time while the front door stood open. Each of those now carries the read permission it belongs to. That also makes the guest role mean something: a site can open exactly the reads it chooses, and nothing follows by accident. Thirty-three endpoints stay public, and each is named with its reason rather than left to be inferred: the app shell and its bootstrap, the settings allowlist (the handler already filters an anonymous caller down to branding and a few keys), images fetched by <img src> which carries no Authorization header, what a display kiosk renders before anybody signs in, the parts and USB kiosks, the client scripts that run as SYSTEM before a user session exists, and a secure-send link whose unguessable token IS the authorisation. tests/test_core/test_public_read_surface.py is the standing gate, the companion to the mutating-route sweep that has guarded writes for months. A new public read fails CI until somebody adds it to that list in front of a reviewer. Four further reads need a login but no permission, because the handler decides from who you are - your own user record, your own API tokens, the role catalogue the grid renders - and the same test proves each still refuses an anonymous caller. Verified not vacuous by ungating one route and watching the sweep name it. The employees plugin gained employees.view; the directory is names, SSOs, photographs and reporting lines, and it had no permission of any kind. Around ninety tests asserted payload shapes against endpoints that answered anybody. They are about what the answer says, not about who may ask, so they are signed in now rather than the gate being loosened. The ones that assert an anonymous caller is refused were left exactly as they were.
76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
"""Plugin introspection + enable/disable API."""
|
|
|
|
from flask import Blueprint, current_app, request
|
|
from flask_jwt_extended import jwt_required
|
|
|
|
from shopdb.utils.responses import success_response, error_response, ErrorCodes
|
|
|
|
from shopdb.utils.authz import require_role
|
|
from shopdb.utils.authz import require_permission
|
|
|
|
plugins_bp = Blueprint('plugins', __name__)
|
|
|
|
|
|
@plugins_bp.route('', methods=['GET'])
|
|
@require_permission('settings.view')
|
|
def list_plugins():
|
|
"""List all discovered plugins (enabled or not) with their metadata and
|
|
the framework contract version."""
|
|
from shopdb import __contract_version__
|
|
pm = current_app.extensions.get('plugin_manager')
|
|
plugins = pm.discover_available() if pm else []
|
|
plugins.sort(key=lambda p: p['name'])
|
|
return success_response({
|
|
'contract_version': __contract_version__,
|
|
'count': len(plugins),
|
|
'plugins': plugins,
|
|
})
|
|
|
|
|
|
@plugins_bp.route('/enabled', methods=['GET'])
|
|
@jwt_required(optional=True)
|
|
def list_enabled_plugins():
|
|
"""Return just the names of enabled plugins as a flat array.
|
|
|
|
Cheap registry read (no DB). Exposed to anonymous callers on purpose:
|
|
the navigation endpoint already leaks the same enabled/disabled signal,
|
|
and the frontend needs it (including unauthenticated kiosk routes like
|
|
/tv) to gate plugin-owned routes. No metadata beyond the names.
|
|
"""
|
|
pm = current_app.extensions.get('plugin_manager')
|
|
names = pm.registry.get_enabled_plugins() if pm else []
|
|
return success_response(sorted(names))
|
|
|
|
|
|
@plugins_bp.route('/<name>', methods=['PUT'])
|
|
@jwt_required()
|
|
@require_role('admin')
|
|
def set_plugin_enabled(name: str):
|
|
"""Enable or disable a plugin. Takes effect on the next app restart for
|
|
route/navigation changes."""
|
|
data = request.get_json() or {}
|
|
if 'enabled' not in data:
|
|
return error_response(ErrorCodes.VALIDATION_ERROR, 'enabled is required')
|
|
|
|
pm = current_app.extensions.get('plugin_manager')
|
|
if not pm:
|
|
return error_response(ErrorCodes.INTERNAL_ERROR,
|
|
'Plugin manager unavailable', http_code=500)
|
|
|
|
want = bool(data['enabled'])
|
|
ok = pm.enable_plugin(name) if want else pm.disable_plugin(name)
|
|
if not ok:
|
|
# enable/disable refused (unknown plugin, or a dependency conflict)
|
|
return error_response(
|
|
ErrorCodes.CONFLICT,
|
|
f"Could not {'enable' if want else 'disable'} '{name}' "
|
|
f"(check it exists and dependencies allow it)",
|
|
http_code=409
|
|
)
|
|
|
|
return success_response(
|
|
{'name': name, 'enabled': want},
|
|
message=f"Plugin {'enabled' if want else 'disabled'} "
|
|
f"(restart to apply route changes)"
|
|
)
|