2025-02-25 06:52:50 +00:00
|
|
|
import { MigrationProgress } from '@helpers/migration.helper';
|
|
|
|
|
import { NestFactory } from '@nestjs/core';
|
|
|
|
|
import { AppsGroupPermissions } from '@entities/apps_group_permissions.entity';
|
|
|
|
|
import { GranularPermissions } from '@entities/granular_permissions.entity';
|
|
|
|
|
import { GroupPermissions } from '@entities/group_permissions.entity';
|
|
|
|
|
import { Organization } from '@entities/organization.entity';
|
|
|
|
|
import { UserGroupPermission } from '@entities/user_group_permission.entity';
|
|
|
|
|
import { EntityManager, MigrationInterface, QueryRunner } from 'typeorm';
|
2024-07-10 08:18:26 +00:00
|
|
|
import {
|
2025-02-25 06:52:50 +00:00
|
|
|
CreateResourcePermissionObjectGeneric,
|
|
|
|
|
DEFAULT_GROUP_PERMISSIONS_MIGRATIONS,
|
|
|
|
|
} from 'src/migration-helpers/constants';
|
2024-07-10 08:18:26 +00:00
|
|
|
import {
|
|
|
|
|
USER_ROLE,
|
2024-09-23 11:34:20 +00:00
|
|
|
DEFAULT_GROUP_PERMISSIONS,
|
2025-02-25 06:52:50 +00:00
|
|
|
ResourceType,
|
|
|
|
|
DEFAULT_RESOURCE_PERMISSIONS,
|
|
|
|
|
} from '@modules/group-permissions/constants';
|
|
|
|
|
import { DEFAULT_GRANULAR_PERMISSIONS_NAME } from '@modules/group-permissions/constants/granular_permissions';
|
|
|
|
|
import { CreateGranularPermissionDto } from '@modules/group-permissions/dto/granular-permissions';
|
2024-07-10 08:18:26 +00:00
|
|
|
import {
|
|
|
|
|
CreateResourcePermissionObject,
|
|
|
|
|
ResourcePermissionMetaData,
|
2025-02-25 06:52:50 +00:00
|
|
|
} from '@modules/group-permissions/types/granular_permissions';
|
|
|
|
|
import { AppModule } from '@modules/app/module';
|
|
|
|
|
import { LicenseInitService } from '@modules/licensing/interfaces/IService';
|
2025-02-25 12:28:43 +00:00
|
|
|
import { TOOLJET_EDITIONS } from '@modules/app/constants';
|
|
|
|
|
import { getTooljetEdition } from '@helpers/utils.helper';
|
2024-07-10 08:18:26 +00:00
|
|
|
|
|
|
|
|
export class CreateDefaultGroupInExistingWorkspace1720352990850 implements MigrationInterface {
|
|
|
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
2025-07-02 05:27:36 +00:00
|
|
|
const edition = getTooljetEdition() as TOOLJET_EDITIONS;
|
|
|
|
|
if (edition === TOOLJET_EDITIONS.Cloud) {
|
|
|
|
|
console.log('Migration is only restricted for non cloud edition.');
|
|
|
|
|
return; // Exit the migration early
|
|
|
|
|
}
|
2024-07-10 08:18:26 +00:00
|
|
|
const manager = queryRunner.manager;
|
2024-09-23 11:34:20 +00:00
|
|
|
|
2024-07-10 08:18:26 +00:00
|
|
|
const organizationIds = (
|
|
|
|
|
await manager.find(Organization, {
|
|
|
|
|
select: ['id'],
|
|
|
|
|
})
|
|
|
|
|
).map((organization) => organization.id);
|
|
|
|
|
|
2025-07-02 05:27:36 +00:00
|
|
|
if (organizationIds?.length === 0) {
|
|
|
|
|
console.log('No organizations found, skipping migration.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nestApp = await NestFactory.createApplicationContext(await AppModule.register({ IS_GET_CONTEXT: true }));
|
|
|
|
|
|
|
|
|
|
const licenseService = nestApp.get<LicenseInitService>(LicenseInitService);
|
|
|
|
|
const licenseValid =
|
|
|
|
|
getTooljetEdition() === TOOLJET_EDITIONS.CE ? true : await licenseService.initForMigration(manager);
|
|
|
|
|
|
2024-09-23 11:34:20 +00:00
|
|
|
const migrationProgress = new MigrationProgress(
|
|
|
|
|
'CreateDefaultGroupInExistingWorkspace1720352990850',
|
|
|
|
|
organizationIds.length
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-10 08:18:26 +00:00
|
|
|
for (const organizationId of organizationIds) {
|
|
|
|
|
for (const defaultGroup of Object.keys(USER_ROLE)) {
|
2024-09-23 11:34:20 +00:00
|
|
|
const groupPermissions = licenseValid
|
|
|
|
|
? DEFAULT_GROUP_PERMISSIONS_MIGRATIONS[defaultGroup]
|
|
|
|
|
: DEFAULT_GROUP_PERMISSIONS[defaultGroup];
|
2024-07-10 08:18:26 +00:00
|
|
|
const query = `
|
|
|
|
|
INSERT INTO permission_groups (
|
|
|
|
|
organization_id,
|
|
|
|
|
name,
|
|
|
|
|
type,
|
|
|
|
|
app_create,
|
|
|
|
|
app_delete,
|
|
|
|
|
folder_crud,
|
|
|
|
|
org_constant_crud,
|
|
|
|
|
data_source_create,
|
|
|
|
|
data_source_delete
|
|
|
|
|
) VALUES (
|
|
|
|
|
'${organizationId}',
|
|
|
|
|
'${groupPermissions.name}',
|
|
|
|
|
'${groupPermissions.type}',
|
|
|
|
|
${groupPermissions.appCreate},
|
|
|
|
|
${groupPermissions.appDelete},
|
|
|
|
|
${groupPermissions.folderCRUD},
|
|
|
|
|
${groupPermissions.orgConstantCRUD},
|
2025-02-25 06:52:50 +00:00
|
|
|
${groupPermissions.dataSourceCreate},
|
|
|
|
|
${groupPermissions.dataSourceDelete}
|
2024-07-10 08:18:26 +00:00
|
|
|
) RETURNING *;
|
|
|
|
|
`;
|
|
|
|
|
const group: GroupPermissions = (await manager.query(query))[0];
|
2025-02-25 06:52:50 +00:00
|
|
|
const groupGranularPermissions: Record<
|
|
|
|
|
ResourceType,
|
|
|
|
|
CreateResourcePermissionObject<any>
|
|
|
|
|
> = DEFAULT_RESOURCE_PERMISSIONS[group.name];
|
2024-07-10 08:18:26 +00:00
|
|
|
|
|
|
|
|
for (const resource of Object.keys(groupGranularPermissions)) {
|
2024-09-23 11:34:20 +00:00
|
|
|
const dtoObject: CreateGranularPermissionDto = {
|
2024-07-10 08:18:26 +00:00
|
|
|
name: DEFAULT_GRANULAR_PERMISSIONS_NAME[resource],
|
|
|
|
|
groupId: group.id,
|
|
|
|
|
type: resource as ResourceType,
|
|
|
|
|
isAll: true,
|
2025-02-25 06:52:50 +00:00
|
|
|
createResourcePermissionObject: {},
|
2024-07-10 08:18:26 +00:00
|
|
|
};
|
|
|
|
|
if (group.name === USER_ROLE.ADMIN) {
|
2025-02-25 06:52:50 +00:00
|
|
|
const createResourcePermissionObj: CreateResourcePermissionObjectGeneric =
|
|
|
|
|
groupGranularPermissions[resource];
|
2024-09-23 11:34:20 +00:00
|
|
|
|
2024-07-10 08:18:26 +00:00
|
|
|
const granularPermissions = await this.createGranularPermission(manager, dtoObject);
|
2024-09-23 11:34:20 +00:00
|
|
|
if (resource === ResourceType.APP) {
|
|
|
|
|
await this.createAppsResourcePermission(
|
|
|
|
|
manager,
|
|
|
|
|
{ granularPermissions, organizationId },
|
2025-02-25 06:52:50 +00:00
|
|
|
createResourcePermissionObj as CreateResourcePermissionObject<ResourceType.APP>
|
|
|
|
|
);
|
|
|
|
|
} else if (resource === ResourceType.DATA_SOURCE) {
|
|
|
|
|
await this.createDataSourceResourcePermission(
|
|
|
|
|
manager,
|
|
|
|
|
{ granularPermissions, organizationId },
|
|
|
|
|
createResourcePermissionObj as CreateResourcePermissionObject<ResourceType.DATA_SOURCE>
|
2024-09-23 11:34:20 +00:00
|
|
|
);
|
|
|
|
|
}
|
2024-07-10 08:18:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Migrating Admins to new Admins
|
|
|
|
|
if (group.name === USER_ROLE.ADMIN) {
|
|
|
|
|
const adminsUsers = await manager
|
|
|
|
|
.createQueryBuilder(UserGroupPermission, 'usersGroup')
|
|
|
|
|
.innerJoin(
|
|
|
|
|
'usersGroup.groupPermission',
|
|
|
|
|
'groupPermission',
|
|
|
|
|
'groupPermission.organizationId = :organizationId',
|
|
|
|
|
{
|
|
|
|
|
organizationId,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.where('groupPermission.group = :admin', {
|
|
|
|
|
admin: 'admin',
|
|
|
|
|
})
|
|
|
|
|
.getMany();
|
2024-11-08 10:46:06 +00:00
|
|
|
const uniqueUserIds = new Set(adminsUsers.map((userGroup) => userGroup.userId));
|
|
|
|
|
if (uniqueUserIds.size === 0) continue;
|
|
|
|
|
const userIds = [...uniqueUserIds];
|
2024-07-10 08:18:26 +00:00
|
|
|
await this.migrateUserGroup(manager, userIds, group.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-23 11:34:20 +00:00
|
|
|
migrationProgress.show();
|
2024-07-10 08:18:26 +00:00
|
|
|
}
|
2025-02-25 06:52:50 +00:00
|
|
|
await nestApp.close();
|
2024-07-10 08:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createGranularPermission(
|
|
|
|
|
manager: EntityManager,
|
|
|
|
|
createObject: CreateGranularPermissionDto
|
|
|
|
|
): Promise<GranularPermissions> {
|
|
|
|
|
const query = `
|
|
|
|
|
INSERT INTO granular_permissions (
|
|
|
|
|
group_id,
|
|
|
|
|
name,
|
|
|
|
|
type,
|
|
|
|
|
is_all
|
|
|
|
|
) VALUES (
|
|
|
|
|
'${createObject.groupId}', '${createObject.name}', '${createObject.type}', ${createObject.isAll}
|
|
|
|
|
) RETURNING *;
|
|
|
|
|
`;
|
|
|
|
|
return (await manager.query(query))[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createAppsResourcePermission(
|
|
|
|
|
manager: EntityManager,
|
|
|
|
|
createMeta: ResourcePermissionMetaData,
|
2025-02-25 06:52:50 +00:00
|
|
|
createObject: CreateResourcePermissionObject<ResourceType.APP>
|
2024-07-10 08:18:26 +00:00
|
|
|
): Promise<AppsGroupPermissions> {
|
|
|
|
|
const { granularPermissions } = createMeta;
|
|
|
|
|
const query = `
|
|
|
|
|
INSERT INTO apps_group_permissions (
|
|
|
|
|
granular_permission_id,
|
|
|
|
|
can_edit,
|
|
|
|
|
can_view,
|
|
|
|
|
hide_from_dashboard
|
|
|
|
|
) VALUES (
|
|
|
|
|
'${granularPermissions.id}', ${createObject.canEdit}, ${createObject.canView}, ${createObject.hideFromDashboard}
|
|
|
|
|
) RETURNING *;
|
|
|
|
|
`;
|
|
|
|
|
return (await manager.query(query))[0];
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 06:52:50 +00:00
|
|
|
async createDataSourceResourcePermission(
|
|
|
|
|
manager: EntityManager,
|
|
|
|
|
createMeta: ResourcePermissionMetaData,
|
|
|
|
|
createObject: CreateResourcePermissionObject<ResourceType.DATA_SOURCE>
|
|
|
|
|
): Promise<AppsGroupPermissions> {
|
|
|
|
|
const { granularPermissions } = createMeta;
|
|
|
|
|
const query = `
|
|
|
|
|
INSERT INTO data_sources_group_permissions (
|
|
|
|
|
granular_permission_id,
|
|
|
|
|
can_configure,
|
|
|
|
|
can_use
|
|
|
|
|
) VALUES (
|
|
|
|
|
'${granularPermissions.id}', ${createObject?.action?.canConfigure || false}, ${
|
2025-07-02 05:27:36 +00:00
|
|
|
createObject?.action?.canUse || false
|
|
|
|
|
}
|
2025-02-25 06:52:50 +00:00
|
|
|
) RETURNING *;
|
|
|
|
|
`;
|
|
|
|
|
return (await manager.query(query))[0];
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 08:18:26 +00:00
|
|
|
async migrateUserGroup(manager: EntityManager, userIds: string[], groupId: string) {
|
|
|
|
|
if (userIds.length == 0) return;
|
|
|
|
|
const valuesString = userIds.map((id) => `('${id}', '${groupId}')`).join(',');
|
|
|
|
|
const query = `
|
|
|
|
|
INSERT INTO group_users (user_id, group_id)
|
|
|
|
|
VALUES ${valuesString};
|
|
|
|
|
`;
|
|
|
|
|
return await manager.query(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {}
|
|
|
|
|
}
|