Fix get account (#4425)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2024-01-24 15:31:42 +06:00 committed by GitHub
parent 87ffb7616e
commit d205cd9370
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -36,7 +36,8 @@ import core, {
generateId,
type SearchQuery,
type SearchOptions,
type SearchResult
type SearchResult,
TxFactory
} from '@hcengineering/core'
import { type Pipeline, type SessionContext } from '@hcengineering/server-core'
import { type Token } from '@hcengineering/server-token'
@ -82,20 +83,31 @@ export class ClientSession implements Session {
async getAccount (ctx: MeasureContext): Promise<Account> {
const account = await this._pipeline.modelDb.findAll(core.class.Account, { email: this.token.email })
if (account.length === 0 && this.token.extra?.admin === 'true') {
// Generate fake account for admin user
const account = {
_id: core.account.System,
_class: 'contact:class:PersonAccount' as Ref<Class<Account>>,
name: 'System,Ghost',
email: this.token.email,
space: core.space.Model,
modifiedBy: core.account.System,
modifiedOn: Date.now(),
role: AccountRole.Owner
const systemAccount = await this._pipeline.modelDb.findAll(core.class.Account, {
_id: this.token.email as Ref<Account>
})
if (systemAccount.length === 0) {
// Generate account for admin user
const factory = new TxFactory(core.account.System)
const email = `system:${this.token.email}`
const createTx = factory.createTxCreateDoc(
core.class.Account,
core.space.Model,
{
role: AccountRole.Owner,
email
},
this.token.email as Ref<Account>
)
const context = ctx as SessionContext
context.userEmail = this.token.email
context.admin = this.token.extra?.admin === 'true'
await this._pipeline.tx(context, createTx)
const acc = TxProcessor.createDoc2Doc(createTx)
return acc
} else {
return systemAccount[0]
}
// Add for other services to work properly
this._pipeline.modelDb.addDoc(account)
return account
}
return account[0]
}