|
1 | 1 | 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'; |
3 | 3 |
|
4 | 4 | import { SystemLogger } from '../../../../server/lib/logger/system'; |
5 | 5 | import { settings } from '../../../settings/server'; |
6 | 6 | import { API } from '../api'; |
7 | 7 |
|
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( |
9 | 22 | 'ldap.testConnection', |
10 | | - { authRequired: true, permissionsRequired: ['test-admin-options'] }, |
11 | 23 | { |
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, |
31 | 30 | }, |
32 | 31 | }, |
| 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 | + }, |
33 | 52 | ); |
34 | 53 |
|
35 | | -API.v1.addRoute( |
| 54 | +API.v1.post( |
36 | 55 | 'ldap.testSearch', |
37 | | - { authRequired: true, permissionsRequired: ['test-admin-options'] }, |
38 | 56 | { |
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 | + } |
54 | 70 |
|
| 71 | + if (settings.get<boolean>('LDAP_Enable') !== true) { |
| 72 | + throw new Error('LDAP_disabled'); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
55 | 76 | await LDAP.testSearch(this.bodyParams.username); |
| 77 | + } catch (err) { |
| 78 | + SystemLogger.error({ err }); |
| 79 | + throw new Error('LDAP_search_failed'); |
| 80 | + } |
56 | 81 |
|
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 | + }); |
61 | 85 | }, |
62 | 86 | ); |
0 commit comments