This commit is contained in:
Philipinho
2026-03-09 00:51:14 +00:00
parent 78c3839ae7
commit ff01355ec3
19 changed files with 115 additions and 50 deletions
@@ -13,7 +13,6 @@ import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { AuthWorkspace } from '../../common/decorators/auth-workspace.decorator';
import { User, Workspace } from '@docmost/db/types/entity.types';
import { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.repo';
import { LicenseCheckService } from '../../integrations/environment/license-check.service';
@UseGuards(JwtAuthGuard)
@Controller('users')
@@ -21,7 +20,6 @@ export class UserController {
constructor(
private readonly userService: UserService,
private readonly workspaceRepo: WorkspaceRepo,
private readonly licenseCheckService: LicenseCheckService,
) {}
@HttpCode(HttpStatus.OK)
@@ -36,16 +34,9 @@ export class UserController {
const { licenseKey, ...rest } = workspace;
const features = this.licenseCheckService.resolveFeatures(
licenseKey,
rest.plan,
);
const workspaceInfo = {
...rest,
memberCount,
hasLicenseKey: Boolean(licenseKey),
features,
};
return { user: authUser, workspace: workspaceInfo };
@@ -32,8 +32,10 @@ import {
} from '../../casl/interfaces/workspace-ability.type';
import { FastifyReply } from 'fastify';
import { EnvironmentService } from '../../../integrations/environment/environment.service';
import { LicenseCheckService } from '../../../integrations/environment/license-check.service';
import { CheckHostnameDto } from '../dto/check-hostname.dto';
import { RemoveWorkspaceUserDto } from '../dto/remove-workspace-user.dto';
import { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.repo';
@UseGuards(JwtAuthGuard)
@Controller('workspace')
@@ -42,7 +44,9 @@ export class WorkspaceController {
private readonly workspaceService: WorkspaceService,
private readonly workspaceInvitationService: WorkspaceInvitationService,
private readonly workspaceAbility: WorkspaceAbilityFactory,
private readonly workspaceRepo: WorkspaceRepo,
private environmentService: EnvironmentService,
private licenseCheckService: LicenseCheckService,
) {}
@Public()
@@ -58,6 +62,23 @@ export class WorkspaceController {
return this.workspaceService.getWorkspaceInfo(workspace.id);
}
@HttpCode(HttpStatus.OK)
@Post('entitlements')
async getEntitlements(@AuthWorkspace() workspace: Workspace) {
let { licenseKey } = workspace;
const { plan } = workspace;
if (!licenseKey) {
licenseKey = await this.workspaceRepo.findLicenseKeyById(workspace.id);
}
return {
cloud: this.environmentService.isCloud(),
tier: this.licenseCheckService.resolveTier(licenseKey, plan),
features: this.licenseCheckService.resolveFeatures(licenseKey, plan),
};
}
@HttpCode(HttpStatus.OK)
@Post('update')
async updateWorkspace(
@@ -106,13 +106,9 @@ export class WorkspaceService {
throw new NotFoundException('Workspace not found');
}
const { licenseKey, ...rest } = workspace;
const { licenseKey, plan, ...rest } = workspace;
return {
...rest,
hasLicenseKey: Boolean(licenseKey),
features: this.licenseCheckService.resolveFeatures(licenseKey, rest.plan),
};
return rest;
}
async create(
@@ -337,6 +333,10 @@ export class WorkspaceService {
.where('id', '=', workspaceId)
.executeTakeFirst();
if (!ws) {
throw new NotFoundException('Workspace not found');
}
if (!this.licenseCheckService.hasFeature(ws.licenseKey, 'security:settings')) {
throw new ForbiddenException(
'This feature requires a valid license',
@@ -504,11 +504,7 @@ export class WorkspaceService {
}
const { licenseKey, ...rest } = workspace;
return {
...rest,
hasLicenseKey: Boolean(licenseKey),
features: this.licenseCheckService.resolveFeatures(licenseKey, rest.plan),
};
return rest;
}
async getWorkspaceUsers(
@@ -69,4 +69,25 @@ export class LicenseCheckService {
return this.getFeatures(licenseKey);
}
resolveTier(licenseKey: string, plan: string): string {
if (this.environmentService.isCloud()) {
return plan ?? 'standard';
}
return this.getLicenseType(licenseKey) ?? 'free';
}
private getLicenseType(licenseKey: string): string | null {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const LicenseModule = require('../../ee/licence/license.service');
const licenseService = this.moduleRef.get(LicenseModule.LicenseService, {
strict: false,
});
return licenseService.getLicenseType(licenseKey);
} catch {
return null;
}
}
}