mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: reset user 2fa from admin panel (#1943)
This commit is contained in:
@ -0,0 +1,159 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
import { msg } from '@lingui/core/macro';
|
||||||
|
import { useLingui } from '@lingui/react';
|
||||||
|
import { Trans } from '@lingui/react/macro';
|
||||||
|
import type { User } from '@prisma/client';
|
||||||
|
import { useRevalidator } from 'react-router';
|
||||||
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
|
import { trpc } from '@documenso/trpc/react';
|
||||||
|
import { Alert, AlertDescription, AlertTitle } from '@documenso/ui/primitives/alert';
|
||||||
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from '@documenso/ui/primitives/dialog';
|
||||||
|
import { Input } from '@documenso/ui/primitives/input';
|
||||||
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
|
export type AdminUserResetTwoFactorDialogProps = {
|
||||||
|
className?: string;
|
||||||
|
user: User;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const AdminUserResetTwoFactorDialog = ({
|
||||||
|
className,
|
||||||
|
user,
|
||||||
|
}: AdminUserResetTwoFactorDialogProps) => {
|
||||||
|
const { _ } = useLingui();
|
||||||
|
const { toast } = useToast();
|
||||||
|
const { revalidate } = useRevalidator();
|
||||||
|
const [email, setEmail] = useState('');
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
|
const { mutateAsync: resetTwoFactor, isPending: isResettingTwoFactor } =
|
||||||
|
trpc.admin.user.resetTwoFactor.useMutation();
|
||||||
|
|
||||||
|
const onResetTwoFactor = async () => {
|
||||||
|
try {
|
||||||
|
await resetTwoFactor({
|
||||||
|
userId: user.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: _(msg`2FA Reset`),
|
||||||
|
description: _(msg`The user's two factor authentication has been reset successfully.`),
|
||||||
|
duration: 5000,
|
||||||
|
});
|
||||||
|
|
||||||
|
await revalidate();
|
||||||
|
setOpen(false);
|
||||||
|
} catch (err) {
|
||||||
|
const error = AppError.parseError(err);
|
||||||
|
|
||||||
|
const errorMessage = match(error.code)
|
||||||
|
.with(AppErrorCode.NOT_FOUND, () => msg`User not found.`)
|
||||||
|
.with(
|
||||||
|
AppErrorCode.UNAUTHORIZED,
|
||||||
|
() => msg`You are not authorized to reset two factor authentcation for this user.`,
|
||||||
|
)
|
||||||
|
.otherwise(
|
||||||
|
() => msg`An error occurred while resetting two factor authentication for the user.`,
|
||||||
|
);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: _(msg`Error`),
|
||||||
|
description: _(errorMessage),
|
||||||
|
variant: 'destructive',
|
||||||
|
duration: 7500,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleOpenChange = (newOpen: boolean) => {
|
||||||
|
setOpen(newOpen);
|
||||||
|
|
||||||
|
if (!newOpen) {
|
||||||
|
setEmail('');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={className}>
|
||||||
|
<Alert
|
||||||
|
className="flex flex-col items-center justify-between gap-4 p-6 md:flex-row"
|
||||||
|
variant="neutral"
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<AlertTitle>Reset Two Factor Authentication</AlertTitle>
|
||||||
|
<AlertDescription className="mr-2">
|
||||||
|
<Trans>
|
||||||
|
Reset the users two factor authentication. This action is irreversible and will
|
||||||
|
disable two factor authentication for the user.
|
||||||
|
</Trans>
|
||||||
|
</AlertDescription>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex-shrink-0">
|
||||||
|
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button variant="destructive">
|
||||||
|
<Trans>Reset 2FA</Trans>
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader className="space-y-4">
|
||||||
|
<DialogTitle>
|
||||||
|
<Trans>Reset Two Factor Authentication</Trans>
|
||||||
|
</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
|
||||||
|
<Alert variant="destructive">
|
||||||
|
<AlertDescription className="selection:bg-red-100">
|
||||||
|
<Trans>
|
||||||
|
This action is irreversible. Please ensure you have informed the user before
|
||||||
|
proceeding.
|
||||||
|
</Trans>
|
||||||
|
</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<DialogDescription>
|
||||||
|
<Trans>
|
||||||
|
To confirm, please enter the accounts email address <br />({user.email}).
|
||||||
|
</Trans>
|
||||||
|
</DialogDescription>
|
||||||
|
|
||||||
|
<Input
|
||||||
|
className="mt-2"
|
||||||
|
type="email"
|
||||||
|
value={email}
|
||||||
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<DialogFooter>
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
disabled={email !== user.email}
|
||||||
|
onClick={onResetTwoFactor}
|
||||||
|
loading={isResettingTwoFactor}
|
||||||
|
>
|
||||||
|
<Trans>Reset 2FA</Trans>
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
</div>
|
||||||
|
</Alert>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
@ -27,6 +27,7 @@ import { AdminOrganisationCreateDialog } from '~/components/dialogs/admin-organi
|
|||||||
import { AdminUserDeleteDialog } from '~/components/dialogs/admin-user-delete-dialog';
|
import { AdminUserDeleteDialog } from '~/components/dialogs/admin-user-delete-dialog';
|
||||||
import { AdminUserDisableDialog } from '~/components/dialogs/admin-user-disable-dialog';
|
import { AdminUserDisableDialog } from '~/components/dialogs/admin-user-disable-dialog';
|
||||||
import { AdminUserEnableDialog } from '~/components/dialogs/admin-user-enable-dialog';
|
import { AdminUserEnableDialog } from '~/components/dialogs/admin-user-enable-dialog';
|
||||||
|
import { AdminUserResetTwoFactorDialog } from '~/components/dialogs/admin-user-reset-two-factor-dialog';
|
||||||
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
|
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
|
||||||
import { AdminOrganisationsTable } from '~/components/tables/admin-organisations-table';
|
import { AdminOrganisationsTable } from '~/components/tables/admin-organisations-table';
|
||||||
|
|
||||||
@ -219,10 +220,11 @@ const AdminUserPage = ({ user }: { user: User }) => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="mt-16 flex flex-col items-center gap-4">
|
<div className="mt-16 flex flex-col gap-4">
|
||||||
{user && <AdminUserDeleteDialog user={user} />}
|
{user && user.twoFactorEnabled && <AdminUserResetTwoFactorDialog user={user} />}
|
||||||
{user && user.disabled && <AdminUserEnableDialog userToEnable={user} />}
|
{user && user.disabled && <AdminUserEnableDialog userToEnable={user} />}
|
||||||
{user && !user.disabled && <AdminUserDisableDialog userToDisable={user} />}
|
{user && !user.disabled && <AdminUserDisableDialog userToDisable={user} />}
|
||||||
|
{user && <AdminUserDeleteDialog user={user} />}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -0,0 +1,50 @@
|
|||||||
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
import { adminProcedure } from '../trpc';
|
||||||
|
import {
|
||||||
|
ZResetTwoFactorRequestSchema,
|
||||||
|
ZResetTwoFactorResponseSchema,
|
||||||
|
} from './reset-two-factor-authentication.types';
|
||||||
|
|
||||||
|
export const resetTwoFactorRoute = adminProcedure
|
||||||
|
.input(ZResetTwoFactorRequestSchema)
|
||||||
|
.output(ZResetTwoFactorResponseSchema)
|
||||||
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
const { userId } = input;
|
||||||
|
|
||||||
|
ctx.logger.info({
|
||||||
|
input: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return await resetTwoFactor({ userId });
|
||||||
|
});
|
||||||
|
|
||||||
|
export type ResetTwoFactorOptions = {
|
||||||
|
userId: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const resetTwoFactor = async ({ userId }: ResetTwoFactorOptions) => {
|
||||||
|
const user = await prisma.user.findFirst({
|
||||||
|
where: {
|
||||||
|
id: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, { message: 'User not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.user.update({
|
||||||
|
where: {
|
||||||
|
id: user.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
twoFactorEnabled: false,
|
||||||
|
twoFactorBackupCodes: null,
|
||||||
|
twoFactorSecret: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
export const ZResetTwoFactorRequestSchema = z.object({
|
||||||
|
userId: z.number(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ZResetTwoFactorResponseSchema = z.void();
|
||||||
|
|
||||||
|
export type TResetTwoFactorRequest = z.infer<typeof ZResetTwoFactorRequestSchema>;
|
||||||
|
export type TResetTwoFactorResponse = z.infer<typeof ZResetTwoFactorResponseSchema>;
|
||||||
@ -21,6 +21,7 @@ import { deleteSubscriptionClaimRoute } from './delete-subscription-claim';
|
|||||||
import { findAdminOrganisationsRoute } from './find-admin-organisations';
|
import { findAdminOrganisationsRoute } from './find-admin-organisations';
|
||||||
import { findSubscriptionClaimsRoute } from './find-subscription-claims';
|
import { findSubscriptionClaimsRoute } from './find-subscription-claims';
|
||||||
import { getAdminOrganisationRoute } from './get-admin-organisation';
|
import { getAdminOrganisationRoute } from './get-admin-organisation';
|
||||||
|
import { resetTwoFactorRoute } from './reset-two-factor-authentication';
|
||||||
import {
|
import {
|
||||||
ZAdminDeleteDocumentMutationSchema,
|
ZAdminDeleteDocumentMutationSchema,
|
||||||
ZAdminDeleteUserMutationSchema,
|
ZAdminDeleteUserMutationSchema,
|
||||||
@ -51,6 +52,9 @@ export const adminRouter = router({
|
|||||||
stripe: {
|
stripe: {
|
||||||
createCustomer: createStripeCustomerRoute,
|
createCustomer: createStripeCustomerRoute,
|
||||||
},
|
},
|
||||||
|
user: {
|
||||||
|
resetTwoFactor: resetTwoFactorRoute,
|
||||||
|
},
|
||||||
|
|
||||||
// Todo: migrate old routes
|
// Todo: migrate old routes
|
||||||
findDocuments: adminProcedure.input(ZAdminFindDocumentsQuerySchema).query(async ({ input }) => {
|
findDocuments: adminProcedure.input(ZAdminFindDocumentsQuerySchema).query(async ({ input }) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user