Skip to content

Commit 499ef4d

Browse files
authored
UBERF-8445: More smart admin mode (#6897)
Signed-off-by: Andrey Sobolev <[email protected]>
1 parent e63d501 commit 499ef4d

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

server/middleware/src/spaceSecurity.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,14 @@ export class SpaceSecurityMiddleware extends BaseMiddleware implements Middlewar
503503
const isSpace = this.context.hierarchy.isDerived(_class, core.class.Space)
504504
const field = this.getKey(domain)
505505

506+
if (
507+
ctx.contextData.admin === true &&
508+
this.context.hierarchy.isDerived(_class, core.class.Space) &&
509+
(newQuery as DocumentQuery<Space>).members !== undefined
510+
) {
511+
delete (newQuery as any).members
512+
}
513+
506514
let clientFilterSpaces: Set<Ref<Space>> | undefined
507515

508516
if (!this.skipFindCheck && !isSystem(account) && account.role !== AccountRole.DocGuest && domain !== DOMAIN_MODEL) {
@@ -558,6 +566,12 @@ export class SpaceSecurityMiddleware extends BaseMiddleware implements Middlewar
558566
}
559567
}
560568
}
569+
if (ctx.contextData.admin === true && this.context.hierarchy.isDerived(_class, core.class.Space)) {
570+
// We need to add amin to all spaces.
571+
for (const d of findResult) {
572+
;(d as unknown as Space).members = [...((d as unknown as Space).members ?? []), ctx.contextData.account._id]
573+
}
574+
}
561575
return findResult
562576
}
563577

server/middleware/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ export function isOwner (account: Account, ctx: MeasureContext<SessionData>): bo
2020
}
2121

2222
export function isSystem (account: Account): boolean {
23-
return account._id === core.account.System
23+
return account._id === core.account.System || account._id.startsWith('system:')
2424
}

server/server/src/client.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,37 @@ export class ClientSession implements Session {
108108
async getAccount (ctx: ClientSessionCtx): Promise<void> {
109109
const account = this._pipeline.context.modelDb.getAccountByEmail(this.token.email)
110110
if (account === undefined && this.token.extra?.admin === 'true') {
111-
const systemAccount = this._pipeline.context.modelDb.findObject(this.token.email as Ref<Account>)
112-
if (systemAccount === undefined) {
113-
// Generate account for admin user
114-
const factory = new TxFactory(core.account.System)
115-
const email = `system:${this.token.email}`
116-
const createTx = factory.createTxCreateDoc(
117-
core.class.Account,
118-
core.space.Model,
119-
{
120-
role: AccountRole.Owner,
121-
email
122-
},
123-
this.token.email as Ref<Account>
124-
)
125-
this.includeSessionContext(ctx.ctx)
126-
await this._pipeline.tx(ctx.ctx, [createTx])
127-
const acc = TxProcessor.createDoc2Doc(createTx)
128-
await ctx.sendResponse(acc)
129-
return
130-
} else {
131-
await ctx.sendResponse(systemAccount)
132-
return
133-
}
111+
await ctx.sendResponse(this.getSystemAccount())
112+
return
134113
}
135114
await ctx.sendResponse(account)
136115
}
137116

117+
private getSystemAccount (): Account {
118+
// Generate account for admin user
119+
const factory = new TxFactory(core.account.System)
120+
const email = `system:${this.token.email}`
121+
const createTx = factory.createTxCreateDoc(
122+
core.class.Account,
123+
core.space.Model,
124+
{
125+
role: AccountRole.Owner,
126+
email
127+
},
128+
email as Ref<Account>
129+
)
130+
return TxProcessor.createDoc2Doc(createTx)
131+
}
132+
138133
includeSessionContext (ctx: MeasureContext): void {
134+
let account: Account | undefined
135+
if (this.token.extra?.admin === 'true') {
136+
account = this._pipeline.context.modelDb.getAccountByEmail(this.token.email)
137+
if (account === undefined) {
138+
account = this.getSystemAccount()
139+
}
140+
}
141+
139142
const contextData = new SessionDataImpl(
140143
this.token.email,
141144
this.sessionId,
@@ -149,7 +152,8 @@ export class ClientSession implements Session {
149152
false,
150153
new Map(),
151154
new Map(),
152-
this._pipeline.context.modelDb
155+
this._pipeline.context.modelDb,
156+
account
153157
)
154158
ctx.contextData = contextData
155159
}

0 commit comments

Comments
 (0)