Enforce client ownership in clients CRUD put/post
Non-super-admin PUT updated the client whose id equalled the user's id; it now updates the requested client only if the user owns it. POST no longer lets non-super-admins overwrite existing clients by id. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -21,7 +21,7 @@ export default async function (
|
|||||||
|
|
||||||
if (!is_user_allowed_to_post_any.success) {
|
if (!is_user_allowed_to_post_any.success) {
|
||||||
final_insert_data = final_insert_data.map((fid) => ({
|
final_insert_data = final_insert_data.map((fid) => ({
|
||||||
...fid,
|
..._.omit(fid, ["id"]),
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,9 @@ export default async function (
|
|||||||
const POST = await BunSQLite.insert({
|
const POST = await BunSQLite.insert({
|
||||||
table,
|
table,
|
||||||
data: final_insert_data,
|
data: final_insert_data,
|
||||||
update_on_duplicate: body?.update_on_duplicate,
|
update_on_duplicate: is_user_allowed_to_post_any.success
|
||||||
|
? body?.update_on_duplicate
|
||||||
|
: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
return POST;
|
return POST;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
import type { BUN_SQLITE_WGUI_CLIENTS } from "@/db/types/db";
|
||||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||||
import type { AdminCrudAPIParams } from "@/src/types";
|
import type { AdminCrudAPIParams, TableType } from "@/src/types";
|
||||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||||
import runClientSetup from "./(functions)/run-client-setup";
|
import runClientSetup from "./(functions)/run-client-setup";
|
||||||
@@ -23,17 +24,41 @@ export default async function (
|
|||||||
});
|
});
|
||||||
|
|
||||||
let final_sql_query = _.merge(query?.sql_query, body?.sql_query) || {};
|
let final_sql_query = _.merge(query?.sql_query, body?.sql_query) || {};
|
||||||
let targetId = id;
|
let update_data: BUN_SQLITE_WGUI_CLIENTS = { ...body.update_data };
|
||||||
|
|
||||||
if (!is_user_allowed_to_put.success) {
|
if (!is_user_allowed_to_put.success) {
|
||||||
|
const existing_res = await BunSQLite.select<
|
||||||
|
BUN_SQLITE_WGUI_CLIENTS,
|
||||||
|
TableType
|
||||||
|
>({
|
||||||
|
table: "clients",
|
||||||
|
targetId: id,
|
||||||
|
});
|
||||||
|
|
||||||
|
const existing = existing_res.singleRes;
|
||||||
|
|
||||||
|
if (!existing?.id) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
msg: `Client not found`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existing.user_id != user.id) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
msg: `Unauthorized`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
final_sql_query = {};
|
final_sql_query = {};
|
||||||
targetId = user.id;
|
update_data = _.omit(update_data, ["id", "user_id"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const PUT = await BunSQLite.update({
|
const PUT = await BunSQLite.update({
|
||||||
table,
|
table,
|
||||||
data: body.update_data,
|
data: update_data,
|
||||||
targetId,
|
targetId: id,
|
||||||
query: final_sql_query,
|
query: final_sql_query,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user