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
47 lines
1.2 KiB
Elixir
47 lines
1.2 KiB
Elixir
defmodule ToweropsWeb.Admin.UserLive.Index do
|
|
@moduledoc """
|
|
Admin interface for viewing and managing users.
|
|
"""
|
|
use ToweropsWeb, :live_view
|
|
|
|
alias Towerops.Admin
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
users = Admin.list_all_users()
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "All Users")
|
|
|> assign(:users, users)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("impersonate", %{"id" => user_id}, socket) do
|
|
{:noreply, redirect(socket, to: ~p"/admin/impersonate/#{user_id}")}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("delete_user", %{"id" => user_id}, socket) do
|
|
superuser = socket.assigns.current_scope.superuser || socket.assigns.current_scope.user
|
|
ip = get_connect_info_ip(socket)
|
|
|
|
case Admin.delete_user(user_id, superuser.id, ip) do
|
|
{:ok, _} ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:info, "User deleted successfully")
|
|
|> assign(:users, Admin.list_all_users())}
|
|
|
|
{:error, _} ->
|
|
{:noreply, put_flash(socket, :error, "Failed to delete user")}
|
|
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
|