Skip to content

Commit c7b2919

Browse files
committed
Validate that updated fields are present in the form when creating/updating a pub
1 parent 3e09375 commit c7b2919

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

core/app/components/pubs/PubEditor/actions.ts

+37-7
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,26 @@ type CreatePubRecursiveProps = Omit<Parameters<typeof createPubRecursiveNew>[0],
2222

2323
export const createPubRecursive = defineServerAction(async function createPubRecursive(
2424
props: CreatePubRecursiveProps & {
25-
formSlug?: string;
25+
formSlug: string;
2626
addUserToForm?: boolean;
2727
}
2828
) {
29-
const { formSlug, addUserToForm, ...createPubProps } = props;
29+
const {
30+
formSlug,
31+
addUserToForm,
32+
body: { values, ...body },
33+
...createPubProps
34+
} = props;
3035
const loginData = await getLoginData();
3136

3237
if (!loginData || !loginData.user) {
3338
return ApiError.NOT_LOGGED_IN;
3439
}
3540
const { user } = loginData;
3641

42+
if (!formSlug) {
43+
return ApiError.UNAUTHORIZED;
44+
}
3745
const [form, canCreatePub] = await Promise.all([
3846
formSlug
3947
? await getForm({ communityId: props.communityId, slug: formSlug }).executeTakeFirst()
@@ -42,11 +50,15 @@ export const createPubRecursive = defineServerAction(async function createPubRec
4250
userId: user.id,
4351
communityId: props.communityId,
4452
formSlug,
45-
pubTypeId: createPubProps.body.pubTypeId as PubTypesId,
53+
pubTypeId: body.pubTypeId as PubTypesId,
4654
}),
4755
]);
4856

49-
const isPublicForm = form?.access === FormAccessType.public;
57+
if (!form) {
58+
return ApiError.UNAUTHORIZED;
59+
}
60+
61+
const isPublicForm = form.access === FormAccessType.public;
5062

5163
if (!canCreatePub && !isPublicForm) {
5264
return ApiError.UNAUTHORIZED;
@@ -62,7 +74,14 @@ export const createPubRecursive = defineServerAction(async function createPubRec
6274
const createdPub = await createPubRecursiveNew({
6375
...createPubProps,
6476
body: {
65-
...createPubProps.body,
77+
...body,
78+
values: values
79+
? Object.fromEntries(
80+
Object.entries(values).filter(([slug]) =>
81+
form.elements.find((element) => element.slug === slug)
82+
)
83+
)
84+
: {},
6685
// adds user to the pub
6786
// TODO: this should be configured on the form
6887
members: { [user.id]: MemberRole.contributor },
@@ -112,7 +131,7 @@ export const updatePub = defineServerAction(async function updatePub({
112131
JsonValue | Date | { value: JsonValue | Date; relatedPubId: PubsId }[]
113132
>;
114133
stageId?: StagesId;
115-
formSlug?: string;
134+
formSlug: string;
116135
continueOnValidationError: boolean;
117136
deleted: { slug: string; relatedPubId: PubsId }[];
118137
}) {
@@ -128,7 +147,14 @@ export const updatePub = defineServerAction(async function updatePub({
128147
return ApiError.COMMUNITY_NOT_FOUND;
129148
}
130149

131-
if (!userCanEditPub({ pubId, userId: loginData.user.id, formSlug })) {
150+
if (!formSlug) {
151+
return ApiError.UNAUTHORIZED;
152+
}
153+
154+
const form = await getForm({ slug: formSlug, communityId: community.id }).executeTakeFirst();
155+
const canEdit = await userCanEditPub({ pubId, userId: loginData.user.id, formSlug });
156+
157+
if (!form || !canEdit) {
132158
return ApiError.UNAUTHORIZED;
133159
}
134160

@@ -154,7 +180,11 @@ export const updatePub = defineServerAction(async function updatePub({
154180
});
155181

156182
const normalizedValues = normalizePubValues(processedVals);
183+
157184
for (const { slug, value, relatedPubId } of normalizedValues) {
185+
if (!form.elements.find((element) => element.slug === slug)) {
186+
continue;
187+
}
158188
if (relatedPubId) {
159189
updateQuery.relate(slug, value, relatedPubId, {
160190
replaceExisting: false,

0 commit comments

Comments
 (0)