11import type { IPermission } from '@rocket.chat/core-typings' ;
22import { Permissions , Roles } from '@rocket.chat/models' ;
3- import { isBodyParamsValidPermissionUpdate } from '@rocket.chat/rest-typings' ;
3+ import {
4+ ajv ,
5+ validateUnauthorizedErrorResponse ,
6+ validateBadRequestErrorResponse ,
7+ validateForbiddenErrorResponse ,
8+ } from '@rocket.chat/rest-typings' ;
49import { Meteor } from 'meteor/meteor' ;
510
611import { permissionsGetMethod } from '../../../authorization/server/streamer/permissions' ;
712import { notifyOnPermissionChangedById } from '../../../lib/server/lib/notifyListener' ;
13+ import type { ExtractRoutesFromAPI } from '../ApiClass' ;
814import { API } from '../api' ;
915
10- API . v1 . addRoute (
11- 'permissions.listAll' ,
12- { authRequired : true } ,
13- {
14- async get ( ) {
16+ type PermissionsListAllProps = {
17+ updatedSince ?: string ;
18+ } ;
19+
20+ type PermissionsUpdateProps = {
21+ permissions : { _id : string ; roles : string [ ] } [ ] ;
22+ } ;
23+
24+ const permissionListAllSchema = {
25+ type : 'object' ,
26+ properties : {
27+ updatedSince : {
28+ type : 'string' ,
29+ nullable : true ,
30+ } ,
31+ } ,
32+ required : [ ] ,
33+ additionalProperties : false ,
34+ } ;
35+
36+ const permissionUpdatePropsSchema = {
37+ type : 'object' ,
38+ properties : {
39+ permissions : {
40+ type : 'array' ,
41+ items : {
42+ type : 'object' ,
43+ properties : {
44+ _id : { type : 'string' } ,
45+ roles : {
46+ type : 'array' ,
47+ items : { type : 'string' } ,
48+ uniqueItems : true ,
49+ } ,
50+ } ,
51+ additionalProperties : false ,
52+ required : [ '_id' , 'roles' ] ,
53+ } ,
54+ } ,
55+ } ,
56+ required : [ 'permissions' ] ,
57+ additionalProperties : false ,
58+ } ;
59+
60+ const isPermissionsListAll = ajv . compile < PermissionsListAllProps > ( permissionListAllSchema ) ;
61+
62+ const isBodyParamsValidPermissionUpdate = ajv . compile < PermissionsUpdateProps > ( permissionUpdatePropsSchema ) ;
63+
64+ const permissionsEndpoints = API . v1
65+ . get (
66+ 'permissions.listAll' ,
67+ {
68+ authRequired : true ,
69+ query : isPermissionsListAll ,
70+ response : {
71+ 200 : ajv . compile < {
72+ update : IPermission [ ] ;
73+ remove : IPermission [ ] ;
74+ } > ( {
75+ type : 'object' ,
76+ properties : {
77+ update : {
78+ type : 'array' ,
79+ items : { $ref : '#/components/schemas/IPermission' } ,
80+ } ,
81+ remove : {
82+ type : 'array' ,
83+ items : { $ref : '#/components/schemas/IPermission' } ,
84+ } ,
85+ success : {
86+ type : 'boolean' ,
87+ enum : [ true ] ,
88+ description : 'Indicates if the request was successful.' ,
89+ } ,
90+ } ,
91+ required : [ 'update' , 'remove' , 'success' ] ,
92+ additionalProperties : false ,
93+ } ) ,
94+ 400 : validateBadRequestErrorResponse ,
95+ 401 : validateUnauthorizedErrorResponse ,
96+ } ,
97+ } ,
98+ async function action ( ) {
1599 const { updatedSince } = this . queryParams ;
16100
17101 let updatedSinceDate : Date | undefined ;
@@ -36,14 +120,38 @@ API.v1.addRoute(
36120
37121 return API . v1 . success ( result ) ;
38122 } ,
39- } ,
40- ) ;
41-
42- API . v1 . addRoute (
43- 'permissions.update' ,
44- { authRequired : true , permissionsRequired : [ 'access-permissions' ] } ,
45- {
46- async post ( ) {
123+ )
124+ . post (
125+ 'permissions.update' ,
126+ {
127+ authRequired : true ,
128+ permissionsRequired : [ 'access-permissions' ] ,
129+ body : isBodyParamsValidPermissionUpdate ,
130+ response : {
131+ 200 : ajv . compile < {
132+ permissions : IPermission [ ] ;
133+ } > ( {
134+ type : 'object' ,
135+ properties : {
136+ permissions : {
137+ type : 'array' ,
138+ items : { $ref : '#/components/schemas/IPermission' } ,
139+ } ,
140+ success : {
141+ type : 'boolean' ,
142+ enum : [ true ] ,
143+ description : 'Indicates if the request was successful.' ,
144+ } ,
145+ } ,
146+ required : [ 'permissions' , 'success' ] ,
147+ additionalProperties : false ,
148+ } ) ,
149+ 400 : validateBadRequestErrorResponse ,
150+ 401 : validateUnauthorizedErrorResponse ,
151+ 403 : validateForbiddenErrorResponse ,
152+ } ,
153+ } ,
154+ async function action ( ) {
47155 const { bodyParams } = this ;
48156
49157 if ( ! isBodyParamsValidPermissionUpdate ( bodyParams ) ) {
@@ -76,5 +184,11 @@ API.v1.addRoute(
76184 permissions : result ,
77185 } ) ;
78186 } ,
79- } ,
80- ) ;
187+ ) ;
188+
189+ export type PermissionsEndpoints = ExtractRoutesFromAPI < typeof permissionsEndpoints > ;
190+
191+ declare module '@rocket.chat/rest-typings' {
192+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
193+ interface Endpoints extends PermissionsEndpoints { }
194+ }
0 commit comments