Skip to content

Commit 8d2307c

Browse files
committed
refactor: migrate ldap endpoints to new chained API pattern
Migrates ldap.testConnection and ldap.testSearch POST endpoints from the legacy addRoute() pattern to the new chained .post() API pattern with typed AJV response schemas. Replaces Meteor check() with isLdapTestSearch AJV validator for request body validation on ldap.testSearch. Part of #38876
1 parent 3145c41 commit 8d2307c

2 files changed

Lines changed: 78 additions & 45 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rocket.chat/meteor': patch
3+
---
4+
5+
Migrated `ldap.testConnection` and `ldap.testSearch` REST API endpoints from legacy `addRoute` pattern to the new chained `.post()` API pattern with typed response schemas and AJV body validation (replacing Meteor `check()`).
Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,90 @@
11
import { LDAP } from '@rocket.chat/core-services';
2-
import { Match, check } from 'meteor/check';
2+
import { ajv, validateUnauthorizedErrorResponse, validateForbiddenErrorResponse } from '@rocket.chat/rest-typings';
33

44
import { SystemLogger } from '../../../../server/lib/logger/system';
55
import { settings } from '../../../settings/server';
66
import { API } from '../api';
77

8-
API.v1.addRoute(
8+
const messageResponseSchema = {
9+
type: 'object' as const,
10+
properties: {
11+
message: { type: 'string' as const },
12+
success: {
13+
type: 'boolean' as const,
14+
enum: [true] as const,
15+
},
16+
},
17+
required: ['message', 'success'] as const,
18+
additionalProperties: false,
19+
};
20+
21+
const isLdapTestSearch = ajv.compile<{ username: string }>({
22+
type: 'object',
23+
properties: {
24+
username: { type: 'string' },
25+
},
26+
required: ['username'],
27+
additionalProperties: false,
28+
});
29+
30+
API.v1.post(
931
'ldap.testConnection',
10-
{ authRequired: true, permissionsRequired: ['test-admin-options'] },
1132
{
12-
async post() {
13-
if (!this.userId) {
14-
throw new Error('error-invalid-user');
15-
}
16-
17-
if (settings.get<boolean>('LDAP_Enable') !== true) {
18-
throw new Error('LDAP_disabled');
19-
}
20-
21-
try {
22-
await LDAP.testConnection();
23-
} catch (err) {
24-
SystemLogger.error({ err });
25-
throw new Error('Connection_failed');
26-
}
27-
28-
return API.v1.success({
29-
message: 'LDAP_Connection_successful' as const,
30-
});
33+
authRequired: true,
34+
permissionsRequired: ['test-admin-options'],
35+
response: {
36+
200: ajv.compile<{ message: string }>(messageResponseSchema),
37+
401: validateUnauthorizedErrorResponse,
38+
403: validateForbiddenErrorResponse,
3139
},
3240
},
41+
async function action() {
42+
if (!this.userId) {
43+
throw new Error('error-invalid-user');
44+
}
45+
46+
if (settings.get<boolean>('LDAP_Enable') !== true) {
47+
throw new Error('LDAP_disabled');
48+
}
49+
50+
try {
51+
await LDAP.testConnection();
52+
} catch (err) {
53+
SystemLogger.error({ err });
54+
throw new Error('Connection_failed');
55+
}
56+
57+
return API.v1.success({
58+
message: 'LDAP_Connection_successful' as const,
59+
});
60+
},
3361
);
3462

35-
API.v1.addRoute(
63+
API.v1.post(
3664
'ldap.testSearch',
37-
{ authRequired: true, permissionsRequired: ['test-admin-options'] },
3865
{
39-
async post() {
40-
check(
41-
this.bodyParams,
42-
Match.ObjectIncluding({
43-
username: String,
44-
}),
45-
);
46-
47-
if (!this.userId) {
48-
throw new Error('error-invalid-user');
49-
}
50-
51-
if (settings.get('LDAP_Enable') !== true) {
52-
throw new Error('LDAP_disabled');
53-
}
54-
55-
await LDAP.testSearch(this.bodyParams.username);
56-
57-
return API.v1.success({
58-
message: 'LDAP_User_Found' as const,
59-
});
66+
authRequired: true,
67+
permissionsRequired: ['test-admin-options'],
68+
validateParams: isLdapTestSearch,
69+
response: {
70+
200: ajv.compile<{ message: string }>(messageResponseSchema),
71+
401: validateUnauthorizedErrorResponse,
72+
403: validateForbiddenErrorResponse,
6073
},
6174
},
75+
async function action() {
76+
if (!this.userId) {
77+
throw new Error('error-invalid-user');
78+
}
79+
80+
if (settings.get('LDAP_Enable') !== true) {
81+
throw new Error('LDAP_disabled');
82+
}
83+
84+
await LDAP.testSearch(this.bodyParams.username);
85+
86+
return API.v1.success({
87+
message: 'LDAP_User_Found' as const,
88+
});
89+
},
6290
);

0 commit comments

Comments
 (0)