Skip to content

Commit

Permalink
Charts 8598 change workbook permission mapping (#29)
Browse files Browse the repository at this point in the history
* change behavior for getEntryPermissionsByWorkbook

* add workbooksMap

* rm await, async

* return includePermissionsInfo

* add scope

* fix after review

* add filter for relation by workbookId

* move filter

* rm async
  • Loading branch information
Sergey-weber authored Nov 21, 2023
1 parent 942a9e4 commit 7de0abb
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 139 deletions.
47 changes: 23 additions & 24 deletions src/db/models/favorite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {getEntryPermissionsByWorkbook} from '../../../services/new/workbook/util

import {EntryPermissions} from '../../../services/new/entry/types';

import {WorkbookInstance} from '../../../registry/common/entities/workbook/types';

interface Favorite extends MT.FavoriteColumns {}
class Favorite extends Model {
static get tableName() {
Expand Down Expand Up @@ -188,33 +190,30 @@ class Favorite extends Model {
},
);

const workbookIds = workbookList.map((workbook) => workbook.model.workbookId);

const workbookPermissionsMap = new Map<string, EntryPermissions>();

if (includePermissionsInfo) {
await Promise.all(
workbookList.map(async (workbook) => {
try {
const permissions = await getEntryPermissionsByWorkbook({
ctx,
workbook,
bypassEnabled: false,
});
workbookPermissionsMap.set(workbook.model.workbookId, permissions);
} catch (e) {}
}),
);
}
const entryPermissionsMap = new Map<string, EntryPermissions>();
const workbooksMap = new Map<string, WorkbookInstance>();

workbookList.forEach((workbook) => {
workbooksMap.set(workbook.model.workbookId, workbook);
});

workbookEntries.forEach((entry) => {
if (entry?.workbookId && workbookIds.includes(entry.workbookId)) {
if (entry?.workbookId && workbooksMap.has(entry.workbookId)) {
const workbook = workbooksMap.get(entry.workbookId);

if (workbook && includePermissionsInfo) {
const permissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
entryPermissionsMap.set(entry.entryId, permissions);
}

let isLocked = false;

if (workbookPermissionsMap.has(entry.workbookId)) {
const isReadPermission = workbookPermissionsMap.get(
entry.workbookId,
)?.read;
if (entryPermissionsMap.has(entry.entryId)) {
const isReadPermission = entryPermissionsMap.get(entry.entryId)?.read;

if (!isReadPermission) {
isLocked = true;
Expand All @@ -224,7 +223,7 @@ class Favorite extends Model {
result.push({
...entry,
permissions: includePermissionsInfo
? workbookPermissionsMap.get(entry.workbookId)
? entryPermissionsMap.get(entry.entryId)
: undefined,
isLocked,
});
Expand Down
50 changes: 23 additions & 27 deletions src/db/models/navigation/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {raw, TransactionOrKnex} from 'objection';
import {raw} from 'objection';
import {Model} from '../..';
import Utils from '../../../utils';
import Revision from '../revision';
Expand All @@ -13,6 +13,7 @@ import {EntryPermissions} from '../../../services/new/entry/types';
import {getEntryPermissionsByWorkbook} from '../../../services/new/workbook/utils';

import {getWorkbooksListByIds} from '../../../services/new/workbook/get-workbooks-list-by-ids';
import {WorkbookInstance} from '../../../registry/common/entities/workbook/types';

interface Navigation extends MT.EntryColumns {
isLocked?: boolean;
Expand Down Expand Up @@ -70,7 +71,6 @@ class Navigation extends Model {
isPrivateRoute,
}: MT.GetEntriesConfig,
ctx: MT.CTX,
trx?: TransactionOrKnex,
) {
ctx.log('GET_ENTRIES_REQUEST', {
tenantId,
Expand Down Expand Up @@ -253,34 +253,30 @@ class Navigation extends Model {
},
);

const workbookIds = workbookList.map((workbook) => workbook.model.workbookId);

const workbookPermissionsMap = new Map<string, EntryPermissions>();

if (includePermissionsInfo) {
await Promise.all(
workbookList.map(async (workbook) => {
try {
const permissions = await getEntryPermissionsByWorkbook({
ctx,
trx,
workbook,
bypassEnabled: false,
});
workbookPermissionsMap.set(workbook.model.workbookId, permissions);
} catch (e) {}
}),
);
}
const entryPermissionsMap = new Map<string, EntryPermissions>();
const workbooksMap = new Map<string, WorkbookInstance>();

workbookList.forEach((workbook) => {
workbooksMap.set(workbook.model.workbookId, workbook);
});

workbookEntries.forEach((entry) => {
if (entry?.workbookId && workbookIds.includes(entry.workbookId)) {
if (entry?.workbookId && workbooksMap.has(entry.workbookId)) {
const workbook = workbooksMap.get(entry.workbookId);

if (workbook && includePermissionsInfo) {
const permissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
entryPermissionsMap.set(entry.entryId, permissions);
}

let isLocked = false;

if (workbookPermissionsMap.has(entry.workbookId)) {
const isReadPermission = workbookPermissionsMap.get(
entry.workbookId,
)?.read;
if (entryPermissionsMap.has(entry.entryId)) {
const isReadPermission = entryPermissionsMap.get(entry.entryId)?.read;

if (!isReadPermission) {
isLocked = true;
Expand All @@ -290,7 +286,7 @@ class Navigation extends Model {
result.push({
...entry,
permissions: includePermissionsInfo
? workbookPermissionsMap.get(entry.workbookId)
? entryPermissionsMap.get(entry.entryId)
: undefined,
isLocked,
});
Expand Down
25 changes: 11 additions & 14 deletions src/services/entry/actions/create-in-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ export async function createEntryInWorkbook(

const {accessServiceEnabled} = ctx.config;

let permissions: Optional<UsPermission>;
const workbook = await getWorkbook(
{ctx, skipCheckPermissions: isPrivateRoute},
{workbookId, includePermissionsInfo},
);

const createdEntry = await transaction(Entry.primary, async (trx) => {
const workbook = await getWorkbook(
{ctx, trx, skipCheckPermissions: isPrivateRoute},
{workbookId},
);

if (accessServiceEnabled && !isPrivateRoute) {
await checkWorkbookPermission({
ctx,
Expand All @@ -118,14 +116,6 @@ export async function createEntryInWorkbook(
});
}

if (includePermissionsInfo) {
permissions = await getEntryPermissionsByWorkbook({
ctx,
workbook,
bypassEnabled: isPrivateRoute,
});
}

const [entryId, revId] = await Promise.all([getId(), getId()]);

// TODO: DLS.addEntity / iam registrate entry
Expand Down Expand Up @@ -174,8 +164,15 @@ export async function createEntryInWorkbook(
});

const resultEntry: Entry & {permissions?: UsPermission} = createdEntry!;
let permissions: Optional<UsPermission>;

if (includePermissionsInfo) {
permissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: resultEntry.scope,
});

resultEntry.permissions = permissions;
}

Expand Down
46 changes: 26 additions & 20 deletions src/services/entry/actions/get-entry-relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ export async function getEntryRelations(ctx: CTX, params: GetEntryRelationsData)
});
}

let iamPermissions: Optional<EntryPermissions>;
if (!entry.workbookId && !isPrivateRoute) {
await checkEntry(ctx, Entry.replica, {verifiableEntry: entry});
}

let relations = await getRelatedEntries(ctx, {
entryIds: [entryId],
direction: validatedDirection,
});

if (entry.workbookId) {
const workbook = await getWorkbook(
Expand All @@ -74,27 +81,26 @@ export async function getEntryRelations(ctx: CTX, params: GetEntryRelationsData)
{workbookId: entry.workbookId, includePermissionsInfo},
);

if (includePermissionsInfo) {
iamPermissions = await getEntryPermissionsByWorkbook({
ctx,
workbook,
bypassEnabled: isPrivateRoute,
});
}
} else if (!isPrivateRoute) {
await checkEntry(ctx, Entry.replica, {verifiableEntry: entry});
}
relations = relations.filter(
(relationEntry) => relationEntry.workbookId === entry.workbookId,
);

let relations = await getRelatedEntries(ctx, {
entryIds: [entryId],
direction: validatedDirection,
});
relations = relations.map((item) => {
let iamPermissions: Optional<EntryPermissions>;

if (entry.workbookId) {
relations = relations.map((item) => ({
...item,
permissions: iamPermissions,
})) as Entry[];
if (includePermissionsInfo) {
iamPermissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
}

return {
...item,
permissions: iamPermissions,
};
}) as Entry[];
} else {
if (!isPrivateRoute && ctx.config.dlsEnabled) {
relations = await DLS.checkBulkPermission(
Expand Down
7 changes: 4 additions & 3 deletions src/services/new/entry/get-entry-by-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {checkFetchedEntry} from './utils';
import {EntryPermissions} from './types';
import {ServiceArgs} from '../types';
import {getReplica} from '../utils';
import {Entry} from '../../../db/models/new/entry';
import {Entry, EntryColumn} from '../../../db/models/new/entry';
import {DlsActions} from '../../../types/models';
import {US_ERRORS} from '../../../const';
import Utils, {logInfo} from '../../../utils';
Expand Down Expand Up @@ -118,13 +118,14 @@ export const getEntryByKey = async (
if (!isPrivateRoute) {
const workbook = await getWorkbook(
{ctx, trx},
{workbookId: joinedEntryRevision.workbookId},
{workbookId: joinedEntryRevision.workbookId, includePermissionsInfo},
);

if (includePermissionsInfo) {
iamPermissions = await getEntryPermissionsByWorkbook({
iamPermissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: joinedEntryRevision[EntryColumn.Scope],
});
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/services/new/entry/get-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {EntryPermissions} from './types';
import {checkFetchedEntry} from './utils';
import {getReplica, checkEntryIdInEmbed} from '../utils';
import {ServiceArgs} from '../types';
import {Entry} from '../../../db/models/new/entry';
import {Entry, EntryColumn} from '../../../db/models/new/entry';
import {JoinedEntryRevisionFavorite} from '../../../db/presentations/joined-entry-revision-favorite';
import {DlsActions} from '../../../types/models';
import Utils, {logInfo} from '../../../utils';
Expand Down Expand Up @@ -118,13 +118,17 @@ export const getEntry = async (
if (checkWorkbookEnabled) {
const workbook = await getWorkbook(
{ctx, trx},
{workbookId: joinedEntryRevisionFavorite.workbookId},
{
workbookId: joinedEntryRevisionFavorite.workbookId,
includePermissionsInfo,
},
);

if (includePermissionsInfo) {
iamPermissions = await getEntryPermissionsByWorkbook({
iamPermissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: joinedEntryRevisionFavorite[EntryColumn.Scope],
});
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/services/new/workbook/get-workbook-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,18 @@ export const getWorkbookContent = async (

const nextPageToken = Utils.getNextPageToken(page, pageSize, entriesPage.total);

let permissions: Optional<UsPermission>;

if (includePermissionsInfo) {
permissions = await getEntryPermissionsByWorkbook({
ctx,
trx: targetTrx,
workbook,
bypassEnabled: skipCheckPermissions,
});
}

const entries: Array<Navigation & {permissions?: UsPermission; isLocked?: boolean}> =
entriesPage.results.map((entry) => {
let permissions: Optional<UsPermission>;

if (includePermissionsInfo) {
permissions = getEntryPermissionsByWorkbook({
ctx,
workbook,
scope: entry.scope,
});
}

entry.permissions = permissions;
entry.isLocked = false;
return entry;
Expand Down
Loading

0 comments on commit 7de0abb

Please sign in to comment.