towerops/lib/towerops_web/live/admin/org_live/index.ex
Graham McIntire 853d548f82
Add superuser system with user impersonation for admin support
Implement comprehensive admin interface allowing designated superusers to view all users and organizations, impersonate users for debugging, and perform administrative operations. All superuser actions are tracked in audit logs for compliance.

Features:
- Superuser authentication with dedicated admin routes at /admin
- User impersonation with session state preservation
- Admin dashboard with system statistics
- User and organization management interfaces
- Comprehensive audit logging with IP tracking
- Visual impersonation banner with exit capability
- Security controls preventing self-impersonation and superuser-to-superuser impersonation

Database:
- Add is_superuser boolean field to users table
- Create audit_logs table for tracking sensitive operations
- Set graham@mcintire.me as initial superuser
2026-01-06 12:50:10 -06:00

42 lines
1.1 KiB
Elixir

defmodule ToweropsWeb.Admin.OrgLive.Index do
@moduledoc """
Admin interface for viewing and managing organizations.
"""
use ToweropsWeb, :live_view
alias Towerops.Admin
@impl true
def mount(_params, _session, socket) do
orgs = Admin.list_all_organizations()
{:ok,
socket
|> assign(:page_title, "All Organizations")
|> assign(:organizations, orgs)}
end
@impl true
def handle_event("delete_org", %{"id" => org_id}, socket) do
superuser = socket.assigns.current_scope.superuser || socket.assigns.current_scope.user
ip = get_connect_info_ip(socket)
case Admin.delete_organization(org_id, superuser.id, ip) do
{:ok, _} ->
{:noreply,
socket
|> put_flash(:info, "Organization deleted successfully")
|> assign(:organizations, Admin.list_all_organizations())}
{:error, _} ->
{:noreply, put_flash(socket, :error, "Failed to delete organization")}
end
end
defp get_connect_info_ip(socket) do
case get_connect_info(socket, :peer_data) do
%{address: address} -> to_string(:inet_parse.ntoa(address))
_ -> "unknown"
end
end
end