Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entitlements + license services #4593

Merged
merged 122 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 118 commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
76a4541
first pass at two new modules for TemplatesManager, TemplateDefault
techsmyth Sep 29, 2024
d01f378
added templates manager to space; removed the SpaceDefaults entity (m…
techsmyth Sep 29, 2024
862e754
added templatesManager to platform
techsmyth Sep 29, 2024
821a96d
moved creating of default innovatin flow input to space defaults
techsmyth Sep 29, 2024
fffaedf
back out space type on Template; tidy up Template module to use switc…
techsmyth Sep 29, 2024
1ac872d
created template applier module
techsmyth Sep 29, 2024
cc60f0d
tidy up naming
techsmyth Sep 29, 2024
09cbbdb
updated set of default template types
techsmyth Sep 29, 2024
04ba73a
fixed circular dependency; moved logic for creating collaboration inp…
techsmyth Sep 29, 2024
00038d5
removed loading of defaults from files for collaboration content
techsmyth Sep 29, 2024
fc4f6e3
removed code based addition of callouts, innovation flow states
techsmyth Sep 29, 2024
df626dd
merge from roles
techsmyth Sep 29, 2024
22300bc
merge from roles; added definitions to bootstrap folder
techsmyth Oct 1, 2024
55ff03c
tidy up naming
techsmyth Oct 1, 2024
0d9c7b4
Merge branch 'roles' into subspaceTemplates
techsmyth Oct 1, 2024
064cf23
Merge branch 'develop' into subspaceTemplates
techsmyth Oct 1, 2024
f56122b
added loading of default templates at platform level in to bootstrap
techsmyth Oct 1, 2024
f8cfd6c
removed option to create new innovation flow template
techsmyth Oct 1, 2024
bfaa76d
added in migration:
techsmyth Oct 1, 2024
9b428dd
loading in templates on bootstrap
techsmyth Oct 1, 2024
02239be
added field for collaboration templates on templatesSet; added lookup…
techsmyth Oct 1, 2024
11fc8cd
added mutation to create template from collaboration; added logic to …
techsmyth Oct 2, 2024
db2d78c
Merge remote-tracking branch 'origin/develop' into subspaceTemplates
ccanos Oct 3, 2024
d497688
Merge branch 'develop' into subspaceTemplates
ccanos Oct 3, 2024
04b9c72
Merge branch 'develop' into subspaceTemplates
techsmyth Oct 6, 2024
d4ec58f
Merge branch 'subspaceTemplates' of https://github.com/alkem-io/serve…
techsmyth Oct 6, 2024
da5ff7c
initial creation of license + entitlements modules
techsmyth Oct 6, 2024
400319e
add license into account
techsmyth Oct 6, 2024
f57142e
updated account to have license service + use that in mutations check…
techsmyth Oct 6, 2024
1ec83f3
ensure data is loaded properly on account for license checking
techsmyth Oct 6, 2024
2aa96f7
added mutation to reset the license calculations on account, includin…
techsmyth Oct 6, 2024
ed45adb
renamed Licensing module to LicensingFramework module; trigger licens…
techsmyth Oct 6, 2024
2d13bc6
removed usage of LicenseEngine outside of license services on space o…
techsmyth Oct 6, 2024
e1f0cff
renamed entitlement to licenseEntitlement as entity; first pass at mi…
techsmyth Oct 6, 2024
8f51052
fixed issues in migration
techsmyth Oct 6, 2024
862f282
fixed issues related to auth reset; tidied up loader creator imports
techsmyth Oct 6, 2024
20e4ef1
fixed auth cascade for templates of type post
techsmyth Oct 6, 2024
dc63e82
license reset running
techsmyth Oct 6, 2024
8343974
reset licenses on space after adding / removing license plans
techsmyth Oct 6, 2024
7b2bc0b
removed need for license check in community; added entitlement check …
techsmyth Oct 6, 2024
1184725
remove auth reset when assigning / removing license plans
techsmyth Oct 6, 2024
14fbfa8
added License to RoleSet
techsmyth Oct 6, 2024
b22514e
added license to collaboration
techsmyth Oct 7, 2024
fcaf0d7
tidied up retrieval of license for whiteboard; added license to colla…
techsmyth Oct 7, 2024
3095e32
fix typo; fix space spec file
techsmyth Oct 7, 2024
2a2e46b
fix additional tests
techsmyth Oct 7, 2024
7b00065
Merge branch 'develop' into subspaceTemplates
techsmyth Oct 7, 2024
8d7b917
moved tempaltesManager to last migration in the list
techsmyth Oct 7, 2024
f301d80
fixed retrieval of template when creating collaboration
techsmyth Oct 8, 2024
3a6174f
added logging
techsmyth Oct 8, 2024
d8ab18b
fixed bootstrap setting of templates
techsmyth Oct 8, 2024
89260bf
refactored inputCreator to do the data loading closer to usage; fixed…
techsmyth Oct 8, 2024
e9f3db2
Merge branch 'subspaceTemplates' into entitlements
techsmyth Oct 8, 2024
66f5e76
added ability to retrieve limits on entitlements + current usage
techsmyth Oct 8, 2024
fbb85a9
updated field names on entitlements
techsmyth Oct 8, 2024
401aca9
updated field names on entitlements
techsmyth Oct 8, 2024
9a8bbdb
fixed account mutaiton logic bug
techsmyth Oct 8, 2024
83f62d5
ensure that licenses are reset when assigning beta tester or vc campa…
techsmyth Oct 8, 2024
b6fa9d5
added reset all account licenses mutation
techsmyth Oct 8, 2024
c52119d
fixed bug on space entitlements; refactored code to reduce duplication
techsmyth Oct 9, 2024
cda9cb7
Merge branch 'develop' into subspaceTemplates
techsmyth Oct 9, 2024
15ec6b8
fixed url generation for templates inside of TempaltesManager
techsmyth Oct 10, 2024
74d3b1d
fixed bootstrap order to create forum earlier
techsmyth Oct 10, 2024
cd8332f
ensure collaboration creation on template provides some defaults for …
techsmyth Oct 10, 2024
56f47c5
fix deletion of templates of type post
techsmyth Oct 10, 2024
486c788
ensure more data is defaulted inside of template service for collabor…
techsmyth Oct 11, 2024
c7b2a28
ensure isTempalte is passed to Collaboration entity
techsmyth Oct 11, 2024
81985c0
merge from parent branch
techsmyth Oct 11, 2024
79c08ab
fixed groups in bootstrap space template; updated signature for creat…
techsmyth Oct 11, 2024
f989d0a
fixed missing field
techsmyth Oct 11, 2024
2052f32
fixed type on mutation to create from collaboration
techsmyth Oct 11, 2024
8489d34
fixed typo
techsmyth Oct 11, 2024
c61560d
fixed groups in bootstrap space template; updated signature for creat…
techsmyth Oct 11, 2024
446232b
fixed missing field
techsmyth Oct 11, 2024
6eaf99e
fixed type on mutation to create from collaboration
techsmyth Oct 11, 2024
b53c77e
fixed typo
techsmyth Oct 11, 2024
6057a74
reworked applying collaboraiton template to collaboration
techsmyth Oct 11, 2024
5025c60
improved error message in wrong type of ID passed in
techsmyth Oct 12, 2024
45e2996
fixed build
techsmyth Oct 12, 2024
5232146
Merge branch 'subspaceTemplates' into entitlements
techsmyth Oct 12, 2024
228c8b7
Merge branch 'develop' into subspaceTemplates
techsmyth Oct 12, 2024
df2491f
Merge branch 'develop' into subspaceTemplates
techsmyth Oct 14, 2024
f9c50cb
merge from develop
techsmyth Oct 16, 2024
eabfc05
made migration last in the list
techsmyth Oct 16, 2024
2303684
merge from base branch
techsmyth Oct 16, 2024
74ae062
rename migration to be last
techsmyth Oct 16, 2024
29228d9
removed read check when looking up collaboration
techsmyth Oct 16, 2024
cb78b63
Merge branch 'develop' into subspaceTemplates
ccanos Oct 18, 2024
f66990c
Merge branch 'develop' into subspaceTemplates
ccanos Oct 22, 2024
da80f4a
Merge branch 'subspaceTemplates' into entitlements
techsmyth Oct 22, 2024
315f81e
track free / plus / premium space entitlements separately
techsmyth Oct 22, 2024
36d0c5b
initial merge from develop
techsmyth Nov 3, 2024
60a1a6d
updated migration order
techsmyth Nov 3, 2024
d8588e5
removed duplicate migration
techsmyth Nov 4, 2024
ffc8379
moved auth reset to mutation for applying the template to another col…
techsmyth Nov 4, 2024
18f8f40
fixed more merge conflicts from develop
techsmyth Nov 5, 2024
45f23c9
extend lookup of entitlement usage to cover new types
techsmyth Nov 5, 2024
09ceebe
updaed license policy to reflect new entitlements; made license engin…
techsmyth Nov 5, 2024
683f1f0
updated migration to not drop indexes already removed
techsmyth Nov 5, 2024
1f1370a
fix for license reset on space
techsmyth Nov 5, 2024
e2c35c8
added license policy rule for free space credential
techsmyth Nov 5, 2024
aea78f7
ensure license entitlements are reset as part of the bootstrap
techsmyth Nov 5, 2024
a587378
fixed typo
techsmyth Nov 5, 2024
fe52ac7
Merge branch 'develop' into entitlements
techsmyth Nov 5, 2024
2d41f91
merge from develop
techsmyth Nov 8, 2024
e4c0df1
extended reset all to include resetting licenses on accounts + AI ser…
techsmyth Nov 8, 2024
6878843
merge from develop
techsmyth Nov 9, 2024
b3c33f6
Merge branch 'develop' into entitlements
valentinyanakiev Nov 11, 2024
4da2ef7
Address pr comment
valentinyanakiev Nov 11, 2024
f1d5c03
Address PR feedback
valentinyanakiev Nov 11, 2024
2d56b51
Address PR comment
valentinyanakiev Nov 11, 2024
81fa8cb
Address PR comments
valentinyanakiev Nov 11, 2024
9e5a53c
Merge remote-tracking branch 'origin/entitlements' into entitlements
valentinyanakiev Nov 11, 2024
1b59c05
Address PR comments
valentinyanakiev Nov 12, 2024
62d0bc9
Address PR comment
valentinyanakiev Nov 12, 2024
928e641
Improved types & naming
valentinyanakiev Nov 12, 2024
516ca9e
Merge branch 'develop' into entitlements
valentinyanakiev Nov 12, 2024
a3ffeae
Address PR comments
valentinyanakiev Nov 12, 2024
39756be
Fixed switch-case logic in entitlements
valentinyanakiev Nov 12, 2024
5680b57
Converge entitlements schema
valentinyanakiev Nov 13, 2024
438811e
Remove unused AuthorizationPrivilege
valentinyanakiev Nov 13, 2024
a85d3df
Merge branch 'develop' into entitlements
valentinyanakiev Nov 14, 2024
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
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import { PlatformHubModule } from '@platform/platform.hub/platform.hub.module';
import { AdminContributorsModule } from '@platform/admin/avatars/admin.avatar.module';
import { InputCreatorModule } from '@services/api/input-creator/input.creator.module';
import { TemplateApplierModule } from '@domain/template/template-applier/template.applier.module';
import { LoaderCreatorModule } from '@core/dataloader/creators/loader.creator.module';
import { Cipher, EncryptionModule } from '@hedger/nestjs-encryption';
import { AdminUsersModule } from '@platform/admin/users/admin.users.module';

Expand Down Expand Up @@ -247,6 +248,7 @@ import { AdminUsersModule } from '@platform/admin/users/admin.users.module';
};
},
}),
LoaderCreatorModule,
ScalarsModule,
AuthenticationModule,
AuthorizationModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export const CREDENTIAL_RULE_TYPES_SPACE_COMMUNITY_APPLY_GLOBAL_REGISTERED =
'credentialRuleTypes-spaceCommunityApplyGlobalRegistered';
export const CREDENTIAL_RULE_TYPES_SPACE_COMMUNITY_JOIN_GLOBAL_REGISTERED =
'credentialRuleTypes-spaceCommunityJoinGlobalRegistered';
export const CREDENTIAL_RULE_TYPES_ACCESS_VIRTUAL_CONTRIBUTORS =
'credentialRuleTypes-accessVirtualContributors';
export const CREDENTIAL_RULE_TYPES_CALLOUT_SAVE_AS_TEMPLATE =
'credentialRuleTypes-calloutSaveAsTemplate';
export const CREDENTIAL_RULE_TYPES_CALLOUT_UPDATE_PUBLISHER_ADMINS =
Expand Down
2 changes: 2 additions & 0 deletions src/common/enums/alkemio.error.status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export enum AlkemioErrorStatus {
NOT_ENABLED = 'NOT_ENABLED',
USER_NOT_REGISTERED = 'USER_NOT_REGISTERED',
LICENSE_NOT_FOUND = 'LICENSE_NOT_FOUND',
LICENSE_ENTITLEMENT_NOT_AVAILABLE = 'LICENSE_ENTITLEMENT_NOT_AVAILABLE',
LICENSE_ENTITLEMENT_NOT_SUPPORTED = 'LICENSE_ENTITLEMENT_NOT_SUPPORTED',
MATRIX_ENTITY_NOT_FOUND_ERROR = 'MATRIX_ENTITY_NOT_FOUND_ERROR',
BOOTSTRAP_FAILED = 'BOOTSTRAP_FAILED',
NOTIFICATION_PAYLOAD_BUILDER_ERROR = 'NOTIFICATION_PAYLOAD_BUILDER_ERROR',
Expand Down
1 change: 1 addition & 0 deletions src/common/enums/authorization.policy.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export enum AuthorizationPolicyType {
LIBRARY = 'library',
IN_MEMORY = 'in-memory',
LICENSING = 'licensing',
LICENSE = 'license',
LICENSE_POLICY = 'license-policy',
UNKNOWN = 'unknown',
AI_PERSONA_SERVICE = 'ai-persona-service',
Expand Down
1 change: 1 addition & 0 deletions src/common/enums/authorization.privilege.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum AuthorizationPrivilege {
GRANT = 'grant', // allow the issuing / revoking of credentials of the same type within a given scope
GRANT_GLOBAL_ADMINS = 'grant-global-admins',
AUTHORIZATION_RESET = 'authorization-reset',
LICENSE_RESET = 'license-reset',
PLATFORM_ADMIN = 'platform-admin', // To determine if the user should have access to the platform administration
CONTRIBUTE = 'contribute',
CREATE_CALLOUT = 'create-callout',
Expand Down
10 changes: 10 additions & 0 deletions src/common/enums/license.entitlement.data.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { registerEnumType } from '@nestjs/graphql';

export enum LicenseEntitlementDataType {
LIMIT = 'limit',
FLAG = 'flag',
}

registerEnumType(LicenseEntitlementDataType, {
name: 'LicenseEntitlementDataType',
});
20 changes: 20 additions & 0 deletions src/common/enums/license.entitlement.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { registerEnumType } from '@nestjs/graphql';

export enum LicenseEntitlementType {
ACCOUNT_SPACE_FREE = 'account-space-free',
ACCOUNT_SPACE_PLUS = 'account-space-plus',
ACCOUNT_SPACE_PREMIUM = 'account-space-premium',
ACCOUNT_VIRTUAL_CONTRIBUTOR = 'account-virtual-contributor',
ACCOUNT_INNOVATION_PACK = 'account-innovation-pack',
ACCOUNT_INNOVATION_HUB = 'account-innovation-hub',
SPACE_FREE = 'space-free',
SPACE_PLUS = 'space-plus',
SPACE_PREMIUM = 'space-premium',
SPACE_FLAG_SAVE_AS_TEMPLATE = 'space-flag-save-as-template',
SPACE_FLAG_VIRTUAL_CONTRIBUTOR_ACCESS = 'space-flag-virtual-contributor-access',
SPACE_FLAG_WHITEBOARD_MULTI_USER = 'space-flag-whiteboard-multi-user',
}

registerEnumType(LicenseEntitlementType, {
name: 'LicenseEntitlementType',
});
15 changes: 0 additions & 15 deletions src/common/enums/license.privilege.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/common/enums/license.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { registerEnumType } from '@nestjs/graphql';

export enum LicenseType {
ACCOUNT = 'account',
SPACE = 'space',
WHITEBOARD = 'whiteboard',
ROLESET = 'roleset',
COLLABORATION = 'collaboration',
}

registerEnumType(LicenseType, {
name: 'LicenseType',
});
4 changes: 2 additions & 2 deletions src/common/exceptions/forbidden.license.policy.exception.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AlkemioErrorStatus, LogContext } from '@common/enums';
import { BaseException } from './base.exception';
import { LicensePrivilege } from '@common/enums/license.privilege';
import { LicenseEntitlementType } from '@common/enums/license.entitlement.type';

export class ForbiddenLicensePolicyException extends BaseException {
constructor(
error: string,
public checkedPrivilege: LicensePrivilege,
public checkedEntitlement: LicenseEntitlementType,
public licensePolicyId: string,
public licenseId: string
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LogContext, AlkemioErrorStatus } from '@common/enums';
import { BaseException } from './base.exception';

export class LicenseEntitlementNotAvailableException extends BaseException {
constructor(error: string, context: LogContext, code?: AlkemioErrorStatus) {
super(
error,
context,
code ?? AlkemioErrorStatus.LICENSE_ENTITLEMENT_NOT_AVAILABLE
);
}
}
12 changes: 12 additions & 0 deletions src/common/exceptions/license.entitlement.not.supported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LogContext, AlkemioErrorStatus } from '@common/enums';
import { BaseException } from './base.exception';

export class LicenseEntitlementNotSupportedException extends BaseException {
constructor(error: string, context: LogContext, code?: AlkemioErrorStatus) {
super(
error,
context,
code ?? AlkemioErrorStatus.LICENSE_ENTITLEMENT_NOT_SUPPORTED
);
}
}
6 changes: 4 additions & 2 deletions src/core/bootstrap/bootstrap.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import { ContributorModule } from '@domain/community/contributor/contributor.mod
import { TemplatesSetModule } from '@domain/template/templates-set/templates.set.module';
import { TemplatesManagerModule } from '@domain/template/templates-manager/templates.manager.module';
import { TemplateDefaultModule } from '@domain/template/template-default/template.default.module';
import { LicensingModule } from '@platform/licensing/licensing.module';
import { LicenseModule } from '@domain/common/license/license.module';
import { LicensePlanModule } from '@platform/license-plan/license.plan.module';
import { LicensingFrameworkModule } from '@platform/licensing-framework/licensing.framework.module';

@Module({
imports: [
AiServerModule,
AgentModule,
AuthorizationPolicyModule,
LicenseModule,
ContributorModule,
SpaceModule,
OrganizationModule,
Expand All @@ -43,7 +45,7 @@ import { LicensePlanModule } from '@platform/license-plan/license.plan.module';
TemplatesSetModule,
TemplatesManagerModule,
TemplateDefaultModule,
LicensingModule,
LicensingFrameworkModule,
LicensePlanModule,
],
providers: [BootstrapService],
Expand Down
21 changes: 17 additions & 4 deletions src/core/bootstrap/bootstrap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ import { bootstrapSpaceCallouts } from './platform-template-definitions/space/bo
import { bootstrapSpaceTutorialsInnovationFlowStates } from './platform-template-definitions/space-tutorials/bootstrap.space.tutorials.innovation.flow.states';
import { bootstrapSpaceTutorialsCalloutGroups } from './platform-template-definitions/space-tutorials/bootstrap.space.tutorials.callout.groups';
import { bootstrapSpaceTutorialsCallouts } from './platform-template-definitions/space-tutorials/bootstrap.space.tutorials.callouts';
import { LicensingService } from '@platform/licensing/licensing.service';
import { LicenseService } from '@domain/common/license/license.service';
import { AccountLicenseService } from '@domain/space/account/account.service.license';
import { LicensePlanService } from '@platform/license-plan/license.plan.service';
import { LicensingFrameworkService } from '@platform/licensing-framework/licensing.framework.service';

@Injectable()
export class BootstrapService {
Expand Down Expand Up @@ -84,7 +86,9 @@ export class BootstrapService {
private templatesManagerService: TemplatesManagerService,
private templatesSetService: TemplatesSetService,
private templateDefaultService: TemplateDefaultService,
private licensingService: LicensingService,
private accountLicenseService: AccountLicenseService,
private licenseService: LicenseService,
private licensingFrameworkService: LicensingFrameworkService,
private licensePlanService: LicensePlanService
) {}

Expand Down Expand Up @@ -273,14 +277,15 @@ export class BootstrapService {

async createLicensePlans(licensePlansData: any[]) {
try {
const licensing = await this.licensingService.getDefaultLicensingOrFail();
const licensing =
await this.licensingFrameworkService.getDefaultLicensingOrFail();
for (const licensePlanData of licensePlansData) {
const planExists =
await this.licensePlanService.licensePlanByNameExists(
licensePlanData.name
);
if (!planExists) {
await this.licensingService.createLicensePlan({
await this.licensingFrameworkService.createLicensePlan({
...licensePlanData,
licensingID: licensing.id,
});
Expand Down Expand Up @@ -454,6 +459,10 @@ export class BootstrapService {
account
);
await this.authorizationPolicyService.saveAll(accountAuthorizations);

const accountEntitlements =
await this.accountLicenseService.applyLicensePolicy(account.id);
await this.licenseService.saveAll(accountEntitlements);
}
}

Expand Down Expand Up @@ -508,6 +517,10 @@ export class BootstrapService {
await this.spaceAuthorizationService.applyAuthorizationPolicy(space);
await this.authorizationPolicyService.saveAll(spaceAuthorizations);

const accountEntitlements =
await this.accountLicenseService.applyLicensePolicy(account.id);
await this.licenseService.saveAll(accountEntitlements);

return this.spaceService.getSpaceOrFail(space.id);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/dataloader/creators/loader.creators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './profile/profile.tagsets.loader.creator';
export * from './callout-framing/callout.framing.whiteboard.loader';

export * from './profile.loader.creator';
export * from './license.loader.creator';
export * from './preferences.loader.creator';
export * from './agent.loader.creator';
export * from './authorization.loader.creator';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { EntityManager } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { InjectEntityManager } from '@nestjs/typeorm';
import { DataLoaderInitError } from '@common/exceptions/data-loader';
import { createTypedRelationDataLoader } from '../../utils';
import { DataLoaderCreator, DataLoaderCreatorOptions } from '../base';
import { ILicense } from '@domain/common/license/license.interface';
import { License } from '@domain/common/license/license.entity';

@Injectable()
export class LicenseLoaderCreator implements DataLoaderCreator<ILicense> {
constructor(@InjectEntityManager() private manager: EntityManager) {}

create(
options?: DataLoaderCreatorOptions<
ILicense,
{ id: string; license?: License }
>
) {
if (!options?.parentClassRef) {
throw new DataLoaderInitError(
`${this.constructor.name} requires the 'parentClassRef' to be provided.`
);
}

return createTypedRelationDataLoader(
this.manager,
options.parentClassRef,
{
license: true,
},
this.constructor.name,
options
);
}
}
36 changes: 19 additions & 17 deletions src/core/license-engine/license.engine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
ForbiddenException,
} from '@common/exceptions';
import { LogContext } from '@common/enums';
import { LicensePrivilege } from '@common/enums/license.privilege';
import { ILicensePolicy } from '@platform/license-policy/license.policy.interface';
import { ForbiddenLicensePolicyException } from '@common/exceptions/forbidden.license.policy.exception';
import { EntityManager } from 'typeorm';
import { InjectEntityManager } from '@nestjs/typeorm';
import { LicensePolicy } from '@platform/license-policy';
import { IAgent, ICredential } from '@domain/agent';
import { ILicensePolicyCredentialRule } from './license.policy.rule.credential.interface';
import { LicenseEntitlementType } from '@common/enums/license.entitlement.type';

@Injectable()
export class LicenseEngineService {
Expand All @@ -23,31 +23,31 @@ export class LicenseEngineService {
private entityManager: EntityManager
) {}

public async grantAccessOrFail(
privilegeRequired: LicensePrivilege,
public async grantEntitlementOrFail(
entitlementRequired: LicenseEntitlementType,
agent: IAgent,
msg: string,
licensePolicy: ILicensePolicy | undefined
) {
const accessGranted = await this.isAccessGranted(
privilegeRequired,
const accessGranted = await this.isEntitlementGranted(
entitlementRequired,
agent,
licensePolicy
);
if (accessGranted) return true;

const errorMsg = `License.engine: unable to grant '${privilegeRequired}' privilege: ${msg} license: ${agent.id}`;
const errorMsg = `License.engine: unable to grant '${entitlementRequired}' privilege: ${msg} license: ${agent.id}`;
// If you get to here then no match was found
throw new ForbiddenLicensePolicyException(
errorMsg,
privilegeRequired,
entitlementRequired,
licensePolicy?.id || 'no license policy',
agent.id
);
}

public async isAccessGranted(
privilegeRequired: LicensePrivilege,
public async isEntitlementGranted(
entitlementRequired: LicenseEntitlementType,
agent: IAgent,
licensePolicy?: ILicensePolicy | undefined
): Promise<boolean> {
Expand All @@ -60,9 +60,11 @@ export class LicenseEngineService {
for (const credentialRule of credentialRules) {
for (const credential of credentials) {
if (credential.type === credentialRule.credentialType) {
if (credentialRule.grantedPrivileges.includes(privilegeRequired)) {
if (
credentialRule.grantedEntitlements.includes(entitlementRequired)
) {
this.logger.verbose?.(
`[CredentialRule] Granted privilege '${privilegeRequired}' using rule '${credentialRule.name}'`,
`[CredentialRule] Granted privilege '${entitlementRequired}' using rule '${credentialRule.name}'`,
LogContext.LICENSE
);
return true;
Expand Down Expand Up @@ -94,29 +96,29 @@ export class LicenseEngineService {
return policy;
}

public async getGrantedPrivileges(
public async getGrantedEntitlements(
agent: IAgent,
licensePolicy?: ILicensePolicy
) {
): Promise<LicenseEntitlementType[]> {
const policy = await this.getLicensePolicyOrFail(licensePolicy);
const credentials = await this.getCredentialsFromAgent(agent);

const grantedPrivileges: LicensePrivilege[] = [];
const grantedEntitlements: LicenseEntitlementType[] = [];

const credentialRules = this.convertCredentialRulesStr(
policy.credentialRulesStr
);
for (const rule of credentialRules) {
for (const credential of credentials) {
if (rule.credentialType === credential.type) {
for (const privilege of rule.grantedPrivileges) {
grantedPrivileges.push(privilege);
for (const entitlement of rule.grantedEntitlements) {
grantedEntitlements.push(entitlement);
}
}
}
}

const uniquePrivileges = grantedPrivileges.filter(
const uniquePrivileges = grantedEntitlements.filter(
(item, i, ar) => ar.indexOf(item) === i
);

Expand Down
Loading