mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-10 20:02:26 +00:00
clients frontend demo first pass
This commit is contained in:
@@ -104,6 +104,12 @@ export async function createClient(
|
||||
return next(createHttpError(HttpCode.NOT_FOUND, "Site not found"));
|
||||
}
|
||||
|
||||
if (site.type !== "newt") {
|
||||
return next(
|
||||
createHttpError(HttpCode.BAD_REQUEST, "Site is not a newt site")
|
||||
);
|
||||
}
|
||||
|
||||
await db.transaction(async (trx) => {
|
||||
const adminRole = await trx
|
||||
.select()
|
||||
|
||||
@@ -3,10 +3,8 @@ import {
|
||||
clients,
|
||||
orgs,
|
||||
roleClients,
|
||||
roleSites,
|
||||
sites,
|
||||
userClients,
|
||||
userSites
|
||||
} from "@server/db/schema";
|
||||
import logger from "@server/logger";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
@@ -41,16 +39,17 @@ const listClientsSchema = z.object({
|
||||
function queryClients(orgId: string, accessibleClientIds: number[]) {
|
||||
return db
|
||||
.select({
|
||||
siteId: sites.siteId,
|
||||
niceId: sites.niceId,
|
||||
name: sites.name,
|
||||
pubKey: sites.pubKey,
|
||||
subnet: sites.subnet,
|
||||
megabytesIn: sites.megabytesIn,
|
||||
megabytesOut: sites.megabytesOut,
|
||||
clientId: clients.clientId,
|
||||
orgId: clients.orgId,
|
||||
siteId: clients.siteId,
|
||||
name: clients.name,
|
||||
pubKey: clients.pubKey,
|
||||
subnet: clients.subnet,
|
||||
megabytesIn: clients.megabytesIn,
|
||||
megabytesOut: clients.megabytesOut,
|
||||
orgName: orgs.name,
|
||||
type: sites.type,
|
||||
online: sites.online
|
||||
type: clients.type,
|
||||
online: clients.online
|
||||
})
|
||||
.from(clients)
|
||||
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
||||
@@ -115,22 +114,22 @@ export async function listClients(
|
||||
)
|
||||
.where(
|
||||
or(
|
||||
eq(userSites.userId, req.user!.userId),
|
||||
eq(roleSites.roleId, req.userOrgRoleId!)
|
||||
eq(userClients.userId, req.user!.userId),
|
||||
eq(roleClients.roleId, req.userOrgRoleId!)
|
||||
)
|
||||
);
|
||||
|
||||
const accessibleSiteIds = accessibleClients.map(
|
||||
const accessibleClientIds = accessibleClients.map(
|
||||
(site) => site.clientId
|
||||
);
|
||||
const baseQuery = queryClients(orgId, accessibleSiteIds);
|
||||
const baseQuery = queryClients(orgId, accessibleClientIds);
|
||||
|
||||
let countQuery = db
|
||||
.select({ count: count() })
|
||||
.from(sites)
|
||||
.where(
|
||||
and(
|
||||
inArray(sites.siteId, accessibleSiteIds),
|
||||
inArray(sites.siteId, accessibleClientIds),
|
||||
eq(sites.orgId, orgId)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -14,7 +14,7 @@ import { fromError } from "zod-validation-error";
|
||||
|
||||
const getSiteSchema = z
|
||||
.object({
|
||||
siteId: z.number().int().positive()
|
||||
siteId: z.string().transform(Number).pipe(z.number())
|
||||
})
|
||||
.strict();
|
||||
|
||||
@@ -26,8 +26,8 @@ export type PickClientDefaultsResponse = {
|
||||
listenPort: number;
|
||||
endpoint: string;
|
||||
subnet: string;
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
olmId: string;
|
||||
olmSecret: string;
|
||||
};
|
||||
|
||||
export async function pickClientDefaults(
|
||||
@@ -57,6 +57,15 @@ export async function pickClientDefaults(
|
||||
return next(createHttpError(HttpCode.NOT_FOUND, "Site not found"));
|
||||
}
|
||||
|
||||
if (site.type !== "newt") {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
"Site is not a newt site"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// make sure all the required fields are present
|
||||
|
||||
const sitesRequiredFields = z.object({
|
||||
@@ -109,7 +118,7 @@ export async function pickClientDefaults(
|
||||
);
|
||||
}
|
||||
|
||||
const clientId = generateId(15);
|
||||
const olmId = generateId(15);
|
||||
const secret = generateId(48);
|
||||
|
||||
return response<PickClientDefaultsResponse>(res, {
|
||||
@@ -121,8 +130,8 @@ export async function pickClientDefaults(
|
||||
listenPort: listenPort,
|
||||
endpoint: endpoint,
|
||||
subnet: newSubnet,
|
||||
clientId,
|
||||
clientSecret: secret
|
||||
olmId: olmId,
|
||||
olmSecret: secret
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
|
||||
@@ -35,7 +35,7 @@ const createSiteSchema = z
|
||||
subnet: z.string().optional(),
|
||||
newtId: z.string().optional(),
|
||||
secret: z.string().optional(),
|
||||
type: z.enum(["newt", "wireguard"])
|
||||
type: z.enum(["newt", "wireguard", "local"])
|
||||
})
|
||||
.strict();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user