Skip to content

Commit 663acba

Browse files
committed
fix: add extra check if record with provided PK already exists (for cases when it is allowed for adminusers to provide PK)
1 parent 7a2b434 commit 663acba

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

adminforth/modules/restApi.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
HttpExtra,
1313
IAdminForthAndOrFilter,
1414
BackendOnlyInput,
15+
Filters,
1516
} from "../types/Back.js";
1617

1718
import { ADMINFORTH_VERSION, listify, md5hash, getLoginPromptHTML } from './utils.js';
@@ -1180,6 +1181,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
11801181
}
11811182
}
11821183

1184+
const primaryKeyColumn = resource.columns.find((col) => col.primaryKey);
1185+
if (record[primaryKeyColumn.name] !== undefined) {
1186+
const existingRecord = await this.adminforth.resource(resource.resourceId).get([Filters.EQ(primaryKeyColumn.name, record[primaryKeyColumn.name])]);
1187+
if (existingRecord) {
1188+
return { error: `Record with ${primaryKeyColumn.name} '${record[primaryKeyColumn.name]}' already exists`, ok: false };
1189+
}
1190+
}
1191+
11831192
const ctxCreate = {
11841193
adminUser,
11851194
resource,
@@ -1296,6 +1305,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
12961305
return { error: allowedError };
12971306
}
12981307

1308+
const primaryKeyColumn = resource.columns.find((col) => col.primaryKey);
1309+
if (record[primaryKeyColumn.name] !== undefined) {
1310+
const existingRecord = await this.adminforth.resource(resource.resourceId).get([Filters.EQ(primaryKeyColumn.name, record[primaryKeyColumn.name])]);
1311+
if (existingRecord) {
1312+
return { error: `Record with ${primaryKeyColumn.name} '${record[primaryKeyColumn.name]}' already exists`, ok: false };
1313+
}
1314+
}
1315+
12991316
const ctxEdit = {
13001317
adminUser,
13011318
resource,

0 commit comments

Comments
 (0)