-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
228 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { type AppLoadContext } from "@remix-run/cloudflare"; | ||
|
||
export const assertSession = async ( | ||
request: Request, | ||
context: AppLoadContext, | ||
) => { | ||
const session = await context.authenticator.isAuthenticated(request); | ||
if (session) { | ||
const user = await context.prisma.users.findUnique({ | ||
where: { id: session.userId }, | ||
select: { | ||
id: true, | ||
discordId: true, | ||
displayName: true, | ||
isAdmin: true, | ||
}, | ||
}); | ||
if (user) { | ||
return user; | ||
} | ||
} | ||
throw new Response("not found", { status: 404 }); | ||
}; | ||
|
||
export const assertAdminSession = async ( | ||
request: Request, | ||
context: AppLoadContext, | ||
) => { | ||
const user = await assertSession(request, context); | ||
const superAdminDiscordIds = | ||
context.cloudflare.env.SUPER_ADMIN_DISCORD_ID.split(","); | ||
const isSuperAdmin = superAdminDiscordIds.includes(user.discordId); | ||
if (isSuperAdmin || user.isAdmin) { | ||
return { ...user, isSuperAdmin }; | ||
} | ||
throw new Response("not found", { status: 404 }); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
DateField, | ||
List, | ||
Resource, | ||
TextField, | ||
DateInput, | ||
SimpleForm, | ||
TextInput, | ||
BooleanInput, | ||
BooleanField, | ||
} from "react-admin"; | ||
import { Datagrid, Edit } from "../components/override"; | ||
|
||
const UsersList = () => ( | ||
<List> | ||
<Datagrid rowClick="edit"> | ||
<TextField source="id" /> | ||
<TextField source="discordId" /> | ||
<TextField source="displayName" /> | ||
<BooleanField source="isAdmin" /> | ||
<DateField source="createdAt" /> | ||
<DateField source="updatedAt" /> | ||
</Datagrid> | ||
</List> | ||
); | ||
|
||
const UsersEdit = () => ( | ||
<Edit> | ||
<SimpleForm> | ||
<TextInput source="id" readOnly /> | ||
<TextInput source="discordId" /> | ||
<TextInput source="displayName" /> | ||
<BooleanInput source="isAdmin" /> | ||
<DateInput source="createdAt" readOnly /> | ||
<DateInput source="updatedAt" readOnly /> | ||
</SimpleForm> | ||
</Edit> | ||
); | ||
|
||
export const usersResource = ( | ||
<Resource name="users" list={UsersList} edit={UsersEdit} /> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import "./styles.css"; | ||
|
||
import { dataProvider } from "ra-data-simple-prisma"; | ||
import { Admin } from "react-admin"; | ||
import { assertAdminSession } from "../../lib/session.server"; | ||
import { type LoaderFunctionArgs } from "@remix-run/cloudflare"; | ||
import { usersResource } from "./resources/users"; | ||
|
||
export const loader = async ({ request, context }: LoaderFunctionArgs) => { | ||
await assertAdminSession(request, context); | ||
return new Response(); | ||
}; | ||
|
||
export default () => { | ||
return ( | ||
<Admin | ||
disableTelemetry | ||
basename="/admin" | ||
dataProvider={dataProvider("/admin/api")} | ||
> | ||
{usersResource} | ||
</Admin> | ||
); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
type ActionFunctionArgs, | ||
type LoaderFunctionArgs, | ||
json, | ||
} from "@remix-run/cloudflare"; | ||
import { type RaPayload, defaultHandler } from "ra-data-simple-prisma"; | ||
import { z } from "zod"; | ||
import { assertAdminSession } from "../lib/session.server"; | ||
import { P, match } from "ts-pattern"; | ||
|
||
const getMethods = [ | ||
"getList", | ||
"getOne", | ||
"getMany", | ||
"getManyReference", | ||
] as const; | ||
const createMethods = ["create"] as const; | ||
const updateMethods = ["update", "updateMany"] as const; | ||
const deleteMethods = ["delete", "deleteMany"] as const; | ||
|
||
const handlerSchema = z.object({ | ||
resource: z.string(), | ||
method: z.enum([ | ||
...getMethods, | ||
...createMethods, | ||
...updateMethods, | ||
...deleteMethods, | ||
]), | ||
params: z.any(), | ||
model: z.string().optional(), | ||
}); | ||
|
||
const handler = async ({ | ||
request, | ||
context, | ||
}: LoaderFunctionArgs | ActionFunctionArgs) => { | ||
const [user, payload] = await Promise.all([ | ||
assertAdminSession(request, context), | ||
request.json(), | ||
]); | ||
|
||
const parseResult = handlerSchema.safeParse(payload); | ||
if (!parseResult.success) { | ||
throw new Response(parseResult.error.message, { status: 400 }); | ||
} | ||
|
||
const data = parseResult.data; | ||
|
||
match({ data, user }).with( | ||
{ | ||
data: { resource: "users", method: P.not(P.union(...getMethods)) }, | ||
user: { isSuperAdmin: P.not(true) }, | ||
}, | ||
() => { | ||
throw new Response("unauthorized", { status: 401 }); | ||
}, | ||
); | ||
|
||
const result = await defaultHandler( | ||
parseResult.data as RaPayload, | ||
context.prisma, | ||
); | ||
return json(result); | ||
}; | ||
|
||
export const loader = handler; | ||
|
||
export const action = handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- CreateTable | ||
CREATE TABLE "UserRoles" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"userId" TEXT NOT NULL, | ||
"isAdmin" BOOLEAN NOT NULL DEFAULT false, | ||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" DATETIME NOT NULL, | ||
CONSTRAINT "UserRoles_userId_fkey" FOREIGN KEY ("userId") REFERENCES "Users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "UserRoles_userId_key" ON "UserRoles"("userId"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- DropIndex | ||
DROP INDEX "UserRoles_userId_key"; | ||
|
||
-- DropTable | ||
PRAGMA foreign_keys=off; | ||
DROP TABLE "UserRoles"; | ||
PRAGMA foreign_keys=on; | ||
|
||
-- RedefineTables | ||
PRAGMA defer_foreign_keys=ON; | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Users" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"discordId" TEXT NOT NULL, | ||
"displayName" TEXT NOT NULL, | ||
"isAdmin" BOOLEAN NOT NULL DEFAULT false, | ||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" DATETIME NOT NULL | ||
); | ||
INSERT INTO "new_Users" ("createdAt", "discordId", "displayName", "id", "updatedAt") SELECT "createdAt", "discordId", "displayName", "id", "updatedAt" FROM "Users"; | ||
DROP TABLE "Users"; | ||
ALTER TABLE "new_Users" RENAME TO "Users"; | ||
CREATE UNIQUE INDEX "Users_discordId_key" ON "Users"("discordId"); | ||
PRAGMA foreign_keys=ON; | ||
PRAGMA defer_foreign_keys=OFF; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters