Skip to content

Commit 3f8168f

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 3f8168f

File tree

2 files changed

+74
-45
lines changed

2 files changed

+74
-45
lines changed
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: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,86 @@
11
import { LDAP } from '@rocket.chat/core-services';
2-
import { Match, check } from 'meteor/check';
2+
import {
3+
ajv,
4+
isLdapTestSearch,
5+
validateUnauthorizedErrorResponse,
6+
validateForbiddenErrorResponse,
7+
} from '@rocket.chat/rest-typings';
38

49
import { SystemLogger } from '../../../../server/lib/logger/system';
510
import { settings } from '../../../settings/server';
611
import { API } from '../api';
712

8-
API.v1.addRoute(
13+
const messageResponseSchema = {
14+
type: 'object' as const,
15+
properties: {
16+
message: { type: 'string' as const },
17+
success: {
18+
type: 'boolean' as const,
19+
enum: [true] as const,
20+
},
21+
},
22+
required: ['message', 'success'] as const,
23+
additionalProperties: false,
24+
};
25+
26+
API.v1.post(
927
'ldap.testConnection',
10-
{ authRequired: true, permissionsRequired: ['test-admin-options'] },
1128
{
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-
});
29+
authRequired: true,
30+
permissionsRequired: ['test-admin-options'],
31+
response: {
32+
200: ajv.compile<{ message: string }>(messageResponseSchema),
33+
401: validateUnauthorizedErrorResponse,
34+
403: validateForbiddenErrorResponse,
3135
},
3236
},
37+
async function action() {
38+
if (!this.userId) {
39+
throw new Error('error-invalid-user');
40+
}
41+
42+
if (settings.get<boolean>('LDAP_Enable') !== true) {
43+
throw new Error('LDAP_disabled');
44+
}
45+
46+
try {
47+
await LDAP.testConnection();
48+
} catch (err) {
49+
SystemLogger.error({ err });
50+
throw new Error('Connection_failed');
51+
}
52+
53+
return API.v1.success({
54+
message: 'LDAP_Connection_successful' as const,
55+
});
56+
},
3357
);
3458

35-
API.v1.addRoute(
59+
API.v1.post(
3660
'ldap.testSearch',
37-
{ authRequired: true, permissionsRequired: ['test-admin-options'] },
3861
{
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-
});
62+
authRequired: true,
63+
permissionsRequired: ['test-admin-options'],
64+
validateParams: isLdapTestSearch,
65+
response: {
66+
200: ajv.compile<{ message: string }>(messageResponseSchema),
67+
401: validateUnauthorizedErrorResponse,
68+
403: validateForbiddenErrorResponse,
6069
},
6170
},
71+
async function action() {
72+
if (!this.userId) {
73+
throw new Error('error-invalid-user');
74+
}
75+
76+
if (settings.get('LDAP_Enable') !== true) {
77+
throw new Error('LDAP_disabled');
78+
}
79+
80+
await LDAP.testSearch(this.bodyParams.username);
81+
82+
return API.v1.success({
83+
message: 'LDAP_User_Found' as const,
84+
});
85+
},
6286
);

0 commit comments

Comments
 (0)