11"use server" ;
22
33import type { JsonValue } from "contracts" ;
4- import type { PubsId , StagesId , UsersId } from "db/public" ;
4+ import type { PubsId , PubTypesId , StagesId , UsersId } from "db/public" ;
55import { Capabilities , FormAccessType , MemberRole , MembershipType } from "db/public" ;
66import { logger } from "logger" ;
77
88import { db } from "~/kysely/database" ;
99import { getLoginData } from "~/lib/authentication/loginData" ;
1010import { isCommunityAdmin } from "~/lib/authentication/roles" ;
11- import { userCan } from "~/lib/authorization/capabilities" ;
11+ import { userCan , userCanCreatePub , userCanEditPub } from "~/lib/authorization/capabilities" ;
1212import { parseRichTextForPubFieldsAndRelatedPubs } from "~/lib/fields/richText" ;
1313import { createLastModifiedBy } from "~/lib/lastModifiedBy" ;
1414import { ApiError , createPubRecursiveNew } from "~/lib/server" ;
1515import { findCommunityBySlug } from "~/lib/server/community" ;
1616import { defineServerAction } from "~/lib/server/defineServerAction" ;
17- import { getForm , grantFormAccess , userHasPermissionToForm } from "~/lib/server/form" ;
17+ import { getForm , grantFormAccess } from "~/lib/server/form" ;
1818import { deletePub , maybeWithTrx , normalizePubValues } from "~/lib/server/pub" ;
1919import { PubOp } from "~/lib/server/pub-op" ;
2020
@@ -34,21 +34,21 @@ export const createPubRecursive = defineServerAction(async function createPubRec
3434 }
3535 const { user } = loginData ;
3636
37- const [ form , canCreatePub , canCreateFromForm ] = await Promise . all ( [
37+ const [ form , canCreatePub ] = await Promise . all ( [
3838 formSlug
3939 ? await getForm ( { communityId : props . communityId , slug : formSlug } ) . executeTakeFirst ( )
4040 : null ,
41- userCan (
42- Capabilities . createPub ,
43- { type : MembershipType . community , communityId : props . communityId } ,
44- user . id
45- ) ,
46- formSlug ? userHasPermissionToForm ( { formSlug , userId : loginData . user . id } ) : false ,
41+ userCanCreatePub ( {
42+ userId : user . id ,
43+ communityId : props . communityId ,
44+ formSlug ,
45+ pubTypeId : createPubProps . body . pubTypeId as PubTypesId ,
46+ } ) ,
4747 ] ) ;
4848
4949 const isPublicForm = form ?. access === FormAccessType . public ;
5050
51- if ( ! canCreatePub && ! canCreateFromForm && ! isPublicForm ) {
51+ if ( ! canCreatePub && ! isPublicForm ) {
5252 return ApiError . UNAUTHORIZED ;
5353 }
5454
@@ -128,16 +128,7 @@ export const updatePub = defineServerAction(async function updatePub({
128128 return ApiError . COMMUNITY_NOT_FOUND ;
129129 }
130130
131- const [ canUpdateFromForm , canUpdatePubValues ] = await Promise . all ( [
132- formSlug ? userHasPermissionToForm ( { formSlug, userId : loginData . user . id , pubId } ) : false ,
133- userCan (
134- Capabilities . updatePubValues ,
135- { type : MembershipType . pub , pubId } ,
136- loginData . user . id
137- ) ,
138- ] ) ;
139-
140- if ( ! canUpdatePubValues && ! canUpdateFromForm ) {
131+ if ( ! userCanEditPub ( { pubId, userId : loginData . user . id , formSlug } ) ) {
141132 return ApiError . UNAUTHORIZED ;
142133 }
143134
0 commit comments