1
1
"use server" ;
2
2
3
3
import type { JsonValue } from "contracts" ;
4
- import type { PubsId , StagesId , UsersId } from "db/public" ;
4
+ import type { PubsId , PubTypesId , StagesId , UsersId } from "db/public" ;
5
5
import { Capabilities , FormAccessType , MemberRole , MembershipType } from "db/public" ;
6
6
import { logger } from "logger" ;
7
7
8
8
import { db } from "~/kysely/database" ;
9
9
import { getLoginData } from "~/lib/authentication/loginData" ;
10
10
import { isCommunityAdmin } from "~/lib/authentication/roles" ;
11
- import { userCan } from "~/lib/authorization/capabilities" ;
11
+ import { userCan , userCanCreatePub , userCanEditPub } from "~/lib/authorization/capabilities" ;
12
12
import { parseRichTextForPubFieldsAndRelatedPubs } from "~/lib/fields/richText" ;
13
13
import { createLastModifiedBy } from "~/lib/lastModifiedBy" ;
14
14
import { ApiError , createPubRecursiveNew } from "~/lib/server" ;
15
15
import { findCommunityBySlug } from "~/lib/server/community" ;
16
16
import { defineServerAction } from "~/lib/server/defineServerAction" ;
17
- import { getForm , grantFormAccess , userHasPermissionToForm } from "~/lib/server/form" ;
17
+ import { getForm , grantFormAccess } from "~/lib/server/form" ;
18
18
import { deletePub , maybeWithTrx , normalizePubValues } from "~/lib/server/pub" ;
19
19
import { PubOp } from "~/lib/server/pub-op" ;
20
20
@@ -34,21 +34,21 @@ export const createPubRecursive = defineServerAction(async function createPubRec
34
34
}
35
35
const { user } = loginData ;
36
36
37
- const [ form , canCreatePub , canCreateFromForm ] = await Promise . all ( [
37
+ const [ form , canCreatePub ] = await Promise . all ( [
38
38
formSlug
39
39
? await getForm ( { communityId : props . communityId , slug : formSlug } ) . executeTakeFirst ( )
40
40
: 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
+ } ) ,
47
47
] ) ;
48
48
49
49
const isPublicForm = form ?. access === FormAccessType . public ;
50
50
51
- if ( ! canCreatePub && ! canCreateFromForm && ! isPublicForm ) {
51
+ if ( ! canCreatePub && ! isPublicForm ) {
52
52
return ApiError . UNAUTHORIZED ;
53
53
}
54
54
@@ -128,16 +128,7 @@ export const updatePub = defineServerAction(async function updatePub({
128
128
return ApiError . COMMUNITY_NOT_FOUND ;
129
129
}
130
130
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 } ) ) {
141
132
return ApiError . UNAUTHORIZED ;
142
133
}
143
134
0 commit comments