Skip to content

Commit

Permalink
CB-4661 remove includes concept from resources api (#2910)
Browse files Browse the repository at this point in the history
Co-authored-by: mr-anton-t <[email protected]>
  • Loading branch information
sergeyteleshev and mr-anton-t authored Sep 23, 2024
1 parent d230275 commit 1746d91
Show file tree
Hide file tree
Showing 91 changed files with 897 additions and 337 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 { injectable } from '@cloudbeaver/core-di';
import { CachedMapAllKey, CachedMapResource, isResourceAlias, type ResourceKey, resourceKeyList, ResourceKeyUtils } from '@cloudbeaver/core-resource';
import { GraphQLService } from '@cloudbeaver/core-sdk';

import type { TeamMetaParameter } from './TeamMetaParametersResource.js';
import { TeamsResource } from './TeamsResource.js';

@injectable()
export class TeamInfoMetaParametersResource extends CachedMapResource<string, TeamMetaParameter> {
constructor(
private readonly graphQLService: GraphQLService,
private readonly teamsResource: TeamsResource,
) {
super();

this.sync(this.teamsResource);
this.teamsResource.onItemDelete.addHandler(this.delete.bind(this));
}

protected async loader(param: ResourceKey<string>): Promise<Map<string, TeamMetaParameter>> {
const all = this.aliases.isAlias(param, CachedMapAllKey);
const teamsList: [string, TeamMetaParameter][] = [];

await ResourceKeyUtils.forEachAsync(param, async key => {
let teamId: string | undefined;

if (!isResourceAlias(key)) {
teamId = key;
}

const { teams } = await this.graphQLService.sdk.getTeamsListMetaParameters({
teamId,
});

if (!teams.length) {
throw new Error(`Team ${teamId} not found`);
}

const metaParameters = teams[0]?.metaParameters;

if (teamId) {
teamsList.push([teamId, metaParameters]);
}
});

const key = resourceKeyList(teamsList.map(([teamId]) => teamId));
const value = teamsList.map(([_, metaParameters]) => metaParameters);

if (all) {
this.replace(key, value);
} else {
this.set(key, value);
}

return this.data;
}

async setMetaParameters(teamId: string, parameters: Record<string, any>): Promise<void> {
await this.performUpdate(teamId, [], async () => {
await this.graphQLService.sdk.saveTeamMetaParameters({ teamId, parameters });

if (this.data) {
this.data.set(teamId, parameters as TeamMetaParameter);
}
});
}

protected validateKey(key: string): boolean {
return typeof key === 'string';
}
}
19 changes: 2 additions & 17 deletions webapp/packages/core-authentication/src/TeamsResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ export class TeamsResource extends CachedMapResource<string, TeamInfo, TeamResou
super();
}

