From 1b42dfc6c1b1fbc7194f68de2bc06b0e3ccebf87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=87=E3=83=B4=E3=81=81=E3=82=93=E3=81=99?= <61188295+Dnouv@users.noreply.github.com> Date: Thu, 31 Aug 2023 01:35:23 +0530 Subject: [PATCH] feat: add roles bridge (#29968) Co-authored-by: Douglas Gubert <1810309+d-gubert@users.noreply.github.com> --- .changeset/fuzzy-glasses-divide.md | 5 +++ .../meteor/app/apps/server/bridges/bridges.js | 6 ++++ apps/meteor/app/apps/server/bridges/roles.ts | 33 +++++++++++++++++++ .../app/apps/server/converters/index.ts | 2 ++ .../app/apps/server/converters/roles.ts | 29 ++++++++++++++++ apps/meteor/ee/server/apps/orchestrator.js | 2 ++ apps/meteor/package.json | 2 +- yarn.lock | 10 +++--- 8 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 .changeset/fuzzy-glasses-divide.md create mode 100644 apps/meteor/app/apps/server/bridges/roles.ts create mode 100644 apps/meteor/app/apps/server/converters/roles.ts diff --git a/.changeset/fuzzy-glasses-divide.md b/.changeset/fuzzy-glasses-divide.md new file mode 100644 index 000000000000..cf77bbde5507 --- /dev/null +++ b/.changeset/fuzzy-glasses-divide.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': minor +--- + +Added a new Roles bridge to RC Apps-Engine for reading and retrieving role details. diff --git a/apps/meteor/app/apps/server/bridges/bridges.js b/apps/meteor/app/apps/server/bridges/bridges.js index 6c67c116ba01..6d52a1d8e56d 100644 --- a/apps/meteor/app/apps/server/bridges/bridges.js +++ b/apps/meteor/app/apps/server/bridges/bridges.js @@ -15,6 +15,7 @@ import { AppMessageBridge } from './messages'; import { AppModerationBridge } from './moderation'; import { AppOAuthAppsBridge } from './oauthApps'; import { AppPersistenceBridge } from './persistence'; +import { AppRoleBridge } from './roles'; import { AppRoomBridge } from './rooms'; import { AppSchedulerBridge } from './scheduler'; import { AppSettingBridge } from './settings'; @@ -51,6 +52,7 @@ export class RealAppBridges extends AppBridges { this._internalFedBridge = new AppInternalFederationBridge(); this._moderationBridge = new AppModerationBridge(orch); this._threadBridge = new AppThreadBridge(orch); + this._roleBridge = new AppRoleBridge(orch); } getCommandBridge() { @@ -144,4 +146,8 @@ export class RealAppBridges extends AppBridges { getModerationBridge() { return this._moderationBridge; } + + getRoleBridge() { + return this._roleBridge; + } } diff --git a/apps/meteor/app/apps/server/bridges/roles.ts b/apps/meteor/app/apps/server/bridges/roles.ts new file mode 100644 index 000000000000..f973b7f49ed2 --- /dev/null +++ b/apps/meteor/app/apps/server/bridges/roles.ts @@ -0,0 +1,33 @@ +import type { IRole } from '@rocket.chat/apps-engine/definition/roles'; +import { RoleBridge } from '@rocket.chat/apps-engine/server/bridges'; +import { Roles } from '@rocket.chat/models'; + +import type { AppServerOrchestrator } from '../../../../ee/server/apps/orchestrator'; + +export class AppRoleBridge extends RoleBridge { + constructor(private readonly orch: AppServerOrchestrator) { + super(); + } + + protected async getOneByIdOrName(idOrName: IRole['id'] | IRole['name'], appId: string): Promise { + this.orch.debugLog(`The App ${appId} is getting the roleByIdOrName: "${idOrName}"`); + + const role = await Roles.findOneByIdOrName(idOrName); + return this.orch.getConverters()?.get('roles').convertRole(role); + } + + protected async getCustomRoles(appId: string): Promise> { + this.orch.debugLog(`The App ${appId} is getting the custom roles`); + + const cursor = Roles.findCustomRoles(); + + const roles: IRole[] = []; + + for await (const role of cursor) { + const convRole = await this.orch.getConverters()?.get('roles').convertRole(role); + roles.push(convRole); + } + + return roles; + } +} diff --git a/apps/meteor/app/apps/server/converters/index.ts b/apps/meteor/app/apps/server/converters/index.ts index 39a6a260f69f..96716af03ca7 100644 --- a/apps/meteor/app/apps/server/converters/index.ts +++ b/apps/meteor/app/apps/server/converters/index.ts @@ -1,5 +1,6 @@ import { AppDepartmentsConverter } from './departments'; import { AppMessagesConverter } from './messages'; +import { AppRolesConverter } from './roles'; import { AppRoomsConverter } from './rooms'; import { AppSettingsConverter } from './settings'; import { AppUploadsConverter } from './uploads'; @@ -16,4 +17,5 @@ export { AppDepartmentsConverter, AppUploadsConverter, AppVisitorsConverter, + AppRolesConverter, }; diff --git a/apps/meteor/app/apps/server/converters/roles.ts b/apps/meteor/app/apps/server/converters/roles.ts new file mode 100644 index 000000000000..4ac1f3956420 --- /dev/null +++ b/apps/meteor/app/apps/server/converters/roles.ts @@ -0,0 +1,29 @@ +import type { IRole as AppsEngineRole } from '@rocket.chat/apps-engine/definition/roles'; +import type { IRole } from '@rocket.chat/core-typings'; +import { Roles } from '@rocket.chat/models'; + +import { transformMappedData } from '../../../../ee/lib/misc/transformMappedData'; + +export class AppRolesConverter { + async convertById(roleId: string): Promise { + const role = await Roles.findOneById(roleId); + + if (!role) { + return; + } + return this.convertRole(role); + } + + async convertRole(role: IRole): Promise { + const map = { + id: '_id', + name: 'name', + description: 'description', + mandatory2fa: 'mandatory2fa', + protected: 'protected', + scope: 'scope', + }; + + return (await transformMappedData(role, map)) as unknown as AppsEngineRole; + } +} diff --git a/apps/meteor/ee/server/apps/orchestrator.js b/apps/meteor/ee/server/apps/orchestrator.js index 4ce5de7d7766..c21508cbc626 100644 --- a/apps/meteor/ee/server/apps/orchestrator.js +++ b/apps/meteor/ee/server/apps/orchestrator.js @@ -15,6 +15,7 @@ import { AppDepartmentsConverter, AppUploadsConverter, AppVisitorsConverter, + AppRolesConverter, } from '../../../app/apps/server/converters'; import { AppThreadsConverter } from '../../../app/apps/server/converters/threads'; import { settings, settingsRegistry } from '../../../app/settings/server'; @@ -66,6 +67,7 @@ export class AppServerOrchestrator { this._converters.set('uploads', new AppUploadsConverter(this)); this._converters.set('videoConferences', new AppVideoConferencesConverter()); this._converters.set('threads', new AppThreadsConverter(this)); + this._converters.set('roles', new AppRolesConverter(this)); this._bridges = new RealAppBridges(this); diff --git a/apps/meteor/package.json b/apps/meteor/package.json index dd1af33f6b5c..0e3167a0886c 100644 --- a/apps/meteor/package.json +++ b/apps/meteor/package.json @@ -225,7 +225,7 @@ "@rocket.chat/account-utils": "workspace:^", "@rocket.chat/agenda": "workspace:^", "@rocket.chat/api-client": "workspace:^", - "@rocket.chat/apps-engine": "1.41.0-alpha.295", + "@rocket.chat/apps-engine": "1.41.0-alpha.305", "@rocket.chat/base64": "workspace:^", "@rocket.chat/cas-validate": "workspace:^", "@rocket.chat/core-services": "workspace:^", diff --git a/yarn.lock b/yarn.lock index aa54f641b42b..2cb0275d9a31 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7183,9 +7183,9 @@ __metadata: languageName: node linkType: hard -"@rocket.chat/apps-engine@npm:1.41.0-alpha.295": - version: 1.41.0-alpha.295 - resolution: "@rocket.chat/apps-engine@npm:1.41.0-alpha.295" +"@rocket.chat/apps-engine@npm:1.41.0-alpha.305": + version: 1.41.0-alpha.305 + resolution: "@rocket.chat/apps-engine@npm:1.41.0-alpha.305" dependencies: adm-zip: ^0.5.9 cryptiles: ^4.1.3 @@ -7197,7 +7197,7 @@ __metadata: vm2: ^3.9.19 peerDependencies: "@rocket.chat/ui-kit": "*" - checksum: 14a7deb978460c5e615fa0103001ebee04b9db0823b4c8bbe1447ee94195233e8cfbb09ef42405249a978a2418f38ce2f55a546138735a69e2ecafc36a866ea7 + checksum: 83ec73dac6e1f25722080cf32143ada914c4adb9aef8ebf770af13a5456cef1c81d1a9eedc4063549b8105277f2254d6c307c3c0c5f6ed7f99233a2d464bf6da languageName: node linkType: hard @@ -7988,7 +7988,7 @@ __metadata: "@rocket.chat/account-utils": "workspace:^" "@rocket.chat/agenda": "workspace:^" "@rocket.chat/api-client": "workspace:^" - "@rocket.chat/apps-engine": 1.41.0-alpha.295 + "@rocket.chat/apps-engine": 1.41.0-alpha.305 "@rocket.chat/base64": "workspace:^" "@rocket.chat/cas-validate": "workspace:^" "@rocket.chat/core-services": "workspace:^"