Files
upstream-rocketchat/apps/meteor/server/api/v1/permissions.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

254 lines
6.7 KiB
TypeScript

import type { IPermission } from '@rocket.chat/core-typings';
import { Permissions, Roles } from '@rocket.chat/models';
import {
ajv,
ajvQuery,
validateUnauthorizedErrorResponse,
validateBadRequestErrorResponse,
validateForbiddenErrorResponse,
} from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';
import { addPermissionToRoleMethod, removeRoleFromPermissionMethod } from '../../lib/authorization/permissionRole';
import { permissionsGetMethod } from '../../lib/authorization/streamer/permissions';
import { notifyOnPermissionChangedById } from '../../lib/notifyListener';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';
type PermissionsListAllProps = {
updatedSince?: string;
};
type PermissionsUpdateProps = {
permissions: { _id: string; roles: string[] }[];
};
const permissionListAllSchema = {
type: 'object',
properties: {
updatedSince: {
type: 'string',
nullable: true,
},
},
required: [],
additionalProperties: false,
};
const permissionUpdatePropsSchema = {
type: 'object',
properties: {
permissions: {
type: 'array',
items: {
type: 'object',
properties: {
_id: { type: 'string' },
roles: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
},
},
additionalProperties: false,
required: ['_id', 'roles'],
},
},
},
required: ['permissions'],
additionalProperties: false,
};
const isPermissionsListAll = ajvQuery.compile<PermissionsListAllProps>(permissionListAllSchema);
const isBodyParamsValidPermissionUpdate = ajv.compile<PermissionsUpdateProps>(permissionUpdatePropsSchema);
type PermissionRolePayload = { permissionId: string; role: string };
const isPermissionRolePayload = ajv.compile<PermissionRolePayload>({
type: 'object',
properties: {
permissionId: { type: 'string', minLength: 1 },
role: { type: 'string', minLength: 1 },
},
required: ['permissionId', 'role'],
additionalProperties: false,
});
const voidPermissionResponse = ajv.compile<void>({
type: 'object',
properties: { success: { type: 'boolean', enum: [true] } },
required: ['success'],
additionalProperties: false,
});
const permissionsEndpoints = API.v1
.get(
'permissions.listAll',
{
authRequired: true,
query: isPermissionsListAll,
response: {
200: ajv.compile<{
update: IPermission[];
remove: IPermission[];
}>({
type: 'object',
properties: {
update: {
type: 'array',
items: { $ref: '#/components/schemas/IPermission' },
},
remove: {
type: 'array',
items: { $ref: '#/components/schemas/IPermission' },
},
success: {
type: 'boolean',
enum: [true],
description: 'Indicates if the request was successful.',
},
},
required: ['update', 'remove', 'success'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { updatedSince } = this.queryParams;
let updatedSinceDate: Date | undefined;
if (updatedSince) {
if (isNaN(Date.parse(updatedSince))) {
throw new Meteor.Error('error-roomId-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
}
updatedSinceDate = new Date(updatedSince);
}
const result = (await permissionsGetMethod(updatedSinceDate)) as {
update: IPermission[];
remove: IPermission[];
};
if (Array.isArray(result)) {
return API.v1.success({
update: result,
remove: [],
});
}
return API.v1.success(result);
},
)
.post(
'permissions.update',
{
authRequired: true,
permissionsRequired: ['access-permissions'],
body: isBodyParamsValidPermissionUpdate,
response: {
200: ajv.compile<{
permissions: IPermission[];
}>({
type: 'object',
properties: {
permissions: {
type: 'array',
items: { $ref: '#/components/schemas/IPermission' },
},
success: {
type: 'boolean',
enum: [true],
description: 'Indicates if the request was successful.',
},
},
required: ['permissions', 'success'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
},
},
async function action() {
const { bodyParams } = this;
if (!isBodyParamsValidPermissionUpdate(bodyParams)) {
return API.v1.failure('Invalid body params', 'error-invalid-body-params');
}
const permissionKeys = bodyParams.permissions.map(({ _id }) => _id);
const permissionsCount = await Permissions.countDocuments({ _id: { $in: permissionKeys } });
if (permissionsCount !== bodyParams.permissions.length) {
return API.v1.failure('Invalid permission', 'error-invalid-permission');
}
const roleKeys = [...new Set(bodyParams.permissions.flatMap((p) => p.roles))];
const rolesCount = await Roles.countDocuments({ _id: { $in: roleKeys } });
if (rolesCount !== roleKeys.length) {
return API.v1.failure('Invalid role', 'error-invalid-role');
}
for (const permission of bodyParams.permissions) {
await Permissions.setRoles(permission._id, permission.roles);
void notifyOnPermissionChangedById(permission._id);
}
const result = (await permissionsGetMethod()) as IPermission[];
return API.v1.success({
permissions: result,
});
},
)
.post(
'permissions.addRole',
{
authRequired: true,
permissionsRequired: ['access-permissions'],
body: isPermissionRolePayload,
response: {
200: voidPermissionResponse,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
},
},
async function action() {
const { permissionId, role } = this.bodyParams;
await addPermissionToRoleMethod(this.userId, permissionId, role);
return API.v1.success();
},
)
.post(
'permissions.removeRole',
{
authRequired: true,
permissionsRequired: ['access-permissions'],
body: isPermissionRolePayload,
response: {
200: voidPermissionResponse,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
},
},
async function action() {
const { permissionId, role } = this.bodyParams;
await removeRoleFromPermissionMethod(this.userId, permissionId, role);
return API.v1.success();
},
);
export type PermissionsEndpoints = ExtractRoutesFromAPI<typeof permissionsEndpoints>;
declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends PermissionsEndpoints {}
}