@@ -43,8 +43,10 @@ function AccessControl(cloudAPIUrl, params) {
43
43
this . setJwtToken ( params . jwt ) ;
44
44
} else if ( params . token ) {
45
45
this . setApiToken ( params . token ) ;
46
+ } else if ( params . key ) {
47
+ this . setApiKey ( params . key ) ;
48
+ this . user = params . user ;
46
49
}
47
-
48
50
this . defaultOrganization = this . getDefaultOrganization ( params ) ;
49
51
}
50
52
@@ -68,7 +70,7 @@ AccessControl.prototype.setJwtToken = function (jwt) {
68
70
throw new Error ( 'TerminusX Access token required' ) ;
69
71
}
70
72
71
- this . apiJwtToken = jwt ;
73
+ this . apiKey = jwt ;
72
74
this . apiType = 'jwt' ;
73
75
} ;
74
76
@@ -81,10 +83,23 @@ AccessControl.prototype.setApiToken = function (token) {
81
83
throw new Error ( 'TerminusX Access token required' ) ;
82
84
}
83
85
84
- this . apiToken = token ;
86
+ this . apiKey = token ;
85
87
this . apiType = 'apikey' ;
86
88
} ;
87
89
90
+ /**
91
+ * Sets the API token for the object, to request a token create an account in https://terminusdb.com/
92
+ * @param {string } atokenpi - The API token to use to connect with TerminusX
93
+ */
94
+ AccessControl . prototype . setApiKey = function ( key ) {
95
+ if ( ! key ) {
96
+ throw new Error ( 'TerminusDB bacis authentication key required' ) ;
97
+ }
98
+
99
+ this . apiKey = key ;
100
+ this . apiType = 'basic' ;
101
+ } ;
102
+
88
103
/**
89
104
* Get a API url from cloudAPIUrl
90
105
* @param {string } cloudAPIUrl - The base url for cloud
@@ -112,14 +127,11 @@ AccessControl.prototype.dispatch = function (requestUrl, action, payload) {
112
127
) ,
113
128
) ;
114
129
}
115
-
116
- const apiToken = this . apiJwtToken || this . apiToken ;
117
-
118
130
return DispatchRequest (
119
131
requestUrl ,
120
132
action ,
121
133
payload ,
122
- { type : this . apiType , key : apiToken } ,
134
+ { type : this . apiType , key : this . apiKey , user : this . user } ,
123
135
) ;
124
136
} ;
125
137
@@ -132,8 +144,19 @@ AccessControl.prototype.getAccessRoles = function () {
132
144
} ;
133
145
134
146
/**
135
- * Any user can create their own organization.
136
- * IMPORTANT This does not work with the API-TOKEN.
147
+ * This end point works only in basic authentication admin user
148
+ * Get all the system organizations list
149
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
150
+ */
151
+
152
+ AccessControl . prototype . getAllOrganizations = function ( ) {
153
+ return this . dispatch ( `${ this . baseURL } /organizations` , CONST . GET ) ;
154
+ } ;
155
+
156
+ /**
157
+ * This works only in the local database
158
+ * TerminusX - Any user can create their own organization. -
159
+ * TerminusX - IMPORTANT This does not work with the API-TOKEN.
137
160
* @param {string } orgName - The organization name to create
138
161
* @return {Promise } A promise that returns the call response object, or an Error if rejected.
139
162
* @example
@@ -142,20 +165,21 @@ AccessControl.prototype.getAccessRoles = function () {
142
165
* })
143
166
*/
144
167
AccessControl . prototype . createOrganization = function ( orgName ) {
145
- if ( ! orgName ) {
146
- return Promise . reject (
147
- new Error (
148
- ErrorMessage . getInvalidParameterMessage (
149
- 'POST' ,
150
- 'Please provide a organization name' ,
151
- ) ,
152
- ) ,
153
- ) ;
154
- }
168
+ // maybe we have to review this
169
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( orgName ) } ` , CONST . POST , { } ) ;
170
+ } ;
155
171
156
- return this . dispatch ( `${ this . baseURL } /private/organizations` , CONST . POST , {
157
- organization : orgName ,
158
- } ) ;
172
+ /**
173
+ * This api works only in the local installation
174
+ * @param {string } orgName - The organization name to create
175
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
176
+ * @example
177
+ * accessControl.createOrganization("my_org_name").then(result=>{
178
+ * console.log(result)
179
+ * })
180
+ */
181
+ AccessControl . prototype . deleteOrganization = function ( orgName ) {
182
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( orgName ) } ` , CONST . DELETE ) ;
159
183
} ;
160
184
161
185
/**
@@ -433,6 +457,53 @@ AccessControl.prototype.getTeamUserRole = function (orgName) {
433
457
return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /role` , CONST . GET ) ;
434
458
} ;
435
459
460
+ /**
461
+ * Get the user role for a given organization or the default organization,
462
+ * @param {string } [userName] - The organization name.
463
+ * @param {string } [orgName] - The organization name.
464
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
465
+ * @example
466
+ * accessControl.getTeamUserRole("myUser").then(result=>{
467
+ * console.log(result)
468
+ * })
469
+ *
470
+ * //response object example
471
+ * {
472
+ * "@id ": "User/myUser",
473
+ * "capability": [
474
+ * {
475
+ * "@id ":"Capability/server_access",
476
+ * "@type":"Capability",
477
+ * "role": [{
478
+ * "@id ":"Role/reader",
479
+ * "@type":"Role",
480
+ * "action": [
481
+ * "instance_read_access",
482
+ * ],
483
+ * "name":"reader"
484
+ * }],
485
+ * "scope":"Organization/myteam"
486
+ * }
487
+ * ],
488
+ * "name": "myUser"
489
+ *}
490
+ */
491
+
492
+ AccessControl . prototype . getTeamUserRoles = function ( userName , orgName ) {
493
+ if ( ! orgName && ! this . defaultOrganization ) {
494
+ return Promise . reject (
495
+ new Error (
496
+ ErrorMessage . getInvalidParameterMessage (
497
+ 'GET' ,
498
+ 'Please provide a organization name' ,
499
+ ) ,
500
+ ) ,
501
+ ) ;
502
+ }
503
+ const org = orgName || this . defaultOrganization ;
504
+ return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /users/${ UTILS . encodeURISegment ( userName ) } ` , CONST . GET ) ;
505
+ } ;
506
+
436
507
/**
437
508
* Remove an user from an organization, only an admin user can remove an user from an organization
438
509
* @param {string } userId - The id of the user to be removed. (this is the document user's @id)
@@ -693,4 +764,100 @@ AccessControl.prototype.deleteAccessRequest = function (acceId, orgName) {
693
764
return this . dispatch ( `${ this . baseURL } /organizations/${ UTILS . encodeURISegment ( org ) } /access_requests/${ acceId } ` , CONST . DELETE ) ;
694
765
} ;
695
766
767
+ /**
768
+ * Create a new role in the system database, (this api is enabled only in the local installation)
769
+ * @param {string } [name] - The role name.
770
+ * @param {array } [actions] - A list of actions
771
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
772
+ * @example
773
+ * accessControl.createRole("Reader",[ACTIONS.INSTANCE_READ_ACCESS]).then(result=>{
774
+ * console.log(result)
775
+ * })
776
+ *
777
+ */
778
+ AccessControl . prototype . createRole = function ( name , actions ) {
779
+ const payload = { name, action : actions } ;
780
+ return this . dispatch ( `${ this . baseURL } /roles` , CONST . POST , payload ) ;
781
+ } ;
782
+
783
+ /**
784
+ * Delete role in the system database, (this api is enabled only in the local installation)
785
+ * @param {string } [name] - The role name.
786
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
787
+ * @example
788
+ * accessControl.deleteRole("Reader").then(result=>{
789
+ * console.log(result)
790
+ * })
791
+ *
792
+ */
793
+ AccessControl . prototype . deleteRole = function ( name ) {
794
+ return this . dispatch ( `${ this . baseURL } /roles/${ UTILS . encodeURISegment ( name ) } ` , CONST . DELETE ) ;
795
+ } ;
796
+
797
+ /**
798
+ * Return the list of all the users (this api is enabled only in the local installation)
799
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
800
+ * @example
801
+ * accessControl.getAllUsers().then(result=>{
802
+ * console.log(result)
803
+ * })
804
+ *
805
+ */
806
+
807
+ AccessControl . prototype . getAllUsers = function ( ) {
808
+ return this . dispatch ( `${ this . baseURL } /users` , CONST . GET ) ;
809
+ } ;
810
+
811
+ /**
812
+ * Remove the user from the system database (this api is enabled only in the local installation)
813
+ * @param {string } userId - the document user id
814
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
815
+ * @example
816
+ * accessControl.deleteUser(userId).then(result=>{
817
+ * console.log(result)
818
+ * })
819
+ *
820
+ */
821
+
822
+ AccessControl . prototype . deleteUser = function ( userId ) {
823
+ return this . dispatch ( `${ this . baseURL } /users/${ UTILS . encodeURISegment ( userId ) } ` , CONST . DELETE ) ;
824
+ } ;
825
+
826
+ /**
827
+ * Add the user into the system database (this api is enabled only in the local installation)
828
+ * @param {string } name - the user name
829
+ * @param {string } [password] - you need the password for basic authentication
830
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
831
+ * @example
832
+ * accessControl.deleteUser(userId).then(result=>{
833
+ * console.log(result)
834
+ * })
835
+ *
836
+ */
837
+
838
+ AccessControl . prototype . createUser = function ( name , password ) {
839
+ const payload = { name, password } ;
840
+ return this . dispatch ( `${ this . baseURL } /users` , CONST . POST , payload ) ;
841
+ } ;
842
+
843
+ /**
844
+ * Grant/Revoke Capability (this api is enabled only in the local installation)
845
+ * @param {string } userId - the document user id
846
+ * @param {string } resourceId - the resource id (database or team)
847
+ * @param {array } rolesArr - the roles list
848
+ * @param {string } operation - grant/revoke operation
849
+ * @return {Promise } A promise that returns the call response object, or an Error if rejected.
850
+ * @example
851
+ { "operation" : "grant",
852
+ "scope" : "Organization/myteam",
853
+ "user" : "User/myUser",
854
+ "roles" : ["Role/reader"] }
855
+ */
856
+ AccessControl . prototype . manageCapability = function ( userId , resourceId , rolesArr , operation ) {
857
+ const payload = {
858
+ operation, user : userId , roles : rolesArr , scope : resourceId ,
859
+ } ;
860
+ return this . dispatch ( `${ this . baseURL } /capabilities` , CONST . POST , payload ) ;
861
+ } ;
862
+
696
863
module . exports = AccessControl ;
0 commit comments