Implement Owner Panel features and maintenance mode handling
- Introduced the Owner Panel with new routes and UI components for managing guilds, user blacklists, feature flags, and bot presence. - Added maintenance mode functionality to prevent non-owner users from executing commands during maintenance periods. - Enhanced localization with new keys for Owner Panel features in both English and German. - Updated job processing to include a presence refresh job, ensuring the bot's status is regularly updated. - Improved environment configuration to support owner user IDs for access control.
This commit is contained in:
55
apps/webui/src/lib/owner-auth.ts
Normal file
55
apps/webui/src/lib/owner-auth.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import {
|
||||
ownerRoleAtLeast,
|
||||
OwnerRoleSchema,
|
||||
type OwnerRole
|
||||
} from '@nexumi/shared';
|
||||
import { ForbiddenError, UnauthorizedError, requireAuth, requireAuthOrRedirect } from './auth';
|
||||
import { env } from './env';
|
||||
import { prisma } from './prisma';
|
||||
import type { SessionPayload } from './session';
|
||||
|
||||
export type OwnerSession = SessionPayload & { ownerRole: OwnerRole };
|
||||
|
||||
function bootstrapOwnerIds(): string[] {
|
||||
return env.OWNER_USER_IDS ?? [];
|
||||
}
|
||||
|
||||
export async function resolveOwnerRole(userId: string): Promise<OwnerRole | null> {
|
||||
if (bootstrapOwnerIds().includes(userId)) {
|
||||
return 'OWNER';
|
||||
}
|
||||
const member = await prisma.ownerTeamMember.findUnique({ where: { userId } });
|
||||
if (!member) {
|
||||
return null;
|
||||
}
|
||||
const parsed = OwnerRoleSchema.safeParse(member.role);
|
||||
return parsed.success ? parsed.data : null;
|
||||
}
|
||||
|
||||
export async function isOwnerUser(userId: string): Promise<boolean> {
|
||||
return (await resolveOwnerRole(userId)) !== null;
|
||||
}
|
||||
|
||||
export async function requireOwner(minimum: OwnerRole = 'VIEWER'): Promise<OwnerSession> {
|
||||
const session = await requireAuth();
|
||||
const role = await resolveOwnerRole(session.user.id);
|
||||
if (!role) {
|
||||
throw new ForbiddenError('Owner panel access required');
|
||||
}
|
||||
if (!ownerRoleAtLeast(role, minimum)) {
|
||||
throw new ForbiddenError('Insufficient owner role');
|
||||
}
|
||||
return { ...session, ownerRole: role };
|
||||
}
|
||||
|
||||
export async function requireOwnerOrRedirect(minimum: OwnerRole = 'VIEWER'): Promise<OwnerSession> {
|
||||
const session = await requireAuthOrRedirect();
|
||||
const role = await resolveOwnerRole(session.user.id);
|
||||
if (!role || !ownerRoleAtLeast(role, minimum)) {
|
||||
redirect('/dashboard');
|
||||
}
|
||||
return { ...session, ownerRole: role };
|
||||
}
|
||||
|
||||
export { UnauthorizedError, ForbiddenError };
|
||||
Reference in New Issue
Block a user