Skip to content

Commit

Permalink
CB-4461 removes includeMetaParameters for users resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyteleshev committed Sep 11, 2024
1 parent f101b9a commit 05093a5
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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, ResourceKey } from '@cloudbeaver/core-resource';
import { GraphQLService } from '@cloudbeaver/core-sdk';

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

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

this.sync(this.userInfoResource);
}

protected async loader(param: ResourceKey<void>): Promise<UserMetaParameter | null> {

Check warning on line 26 in webapp/packages/core-authentication/src/UserInfoMetaParametersResource.ts

View check run for this annotation

Jenkins-CI-integration / CheckStyle TypeScript Report

webapp/packages/core-authentication/src/UserInfoMetaParametersResource.ts#L26

param is defined but never used. (@typescript-eslint/no-unused-vars)
try {
const { user } = await this.graphQLService.sdk.getActiveUserMetaParameters();

return user?.metaParameters;
} catch (exception: any) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ export class UserInfoResource extends CachedDataResource<UserInfo | null, void,
return {
customIncludeOriginDetails: true,
includeConfigurationParameters: false,
includeMetaParameters: false,
};
}
}
112 changes: 112 additions & 0 deletions webapp/packages/core-authentication/src/UsersMetaDataResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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 {
CACHED_RESOURCE_DEFAULT_PAGE_LIMIT,
CACHED_RESOURCE_DEFAULT_PAGE_OFFSET,
CachedMapAllKey,
CachedMapResource,
CachedResourceOffsetPageKey,
CachedResourceOffsetPageListKey,
isResourceAlias,
ResourceKey,
resourceKeyList,
ResourceKeyUtils,
} from '@cloudbeaver/core-resource';
import { GraphQLService } from '@cloudbeaver/core-sdk';

import { UserMetaParameter } from './UserMetaParametersResource';
import { UsersResource, UsersResourceFilterKey } from './UsersResource';

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

this.sync(this.usersResource);
}

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

// TODO after CB-5511 is merged. fix the logic according to the UsersResource loader
protected async loader(originalKey: ResourceKey<string>): Promise<Map<string, UserMetaParameter>> {
const all = this.aliases.isAlias(originalKey, CachedMapAllKey);
const keys: 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);
} else {
const pageKey =
this.aliases.isAlias(originalKey, CachedResourceOffsetPageKey) || this.aliases.isAlias(originalKey, CachedResourceOffsetPageListKey);
const filterKey = this.aliases.isAlias(originalKey, UsersResourceFilterKey);
let offset = CACHED_RESOURCE_DEFAULT_PAGE_OFFSET;
let limit = CACHED_RESOURCE_DEFAULT_PAGE_LIMIT;
let userIdMask: string | undefined;
let enabledState: boolean | undefined;

if (pageKey) {
offset = pageKey.options.offset;
limit = pageKey.options.limit;
}

if (filterKey) {
userIdMask = filterKey.options.userId;
enabledState = filterKey.options.enabledState;
}

const { users } = await this.graphQLService.sdk.getUsersMetaParametersList({
page: {
offset,
limit,
},
filter: {
userIdMask,
enabledState,
},
});

userMetaParametersList.push(...users.map(user => user.metaParameters));
keys.push(...users.map(user => user.userId));

this.offsetPagination.setPageEnd(CachedResourceOffsetPageListKey(offset, users.length).setTarget(filterKey), users.length === limit);
}
});

this.set(resourceKeyList(keys), userMetaParametersList);

return this.data;
}

