2
2
const DispatchRequest = require ( './dispatchRequest' ) ;
3
3
const ErrorMessage = require ( './errorMessage' ) ;
4
4
const CONST = require ( './const' ) ;
5
+ const UTILS = require ( './utils' )
5
6
6
7
/**
7
8
* @license Apache Version 2
@@ -103,7 +104,11 @@ AccessControl.prototype.getUserRoles = function() {
103
104
/**
104
105
* Any user can create their own organization.
105
106
* @param {string } orgName - The organization name to create
106
- * @return {Promise } A promise that returns the call response object, or an Error if rejected.
107
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
108
+ * @example
109
+ * accessControl.createOrganization("my_org_name").then(result=>{
110
+ * console.log(result)
111
+ * })
107
112
*/
108
113
AccessControl . prototype . createOrganization = function ( orgName ) {
109
114
if ( ! orgName ) {
@@ -138,41 +143,28 @@ AccessControl.prototype.ifOrganizationExists = function (orgName) {
138
143
) ,
139
144
) ;
140
145
}
141
-
142
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName } ` , CONST . HEAD ) ;
143
- } ;
144
-
145
- /**
146
- * Remove an user from an organization, only an admin user can remove an user from an organization
147
- * @param {string } userId - The if of the user to be removed.
148
- * @param {string } [orgName] - The organization name in which the user is to be removed.
149
- * @return {Promise } A promise that returns the call response object, or an Error if rejected.
150
- */
151
- AccessControl . prototype . removeUserFromOrg = function ( userId , orgName ) {
152
- let errorMessage ;
153
- if ( ! orgName && ! this . defaultOrganization ) {
154
- errorMessage = 'Please provide a organization name' ;
155
- } else if ( ! userId ) {
156
- errorMessage = 'Please provide a userId' ;
157
- }
158
-
159
- if ( errorMessage ) {
160
- return Promise . reject (
161
- new Error (
162
- ErrorMessage . getInvalidParameterMessage (
163
- "DELETE" ,
164
- errorMessage
165
- ) ,
166
- ) ,
167
- ) ;
168
- }
169
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /users/${ userId } ` , CONST . DELETE ) ;
146
+
147
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( orgName ) } ` , CONST . HEAD ) ;
170
148
} ;
171
149
172
150
/**
173
151
* Get the pending invitations list.
174
152
* @param {string } [orgName] - The organization name.
175
153
* @return {Promise } A promise that returns the call response object, or an Error if rejected.
154
+ * @example
155
+ * const invitationList = accessControl.getPendingOrgInvites.then(result=>{
156
+ * console.log(invitationList)
157
+ *
158
+ * })
159
+ * //this will return an array of invitations object like this
160
+ * //[{@id : "Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9c0dfc2ac51161ef5ba7cb0988d992c4bce82b3fa5d25"
161
+ * // @type : "Invitation"
162
+ * // creation_date: "2021-10-22T11:13:28.762Z"
163
+ * // email_to: "[email protected] "
164
+ * // invited_by: "User/auth0%7C6162f8ab33567406a6bee0c"
165
+ * // role: "Role/dataReader"
166
+ * // status: "needs_invite" }]
167
+ *
176
168
*/
177
169
AccessControl . prototype . getPendingOrgInvites = function ( orgName ) {
178
170
if ( ! orgName && ! this . defaultOrganization ) {
@@ -185,17 +177,21 @@ AccessControl.prototype.getPendingOrgInvites = function (orgName) {
185
177
) ,
186
178
) ;
187
179
}
188
-
189
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /invites` , CONST . GET ) ;
180
+ const org = orgName ? orgName : this . defaultOrganization
181
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /invites` , CONST . GET ) ;
190
182
} ;
191
183
192
184
/**
193
185
* Send a new invitation
194
186
* @param {string } userEmail - The email of user.
195
- * @param {string } role - The role for user.
187
+ * @param {string } role - The role for user. (the document @id role like Role/collaborator)
196
188
* @param {string } [note] - The note to send with the invitation.
197
189
* @param {string } [orgName] - The organization name.
198
- * @return {Promise } A promise that returns the call response object, or an Error if rejected.
190
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
191
+ * @example
192
+ * accessControl.sendOrgInvite("[email protected] ","Role/admin","please join my team").then(result=>{
193
+ * console.log(result)
194
+ * })
199
195
*/
200
196
AccessControl . prototype . sendOrgInvite = function ( userEmail , role , note = '' , orgName ) {
201
197
let errorMessage ;
@@ -217,8 +213,8 @@ AccessControl.prototype.sendOrgInvite = function (userEmail, role, note = '', or
217
213
) ,
218
214
) ;
219
215
}
220
-
221
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /invites` , CONST . POST , {
216
+ const org = orgName ? orgName : this . defaultOrganization
217
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /invites` , CONST . POST , {
222
218
email_to : userEmail ,
223
219
role,
224
220
note,
@@ -230,6 +226,11 @@ AccessControl.prototype.sendOrgInvite = function (userEmail, role, note = '', or
230
226
* @param {string } inviteId - The invite id to retrieve.
231
227
* @param {string } [orgName] - The organization name.
232
228
* @return {Promise } A promise that returns the call response object, or an Error if rejected.
229
+ * @example
230
+ * const fullInviteId="Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9c0dfc2ac51161ef5ba7cb0988d992c4bce82b3fa5d25"
231
+ * accessControl.getOrgInvite(fullInviteId).then(result=>{
232
+ * console.log(result)
233
+ * })
233
234
*/
234
235
AccessControl . prototype . getOrgInvite = function ( inviteId , orgName ) {
235
236
let errorMessage ;
@@ -249,14 +250,21 @@ AccessControl.prototype.getOrgInvite = function (inviteId, orgName) {
249
250
) ,
250
251
) ;
251
252
}
252
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /invites/${ inviteId } ` , CONST . GET ) ;
253
+ const org = orgName ? orgName : this . defaultOrganization
254
+ const inviteHash = UTILS . removeDocType ( inviteId )
255
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /invites/${ inviteHash } ` , CONST . GET ) ;
253
256
} ;
254
257
255
258
/**
256
259
* Delete an invitation
257
260
* @param {string } inviteId - The invite id to delete.
258
261
* @param {string } [orgName] - The organization name.
259
- * @return {Promise } A promise that returns the call response object, or an Error if rejected.
262
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
263
+ * @example
264
+ * const fullInviteId="Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9c0dfc2ac51161ef5ba7cb0988d992c4bce82b3fa5d25"
265
+ * accessControl.deleteOrgInvite(fullInviteId).then(result=>{
266
+ * console.log(result)
267
+ * })
260
268
*/
261
269
AccessControl . prototype . deleteOrgInvite = function ( inviteId , orgName ) {
262
270
let errorMessage ;
@@ -276,7 +284,9 @@ AccessControl.prototype.deleteOrgInvite = function (inviteId, orgName) {
276
284
) ,
277
285
) ;
278
286
}
279
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /invites/${ inviteId } ` , CONST . DELETE ) ;
287
+ const org = orgName ? orgName : this . defaultOrganization
288
+ const inviteHash = UTILS . removeDocType ( inviteId )
289
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /invites/${ inviteHash } ` , CONST . DELETE ) ;
280
290
} ;
281
291
282
292
/**
@@ -285,6 +295,11 @@ AccessControl.prototype.deleteOrgInvite = function (inviteId, orgName) {
285
295
* @param {boolean } accepted - The status of the invitation.
286
296
* @param {string } [orgName] - The organization name.
287
297
* @return {Promise } A promise that returns the call response object, or an Error if rejected.
298
+ * @example
299
+ * const fullInviteId="Organization/my_team_name/invitations/Invitation/7ad0c9eb82b6175bcda9c0dfc2ac51161ef5ba7cb0988d992c4bce82b3fa5d25"
300
+ * accessControl.updateOrgInviteStatus(fullInviteId,true).then(result=>{
301
+ * console.log(result)
302
+ * })
288
303
*/
289
304
AccessControl . prototype . updateOrgInviteStatus = function ( inviteId , accepted , orgName ) {
290
305
let errorMessage ;
@@ -306,8 +321,9 @@ AccessControl.prototype.updateOrgInviteStatus = function (inviteId, accepted, or
306
321
) ,
307
322
) ;
308
323
}
309
-
310
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /invites/${ inviteId } ` , CONST . PUT , {
324
+ const org = orgName ? orgName : this . defaultOrganization
325
+ const inviteHash = UTILS . removeDocType ( inviteId )
326
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /invites/${ inviteHash } ` , CONST . PUT , {
311
327
accepted
312
328
} ) ;
313
329
} ;
@@ -316,6 +332,18 @@ AccessControl.prototype.updateOrgInviteStatus = function (inviteId, accepted, or
316
332
* Get all the organization's users and roles
317
333
* @param {string } [orgName] - The organization name.
318
334
* @return {Promise } A promise that returns the call response object, or an Error if rejected.
335
+ * @example
336
+ * accessControl.getOrgUsers().then(result=>{
337
+ * console.log(result)
338
+ * })
339
+ *
340
+ * //this function will return an array of capabilities with users and roles
341
+ * //[{capability: "Capability/3ea26e1d698821c570afe9cb4fe81a3......"
342
+ * // email: {@type: "xsd:string", @value: "[email protected] " }
343
+ * // picture: {@type: "xsd:string",… }
344
+ * // role: "Role/dataReader"
345
+ * // scope: "Organization/my_org_name"
346
+ * // user: "User/auth0%7C613f5dnndjdjkTTT" }]
319
347
*/
320
348
AccessControl . prototype . getOrgUsers = function ( orgName ) {
321
349
if ( ! orgName && ! this . defaultOrganization ) {
@@ -328,15 +356,68 @@ AccessControl.prototype.getOrgUsers = function (orgName) {
328
356
) ,
329
357
) ;
330
358
}
359
+ const org = orgName ? orgName : this . defaultOrganization
360
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /users` , CONST . GET ) ;
361
+ } ;
331
362
332
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /users` , CONST . GET ) ;
363
+ /**
364
+ * Remove an user from an organization, only an admin user can remove an user from an organization
365
+ * @param {string } userId - The id of the user to be removed. (this is the document user's @id)
366
+ * @param {string } [orgName] - The organization name in which the user is to be removed.
367
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
368
+ * @example
369
+ * accessControl.removeUserFromOrg("User/auth0%7C613f5dnndjdjkTTT","my_org_name").then(result=>{
370
+ * console.log(result)
371
+ * })
372
+ *
373
+ */
374
+ AccessControl . prototype . removeUserFromOrg = function ( userId , orgName ) {
375
+ let errorMessage ;
376
+ if ( ! orgName && ! this . defaultOrganization ) {
377
+ errorMessage = 'Please provide a organization name' ;
378
+ } else if ( ! userId ) {
379
+ errorMessage = 'Please provide a userId' ;
380
+ }
381
+
382
+ if ( errorMessage ) {
383
+ return Promise . reject (
384
+ new Error (
385
+ ErrorMessage . getInvalidParameterMessage (
386
+ "DELETE" ,
387
+ errorMessage
388
+ ) ,
389
+ ) ,
390
+ ) ;
391
+ }
392
+ const org = orgName ? orgName : this . defaultOrganization
393
+ const user = UTILS . removeDocType ( userId )
394
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /users/${ user } ` , CONST . DELETE ) ;
333
395
} ;
334
396
397
+
335
398
/**
336
399
* Get the user's role for every databases under the organization
337
400
* @param {string } userId - The user's id.
338
401
* @param {string } [orgName] - The organization name.
339
- * @return {Promise } A promise that returns the call response object, or an Error if rejected.
402
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
403
+ * @example
404
+ * accessControl.getDatabaseRolesOfUser('User/auth0%7C61790e366377Yu6596a').then(result=>{
405
+ * console.log(result)
406
+ * })
407
+ *
408
+ * //this is a capabilities list of databases and roles
409
+ * [
410
+ * {capability: "Capability/b395e8523d509dec6b33aefc9baed3b2e2bfadbd4c79d4ff9b20dce2b14e2edc" //if there is an id we have a user specific capabality for this database
411
+ * name: {@type: "xsd:string", @value: "profiles_test" }
412
+ * role: "Role/dataUpdater"
413
+ * scope: "UserDatabase/7ebdfae5a02bc7e8f6d79sjjjsa4e179b1df9d4576a3b1d2e5ff3b4859"
414
+ * user: "User/auth0%7C61790e11a3966d006906596a" },
415
+ *
416
+ * { capability: null // if the capability id is null the user level of access for this database if the team level
417
+ * name: {@type: "xsd:string", @value: "Collab002" }
418
+ * role: "Role/dataReader"
419
+ * scope: "UserDatabase/acfcc2db02b83792sssb15239ccdf586fc5b176846ffe4878b1aea6a36c8f"
420
+ * user: "User/auth0%7C61790e11a3966d006906596a" }]
340
421
*/
341
422
AccessControl . prototype . getDatabaseRolesOfUser = function ( userId , orgName ) {
342
423
let errorMessage ;
@@ -356,8 +437,9 @@ AccessControl.prototype.getDatabaseRolesOfUser = function (userId, orgName) {
356
437
) ,
357
438
) ;
358
439
}
359
-
360
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /users/${ userId } /databases` , CONST . GET ) ;
440
+ const org = orgName ? orgName : this . defaultOrganization
441
+ const user = UTILS . removeDocType ( userId )
442
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /users/${ user } /databases` , CONST . GET ) ;
361
443
} ;
362
444
363
445
/**
@@ -367,6 +449,12 @@ AccessControl.prototype.getDatabaseRolesOfUser = function (userId, orgName) {
367
449
* @param {string } role - The user role to be assigned.
368
450
* @param {string } [orgName] - The organization name.
369
451
* @return {Promise } A promise that returns the call response object, or an Error if rejected.
452
+ * @example
453
+ * const dbId = "UserDatabase/acfcc2db02b83792sssb15239ccdf586fc5b176846ffe4878b1aea6a36c8f"
454
+ * accessControl.assignUserRole('User/auth0%7C61790e11a3966d006906596a',dbId,"Role/collaborator").then(result=>{
455
+ * console.log(result)
456
+ *
457
+ * })
370
458
*/
371
459
AccessControl . prototype . assignUserRole = function ( userId , scope , role , orgName ) {
372
460
@@ -392,8 +480,9 @@ AccessControl.prototype.assignUserRole = function (userId, scope, role, orgName)
392
480
) ,
393
481
) ;
394
482
}
395
-
396
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /users/${ userId } /capabilities` , CONST . POST , {
483
+ const org = orgName ? orgName : this . defaultOrganization
484
+ const user = UTILS . removeDocType ( userId )
485
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /users/${ user } /capabilities` , CONST . POST , {
397
486
scope,
398
487
role,
399
488
} ) ;
@@ -407,6 +496,13 @@ AccessControl.prototype.assignUserRole = function (userId, scope, role, orgName)
407
496
* @param {string } role - The user role to be updated.
408
497
* @param {string } [orgName] - The organization name.
409
498
* @return {Promise } A promise that returns the call response object, or an Error if rejected.
499
+ * @example
500
+ * const dbId = "UserDatabase/acfcc2db02b83792sssb15239ccdf586fc5b176846ffe4878b1aea6a36c8f"
501
+ * const capId= "Capability/b395e8523d509dec6b33aefc9baed3b2e2bfadbd4c79d4ff9b20dce2b14e2edc"
502
+ * accessControl.updateUserRole('User/auth0%7C61790e11a3966d006906596a',capId,dbId,"Role/dataUpdater").then(result=>{
503
+ * console.log(result)
504
+ *
505
+ * })
410
506
*/
411
507
AccessControl . prototype . updateUserRole = function ( userId , capabilityId , scope , role , orgName ) {
412
508
let errorMessage ;
@@ -433,8 +529,10 @@ AccessControl.prototype.updateUserRole = function (userId, capabilityId, scope,
433
529
) ,
434
530
) ;
435
531
}
436
-
437
- return this . dispatch ( `${ this . baseURL } /organizations/${ orgName ? orgName : this . defaultOrganization } /users/${ userId } /capabilities/${ capabilityId } ` , CONST . PUT , {
532
+ const org = orgName ? orgName : this . defaultOrganization
533
+ const user = UTILS . removeDocType ( userId )
534
+ const capHash = UTILS . removeDocType ( capabilityId )
535
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /users/${ user } /capabilities/${ capHash } ` , CONST . PUT , {
438
536
scope,
439
537
role,
440
538
} ) ;
0 commit comments