|
1 | 1 | import { LDAP } from '@rocket.chat/core-services'; |
2 | | -import { Match, check } from 'meteor/check'; |
| 2 | +import { ajv, 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 | +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( |
9 | 31 | 'ldap.testConnection', |
10 | | - { authRequired: true, permissionsRequired: ['test-admin-options'] }, |
11 | 32 | { |
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, |
31 | 39 | }, |
32 | 40 | }, |
| 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 | + }, |
33 | 61 | ); |
34 | 62 |
|
35 | | -API.v1.addRoute( |
| 63 | +API.v1.post( |
36 | 64 | 'ldap.testSearch', |
37 | | - { authRequired: true, permissionsRequired: ['test-admin-options'] }, |
38 | 65 | { |
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 | + body: isLdapTestSearch, |
| 69 | + response: { |
| 70 | + 200: ajv.compile<{ message: string }>(messageResponseSchema), |
| 71 | + 401: validateUnauthorizedErrorResponse, |
| 72 | + 403: validateForbiddenErrorResponse, |
60 | 73 | }, |
61 | 74 | }, |
| 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 | + }, |
62 | 90 | ); |
0 commit comments