Skip to content

Commit

Permalink
feat: add roles bridge (#29968)
Browse files Browse the repository at this point in the history
Co-authored-by: Douglas Gubert <[email protected]>
  • Loading branch information
Dnouv and d-gubert authored Aug 30, 2023
1 parent 69a5213 commit 1b42dfc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-glasses-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': minor
---

Added a new Roles bridge to RC Apps-Engine for reading and retrieving role details.
6 changes: 6 additions & 0 deletions apps/meteor/app/apps/server/bridges/bridges.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -144,4 +146,8 @@ export class RealAppBridges extends AppBridges {
getModerationBridge() {
return this._moderationBridge;
}

getRoleBridge() {
return this._roleBridge;
}
}
33 changes: 33 additions & 0 deletions apps/meteor/app/apps/server/bridges/roles.ts
Original file line number Diff line number Diff line change
@@ -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<IRole | null> {
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<Array<IRole>> {
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;
}
}
2 changes: 2 additions & 0 deletions apps/meteor/app/apps/server/converters/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,4 +17,5 @@ export {
AppDepartmentsConverter,
AppUploadsConverter,
AppVisitorsConverter,
AppRolesConverter,
};
29 changes: 29 additions & 0 deletions apps/meteor/app/apps/server/converters/roles.ts
Original file line number Diff line number Diff line change
@@ -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<AppsEngineRole | undefined> {
const role = await Roles.findOneById(roleId);

if (!role) {
return;
}
return this.convertRole(role);
}

async convertRole(role: IRole): Promise<AppsEngineRole> {
const map = {
id: '_id',
name: 'name',
description: 'description',
mandatory2fa: 'mandatory2fa',
protected: 'protected',
scope: 'scope',
};

return (await transformMappedData(role, map)) as unknown as AppsEngineRole;
}
}
2 changes: 2 additions & 0 deletions apps/meteor/ee/server/apps/orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:^",
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -7197,7 +7197,7 @@ __metadata:
vm2: ^3.9.19
peerDependencies:
"@rocket.chat/ui-kit": "*"
checksum: 14a7deb978460c5e615fa0103001ebee04b9db0823b4c8bbe1447ee94195233e8cfbb09ef42405249a978a2418f38ce2f55a546138735a69e2ecafc36a866ea7
checksum: 83ec73dac6e1f25722080cf32143ada914c4adb9aef8ebf770af13a5456cef1c81d1a9eedc4063549b8105277f2254d6c307c3c0c5f6ed7f99233a2d464bf6da
languageName: node
linkType: hard

Expand Down Expand Up @@ -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:^"
Expand Down

0 comments on commit 1b42dfc

Please sign in to comment.