22const DispatchRequest = require ( './dispatchRequest' ) ;
33const ErrorMessage = require ( './errorMessage' ) ;
44const CONST = require ( './const' ) ;
5+ const UTILS = require ( './utils' )
56
67/**
78 * @license Apache Version 2
@@ -103,7 +104,11 @@ AccessControl.prototype.getUserRoles = function() {
103104/**
104105 * Any user can create their own organization.
105106 * @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+ * })
107112 */
108113AccessControl . prototype . createOrganization = function ( orgName ) {
109114 if ( ! orgName ) {
@@ -138,41 +143,28 @@ AccessControl.prototype.ifOrganizationExists = function (orgName) {
138143 ) ,
139144 ) ;
140145 }
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 ) ;
170148} ;
171149
172150/**
173151 * Get the pending invitations list.
174152 * @param {string } [orgName] - The organization name.
175153 * @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+ *
176168 */
177169AccessControl . prototype . getPendingOrgInvites = function ( orgName ) {
178170 if ( ! orgName && ! this . defaultOrganization ) {
@@ -185,17 +177,21 @@ AccessControl.prototype.getPendingOrgInvites = function (orgName) {
185177 ) ,
186178 ) ;
187179 }
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 ) ;
190182} ;
191183
192184/**
193185 * Send a new invitation
194186 * @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)
196188 * @param {string } [note] - The note to send with the invitation.
197189 * @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+ * })
199195 */
200196AccessControl . prototype . sendOrgInvite = function ( userEmail , role , note = '' , orgName ) {
201197 let errorMessage ;
@@ -217,8 +213,8 @@ AccessControl.prototype.sendOrgInvite = function (userEmail, role, note = '', or
217213 ) ,
218214 ) ;
219215 }
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 , {
222218 email_to : userEmail ,
223219 role,
224220 note,
@@ -230,6 +226,11 @@ AccessControl.prototype.sendOrgInvite = function (userEmail, role, note = '', or
230226 * @param {string } inviteId - The invite id to retrieve.
231227 * @param {string } [orgName] - The organization name.
232228 * @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+ * })
233234 */
234235AccessControl . prototype . getOrgInvite = function ( inviteId , orgName ) {
235236 let errorMessage ;
@@ -249,14 +250,21 @@ AccessControl.prototype.getOrgInvite = function (inviteId, orgName) {
249250 ) ,
250251 ) ;
251252 }
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 ) ;
253256} ;
254257
255258/**
256259 * Delete an invitation
257260 * @param {string } inviteId - The invite id to delete.
258261 * @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+ * })
260268 */
261269AccessControl . prototype . deleteOrgInvite = function ( inviteId , orgName ) {
262270 let errorMessage ;
@@ -276,7 +284,9 @@ AccessControl.prototype.deleteOrgInvite = function (inviteId, orgName) {
276284 ) ,
277285 ) ;
278286 }
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 ) ;
280290} ;
281291
282292/**
@@ -285,6 +295,11 @@ AccessControl.prototype.deleteOrgInvite = function (inviteId, orgName) {
285295 * @param {boolean } accepted - The status of the invitation.
286296 * @param {string } [orgName] - The organization name.
287297 * @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+ * })
288303 */
289304AccessControl . prototype . updateOrgInviteStatus = function ( inviteId , accepted , orgName ) {
290305 let errorMessage ;
@@ -306,8 +321,9 @@ AccessControl.prototype.updateOrgInviteStatus = function (inviteId, accepted, or
306321 ) ,
307322 ) ;
308323 }
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 , {
311327 accepted
312328 } ) ;
313329} ;
@@ -316,6 +332,18 @@ AccessControl.prototype.updateOrgInviteStatus = function (inviteId, accepted, or
316332 * Get all the organization's users and roles
317333 * @param {string } [orgName] - The organization name.
318334 * @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" }]
319347 */
320348AccessControl . prototype . getOrgUsers = function ( orgName ) {
321349 if ( ! orgName && ! this . defaultOrganization ) {
@@ -328,15 +356,68 @@ AccessControl.prototype.getOrgUsers = function (orgName) {
328356 ) ,
329357 ) ;
330358 }
359+ const org = orgName ? orgName : this . defaultOrganization
360+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /users` , CONST . GET ) ;
361+ } ;
331362
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 ) ;
333395} ;
334396
397+
335398/**
336399 * Get the user's role for every databases under the organization
337400 * @param {string } userId - The user's id.
338401 * @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" }]
340421 */
341422AccessControl . prototype . getDatabaseRolesOfUser = function ( userId , orgName ) {
342423 let errorMessage ;
@@ -356,8 +437,9 @@ AccessControl.prototype.getDatabaseRolesOfUser = function (userId, orgName) {
356437 ) ,
357438 ) ;
358439 }
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 ) ;
361443} ;
362444
363445/**
@@ -367,6 +449,12 @@ AccessControl.prototype.getDatabaseRolesOfUser = function (userId, orgName) {
367449 * @param {string } role - The user role to be assigned.
368450 * @param {string } [orgName] - The organization name.
369451 * @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+ * })
370458 */
371459AccessControl . prototype . assignUserRole = function ( userId , scope , role , orgName ) {
372460
@@ -392,8 +480,9 @@ AccessControl.prototype.assignUserRole = function (userId, scope, role, orgName)
392480 ) ,
393481 ) ;
394482 }
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 , {
397486 scope,
398487 role,
399488 } ) ;
@@ -407,6 +496,13 @@ AccessControl.prototype.assignUserRole = function (userId, scope, role, orgName)
407496 * @param {string } role - The user role to be updated.
408497 * @param {string } [orgName] - The organization name.
409498 * @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+ * })
410506 */
411507AccessControl . prototype . updateUserRole = function ( userId , capabilityId , scope , role , orgName ) {
412508 let errorMessage ;
@@ -433,8 +529,10 @@ AccessControl.prototype.updateUserRole = function (userId, capabilityId, scope,
433529 ) ,
434530 ) ;
435531 }
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 , {
438536 scope,
439537 role,
440538 } ) ;
0 commit comments