async createTeam({ teamId, teamPermissions, teamName, description, metaParameters }: TeamInfo): Promise<TeamInfo> {
async createTeam({ teamId, teamPermissions, teamName, description }: TeamInfo): Promise<TeamInfo> {
const response = await this.graphQLService.sdk.createTeam({
teamId,
teamName,
description,
...this.getDefaultIncludes(),
...this.getIncludesMap(teamId),
});

Expand All @@ -55,24 +54,21 @@ export class TeamsResource extends CachedMapResource<string, TeamInfo, TeamResou

this.set(newTeam.teamId, newTeam);

await this.setMetaParameters(newTeam.teamId, metaParameters);
await this.setSubjectPermissions(newTeam.teamId, teamPermissions);

return this.get(teamId)!;
}

async updateTeam({ teamId, teamPermissions, teamName, description, metaParameters }: TeamInfo): Promise<TeamInfo> {
async updateTeam({ teamId, teamPermissions, teamName, description }: TeamInfo): Promise<TeamInfo> {
const { team } = await this.graphQLService.sdk.updateTeam({
teamId,
teamName,
description,
...this.getDefaultIncludes(),
...this.getIncludesMap(teamId),
});

this.set(team.teamId, team);

await this.setMetaParameters(team.teamId, metaParameters);
await this.setSubjectPermissions(team.teamId, teamPermissions);

this.markOutdated(team.teamId);
Expand Down Expand Up @@ -123,10 +119,6 @@ export class TeamsResource extends CachedMapResource<string, TeamInfo, TeamResou
}
}

async setMetaParameters(teamId: string, parameters: Record<string, any>): Promise<void> {
await this.graphQLService.sdk.saveTeamMetaParameters({ teamId, parameters });
}

protected async loader(originalKey: ResourceKey<string>, includes?: string[]): Promise<Map<string, TeamInfo>> {
const all = this.aliases.isAlias(originalKey, CachedMapAllKey);
const teamsList: TeamInfo[] = [];
Expand All @@ -140,7 +132,6 @@ export class TeamsResource extends CachedMapResource<string, TeamInfo, TeamResou

const { teams } = await this.graphQLService.sdk.getTeamsList({
teamId,
...this.getDefaultIncludes(),
...this.getIncludesMap(teamId, includes),
});

Expand Down Expand Up @@ -168,12 +159,6 @@ export class TeamsResource extends CachedMapResource<string, TeamInfo, TeamResou
super.dataSet(key, { ...oldTeam, ...value });
}

private getDefaultIncludes(): TeamResourceIncludes {
return {
includeMetaParameters: false,
};
}

protected validateKey(key: string): boolean {
return typeof key === 'string';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { injectable } from '@cloudbeaver/core-di';
import { CachedDataResource, type ResourceKey } from '@cloudbeaver/core-resource';
import { GraphQLService } from '@cloudbeaver/core-sdk';

import { UserInfoResource } from './UserInfoResource.js';
import type { UserMetaParameter } from './UserMetaParametersResource.js';

@injectable()
export class UserInfoMetaParametersResource extends CachedDataResource<UserMetaParameter | undefined> {
constructor(
private readonly graphQLService: GraphQLService,
private readonly userInfoResource: UserInfoResource,
) {
super(() => undefined, undefined);

this.sync(this.userInfoResource);
}

protected async loader(param: ResourceKey<void>): Promise<UserMetaParameter | undefined> {
const { user } = await this.graphQLService.sdk.getActiveUserMetaParameters();

return user?.metaParameters;
}
}
6 changes: 1 addition & 5 deletions webapp/packages/core-authentication/src/UserInfoResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class UserInfoResource extends CachedDataResource<UserInfo | null, void,
private readonly authProviderService: AuthProviderService,
private readonly sessionResource: SessionResource,
) {
super(() => null, undefined, ['customIncludeOriginDetails', 'includeConfigurationParameters']);
super(() => null, undefined, ['includeConfigurationParameters']);

this.onUserChange = new SyncExecutor();
this.onException = new SyncExecutor();
Expand Down Expand Up @@ -110,7 +110,6 @@ export class UserInfoResource extends CachedDataResource<UserInfo | null, void,
configuration: configurationId,
credentials: processedCredentials,
linkUser,
customIncludeOriginDetails: true,
forceSessionsLogout,
});

Expand Down Expand Up @@ -142,7 +141,6 @@ export class UserInfoResource extends CachedDataResource<UserInfo | null, void,
const { authInfo } = await this.graphQLService.sdk.getAuthStatus({
authId,
linkUser,
customIncludeOriginDetails: true,
});
return authInfo as AuthInfo;
},
Expand Down Expand Up @@ -288,9 +286,7 @@ export class UserInfoResource extends CachedDataResource<UserInfo | null, void,

