Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion api/registry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export type {
GetServicePlan,
ProcessTenantSettings,
} from '../../src/registry/plugins/common/utils/tenant/types';
export type {GetEntryResolveUserLogin} from '../../src/registry/plugins/common/utils/entry/types';
export type {
GetEntryResolveUserLogin,
IsLicenseRequired,
CheckLicense,
} from '../../src/registry/plugins/common/utils/entry/types';
export type {
CollectionConstructor,
CollectionInstance,
Expand Down
9 changes: 9 additions & 0 deletions src/db/models/new/entry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Model} from '../../..';
import {EntryPermissions} from '../../../../services/new/entry/types';
import {CollectionModel, CollectionModelColumn} from '../collection';
import {Favorite} from '../favorite';
import {LicenseAssignment, LicenseAssignmentColumn} from '../license-assignment';
import {RevisionModel} from '../revision';
import {Tenant, TenantColumn} from '../tenant';
import {WorkbookModel} from '../workbook';
Expand Down Expand Up @@ -127,6 +128,14 @@ export class Entry extends Model {
to: `${Tenant.tableName}.${TenantColumn.TenantId}`,
},
},
licenseAssignment: {
relation: Model.HasOneRelation,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like ManyToMany

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this context the result can have only one LicenseAssignment. Something similar we have with Favorites

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presentation would be more transparent in this case ;)

