forked from administration/panel
feat: add button to change a user's email
parent
4f6be05162
commit
1349951135
|
@ -7,6 +7,7 @@ import type { Account } from "@/lib/db";
|
||||||
import { User } from "revolt-api";
|
import { User } from "revolt-api";
|
||||||
import {
|
import {
|
||||||
cancelAccountDeletion,
|
cancelAccountDeletion,
|
||||||
|
changeAccountEmail,
|
||||||
disableAccount,
|
disableAccount,
|
||||||
queueAccountDeletion,
|
queueAccountDeletion,
|
||||||
restoreAccount,
|
restoreAccount,
|
||||||
|
@ -24,6 +25,8 @@ import {
|
||||||
AlertDialogTitle,
|
AlertDialogTitle,
|
||||||
AlertDialogTrigger,
|
AlertDialogTrigger,
|
||||||
} from "../../ui/alert-dialog";
|
} from "../../ui/alert-dialog";
|
||||||
|
import { AlertDialogDescription } from "@radix-ui/react-alert-dialog";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
dayjs.extend(relativeTime);
|
dayjs.extend(relativeTime);
|
||||||
|
|
||||||
export function AccountActions({
|
export function AccountActions({
|
||||||
|
@ -36,9 +39,54 @@ export function AccountActions({
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
const [accountDraft, setAccountDraft] = useState(account);
|
const [accountDraft, setAccountDraft] = useState(account);
|
||||||
|
const [emailDraft, setEmailDraft] = useState("");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger asChild>
|
||||||
|
<Button className="flex-1">
|
||||||
|
Change Email
|
||||||
|
</Button>
|
||||||
|
</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>
|
||||||
|
Update account email
|
||||||
|
</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
<Input
|
||||||
|
placeholder={account.email}
|
||||||
|
onChange={(e) => setEmailDraft(e.currentTarget.value)}
|
||||||
|
value={emailDraft}
|
||||||
|
/>
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
|
<AlertDialogAction
|
||||||
|
disabled={!/^.+@.+$/.test(emailDraft)}
|
||||||
|
onClick={async () => {
|
||||||
|
try {
|
||||||
|
await changeAccountEmail(account._id, emailDraft);
|
||||||
|
setEmailDraft("");
|
||||||
|
toast({ title: "Updated email" });
|
||||||
|
window.location.reload();
|
||||||
|
} catch (err) {
|
||||||
|
toast({
|
||||||
|
title: "Failed to execute action",
|
||||||
|
description: String(err),
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Change
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
|
||||||
<AlertDialog>
|
<AlertDialog>
|
||||||
<AlertDialogTrigger asChild>
|
<AlertDialogTrigger asChild>
|
||||||
<Button
|
<Button
|
||||||
|
|
|
@ -8,6 +8,7 @@ type Permission =
|
||||||
| `accounts${
|
| `accounts${
|
||||||
| ""
|
| ""
|
||||||
| `/fetch${"" | "/by-id"}`
|
| `/fetch${"" | "/by-id"}`
|
||||||
|
| "/update/email"
|
||||||
| "/disable"
|
| "/disable"
|
||||||
| "/restore"
|
| "/restore"
|
||||||
| `/deletion${"" | "/queue" | "/cancel"}`}`
|
| `/deletion${"" | "/queue" | "/cancel"}`}`
|
||||||
|
@ -104,6 +105,7 @@ const PermissionSets = {
|
||||||
"accounts/restore",
|
"accounts/restore",
|
||||||
"accounts/deletion/queue",
|
"accounts/deletion/queue",
|
||||||
"accounts/deletion/cancel",
|
"accounts/deletion/cancel",
|
||||||
|
"accounts/update/email",
|
||||||
] as Permission[],
|
] as Permission[],
|
||||||
|
|
||||||
// Moderate users
|
// Moderate users
|
||||||
|
|
|
@ -192,6 +192,25 @@ export async function disableAccount(userId: string) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function changeAccountEmail(userId: string, email: string) {
|
||||||
|
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";
|
||||||
|
await checkPermission("accounts/update/email", userId);
|
||||||
|
|
||||||
|
await mongo()
|
||||||
|
.db("revolt")
|
||||||
|
.collection<Account>("accounts")
|
||||||
|
.updateOne(
|
||||||
|
{ _id: userId },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
email: email,
|
||||||
|
email_normalised: email,
|
||||||
|
verification: { status: "Verified" },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export async function suspendUser(userId: string) {
|
export async function suspendUser(userId: string) {
|
||||||
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";
|
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue