Skip to content

Commit

Permalink
Add super basic admin feature (#1213)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin authored May 29, 2024
1 parent 6ff704b commit ec61483
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface User extends Timestamps {
email?: string;
avatarUrl: string | undefined;
hfUserId: string;
isAdmin?: boolean;
}
1 change: 1 addition & 0 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
avatarUrl: locals.user.avatarUrl,
email: locals.user.email,
logoutDisabled: locals.user.logoutDisabled,
isAdmin: locals.user.isAdmin ?? false,
},
assistant,
enableAssistants,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import { env } from "$env/dynamic/private";
import { z } from "zod";
import type { Assistant } from "$lib/types/Assistant";
import { logger } from "$lib/server/logger";

async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) {
const assistant = await collections.assistants.findOne({ _id: new ObjectId(assistantId) });

if (!assistant) {
throw Error("Assistant not found");
}

if (assistant.createdById.toString() !== (locals.user?._id ?? locals.sessionId).toString()) {
if (
assistant.createdById.toString() !== (locals.user?._id ?? locals.sessionId).toString() &&
!locals.user?.isAdmin
) {
throw Error("You are not the author of this assistant");
}

Expand Down Expand Up @@ -166,4 +170,29 @@ export const actions: Actions = {

throw redirect(302, `${base}/settings`);
},

unfeature: async ({ params, locals }) => {
if (!locals.user?.isAdmin) {
return fail(403, { error: true, message: "Permission denied" });
}

const assistant = await collections.assistants.findOne({
_id: new ObjectId(params.assistantId),
});

if (!assistant) {
return fail(404, { error: true, message: "Assistant not found" });
}

const result = await collections.assistants.updateOne(
{ _id: assistant._id },
{ $set: { featured: false } }
);

if (result.modifiedCount === 0) {
return fail(500, { error: true, message: "Failed to unfeature assistant" });
}

return { from: "unfeature", ok: true, message: "Assistant unfeatured" };
},
};
14 changes: 14 additions & 0 deletions src/routes/settings/(nav)/assistants/[assistantId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@
>
{/if}
{/if}
{#if data?.user?.isAdmin}
<form method="POST" action="?/delete" use:enhance>
<button type="submit" class="flex items-center text-red-600 underline">
<CarbonTrash class="mr-1.5 inline text-xs" />Delete</button
>
</form>
{#if assistant?.featured}
<form method="POST" action="?/unfeature" use:enhance>
<button type="submit" class="flex items-center text-red-600 underline">
<CarbonTrash class="mr-1.5 inline text-xs" />Un-feature</button
>
</form>
{/if}
{/if}
</div>
</div>
</div>
Expand Down

0 comments on commit ec61483

Please sign in to comment.