protected validateKey(key: string): boolean {
return typeof key === 'string';
}
}
5 changes: 0 additions & 5 deletions webapp/packages/core-authentication/src/UsersResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ 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,
Expand Down Expand Up @@ -303,7 +299,6 @@ export class UsersResource extends CachedMapResource<string, AdminUser, UserReso
private getDefaultIncludes(): UserResourceIncludes {
return {
customIncludeOriginDetails: false,
includeMetaParameters: false,
};
}

Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/core-authentication/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export * from './TeamsResource';
export * from './UserDataService';
export * from './UserInfoResource';
export * from './UserMetaParametersResource';
export * from './UserInfoMetaParametersResource';
export * from './UsersMetaDataResource';
export * from './UsersResource';
export * from './TeamMetaParametersResource';
export * from './TeamInfoMetaParametersResource';
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/core-authentication/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const coreAuthenticationManifest: PluginManifest = {
() => import('./UserDataService').then(m => m.UserDataService),
() => import('./UserInfoResource').then(m => m.UserInfoResource),
() => import('./UserMetaParametersResource').then(m => m.UserMetaParametersResource),
() => import('./UserInfoMetaParametersResource').then(m => m.UserInfoMetaParametersResource),
() => import('./UsersMetaDataResource').then(m => m.UsersMetaParametersResource),
() => import('./UsersResource').then(m => m.UsersResource),
],
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
query getActiveUser($includeMetaParameters: Boolean!, $includeConfigurationParameters: Boolean!, $customIncludeOriginDetails: Boolean!) {
query getActiveUser($includeConfigurationParameters: Boolean!, $customIncludeOriginDetails: Boolean!) {
user: activeUser {
userId
displayName
authRole
linkedAuthProviders
metaParameters @include(if: $includeMetaParameters)
configurationParameters @include(if: $includeConfigurationParameters)
teams {
teamId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query getActiveUserMetaParameters {
user: activeUser {
metaParameters
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
mutation updateUserPreferences(
$preferences: Object!
$includeMetaParameters: Boolean!
$includeConfigurationParameters: Boolean!
$customIncludeOriginDetails: Boolean!
) {
mutation updateUserPreferences($preferences: Object!, $includeConfigurationParameters: Boolean!, $customIncludeOriginDetails: Boolean!) {
user: setUserPreferences(preferences: $preferences) {
userId
displayName
authRole
linkedAuthProviders
metaParameters @include(if: $includeMetaParameters)
configurationParameters @include(if: $includeConfigurationParameters)
teams {
teamId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
query createUser(
$userId: ID!
$enabled: Boolean!
$authRole: String
$includeMetaParameters: Boolean!
$customIncludeOriginDetails: Boolean!
) {
user: createUser(
userId: $userId
enabled: $enabled
authRole: $authRole
) {
query createUser($userId: ID!, $enabled: Boolean!, $authRole: String, $customIncludeOriginDetails: Boolean!) {
user: createUser(userId: $userId, enabled: $enabled, authRole: $authRole) {
...AdminUserInfo
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query getAdminUserMetaParameters($userId: ID!) {
user: adminUserInfo(userId: $userId) {
metaParameters
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query getAdminUserInfo($userId: ID!, $includeMetaParameters: Boolean!, $customIncludeOriginDetails: Boolean!) {
query getAdminUserInfo($userId: ID!, $customIncludeOriginDetails: Boolean!) {
user: adminUserInfo(userId: $userId) {
...AdminUserInfo
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query getUsersList($page: PageInput!, $filter: AdminUserFilterInput!, $includeMetaParameters: Boolean!, $customIncludeOriginDetails: Boolean!) {
query getUsersList($page: PageInput!, $filter: AdminUserFilterInput!, $customIncludeOriginDetails: Boolean!) {
users: listUsers(page: $page, filter: $filter) {
...AdminUserInfo
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query getUsersMetaParametersList($page: PageInput!, $filter: AdminUserFilterInput!) {
users: listUsers(page: $page, filter: $filter) {
userId
metaParameters
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ fragment AdminUserInfo on AdminUserInfo {
userId
grantedTeams
linkedAuthProviders
metaParameters @include(if: $includeMetaParameters)


origins {
...ObjectOriginInfo
}
enabled
authRole
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ interface Props extends UserFormProps {
export const UserFormInfoCredentials = observer<Props>(function UserFormInfoCredentials({ formState, tabState, tabSelected, disabled }) {
const translate = useTranslate();
const editing = formState.mode === FormMode.Edit;
const userInfo = useResource(
UserFormInfoCredentials,
UsersResource,
{ key: tabState.initialState.userId, includes: ['includeMetaParameters'] },
{ active: tabSelected && editing },
);
const userInfo = useResource(UserFormInfoCredentials, UsersResource, tabState.initialState.userId, { active: tabSelected && editing });
const authProvidersResource = useResource(UserFormInfoCredentials, AuthProvidersResource, null);
const passwordValidationRef = usePasswordValidation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
*/
import { observable, toJS } from 'mobx';

import type { AdminUser, AuthRolesResource, UserResourceIncludes, UsersResource } from '@cloudbeaver/core-authentication';
import type { AdminUser, AuthRolesResource, UserMetaParameter, UsersMetaParametersResource, UsersResource } from '@cloudbeaver/core-authentication';
import type { IExecutionContextProvider } from '@cloudbeaver/core-executor';
import { CachedResourceIncludeArgs, getCachedDataResourceLoaderState } from '@cloudbeaver/core-resource';
import { getCachedDataResourceLoaderState } from '@cloudbeaver/core-resource';
import type { ServerConfigResource } from '@cloudbeaver/core-root';
import type { AdminUserInfoFragment } from '@cloudbeaver/core-sdk';
import { FormMode, FormPart, formValidationContext, IFormState } from '@cloudbeaver/core-ui';
import { isArraysEqual, isDefined, isObjectsEqual, isValuesEqual } from '@cloudbeaver/core-utils';
import { DATA_CONTEXT_LOADABLE_STATE } from '@cloudbeaver/core-view';
Expand All @@ -22,12 +21,12 @@ import type { IUserFormInfoState } from './IUserFormInfoState';
const DEFAULT_ENABLED = true;

export class UserFormInfoPart extends FormPart<IUserFormInfoState, IUserFormState> {
private baseIncludes: CachedResourceIncludeArgs<AdminUserInfoFragment, UserResourceIncludes>;
constructor(
private readonly authRolesResource: AuthRolesResource,
private readonly serverConfigResource: ServerConfigResource,
formState: IFormState<IUserFormState>,
private readonly usersResource: UsersResource,
private readonly usersMetaParametersResource: UsersMetaParametersResource,
) {
super(formState, {
userId: formState.state.userId || '',
Expand All @@ -37,7 +36,6 @@ export class UserFormInfoPart extends FormPart<IUserFormInfoState, IUserFormStat
teams: [],
authRole: '',
});
this.baseIncludes = ['includeMetaParameters'];
}

protected format(data: IFormState<IUserFormState>, contexts: IExecutionContextProvider<IFormState<IUserFormState>>): void | Promise<void> {
Expand All @@ -59,7 +57,7 @@ export class UserFormInfoPart extends FormPart<IUserFormInfoState, IUserFormStat

isOutdated(): boolean {
if (this.formState.mode === FormMode.Edit && this.initialState.userId) {
return this.usersResource.isOutdated(this.initialState.userId, this.baseIncludes);
return this.usersResource.isOutdated(this.initialState.userId) || this.usersMetaParametersResource.isOutdated(this.initialState.userId);
}

return false;
Expand All @@ -69,7 +67,8 @@ export class UserFormInfoPart extends FormPart<IUserFormInfoState, IUserFormStat
if (
this.formState.mode === FormMode.Edit &&
this.initialState.userId &&
!this.usersResource.isLoaded(this.initialState.userId, this.baseIncludes)
!this.usersResource.isLoaded(this.initialState.userId) &&
!this.usersMetaParametersResource.isLoaded(this.initialState.userId)
) {
return false;
}
Expand Down Expand Up @@ -209,27 +208,32 @@ export class UserFormInfoPart extends FormPart<IUserFormInfoState, IUserFormStat

if (this.state.userId) {
const user = this.usersResource.get(this.state.userId);
const userMetaParameters = this.usersMetaParametersResource.get(this.state.userId);

if (user && isObjectsEqual(user.metaParameters, metaParameters)) {
if (user && isObjectsEqual(userMetaParameters, metaParameters)) {
return;
}
}

await this.usersResource.setMetaParameters(this.state.userId, metaParameters);
await this.usersMetaParametersResource.setMetaParameters(this.state.userId, metaParameters);
}

protected override async loader() {
let user: AdminUser | null = null;
let metaParameters: UserMetaParameter | object = {};
const serverConfig = await this.serverConfigResource.load();

if (this.formState.mode === FormMode.Edit && this.initialState.userId) {
user = await this.usersResource.load(this.initialState.userId, this.baseIncludes);
[user, metaParameters] = await Promise.all([
this.usersResource.load(this.initialState.userId),
this.usersMetaParametersResource.load(this.initialState.userId),
]);
}

this.setInitialState({
userId: user?.userId || this.formState.state.userId || '',
enabled: user?.enabled ?? DEFAULT_ENABLED,
metaParameters: observable(user?.metaParameters || {}),
metaParameters: observable(metaParameters),
teams: observable(user?.grantedTeams || [serverConfig?.defaultUserTeam].filter(isDefined)),
password: '',

Expand Down
Loading

0 comments on commit 05093a5

Please sign in to comment.