Skip to content

Commit 46f38c9

Browse files
smirk-devggazzo
andauthored
chore: migrate ldap endpoints to new API pattern (#38883)
Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
1 parent 1b7d4d8 commit 46f38c9

File tree

3 files changed

+73
-43
lines changed

3 files changed

+73
-43
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': minor
3+
---
4+
5+
Migrates `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: 67 additions & 43 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 { ajv, isLdapTestSearch, 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+
API.v1.post(
922
'ldap.testConnection',
10-
{ authRequired: true, permissionsRequired: ['test-admin-options'] },
1123
{
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-
});
24+
authRequired: true,
25+
permissionsRequired: ['test-admin-options'],
26+
response: {
27+
200: ajv.compile<{ message: string; success: true }>(messageResponseSchema),
28+
401: validateUnauthorizedErrorResponse,
29+
403: validateForbiddenErrorResponse,
3130
},
3231
},
32+
async function action() {
33+
if (!this.userId) {
34+
throw new Error('error-invalid-user');
35+
}
36+
37+
if (settings.get<boolean>('LDAP_Enable') !== true) {
38+
throw new Error('LDAP_disabled');
39+
}
40+
41+
try {
42+
await LDAP.testConnection();
43+
} catch (err) {
44+
SystemLogger.error({ err });
45+
throw new Error('Connection_failed');
46+
}
47+
48+
return API.v1.success({
49+
message: 'LDAP_Connection_successful' as const,
50+
});
51+
},
3352
);
3453

35-
API.v1.addRoute(
54+
API.v1.post(
3655
'ldap.testSearch',
37-
{ authRequired: true, permissionsRequired: ['test-admin-options'] },
3856
{
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-
}
57+
authRequired: true,
58+
permissionsRequired: ['test-admin-options'],
59+
body: isLdapTestSearch,
60+
response: {
61+
200: ajv.compile<{ message: string; success: true }>(messageResponseSchema),
62+
401: validateUnauthorizedErrorResponse,
63+
403: validateForbiddenErrorResponse,
64+
},
65+
},
66+
async function action() {
67+
if (!this.userId) {
68+
throw new Error('error-invalid-user');
69+
}
5470

71+
if (settings.get<boolean>('LDAP_Enable') !== true) {
72+
throw new Error('LDAP_disabled');
73+
}
74+
75+
try {
5576
await LDAP.testSearch(this.bodyParams.username);
77+
} catch (err) {
78+
SystemLogger.error({ err });
79+
throw new Error('LDAP_search_failed');
80+
}
5681

57-
return API.v1.success({
58-
message: 'LDAP_User_Found' as const,
59-
});
60-
},
82+
return API.v1.success({
83+
message: 'LDAP_User_Found' as const,
84+
});
6185
},
6286
);

packages/rest-typings/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export * from './helpers/ReplacePlaceholders';
230230
export * from './helpers/WithItemCount';
231231
export * from './v1/emojiCustom';
232232
export * from './v1/instances';
233+
export * from './v1/ldap';
233234
export * from './v1/users';
234235
export * from './v1/users/UsersSetAvatarParamsPOST';
235236
export * from './v1/users/UsersSetPreferenceParamsPOST';

0 commit comments

Comments
 (0)