Updates
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
|
||||
import expandClientRuleDestinations from "@/src/functions/backend/setup/expand-client-rule-destinations";
|
||||
import sanitizeClientRule from "@/src/functions/backend/setup/sanitize-client-rule";
|
||||
import validateClientRule from "@/src/functions/backend/setup/validate-client-rule";
|
||||
|
||||
type Params = {
|
||||
rules?: BUN_SQLITE_WGUI_CLIENT_RULES[];
|
||||
client_id?: number | "";
|
||||
user_id?: number | "";
|
||||
};
|
||||
|
||||
export default function prepareClientRules({
|
||||
rules,
|
||||
client_id,
|
||||
user_id,
|
||||
}: Params) {
|
||||
const prepared: BUN_SQLITE_WGUI_CLIENT_RULES[] = [];
|
||||
|
||||
for (let i = 0; i < (rules || []).length; i++) {
|
||||
const sanitized = sanitizeClientRule({
|
||||
rule: rules?.[i],
|
||||
client_id,
|
||||
user_id,
|
||||
});
|
||||
|
||||
if (!sanitized) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const expanded = expandClientRuleDestinations({ rule: sanitized });
|
||||
|
||||
for (let e = 0; e < expanded.length; e++) {
|
||||
const rule = expanded[e];
|
||||
|
||||
if (!rule) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const valid = validateClientRule({ rule });
|
||||
|
||||
if (!valid.success) {
|
||||
return {
|
||||
success: false as const,
|
||||
msg: valid.msg,
|
||||
rules: [] as BUN_SQLITE_WGUI_CLIENT_RULES[],
|
||||
};
|
||||
}
|
||||
|
||||
prepared.push(rule);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true as const,
|
||||
rules: prepared,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type { AdminCrudAPIParams, TableType } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { user, user_types, body, id } = params;
|
||||
|
||||
const can_delete_rule = checkUserAccess({
|
||||
user_types,
|
||||
});
|
||||
|
||||
const existing_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
TableType
|
||||
>({
|
||||
table: "client_rules",
|
||||
query: _.merge(body?.sql_query || {}, {}),
|
||||
targetId: id,
|
||||
});
|
||||
|
||||
const rules = existing_res.payload || [];
|
||||
|
||||
if (!rules[0]) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No client rule to delete`,
|
||||
};
|
||||
}
|
||||
|
||||
const client_ids = new Set<number>();
|
||||
|
||||
for (let i = 0; i < rules.length; i++) {
|
||||
const rule = rules[i];
|
||||
|
||||
if (!rule?.id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!can_delete_rule.success && rule.user_id != user.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Can't delete client rule`,
|
||||
};
|
||||
}
|
||||
|
||||
if (rule.client_id) {
|
||||
client_ids.add(Number(rule.client_id));
|
||||
}
|
||||
|
||||
const del_res = await BunSQLite.delete<
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
TableType
|
||||
>({
|
||||
table: "client_rules",
|
||||
targetId: rule.id,
|
||||
});
|
||||
|
||||
if (!del_res.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: del_res.msg || `Could not delete client rule`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: `${rules.length} client rule(s) deleted`,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type { AdminCrudAPIParams, TableType } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { table, user_types, body, id, query, user } = params;
|
||||
|
||||
let final_sql_query = _.merge(query?.sql_query, body?.sql_query) || {};
|
||||
|
||||
delete final_sql_query.join;
|
||||
|
||||
const can_user_see_all_rules = checkUserAccess({
|
||||
user_types,
|
||||
includes: ["admin"],
|
||||
});
|
||||
|
||||
if (!can_user_see_all_rules.success) {
|
||||
final_sql_query.query = {
|
||||
...final_sql_query.query,
|
||||
user_id: {
|
||||
value: user.id,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const GET = await BunSQLite.select<BUN_SQLITE_WGUI_CLIENT_RULES, TableType>({
|
||||
table,
|
||||
query: final_sql_query,
|
||||
targetId: id,
|
||||
});
|
||||
|
||||
return GET;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { AdminCrudAPIParams } from "@/src/types";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import get from "./get";
|
||||
import post from "./post";
|
||||
import put from "./put";
|
||||
import del from "./del";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { req } = params;
|
||||
|
||||
switch (req.method) {
|
||||
case "GET":
|
||||
return await get(params);
|
||||
case "POST":
|
||||
return await post(params);
|
||||
case "PUT":
|
||||
return await put(params);
|
||||
case "DELETE":
|
||||
return await del(params);
|
||||
|
||||
default:
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type { AdminCrudAPIParams } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import prepareClientRules from "./(functions)/prepare-client-rules";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { table, body, user_types, user } = params;
|
||||
|
||||
const is_user_allowed_to_post_any = checkUserAccess({
|
||||
user_types,
|
||||
});
|
||||
|
||||
const prepared = prepareClientRules({
|
||||
rules: body?.insert_data as BUN_SQLITE_WGUI_CLIENT_RULES[] | undefined,
|
||||
user_id: user.id,
|
||||
});
|
||||
|
||||
if (!prepared.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: prepared.msg,
|
||||
};
|
||||
}
|
||||
|
||||
let final_insert_data = prepared.rules.map((rule) => ({
|
||||
...rule,
|
||||
user_id: is_user_allowed_to_post_any.success
|
||||
? rule.user_id || user.id
|
||||
: user.id,
|
||||
}));
|
||||
|
||||
if (!final_insert_data[0]) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No client rules to insert`,
|
||||
};
|
||||
}
|
||||
|
||||
const POST = await BunSQLite.insert({
|
||||
table,
|
||||
data: final_insert_data,
|
||||
update_on_duplicate: body?.update_on_duplicate,
|
||||
});
|
||||
|
||||
return POST;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import sanitizeClientRule from "@/src/functions/backend/setup/sanitize-client-rule";
|
||||
import validateClientRule from "@/src/functions/backend/setup/validate-client-rule";
|
||||
import type { AdminCrudAPIParams, TableType } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { table, body, user_types, id, query, user } = params;
|
||||
|
||||
if (!body?.update_data) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No Update Data`,
|
||||
};
|
||||
}
|
||||
|
||||
const is_user_allowed_to_put = checkUserAccess({
|
||||
user_types,
|
||||
});
|
||||
|
||||
let final_sql_query = _.merge(query?.sql_query, body?.sql_query) || {};
|
||||
|
||||
if (!is_user_allowed_to_put.success) {
|
||||
final_sql_query = {};
|
||||
}
|
||||
|
||||
const existing_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
TableType
|
||||
>({
|
||||
table: "client_rules",
|
||||
targetId: id,
|
||||
});
|
||||
|
||||
const existing = existing_res.singleRes;
|
||||
|
||||
if (!existing?.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Client rule not found`,
|
||||
};
|
||||
}
|
||||
|
||||
if (!is_user_allowed_to_put.success && existing.user_id != user.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
const sanitized = sanitizeClientRule({
|
||||
rule: {
|
||||
...existing,
|
||||
...body.update_data,
|
||||
},
|
||||
client_id: existing.client_id,
|
||||
user_id: existing.user_id,
|
||||
});
|
||||
|
||||
if (!sanitized) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Invalid client rule`,
|
||||
};
|
||||
}
|
||||
|
||||
const valid = validateClientRule({ rule: sanitized });
|
||||
|
||||
if (!valid.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: valid.msg,
|
||||
};
|
||||
}
|
||||
|
||||
const PUT = await BunSQLite.update({
|
||||
table,
|
||||
data: sanitized,
|
||||
targetId: existing.id,
|
||||
query: final_sql_query,
|
||||
});
|
||||
|
||||
return PUT;
|
||||
}
|
||||
@@ -54,5 +54,9 @@ export default async function runClientSetup({
|
||||
host = host_res.singleRes || undefined;
|
||||
}
|
||||
|
||||
return await setupWireguardClient({ client, host, user });
|
||||
return await setupWireguardClient({
|
||||
client,
|
||||
host,
|
||||
user,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
|
||||
import expandClientRuleDestinations from "@/src/functions/backend/setup/expand-client-rule-destinations";
|
||||
import sanitizeClientRule from "@/src/functions/backend/setup/sanitize-client-rule";
|
||||
import validateClientRule from "@/src/functions/backend/setup/validate-client-rule";
|
||||
import type { TableType } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
|
||||
type Params = {
|
||||
client_id: number;
|
||||
user_id?: number | "";
|
||||
rules?: BUN_SQLITE_WGUI_CLIENT_RULES[];
|
||||
};
|
||||
|
||||
export default async function syncClientRules({
|
||||
client_id,
|
||||
user_id,
|
||||
rules,
|
||||
}: Params): Promise<APIResponseObject> {
|
||||
const existing_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
TableType
|
||||
>({
|
||||
table: "client_rules",
|
||||
query: {
|
||||
query: {
|
||||
client_id: {
|
||||
value: String(client_id),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing_res.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: existing_res.msg || `Could not load client rules`,
|
||||
};
|
||||
}
|
||||
|
||||
const incoming: BUN_SQLITE_WGUI_CLIENT_RULES[] = [];
|
||||
|
||||
for (let i = 0; i < (rules || []).length; i++) {
|
||||
const sanitized = sanitizeClientRule({
|
||||
rule: rules?.[i],
|
||||
client_id,
|
||||
user_id,
|
||||
});
|
||||
|
||||
if (!sanitized) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const expanded = expandClientRuleDestinations({ rule: sanitized });
|
||||
|
||||
for (let e = 0; e < expanded.length; e++) {
|
||||
const rule = expanded[e];
|
||||
|
||||
if (!rule) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const valid = validateClientRule({ rule });
|
||||
|
||||
if (!valid.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: valid.msg,
|
||||
};
|
||||
}
|
||||
|
||||
incoming.push(rule);
|
||||
}
|
||||
}
|
||||
|
||||
const incoming_ids = new Set(
|
||||
incoming
|
||||
.map((rule) => rule.id)
|
||||
.filter((id): id is number => Boolean(id) && typeof id == "number"),
|
||||
);
|
||||
|
||||
const existing = existing_res.payload || [];
|
||||
|
||||
for (let i = 0; i < existing.length; i++) {
|
||||
const rule = existing[i];
|
||||
|
||||
if (!rule?.id || incoming_ids.has(rule.id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const del_res = await BunSQLite.delete<
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
TableType
|
||||
>({
|
||||
table: "client_rules",
|
||||
targetId: rule.id,
|
||||
});
|
||||
|
||||
if (!del_res.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: del_res.msg || `Could not delete client rule`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const to_insert = incoming.filter((rule) => !rule.id);
|
||||
const to_update = incoming.filter(
|
||||
(rule) => rule.id && typeof rule.id == "number",
|
||||
);
|
||||
|
||||
for (let i = 0; i < to_update.length; i++) {
|
||||
const rule = to_update[i];
|
||||
|
||||
if (!rule?.id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const update_res = await BunSQLite.update<
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
TableType
|
||||
>({
|
||||
table: "client_rules",
|
||||
data: rule,
|
||||
targetId: rule.id,
|
||||
});
|
||||
|
||||
if (!update_res.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: update_res.msg || `Could not update client rule`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (to_insert[0]) {
|
||||
const insert_res = await BunSQLite.insert<
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
TableType
|
||||
>({
|
||||
table: "client_rules",
|
||||
data: to_insert,
|
||||
});
|
||||
|
||||
if (!insert_res.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: insert_res.msg || `Could not insert client rules`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import setupWireguardHost from "@/src/functions/backend/setup/setup-wireguard-ho
|
||||
import type {
|
||||
BUN_SQLITE_WGUI_ALL_TYPEDEFS,
|
||||
BUN_SQLITE_WGUI_CLIENTS,
|
||||
BUN_SQLITE_WGUI_CLIENT_RULES,
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
} from "@/db/types/db";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
@@ -76,6 +77,17 @@ export default async function (
|
||||
|
||||
affected_host_ids.add(host_id);
|
||||
|
||||
await BunSQLite.delete<BUN_SQLITE_WGUI_CLIENT_RULES, TableType>({
|
||||
table: "client_rules",
|
||||
query: {
|
||||
query: {
|
||||
client_id: {
|
||||
value: String(client.id),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await BunSQLite.delete<BUN_SQLITE_WGUI_CLIENTS, TableType>({
|
||||
table: "clients",
|
||||
targetId: client.id,
|
||||
|
||||
@@ -3,7 +3,6 @@ import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type { AdminCrudAPIParams } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import runClientSetup from "./(functions)/run-client-setup";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function (
|
||||
@@ -33,20 +32,5 @@ export default async function (
|
||||
update_on_duplicate: body?.update_on_duplicate,
|
||||
});
|
||||
|
||||
if (POST.success) {
|
||||
const client_setup_res = await runClientSetup({
|
||||
client_id: POST.postInsertReturn?.insertId,
|
||||
user,
|
||||
});
|
||||
|
||||
if (!client_setup_res.success) {
|
||||
POST.debug = {
|
||||
...(POST.debug || {}),
|
||||
client_setup: client_setup_res,
|
||||
};
|
||||
POST.error = client_setup_res.msg || client_setup_res.error;
|
||||
}
|
||||
}
|
||||
|
||||
return POST;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,16 +37,16 @@ export default async function (
|
||||
query: final_sql_query,
|
||||
});
|
||||
|
||||
if (PUT.success) {
|
||||
const client_setup_res = await runClientSetup({
|
||||
client_id: targetId,
|
||||
user,
|
||||
});
|
||||
// if (PUT.success) {
|
||||
// const client_setup_res = await runClientSetup({
|
||||
// client_id: targetId,
|
||||
// user,
|
||||
// });
|
||||
|
||||
if (!client_setup_res.success) {
|
||||
return client_setup_res;
|
||||
}
|
||||
}
|
||||
// if (!client_setup_res.success) {
|
||||
// return client_setup_res;
|
||||
// }
|
||||
// }
|
||||
|
||||
return PUT;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import type { AdminCrudAPIParams, ApiReqParams, TableType } from "@/src/types";
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import clients from "./(tables)/clients";
|
||||
import clientRules from "./(tables)/client_rules";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
params,
|
||||
@@ -61,6 +62,8 @@ export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
return await media(crud_params);
|
||||
case "clients":
|
||||
return await clients(crud_params);
|
||||
case "client_rules":
|
||||
return await clientRules(crud_params);
|
||||
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import type { ApiReqParams } from "@/src/types";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
import _ from "lodash";
|
||||
import runClientSetup from "./crud/(tables)/clients/(functions)/run-client-setup";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
params,
|
||||
) => {
|
||||
const req = params.req;
|
||||
const body = params.body as ApiReqParams;
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
logoutUser: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const is_super_admin = checkUserAccess({ user_types });
|
||||
|
||||
if (!is_super_admin.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
const client_id = body.client_id ? Number(body.client_id) : undefined;
|
||||
|
||||
if (!client_id) {
|
||||
throw new Error(`No Client ID found!`);
|
||||
}
|
||||
|
||||
const client_setup_res = await runClientSetup({
|
||||
client_id,
|
||||
user,
|
||||
});
|
||||
|
||||
if (!client_setup_res.success) {
|
||||
return client_setup_res;
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user