Skip to content

Commit 5f00f62

Browse files
Allow guest update profile (avatar, name etc) (#10429)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
1 parent d8653ad commit 5f00f62

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

foundations/server/packages/middleware/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"dependencies": {
3838
"@hcengineering/core": "workspace:^0.7.24",
39+
"@hcengineering/contact": "workspace:^0.7.0",
3940
"@hcengineering/platform": "workspace:^0.7.19",
4041
"@hcengineering/server-core": "workspace:^0.7.18",
4142
"@hcengineering/query": "workspace:^0.7.17",

foundations/server/packages/middleware/src/guestPermissions.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type TxMiddlewareResult
66
} from '@hcengineering/server-core'
77
import core, {
8+
type Account,
89
AccountRole,
910
type Doc,
1011
hasAccountRole,
@@ -19,6 +20,7 @@ import core, {
1920
type TxUpdateDoc
2021
} from '@hcengineering/core'
2122
import platform, { PlatformError, Severity, Status } from '@hcengineering/platform'
23+
import contact, { type Person } from '@hcengineering/contact'
2224

2325
export 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

models/contact/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ export function createModel (builder: Builder): void {
325325
})
326326

327327
builder.mixin(contact.class.Person, core.class.Class, core.mixin.TxAccessLevel, {
328-
createAccessLevel: AccountRole.Guest
328+
createAccessLevel: AccountRole.Guest,
329+
isIdentity: true
329330
})
330331

331332
builder.mixin(contact.class.SocialIdentity, core.class.Class, core.mixin.TxAccessLevel, {

0 commit comments

Comments
 (0)