Files
cloudbeaver/webapp/packages/plugin-user-profile/src/UserProfileOptionsPanelService.ts
T
f677f48890 CB-5710 return anonymous user info (#2972)
* CB-5710 return anonymous user info

* CB-5710 support anonymous user

* CB-5710 anonymous access disabled active user fix

* CB-5710 simplify logic

* CB-5710 fix error messages

* CB-5710 review fixes

* CB-5710 add computed

* CB-5710 change getters to methods

* CB-5710 fixes after review

---------

Co-authored-by: naumov <iamemptyhuh@gmail.com>
Co-authored-by: alex <48489896+devnaumov@users.noreply.github.com>
Co-authored-by: Daria Marutkina <125263541+dariamarutkina@users.noreply.github.com>
Co-authored-by: Serge Rider <serge@jkiss.org>
2024-10-16 07:01:37 +02:00

90 lines
2.7 KiB
TypeScript

/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { action, makeObservable } from 'mobx';
import { UserInfoResource } from '@cloudbeaver/core-authentication';
import { importLazyComponent } from '@cloudbeaver/core-blocks';
import { injectable } from '@cloudbeaver/core-di';
import { ExecutionContext, Executor, type IExecutor, type ISyncExecutor, SyncExecutor } from '@cloudbeaver/core-executor';
import { OptionsPanelService } from '@cloudbeaver/core-ui';
import { isNotNullDefined } from '@cloudbeaver/core-utils';
import { userProfileContext } from './userProfileContext.js';
import { UserProfileTabsService } from './UserProfileTabsService.js';
const UserProfileOptionsPanel = importLazyComponent(() => import('./UserProfileOptionsPanel.js').then(m => m.UserProfileOptionsPanel));
const panelGetter = () => UserProfileOptionsPanel;
@injectable()
export class UserProfileOptionsPanelService {
readonly onOpen: ISyncExecutor;
readonly onClose: IExecutor;
constructor(
private readonly optionsPanelService: OptionsPanelService,
private readonly userInfoResource: UserInfoResource,
private readonly userProfileTabsService: UserProfileTabsService,
) {
this.onOpen = new SyncExecutor();
this.onClose = new Executor();
this.optionsPanelService.closeTask.next(this.onClose, undefined, () => this.optionsPanelService.isOpen(panelGetter));
this.userInfoResource.onDataUpdate.addHandler(this.userUpdateHandler.bind(this));
makeObservable(this, {
open: action,
close: action,
});
}
async open(tabId?: string): Promise<boolean> {
if (isNotNullDefined(tabId)) {
this.userProfileTabsService.tabContainer.select(tabId);
} else {
this.userProfileTabsService.tabContainer.select(null);
}
if (this.optionsPanelService.isOpen(panelGetter)) {
return true;
}
const state = await this.optionsPanelService.open(panelGetter);
if (state) {
this.onOpen.execute();
}
return state;
}
async close(force?: boolean): Promise<void> {
if (!this.optionsPanelService.isOpen(panelGetter)) {
return;
}
const context = new ExecutionContext<void>(undefined);
if (force) {
const userProfile = context.getContext(userProfileContext);
userProfile.setForce(force);
}
await this.optionsPanelService.close(context);
}
private userUpdateHandler() {
if (!this.optionsPanelService.isOpen(panelGetter)) {
return;
}
if (!this.userInfoResource.hasAccess()) {
this.close(true);
}
}
}