55 type TxMiddlewareResult
66} from '@hcengineering/server-core'
77import core , {
8+ type Account ,
89 AccountRole ,
910 type Doc ,
1011 hasAccountRole ,
@@ -19,6 +20,7 @@ import core, {
1920 type TxUpdateDoc
2021} from '@hcengineering/core'
2122import platform , { PlatformError , Severity , Status } from '@hcengineering/platform'
23+ import contact , { type Person } from '@hcengineering/contact'
2224
2325export class GuestPermissionsMiddleware extends BaseMiddleware implements Middleware {
2426 static async create (
@@ -40,44 +42,44 @@ export class GuestPermissionsMiddleware extends BaseMiddleware implements Middle
4042 }
4143
4244 for ( const tx of txes ) {
43- this . processTx ( ctx , tx )
45+ await this . processTx ( ctx , tx )
4446 }
4547
4648 return await this . provideTx ( ctx , txes )
4749 }
4850
49- private processTx ( ctx : MeasureContext < SessionData > , tx : Tx ) : void {
51+ private async processTx ( ctx : MeasureContext < SessionData > , tx : Tx ) : Promise < void > {
5052 const h = this . context . hierarchy
5153 if ( tx . _class === core . class . TxApplyIf ) {
5254 const applyTx = tx as TxApplyIf
5355 for ( const t of applyTx . txes ) {
54- this . processTx ( ctx , t )
56+ await this . processTx ( ctx , t )
5557 }
5658 return
5759 }
5860 if ( TxProcessor . isExtendsCUD ( tx . _class ) ) {
59- const socialIds = ctx . contextData . account . socialIds
61+ const { account } = ctx . contextData
6062 const cudTx = tx as TxCUD < Doc >
6163 const isSpace = h . isDerived ( cudTx . objectClass , core . class . Space )
6264 if ( isSpace ) {
63- if ( this . isForbiddenSpaceTx ( cudTx as TxCUD < Space > , socialIds ) ) {
65+ if ( await this . isForbiddenSpaceTx ( ctx , cudTx as TxCUD < Space > , account ) ) {
6466 throw new PlatformError ( new Status ( Severity . ERROR , platform . status . Forbidden , { } ) )
6567 }
66- } else if ( cudTx . space !== core . space . DerivedTx && this . isForbiddenTx ( cudTx , socialIds ) ) {
68+ } else if ( cudTx . space !== core . space . DerivedTx && ( await this . isForbiddenTx ( ctx , cudTx , account ) ) ) {
6769 throw new PlatformError ( new Status ( Severity . ERROR , platform . status . Forbidden , { } ) )
6870 }
6971 }
7072 }
7173
72- private isForbiddenTx ( tx : TxCUD < Doc > , socialIds : PersonId [ ] ) : boolean {
74+ private async isForbiddenTx ( ctx : MeasureContext , tx : TxCUD < Doc > , account : Account ) : Promise < boolean > {
7375 if ( tx . _class === core . class . TxMixin ) return false
74- return ! this . hasMixinAccessLevel ( tx , socialIds )
76+ return ! ( await this . hasMixinAccessLevel ( ctx , tx , account ) )
7577 }
7678
77- private isForbiddenSpaceTx ( tx : TxCUD < Space > , socialIds : PersonId [ ] ) : boolean {
79+ private async isForbiddenSpaceTx ( ctx : MeasureContext , tx : TxCUD < Space > , account : Account ) : Promise < boolean > {
7880 if ( tx . _class === core . class . TxRemoveDoc ) return true
7981 if ( tx . _class === core . class . TxCreateDoc ) {
80- return ! this . hasMixinAccessLevel ( tx , socialIds )
82+ return ! ( await this . hasMixinAccessLevel ( ctx , tx , account ) )
8183 }
8284 if ( tx . _class === core . class . TxUpdateDoc ) {
8385 const updateTx = tx as TxUpdateDoc < Space >
@@ -93,7 +95,7 @@ export class GuestPermissionsMiddleware extends BaseMiddleware implements Middle
9395 return false
9496 }
9597
96- private hasMixinAccessLevel ( tx : TxCUD < Doc > , socialIds : PersonId [ ] ) : boolean {
98+ private async hasMixinAccessLevel ( ctx : MeasureContext , tx : TxCUD < Doc > , account : Account ) : Promise < boolean > {
9799 const h = this . context . hierarchy
98100 const accessLevelMixin = h . classHierarchyMixin ( tx . objectClass , core . mixin . TxAccessLevel )
99101 if ( accessLevelMixin === undefined ) return false
@@ -104,9 +106,15 @@ export class GuestPermissionsMiddleware extends BaseMiddleware implements Middle
104106 return accessLevelMixin . removeAccessLevel === AccountRole . Guest
105107 }
106108 if ( tx . _class === core . class . TxUpdateDoc ) {
107- if ( accessLevelMixin . isIdentity === true && socialIds . includes ( tx . objectId as unknown as PersonId ) ) {
109+ if ( accessLevelMixin . isIdentity === true && account . socialIds . includes ( tx . objectId as unknown as PersonId ) ) {
108110 return true
109111 }
112+ if ( accessLevelMixin . isIdentity === true && h . isDerived ( tx . objectClass , contact . class . Person ) ) {
113+ const person = ( await this . findAll ( ctx , tx . objectClass , { _id : tx . objectId } , { limit : 1 } ) ) [ 0 ] as
114+ | Person
115+ | undefined
116+ return person ?. personUuid === account . uuid
117+ }
110118 return accessLevelMixin . updateAccessLevel === AccountRole . Guest
111119 }
112120 return false
0 commit comments