private getDefaultIncludes(): UserInfoIncludes {
return {
customIncludeOriginDetails: true,
includeConfigurationParameters: false,
includeMetaParameters: false,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 { injectable } from '@cloudbeaver/core-di';
import { CachedMapAllKey, CachedMapResource, isResourceAlias, type ResourceKey, resourceKeyList, ResourceKeyUtils } from '@cloudbeaver/core-resource';
import { GraphQLService } from '@cloudbeaver/core-sdk';

import type { UserMetaParameter } from './UserMetaParametersResource.js';
import { UsersResource } from './UsersResource.js';

@injectable()
export class UsersMetaParametersResource extends CachedMapResource<string, UserMetaParameter> {
constructor(
private readonly graphQLService: GraphQLService,
private readonly usersResource: UsersResource,
) {
super();

this.sync(this.usersResource);
this.usersResource.onItemDelete.addHandler(this.delete.bind(this));
}

async setMetaParameters(userId: string, parameters: Record<string, any>): Promise<void> {
await this.performUpdate(userId, undefined, async () => {
await this.graphQLService.sdk.saveUserMetaParameters({ userId, parameters });

if (this.data) {
this.data.set(userId, parameters as UserMetaParameter);
}
});
}

protected async loader(originalKey: ResourceKey<string>): Promise<Map<string, UserMetaParameter>> {
const all = this.aliases.isAlias(originalKey, CachedMapAllKey);
const keys = resourceKeyList<string>([]);

if (all) {
throw new Error('Loading all users is prohibited');
}

const userMetaParametersList: UserMetaParameter[] = [];

await ResourceKeyUtils.forEachAsync(originalKey, async key => {
let userId: string | undefined;

if (!isResourceAlias(key)) {
userId = key;
}

if (userId !== undefined) {
const { user } = await this.graphQLService.sdk.getAdminUserMetaParameters({
userId,
});

keys.push(userId);
userMetaParametersList.push(user.metaParameters);
}
});

this.set(keys, userMetaParametersList);

return this.data;
}

protected validateKey(key: string): boolean {
return typeof key === 'string';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 { injectable } from '@cloudbeaver/core-di';
import { CachedMapAllKey, CachedMapResource, isResourceAlias, type ResourceKey, resourceKeyList, ResourceKeyUtils } from '@cloudbeaver/core-resource';
import { type AdminOriginDetailsFragment, GraphQLService } from '@cloudbeaver/core-sdk';

import { UsersResource } from './UsersResource.js';

@injectable()
export class UsersOriginDetailsResource extends CachedMapResource<string, AdminOriginDetailsFragment> {
constructor(
private readonly graphQLService: GraphQLService,
private readonly usersResource: UsersResource,
) {
super();

this.sync(this.usersResource);
this.usersResource.onItemDelete.addHandler(this.delete.bind(this));
}

protected async loader(originalKey: ResourceKey<string>): Promise<Map<string, AdminOriginDetailsFragment>> {
const all = this.aliases.isAlias(originalKey, CachedMapAllKey);
const keys = resourceKeyList<string>([]);

if (all) {
throw new Error('Loading all users is prohibited');
}

const userMetaParametersList: AdminOriginDetailsFragment[] = [];

await ResourceKeyUtils.forEachAsync(originalKey, async key => {
let userId: string | undefined;

if (!isResourceAlias(key)) {
userId = key;
}

if (userId !== undefined) {
const { user } = await this.graphQLService.sdk.getAdminUserOriginDetails({
userId,
});

keys.push(userId);
userMetaParametersList.push(user);
}
});

this.set(keys, userMetaParametersList);

return this.data;
}

protected validateKey(key: string): boolean {
return typeof key === 'string';
}
}
14 changes: 0 additions & 14 deletions webapp/packages/core-authentication/src/UsersResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,11 @@ export class UsersResource extends CachedMapResource<string, AdminUser, UserReso
});
}

async setMetaParameters(userId: string, parameters: Record<string, any>): Promise<void> {
await this.graphQLService.sdk.saveUserMetaParameters({ userId, parameters });
}

async create({ userId, authRole }: UserCreateOptions): Promise<AdminUser> {
const { user } = await this.graphQLService.sdk.createUser({
userId,
authRole,
enabled: false,
...this.getDefaultIncludes(),
...this.getIncludesMap(userId),
});

Expand Down Expand Up @@ -259,7 +254,6 @@ export class UsersResource extends CachedMapResource<string, AdminUser, UserReso
if (userId !== undefined) {
const { user } = await this.graphQLService.sdk.getAdminUserInfo({
userId,
...this.getDefaultIncludes(),
...this.getIncludesMap(userId, includes),
});

Expand All @@ -284,7 +278,6 @@ export class UsersResource extends CachedMapResource<string, AdminUser, UserReso
userIdMask,
enabledState,
},
...this.getDefaultIncludes(),
...this.getIncludesMap(userId, includes),
});

Expand All @@ -311,13 +304,6 @@ export class UsersResource extends CachedMapResource<string, AdminUser, UserReso
return this.data;
}

private getDefaultIncludes(): UserResourceIncludes {
return {
customIncludeOriginDetails: false,
includeMetaParameters: false,
};
}

protected override dataSet(key: string, value: AdminUserInfoFragment): void {
const oldValue = this.data.get(key);
super.dataSet(key, { ...oldValue, ...value });
Expand Down
Loading

0 comments on commit 1746d91

Please sign in to comment.