Skip to content

Commit

Permalink
Default group (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
albinmedoc authored Dec 2, 2024
1 parent 9be08ee commit 5492817
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Config = {
security: {
passwordStrength: number;
};
defaultGroup?: string | null;
};

type Option = {
Expand Down
24 changes: 24 additions & 0 deletions src/lib/components/admin/SettingsForm/DefaultGroup.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script lang="ts">
import BaseSetting from "./BaseSetting.svelte";
type Group = {
id: string;
name: string;
};
interface Props {
groupId?: string | null;
groups: Group[];
}
let { groupId = $bindable(), groups = [] }: Props = $props();
</script>

<BaseSetting title="Default group">
<select id="defaultGroup" name="defaultGroup" class="select w-fit min-w-64" bind:value={groupId}>
<option value="">Select a group</option>
{#each groups as group}
<option value={group.id}>{group.name}</option>
{/each}
</select>
</BaseSetting>
21 changes: 19 additions & 2 deletions src/lib/components/admin/SettingsForm/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@
import { ProgressRadial } from "@skeletonlabs/skeleton";
import PublicSignup from "./PublicSignup.svelte";
import Suggestions from "./Suggestions.svelte";
import Smtp from "./SMTP.svelte";
import Claims from "./Claims.svelte";
import Security from "./Security.svelte";
import DefaultGroup from "./DefaultGroup.svelte";
import Smtp from "./SMTP.svelte";
type Group = {
id: string;
name: string;
};
interface Props {
config: Config;
groups: Group[];
hideActions?: boolean;
saved?: boolean;
sending?: boolean;
sent?: boolean;
}
let { config = $bindable(), hideActions = false, saved = false, sending = false, sent = false }: Props = $props();
let {
config = $bindable(),
groups = [],
hideActions = false,
saved = false,
sending = false,
sent = false
}: Props = $props();
let form = $derived($page.form);
</script>
Expand All @@ -34,6 +48,9 @@
<div class="col-span-1">
<Security bind:passwordStrength={config.security.passwordStrength} />
</div>
<div class="col-span-1">
<DefaultGroup {groups} bind:groupId={config.defaultGroup} />
</div>

<div class="col-span-1 md:col-span-2">
<Smtp
Expand Down
7 changes: 5 additions & 2 deletions src/lib/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum ConfigKey {
SMTP_FROM_NAME = "smtp.fromName",
CLAIMS_SHOW_NAME = "claims.showName",
LIST_MODE = "listMode",
SECURITY_PASSWORD_STRENGTH = "security.passwordStrength"
SECURITY_PASSWORD_STRENGTH = "security.passwordStrength",
DEFAULT_GROUP = "defaultGroup"
}

export const getConfig = async (groupId?: string, includeSensitive = false): Promise<Config> => {
Expand Down Expand Up @@ -79,7 +80,8 @@ export const getConfig = async (groupId?: string, includeSensitive = false): Pro
listMode: (configMap[ConfigKey.LIST_MODE] as ListMode) || "standard",
security: {
passwordStrength: Number(configMap[ConfigKey.SECURITY_PASSWORD_STRENGTH] || 2)
}
},
defaultGroup: configMap[ConfigKey.DEFAULT_GROUP]!
};

return config;
Expand Down Expand Up @@ -141,6 +143,7 @@ export const writeConfig = async (config: Partial<Config>, groupId = GLOBAL) =>
configMap[ConfigKey.CLAIMS_SHOW_NAME] = config?.claims?.showName.toString();
configMap[ConfigKey.LIST_MODE] = config?.listMode;
configMap[ConfigKey.SECURITY_PASSWORD_STRENGTH] = config?.security?.passwordStrength.toString();
configMap[ConfigKey.DEFAULT_GROUP] = config?.defaultGroup;

for (const [key, value] of Object.entries(configMap)) {
await client.systemConfig.upsert({
Expand Down
3 changes: 2 additions & 1 deletion src/lib/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export const settingSchema = z.object({
smtpFromName: z.string().optional(),
claimsShowName: z.coerce.boolean().default(false),
listMode: z.enum(["standard", "registry"]).default("standard"),
passwordStrength: z.coerce.number().min(-1).max(5).default(2)
passwordStrength: z.coerce.number().min(-1).max(5).default(2),
defaultGroup: z.string().optional()
});

export const publicListCreateSchema = z.object({
Expand Down
8 changes: 8 additions & 0 deletions src/routes/admin/groups/[groupId]/members/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
};
const deleteGroup = () => {
if (data.config.defaultGroup === $page.params.groupId) {
modalStore.trigger({
type: "alert",
title: "Cannot Delete Default Group",
body: "You cannot delete the default group. Please change the default group before deleting this group."
});
return;
}
modalStore.trigger({
type: "confirm",
title: "Delete Group",
Expand Down
14 changes: 12 additions & 2 deletions src/routes/admin/settings/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Role } from "$lib/schema";
import { client } from "$lib/server/prisma";
import { getConfig, writeConfig } from "$lib/server/config";
import { redirect, error, fail, type Actions } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
Expand All @@ -16,8 +17,16 @@ export const load: PageServerLoad = async ({ locals }) => {

const config = await getConfig(undefined, true);

const groups = await client.group.findMany({
select: {
id: true,
name: true
}
});

return {
config
config,
groups
};
};

Expand Down Expand Up @@ -82,7 +91,8 @@ const generateConfig = (configData: z.infer<typeof settingSchema>) => {
listMode: "standard",
security: {
passwordStrength: configData.passwordStrength
}
},
defaultGroup: configData.defaultGroup
};

return newConfig;
Expand Down
3 changes: 2 additions & 1 deletion src/routes/admin/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let { data }: Props = $props();
let config = $state(data.config);
let groups = $state(data.groups);
let sending = $state(false);
let saved = $state(false);
let sent = $state(false);
Expand All @@ -36,5 +37,5 @@
};
}}
>
<SettingsForm {config} {saved} {sending} {sent} />
<SettingsForm {config} {groups} {saved} {sending} {sent} />
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import { getContext } from "svelte";
import type { Writable } from "svelte/store";
import type { Props } from "./steps";
import type { Group } from "@prisma/client";
let { onSuccess }: Props = $props();
let config: Config = $state($page.data.config);
let groups: Group[] = $state($page.data.groups);
let form: HTMLFormElement | undefined = $state();
let sending = $state(false);
let saved = $state(false);
Expand Down Expand Up @@ -51,7 +53,7 @@
};
}}
>
<SettingsForm {config} hideActions {saved} {sending} {sent} />
<SettingsForm {config} {groups} hideActions {saved} {sending} {sent} />
</form>
</div>
</div>
3 changes: 3 additions & 0 deletions src/routes/signup/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const load: PageServerLoad = async ({ locals, request }) => {

export const actions: Actions = {
default: async ({ request, cookies }) => {
const config = await getConfig();
const formData = Object.fromEntries(await request.formData());
const signupSchema = await getSignupSchema();
const signupData = signupSchema.safeParse(formData);
Expand Down Expand Up @@ -79,6 +80,8 @@ export const actions: Actions = {
}
})
)?.id;
} else if (config.defaultGroup) {
groupId = config.defaultGroup;
}

try {
Expand Down

0 comments on commit 5492817

Please sign in to comment.