From 7a2df877b7e92e27abc2cb03bd2361991e67a5f5 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 16 Mar 2023 10:47:11 +0100 Subject: [PATCH 1/3] feat: Add allow registration field to admin settings --- .../locales/en-US/admin-global-settings.json | 4 +++ src/constants/admin-settings.ts | 1 + .../GlobalSettingsRoute/SettingsForm.tsx | 26 +++++++++++++++++++ src/server-types.ts | 1 + 4 files changed, 32 insertions(+) diff --git a/public/locales/en-US/admin-global-settings.json b/public/locales/en-US/admin-global-settings.json index 155606c..b2668fd 100644 --- a/public/locales/en-US/admin-global-settings.json +++ b/public/locales/en-US/admin-global-settings.json @@ -66,6 +66,10 @@ "maxAliasesPerUser": { "label": "Maximum aliases per user", "description": "The maximum number of aliases a user can create. 0 means unlimited. Existing aliases will not be affected." + }, + "allowRegistrations": { + "label": "Allow registrations", + "description": "If enabled, users will be able to register on your instance. This only affects new account registrations." } } } diff --git a/src/constants/admin-settings.ts b/src/constants/admin-settings.ts index 42c99cf..c98cc7c 100644 --- a/src/constants/admin-settings.ts +++ b/src/constants/admin-settings.ts @@ -14,4 +14,5 @@ export const DEFAULT_ADMIN_SETTINGS: AdminSettings = { allowStatistics: true, allowAliasDeletion: false, maxAliasesPerUser: 0, + allowRegistrations: true, } diff --git a/src/route-widgets/GlobalSettingsRoute/SettingsForm.tsx b/src/route-widgets/GlobalSettingsRoute/SettingsForm.tsx index 948867a..198e3da 100644 --- a/src/route-widgets/GlobalSettingsRoute/SettingsForm.tsx +++ b/src/route-widgets/GlobalSettingsRoute/SettingsForm.tsx @@ -79,6 +79,7 @@ export default function SettingsForm({settings, queryKey}: SettingsFormProps) { allowStatistics: yup.boolean().label(t("fields.allowStatistics.label")), allowAliasDeletion: yup.boolean().label(t("fields.allowAliasDeletion.label")), maxAliasesPerUser: yup.number().label(t("fields.maxAliasesPerUser.label")).min(0), + allowRegistrations: yup.boolean().label(t("fields.allowRegistrations.label")), } as Record) const {mutateAsync} = useMutation< @@ -520,6 +521,31 @@ export default function SettingsForm({settings, queryKey}: SettingsFormProps) { + + + + } + disabled={formik.isSubmitting} + label={t("fields.allowRegistrations.label")} + /> + + {(formik.touched.allowRegistrations && + formik.errors.allowRegistrations) || + t("fields.allowRegistrations.description")} + + + diff --git a/src/server-types.ts b/src/server-types.ts index 7f6d24e..c835f8f 100644 --- a/src/server-types.ts +++ b/src/server-types.ts @@ -241,6 +241,7 @@ export interface AdminSettings { allowStatistics: boolean allowAliasDeletion: boolean maxAliasesPerUser: number + allowRegistrations: boolean } export interface ServerCronReport { From 4049117a01f3bff4d36b5eaedb3697b001976042 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 16 Mar 2023 10:54:43 +0100 Subject: [PATCH 2/3] feat: Show RegistrationsDisabled on login page --- public/locales/en-US/signup.json | 5 +++ .../SignupRoute/RegistrationsDisabled.tsx | 40 +++++++++++++++++++ src/routes/SignupRoute.tsx | 5 +++ src/server-types.ts | 1 + 4 files changed, 51 insertions(+) create mode 100644 src/route-widgets/SignupRoute/RegistrationsDisabled.tsx diff --git a/public/locales/en-US/signup.json b/public/locales/en-US/signup.json index e7486e7..1c5a0d7 100644 --- a/public/locales/en-US/signup.json +++ b/public/locales/en-US/signup.json @@ -14,5 +14,10 @@ "continueActionLabel": "Yes, edit email" } } + }, + "registrationsDisabled": { + "title": "Registrations are disabled", + "description": "We are sorry, but registrations on this instance are disabled.", + "login": "Login" } } diff --git a/src/route-widgets/SignupRoute/RegistrationsDisabled.tsx b/src/route-widgets/SignupRoute/RegistrationsDisabled.tsx new file mode 100644 index 0000000..61c4446 --- /dev/null +++ b/src/route-widgets/SignupRoute/RegistrationsDisabled.tsx @@ -0,0 +1,40 @@ +import {ReactElement} from "react" +import {Link} from "react-router-dom" +import {useTranslation} from "react-i18next" + +import {Box, Button, Grid, Paper, Typography} from "@mui/material" + +export default function RegistrationsDisabled(): ReactElement { + const {t} = useTranslation("signup") + + return ( + + + + + + {t("registrationsDisabled.title")} + + + + + {t("registrationsDisabled.description")} + + + + + + + + + ) +} diff --git a/src/routes/SignupRoute.tsx b/src/routes/SignupRoute.tsx index 7297570..da4afc4 100644 --- a/src/routes/SignupRoute.tsx +++ b/src/routes/SignupRoute.tsx @@ -5,6 +5,7 @@ import {useLoaderData} from "react-router-dom" import {MultiStepForm} from "~/components" import {ServerSettings} from "~/server-types" import EmailForm from "~/route-widgets/SignupRoute/EmailForm" +import RegistrationsDisabled from "~/route-widgets/SignupRoute/RegistrationsDisabled" import YouGotMail from "~/route-widgets/SignupRoute/YouGotMail" export default function SignupRoute(): ReactElement { @@ -13,6 +14,10 @@ export default function SignupRoute(): ReactElement { const index = email ? 1 : 0 + if (!serverSettings.allowRegistrations) { + return + } + return ( Date: Thu, 16 Mar 2023 11:00:28 +0100 Subject: [PATCH 3/3] fix: refresh server settings in background --- src/apis/get-server-settings.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/apis/get-server-settings.ts b/src/apis/get-server-settings.ts index 0ad0778..f8a420a 100644 --- a/src/apis/get-server-settings.ts +++ b/src/apis/get-server-settings.ts @@ -1,16 +1,23 @@ import {ServerSettings} from "~/server-types" import {client} from "~/constants/axios-client" -export default async function getServerSettings(): Promise { - const savedData = sessionStorage.getItem("server-settings") - - if (savedData) { - return JSON.parse(savedData) - } - +const loadData = async () => { const {data} = await client.get(`${import.meta.env.VITE_SERVER_BASE_URL}/v1/server/settings`) sessionStorage.setItem("server-settings", JSON.stringify(data)) return data } + +export default function getServerSettings(): Promise { + const savedData = sessionStorage.getItem("server-settings") + + if (savedData) { + // Refresh data in background + loadData() + + return JSON.parse(savedData) + } + + return loadData() +}