|
1 | 1 | 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'; |
3 | 8 |
|
4 | 9 | import { SystemLogger } from '../../../../server/lib/logger/system'; |
5 | 10 | import { settings } from '../../../settings/server'; |
6 | 11 | import { API } from '../api'; |
7 | 12 |
|
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( |
9 | 27 | 'ldap.testConnection', |
10 | | - { authRequired: true, permissionsRequired: ['test-admin-options'] }, |
11 | 28 | { |
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, |
31 | 35 | }, |
32 | 36 | }, |
| 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 | + }, |
33 | 57 | ); |
34 | 58 |
|
35 | | -API.v1.addRoute( |
| 59 | +API.v1.post( |
36 | 60 | 'ldap.testSearch', |
37 | | - { authRequired: true, permissionsRequired: ['test-admin-options'] }, |
38 | 61 | { |
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, |
60 | 69 | }, |
61 | 70 | }, |
| 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 | + }, |
62 | 86 | ); |
0 commit comments