Skip to content

Commit

Permalink
Fix check pemissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey-weber committed Nov 7, 2024
1 parent 94eac34 commit c36c6fe
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
37 changes: 33 additions & 4 deletions src/services/new/collection/delete-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import {CollectionPermission} from '../../../entities/collection';
import {AppError} from '@gravity-ui/nodekit';
import {getCollectionsListByIds} from './get-collections-list-by-ids';
import {markCollectionsAsDeleted} from './utils/mark-collections-as-deleted';
import {getParents, getParentsIdsFromMap} from './utils';
import {registry} from '../../../registry';
import {CollectionInstance} from '../../../registry/common/entities/collection/types';

const validateArgs = makeSchemaValidator({
type: 'object',
Expand Down Expand Up @@ -45,6 +48,11 @@ export const deleteCollections = async (

const targetTrx = getPrimary(trx);

await getCollectionsListByIds(
{ctx, trx: targetTrx, skipValidation, skipCheckPermissions},
{collectionIds, permission: CollectionPermission.Delete},
);

const recursiveName = 'collectionChildren';

const collectionsForDelete = await CollectionModel.query(getReplica(trx))
Expand Down Expand Up @@ -81,10 +89,31 @@ export const deleteCollections = async (

const collectionsForDeleteIds = collectionsForDelete.map((item) => item.collectionId);

const collectionsMap = await getCollectionsListByIds(
{ctx, trx: targetTrx, skipValidation, skipCheckPermissions},
{collectionIds: collectionsForDeleteIds, permission: CollectionPermission.Delete},
);
const collectionsMap = new Map<CollectionInstance, string[]>();

const parents = await getParents({
ctx,
trx: targetTrx,
collectionIds: collectionsForDeleteIds,
});

const parentsMap = new Map<string, Nullable<string>>();

parents.forEach((parent: CollectionModel) => {
parentsMap.set(parent.collectionId, parent.parentId);
});

const {Collection} = registry.common.classes.get();

collectionsForDelete.forEach((model) => {
const collectionId = model.collectionId;

const parentsForCollection = getParentsIdsFromMap(collectionId, parentsMap);

const collection = new Collection({ctx, model});

collectionsMap.set(collection, parentsForCollection);
});

const workbooksForDelete = await WorkbookModel.query(getReplica(trx))
.select()
Expand Down
2 changes: 1 addition & 1 deletion src/services/new/collection/get-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const getCollection = async <T extends CollectionInstance = CollectionIns
model,
});

const {collectionInstance: collection} = await checkAndSetCollectionPermission(
const collection = await checkAndSetCollectionPermission(
{ctx, trx},
{collectionInstance, skipCheckPermissions, includePermissionsInfo, permission},
);
Expand Down
16 changes: 4 additions & 12 deletions src/services/new/collection/get-collections-list-by-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {CollectionPermission} from '../../../entities/collection';
import Utils from '../../../utils';
import {registry} from '../../../registry';
import {checkAndSetCollectionPermission} from './utils';
import {CollectionInstance} from '../../../registry/common/entities/collection/types';

const validateArgs = makeSchemaValidator({
type: 'object',
Expand Down Expand Up @@ -57,9 +56,7 @@ export const getCollectionsListByIds = async (
.whereIn(CollectionModelColumn.CollectionId, collectionIds)
.timeout(CollectionModel.DEFAULT_QUERY_TIMEOUT);

const collectionsMap: Map<CollectionInstance, string[]> = new Map();

await Promise.all(
const modelsWithPermissions = await Promise.all(
models.map(async (model) => {
const {Collection} = registry.common.classes.get();

Expand All @@ -68,17 +65,12 @@ export const getCollectionsListByIds = async (
model,
});

const collectionWithParents = await checkAndSetCollectionPermission(
const collection = await checkAndSetCollectionPermission(
{ctx, trx},
{collectionInstance, skipCheckPermissions, includePermissionsInfo, permission},
);

collectionsMap.set(
collectionWithParents.collectionInstance,
collectionWithParents.parentIds,
);

return collectionWithParents;
return collection;
}),
);

Expand All @@ -89,5 +81,5 @@ export const getCollectionsListByIds = async (
),
});

return collectionsMap;
return modelsWithPermissions;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,9 @@ export const checkAndSetCollectionPermission = async (
const {accessServiceEnabled} = ctx.config;

const targetTrx = getReplica(trx);
let parentIds: string[] = [];

if (accessServiceEnabled) {
if (collectionInstance.model.parentId !== null) {
parentIds = await getParentIds({
ctx,
trx: targetTrx,
collectionId: collectionInstance.model.parentId,
});
}
}

if (accessServiceEnabled && !skipCheckPermissions && !isPrivateRoute) {
let parentIds: string[] = [];
let localPermission: CollectionPermission;

if (permission) {
Expand All @@ -54,6 +44,14 @@ export const checkAndSetCollectionPermission = async (
localPermission = CollectionPermission.View;
}

if (collectionInstance.model.parentId !== null) {
parentIds = await getParentIds({
ctx,
trx: targetTrx,
collectionId: collectionInstance.model.parentId,
});
}

ctx.log('CHECK_PERMISSION', {permission: localPermission});

await collectionInstance.checkPermission({
Expand All @@ -70,5 +68,5 @@ export const checkAndSetCollectionPermission = async (

ctx.log('CHECK_COLLECTION_PERMISSION_FINISH');

return {collectionInstance, parentIds};
return collectionInstance;
};
18 changes: 18 additions & 0 deletions src/services/new/collection/utils/get-parents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,21 @@ export const getCollectionsParentIds = async ({
const parents = await getParents({ctx, trx, collectionIds});
return parents.map((item) => item.collectionId);
};

export const getParentsIdsFromMap = (
collectionId: string | null,
parentsMap: Map<string, Nullable<string>>,
): string[] => {
let id: Nullable<string> = collectionId;
const arr: string[] = id ? [id] : [];

while (id !== null) {
const curr: Nullable<string> = parentsMap.get(id) || null;

if (curr) arr.push(curr);

id = curr;
}

return arr;
};
20 changes: 1 addition & 19 deletions src/services/new/workbook/get-workbooks-list-by-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {getReplica} from '../utils';
import Utils from '../../../utils';
import {registry} from '../../../registry';

import {getParents} from '../collection/utils';
import {getParents, getParentsIdsFromMap} from '../collection/utils';

import {WorkbookPermission} from '../../../entities/workbook';

Expand All @@ -32,24 +32,6 @@ type GetWorkbooksListAndAllParentsArgs = {
includePermissionsInfo?: boolean;
};

export const getParentsIdsFromMap = (
collectionId: string | null,
parentsMap: Map<string, Nullable<string>>,
): string[] => {
let id: Nullable<string> = collectionId;
const arr: string[] = id ? [id] : [];

while (id !== null) {
const curr: Nullable<string> = parentsMap.get(id) || null;

if (curr) arr.push(curr);

id = curr;
}

return arr;
};

export const getWorkbooksListByIds = async (
{ctx, trx, skipValidation = false, skipCheckPermissions = false}: ServiceArgs,
args: GetWorkbooksListAndAllParentsArgs,
Expand Down

0 comments on commit c36c6fe

Please sign in to comment.