Skip to content

Commit

Permalink
CB-5710 review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
devnaumov committed Oct 10, 2024
1 parent 1688a7e commit 6cbb592
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 25 deletions.
8 changes: 3 additions & 5 deletions webapp/packages/core-authentication/src/AppAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import { UserInfoResource } from './UserInfoResource.js';
@injectable()
export class AppAuthService extends Bootstrap {
get authenticated(): boolean {
const user = this.userInfoResource.data;

return this.serverConfigResource.anonymousAccessEnabled || this.serverConfigResource.configurationMode || user !== null;
return this.serverConfigResource.configurationMode || this.userInfoResource.hasAccess;
}

get loaders(): ILoadableState[] {
Expand Down Expand Up @@ -56,9 +54,9 @@ export class AppAuthService extends Bootstrap {
throw new Error("Can't configure Authentication");
}

const user = await this.userInfoResource.load();
await this.userInfoResource.load();

return !this.serverConfigResource.configurationMode && !this.serverConfigResource.anonymousAccessEnabled && user === null;
return !this.serverConfigResource.configurationMode && !this.userInfoResource.hasAccess;
}

async authUser(): Promise<boolean> {
Expand Down
14 changes: 11 additions & 3 deletions webapp/packages/core-authentication/src/UserInfoResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ export class UserInfoResource extends CachedDataResource<UserInfo | null, void,
}

get isAnonymous() {
return !this.data || this.data.isAnonymous;
return this.data?.isAnonymous === true;
}

get isAuthenticated() {
return !!this.data && !this.isAnonymous;
}

get hasAccess() {
return this.isAnonymous || this.isAuthenticated;
}

constructor(
Expand All @@ -81,8 +89,8 @@ export class UserInfoResource extends CachedDataResource<UserInfo | null, void,
});
}

isAuthenticated(): this is { data: UserInfo } {
return !this.isAnonymous;
isData(): this is { data: UserInfo } {
return !!this.data;
}

isLinked(provideId: string): boolean {
Expand Down
6 changes: 2 additions & 4 deletions webapp/packages/core-projects/src/ProjectsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { computed, makeObservable } from 'mobx';

import { ANONYMOUS_USER_ID, UserDataService, UserInfoResource } from '@cloudbeaver/core-authentication';
import { UserDataService, UserInfoResource } from '@cloudbeaver/core-authentication';
import { Dependency, injectable } from '@cloudbeaver/core-di';
import { Executor, ExecutorInterrupter, type IExecutor, type ISyncExecutor, SyncExecutor } from '@cloudbeaver/core-executor';
import { CachedMapAllKey, resourceKeyList, ResourceKeyUtils } from '@cloudbeaver/core-resource';
Expand All @@ -33,10 +33,8 @@ export class ProjectsService extends Dependency {
get userProject(): ProjectInfo | undefined {
let project: ProjectInfo | undefined;

if (this.userInfoResource.data) {
if (this.userInfoResource.isData()) {
project = this.projectInfoResource.getUserProject(this.userInfoResource.data.userId);
} else {
project = this.projectInfoResource.get(ANONYMOUS_USER_ID);
}

return project;
Expand Down
8 changes: 4 additions & 4 deletions webapp/packages/core-settings-user/src/UserSettingsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class UserSettingsService extends SettingsSource {
}

async save() {
if (this.userInfoResource.isAuthenticated()) {
if (this.userInfoResource.isAuthenticated) {
await this.userInfoResource.updatePreferences(Object.fromEntries(this.changes));
} else {
this.update(() => {
Expand Down Expand Up @@ -94,13 +94,13 @@ export class UserSettingsService extends SettingsSource {

private refreshConfig() {
this.update(() => {
if (!this.userInfoResource.isAuthenticated()) {
if (!this.userInfoResource.isAuthenticated) {
this.clear();
this.lastConfig = null;
return;
}

if (this.userInfoResource.data.configurationParameters !== this.lastConfig) {
if (this.userInfoResource.isData() && this.userInfoResource.data.configurationParameters !== this.lastConfig) {
this.clear();
this.localSettings.clear();
this.lastConfig = this.userInfoResource.data.configurationParameters;
Expand All @@ -121,7 +121,7 @@ export class UserSettingsService extends SettingsSource {
}

private getSource() {
if (this.userInfoResource.isAuthenticated()) {
if (this.userInfoResource.isAuthenticated) {
return this.settings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class ConnectionFoldersBootstrap extends Bootstrap {
isActionApplicable: (context, action) => {
const tree = context.get(DATA_CONTEXT_ELEMENTS_TREE)!;

if (action !== ACTION_NEW_FOLDER || !this.userInfoResource.data || tree.baseRoot !== ROOT_NODE_PATH) {
if (action !== ACTION_NEW_FOLDER || !this.userInfoResource.isAuthenticated || tree.baseRoot !== ROOT_NODE_PATH) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class PublicConnectionFormService {
executorHandlerFilter(
() => !!this.formState && this.optionsPanelService.isOpen(formGetter),
async (event, context) => {
if (event === 'before' && this.userInfoResource.data === null) {
if (event === 'before' && !this.userInfoResource.isData()) {
const confirmed = await this.showUnsavedChangesDialog();
if (!confirmed) {
ExecutorInterrupter.interrupt(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ResourceFoldersBootstrap extends Bootstrap {
isActionApplicable: context => {
const tree = context.get(DATA_CONTEXT_ELEMENTS_TREE);

if (!tree?.baseRoot.startsWith(RESOURCES_NODE_PATH) || !this.userInfoResource.data) {
if (!tree?.baseRoot.startsWith(RESOURCES_NODE_PATH) || !this.userInfoResource.isAuthenticated) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
*/
import { computed, makeObservable } from 'mobx';

import { AuthInfoService } from '@cloudbeaver/core-authentication';
import { UserInfoResource } from '@cloudbeaver/core-authentication';
import { injectable } from '@cloudbeaver/core-di';
import type { ProjectInfo } from '@cloudbeaver/core-projects';
import { ServerConfigResource } from '@cloudbeaver/core-root';

@injectable()
export class ResourceManagerService {
get enabled() {
return !!this.serverConfigResource.data?.resourceManagerEnabled && !!this.authInfoService.userInfo;
return !!this.serverConfigResource.data?.resourceManagerEnabled && this.userInfoResource.isAuthenticated;
}

constructor(
private readonly authInfoService: AuthInfoService,
private readonly userInfoResource: UserInfoResource,
private readonly serverConfigResource: ServerConfigResource,
) {
makeObservable(this, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class SessionExpireWarningDialogBootstrap extends Bootstrap {
}

private handleSessionResourceDataUpdate(isValid?: boolean, remainingTime?: number) {
if (!this.serverConfigResource.anonymousAccessEnabled && !this.userInfoResource.data && !this.serverConfigResource.configurationMode) {
if (!this.serverConfigResource.configurationMode && !this.userInfoResource.hasAccess) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const UserMenu = observer(function UserMenu() {
context.set(DATA_CONTEXT_USER, userInfoResource.data, id);
});

if (!userInfoResource.isAuthenticated()) {
if (!userInfoResource.isData() || !userInfoResource.isAuthenticated) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class UserProfileOptionsPanelService {
return;
}

if (this.userInfoResource.data === null) {
if (!this.userInfoResource.isData()) {
this.close(true);
}
}
Expand Down

0 comments on commit 6cbb592

Please sign in to comment.