modelClass: LicenseAssignment,
join: {
from: `${Entry.tableName}.${EntryColumn.TenantId}`,
to: `${LicenseAssignment.tableName}.${LicenseAssignmentColumn.TenantId}`,
},
},
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/registry/plugins/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import type {WorkbookConstructor} from './entities/workbook/types';
import type {CheckColorPalettesAdmin} from './utils/color-palettes/types';
import type {CheckEmbedding} from './utils/embedding/types';
import type {
CheckLicense,
GetEntryAddFormattedFieldsHook,
GetEntryBeforeDbRequestHook,
GetEntryResolveUserLogin,
IsLicenseRequired,
IsNeedBypassEntryByKey,
} from './utils/entry/types';
import type {LogEvent} from './utils/log-event/types';
Expand All @@ -42,5 +44,7 @@ export const commonPlugin = {
processTenantSettings: makeFunctionTemplate<ProcessTenantSettings>(),
getZitadelUserRole: makeFunctionTemplate<GetZitadelUserRole>(),
checkColorPalettesAdmin: makeFunctionTemplate<CheckColorPalettesAdmin>(),
isLicenseRequired: makeFunctionTemplate<IsLicenseRequired>(),
checkLicense: makeFunctionTemplate<CheckLicense>(),
}),
};
4 changes: 4 additions & 0 deletions src/registry/plugins/common/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {Workbook} from './entities/workbook/workbook';
import {checkColorPalettesAdmin} from './utils/color-palettes/utils';
import {checkEmbedding} from './utils/embedding/utils';
import {
checkLicense,
getEntryAddFormattedFieldsHook,
getEntryBeforeDbRequestHook,
getEntryResolveUserLogin,
isLicenseRequired,
isNeedBypassEntryByKey,
} from './utils/entry/utils';
import {logEvent} from './utils/log-event/utils';
Expand All @@ -31,6 +33,8 @@ export const setupCommonPlugin = () => {
getEntryBeforeDbRequestHook,
getEntryAddFormattedFieldsHook,
getEntryResolveUserLogin,
isLicenseRequired,
checkLicense,
checkEmbedding,
logEvent,
checkTenant,
Expand Down
11 changes: 11 additions & 0 deletions src/registry/plugins/common/utils/entry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ export type GetEntryBeforeDbRequestHook = (args: {
export type GetEntryAddFormattedFieldsHook = (args: {ctx: AppContext}) => Record<string, unknown>;

export type GetEntryResolveUserLogin = (args: {ctx: AppContext}) => Promise<string | undefined>;

export type IsLicenseRequired = (args: {ctx: AppContext}) => boolean;

export type CheckLicense = (args: {
ctx: AppContext;
licenseAssignment?: {
licenseAssignmentId?: string;
expiredAt?: string | null;
licenseType?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We have LicenseType enum.
  2. Why all fields are optional?

};
}) => void | Promise<void>;
10 changes: 10 additions & 0 deletions src/registry/plugins/common/utils/entry/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type {
CheckLicense,
GetEntryAddFormattedFieldsHook,
GetEntryBeforeDbRequestHook,
GetEntryResolveUserLogin,
IsLicenseRequired,
IsNeedBypassEntryByKey,
} from './types';

Expand All @@ -12,3 +14,11 @@ export const getEntryBeforeDbRequestHook: GetEntryBeforeDbRequestHook = () => Pr
export const getEntryAddFormattedFieldsHook: GetEntryAddFormattedFieldsHook = () => ({});

export const getEntryResolveUserLogin: GetEntryResolveUserLogin = () => Promise.resolve(undefined);

export const isLicenseRequired: IsLicenseRequired = () => {
return false;
};

export const checkLicense: CheckLicense = async () => {
return;
};
14 changes: 14 additions & 0 deletions src/services/new/entry/get-entry/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {CollectionModel, CollectionModelColumn} from '../../../../db/models/new/collection';
import {Entry, EntryColumn} from '../../../../db/models/new/entry';
import {Favorite, FavoriteColumn} from '../../../../db/models/new/favorite';
import {
LicenseAssignment,
LicenseAssignmentColumn,
} from '../../../../db/models/new/license-assignment';
import {RevisionModel, RevisionModelColumn} from '../../../../db/models/new/revision';
import {Tenant, TenantColumn} from '../../../../db/models/new/tenant';

Expand Down Expand Up @@ -70,6 +74,16 @@ export const selectedCollectionColumns = collectionColumns.map(
(column) => `${CollectionModel.tableName}.${column}`,
);

export const licenseAssignmentColumns = [
LicenseAssignmentColumn.LicenseAssignmentId,
LicenseAssignmentColumn.ExpiredAt,
LicenseAssignmentColumn.LicenseType,
] as const;

export const selectedLicenseAssignmentColumns = licenseAssignmentColumns.map(
(column) => `${LicenseAssignment.tableName}.${column}`,
);

export const ENTRY_QUERY_TIMEOUT = 3000;
export const ENTITY_BINDING_QUERY_TIMEOUT = 3000;
export const GET_PARENTS_QUERY_TIMEOUT = 3000;
Expand Down
31 changes: 29 additions & 2 deletions src/services/new/entry/get-entry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
selectedCollectionColumns,
selectedEntryColumns,
selectedFavoriteColumns,
selectedLicenseAssignmentColumns,
selectedRevisionColumns,
selectedTenantColumns,
} from './constants';
Expand Down Expand Up @@ -86,8 +87,13 @@ export const getEntry = async (

const {isPrivateRoute, user, onlyPublic, onlyMirrored, tenantId} = ctx.get('info');

const {getEntryBeforeDbRequestHook, checkEmbedding, getEntryResolveUserLogin} =
registry.common.functions.get();
const {
getEntryBeforeDbRequestHook,
checkEmbedding,
getEntryResolveUserLogin,
isLicenseRequired,
checkLicense,
} = registry.common.functions.get();

let userLoginPromise: Promise<string | undefined> = Promise.resolve(undefined);

Expand All @@ -106,6 +112,13 @@ export const getEntry = async (

const graphRelations = ['workbook', 'tenant(tenantModifier)', 'collection(collectionModifier)'];

const licenseRequired =
!isPrivateRoute && !onlyPublic && !isEmbedding && isLicenseRequired({ctx});

if (licenseRequired) {
graphRelations.push('licenseAssignment(licenseAssignmentModifier)');
}

if (revId) {
graphRelations.push('revisions(revisionsModifier)');
} else if (branch === 'saved') {
Expand Down Expand Up @@ -162,6 +175,16 @@ export const getEntry = async (
favoriteModifier(builder) {
builder.select(selectedFavoriteColumns).where({login: userLogin});
},

licenseAssignmentModifier(builder) {
builder
.select(selectedLicenseAssignmentColumns)
.where({
tenantId,
userId: user.userId,
})
.first();
},
})
.first()
.timeout(ENTRY_QUERY_TIMEOUT);
Expand All @@ -180,6 +203,10 @@ export const getEntry = async (
});
}

if (licenseRequired) {
await checkLicense({ctx, licenseAssignment: entry.licenseAssignment});
}

const checkWorkbookIsolationEnabled =
!isPrivateRoute &&
!onlyPublic &&
Expand Down
3 changes: 3 additions & 0 deletions src/services/new/entry/get-entry/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {CollectionModel} from '../../../../db/models/new/collection';
import {Entry} from '../../../../db/models/new/entry';
import {Favorite} from '../../../../db/models/new/favorite';
import {LicenseAssignment} from '../../../../db/models/new/license-assignment';
import {RevisionModel} from '../../../../db/models/new/revision';
import {Tenant} from '../../../../db/models/new/tenant';
import {WorkbookModel} from '../../../../db/models/new/workbook';
Expand All @@ -9,6 +10,7 @@ import {
collectionColumns,
entryColumns,
favoriteColumns,
licenseAssignmentColumns,
revisionColumns,
tenantColumns,
} from './constants';
Expand All @@ -29,4 +31,5 @@ export type SelectedEntry = Pick<Entry, ArrayElement<typeof entryColumns>> & {
collection?: SelectedCollection;
favorite?: SelectedFavorite;
tenant?: SelectedTenant;
licenseAssignment?: Pick<LicenseAssignment, ArrayElement<typeof licenseAssignmentColumns>>;
};