towerops/lib/towerops_web/plugs/require_superuser.ex
Graham McIntire e0bcd4feda
feat: add profile export/import workflow via API
Add export and import functionality for device SNMP profiles:

Export (local):
- New mix task: mix export_profiles --librenms-path ~/dev/librenms --output profiles.json
- Exports all profiles to JSON format
- Can filter specific profiles with --profiles flag

Import (production):
- New API endpoint: POST /api/v1/admin/profiles/import
- Requires superuser authentication via browser session
- Processes imports in background via Exq worker (maintenance queue)
- Returns job ID for tracking

New modules:
- Mix.Tasks.ExportProfiles - Export profiles to JSON
- ProfileImportWorker - Background job for importing profiles
- ProfilesController - API endpoint for bulk import
- RequireSuperuser plug - Restricts access to superusers
- Importer.import_profile_from_data/2 - Import from data structures

This enables bulk profile management without SSH access to production.
2026-01-18 09:23:38 -06:00

26 lines
601 B
Elixir

defmodule ToweropsWeb.Plugs.RequireSuperuser do
@moduledoc """
Plug that requires the current user to be a superuser.
Must be used after authentication plug that sets :current_user assign.
Returns 403 Forbidden if user is not a superuser.
"""
import Phoenix.Controller, only: [json: 2]
import Plug.Conn
def init(opts), do: opts
def call(conn, _opts) do
user = conn.assigns[:current_user]
if user && user.is_superuser do
conn
else
conn
|> put_status(:forbidden)
|> json(%{error: "Superuser access required"})
|> halt()
end
end
end