auth-yes/ui/db_queries.ts

138 lines
3.6 KiB
TypeScript

import { sql } from "../server/db.ts";
export async function getDashboardApps(userId: string, isAdmin: boolean) {
if (isAdmin) {
return await sql`
SELECT id, name, description, domain, 'Admin' as role
FROM apps
WHERE domain IS NOT NULL
ORDER BY name ASC
` as any[];
} else {
return await sql`
SELECT a.id, a.name, a.description, a.domain, g.role
FROM apps a
JOIN grants g ON a.id = g.app_id
WHERE g.user_id = ${userId} AND a.domain IS NOT NULL
ORDER BY a.name ASC
` as any[];
}
}
export async function getSessionApps() {
return await sql`
SELECT id, name, domain, spiffe_id
FROM apps
ORDER BY name ASC
`;
}
export async function getUserSessions(userId: string) {
return await sql`
SELECT id, label, is_agent, custom_scopes, last_activity_at, last_activity_action, created_at, expires_at
FROM sessions
WHERE user_id = ${userId} AND expires_at > NOW()
ORDER BY created_at DESC
`;
}
export async function getUserEventPasses(userId: string) {
return await sql`
SELECT id, slug, pin_code, name, max_seats, seats_claimed, is_active, expires_at
FROM event_passes
WHERE created_by = ${userId} AND is_active = TRUE
ORDER BY created_at DESC
`;
}
export async function getUserPasskeys(userId: string) {
return await sql`
SELECT id, credential_id, counter
FROM passkeys
WHERE user_id = ${userId}
`;
}
export async function getAdminUsers() {
return await sql`
SELECT id, username, display_name, account_status
FROM users
ORDER BY username ASC
`;
}
export async function getAdminApps() {
return await sql`
SELECT a.id, a.name, a.spiffe_id, a.description, a.created_at,
COUNT(g.id) AS active_grants_count
FROM apps a
LEFT JOIN grants g ON a.id = g.app_id
GROUP BY a.id, a.name, a.spiffe_id, a.description, a.created_at
ORDER BY a.created_at ASC
`;
}
export async function getAdminRoles() {
return await sql`
SELECT r.id, r.name, r.description, r.app_id, r.created_at,
a.name AS app_name
FROM roles r
LEFT JOIN apps a ON r.app_id = a.id
ORDER BY r.app_id NULLS FIRST, r.name ASC
`;
}
export async function getAdminInvites() {
return await sql`
SELECT i.id, i.code, i.role, i.max_uses, i.uses_count, i.auto_activate, i.expires_at, i.created_at, i.used_at,
a.name AS app_name, a.id AS app_id,
u.username AS used_by_username
FROM invites i
LEFT JOIN apps a ON i.app_id = a.id
LEFT JOIN users u ON i.used_by = u.id
ORDER BY i.created_at DESC
`;
}
export async function getAllRoles() {
return await sql`
SELECT id, name, description, app_id FROM roles ORDER BY name ASC
`;
}
export async function getAaguidAllowlist() {
return await sql`
SELECT id, aaguid, description, created_at
FROM aaguid_allowlist
ORDER BY created_at DESC
`;
}
export async function getAdminUserDetails(targetUserId: string) {
return await sql`
SELECT id, username, display_name, account_status
FROM users
WHERE id = ${targetUserId}
`.then((res) => res[0]);
}
export async function getUserGrants(targetUserId: string) {
return await sql`
SELECT g.id, g.app_id, g.role, g.created_at, a.name AS app_name, a.spiffe_id
FROM grants g
JOIN apps a ON g.app_id = a.id
WHERE g.user_id = ${targetUserId}
ORDER BY a.name ASC
`;
}
export async function getAdminAuditLogs() {
return await sql`
SELECT a.id, a.action, a.resource, a.details, a.ip_address, a.created_at, u.username as user
FROM audit_records a
LEFT JOIN users u ON a.user_id = u.id
ORDER BY a.created_at DESC
LIMIT 100
`;
}