1
- const UTILS = require ( './utils.js' ) ;
2
- const CONST = require ( './const.js' ) ;
3
- const ErrorMessage = require ( './errorMessage' ) ;
4
- /*
1
+ /**
2
+ * @file Connection Capabilities
3
+ * @license Apache Version 2
4
+ * @summary Object which helps manage the capabilities available for a given TerminusDB connection
5
5
* Creates an entry in the connection registry for the server
6
6
* and all the databases that the client has access to
7
7
* maps the input authorties to a per-db array for internal storage and easy
8
8
* access control checks
9
- * {doc:dbid => {terminus:authority =>
10
- * [terminus:woql_select, terminus:create_document, auth3, ...]}}
11
- *
12
9
*/
10
+ const CONST = require ( './const.js' ) ;
11
+ const ErrorMessage = require ( './errorMessage' ) ;
13
12
13
+ /**
14
+ * @constructor
15
+ * @param {connectionConfig } connectionConfig connectionConfig object containing the connection parameters
16
+ * @param {String } key API key
17
+ */
14
18
function ConnectionCapabilities ( connectionConfig , key ) {
15
19
this . connection = { } ;
16
20
this . connectionConfig = connectionConfig ;
@@ -19,6 +23,10 @@ function ConnectionCapabilities(connectionConfig, key) {
19
23
}
20
24
}
21
25
26
+ /**
27
+ * @summary Retrieves the API key for the given TerminusDB Server
28
+ * @param {String } serverURL optional - URL of TerminusDB - if ommited the current server URL is used
29
+ */
22
30
ConnectionCapabilities . prototype . getClientKey = function ( serverURL ) {
23
31
if ( ! serverURL ) serverURL = this . connectionConfig . serverURL ( ) ;
24
32
if ( serverURL && this . connection [ serverURL ] ) return this . connection [ serverURL ] . key ;
@@ -29,6 +37,12 @@ ConnectionCapabilities.prototype.getClientKey = function (serverURL) {
29
37
/*
30
38
* Utility functions for changing the state of connections with Terminus servers
31
39
*/
40
+
41
+ /**
42
+ * @summary sets the api key for the given url
43
+ * @param {String } curl a valid terminusDB server URL
44
+ * @param {String } key an optional API key
45
+ */
32
46
ConnectionCapabilities . prototype . setClientKey = function ( curl , key ) {
33
47
if ( typeof key === 'string' && key . trim ( ) ) {
34
48
if ( typeof this . connection [ curl ] === 'undefined' ) {
@@ -40,8 +54,8 @@ ConnectionCapabilities.prototype.setClientKey = function (curl, key) {
40
54
41
55
42
56
/**
43
- * @params {string} curl a valid terminusDB server URL
44
- * @params {object} capabilities it is the connect call response
57
+ * @param {string } curl a valid terminusDB server URL
58
+ * @param {object } capabilities the JSON object returned by the connect API call
45
59
*/
46
60
ConnectionCapabilities . prototype . addConnection = function ( curl , capabilities ) {
47
61
if ( typeof this . connection [ curl ] === 'undefined' ) {
@@ -59,9 +73,8 @@ ConnectionCapabilities.prototype.addConnection = function (curl, capabilities) {
59
73
let scope = auths [ i ] [ 'terminus:authority_scope' ] ;
60
74
const actions = auths [ i ] [ 'terminus:action' ] ;
61
75
62
- if ( Array . isArray ( scope ) === false ) scope = [ scope ] ;
63
- const actionsArr = UTILS . authorityActionsToArr ( actions ) ;
64
-
76
+ if ( Array . isArray ( scope ) === false ) scope = [ scope ] ;
77
+ const actionsArr = ( ( Array . isArray ( actions ) ) ? actions . map ( item => item [ '@id' ] ) : [ ] ) ;
65
78
for ( let j = 0 ; j < scope . length ; j += 1 ) {
66
79
const nrec = scope [ j ] ;
67
80
if ( typeof this . connection [ curl ] [ nrec [ '@id' ] ] === 'undefined' ) {
@@ -76,34 +89,20 @@ ConnectionCapabilities.prototype.addConnection = function (curl, capabilities) {
76
89
}
77
90
} ;
78
91
92
+ /**
93
+ * @summary returns true if the client is currently connected to a server
94
+ * @returns {Boolean }
95
+ */
79
96
ConnectionCapabilities . prototype . serverConnected = function ( ) {
80
97
return typeof this . connection [ this . connectionConfig . server ] !== 'undefined' ;
81
98
} ;
82
99
83
- ConnectionCapabilities . prototype . capabilitiesPermit = function ( action , dbid , server ) {
84
- if ( ! this . connectionConfig . connectionMode ( )
85
- || this . connectionConfig . client_checks_capabilities !== true
86
- || action === CONST . CONNECT ) { return true ; }
87
-
88
- server = server || this . connectionConfig . server ;
89
- dbid = dbid || this . connectionConfig . dbid ;
90
-
91
- let rec ;
92
-
93
- if ( action === CONST . CREATE_DATABASE ) {
94
- rec = this . getServerRecord ( server ) ;
95
- } else if ( dbid ) rec = this . getDBRecord ( dbid ) ;
96
- else console . log ( 'no dbid' , server , dbid ) ;
97
- if ( rec ) {
98
- const auths = rec [ 'terminus:authority' ] ;
99
- if ( auths && auths . indexOf ( `terminus:${ action } ` ) !== - 1 ) return true ;
100
- } else {
101
- console . log ( 'problem with ' , this . connection , action , server , dbid ) ;
102
- }
103
- this . error = ErrorMessage . accessDenied ( action , dbid , server ) ;
104
- return false ;
105
- } ;
106
100
101
+ /**
102
+ * @summary retrieves the meta-data record returned by connect for a particular server
103
+ * @param {String } [srvr] optional server URL - if omitted current connection config will be used
104
+ * @returns {terminus:Server } JSON server record as returned by WOQLClient.connect
105
+ */
107
106
ConnectionCapabilities . prototype . getServerRecord = function ( srvr ) {
108
107
const url = ( srvr || this . connectionConfig . server ) ;
109
108
const connectionObj = this . connection [ url ] || { } ;
@@ -117,6 +116,12 @@ ConnectionCapabilities.prototype.getServerRecord = function (srvr) {
117
116
return false ;
118
117
} ;
119
118
119
+ /**
120
+ * @summary retrieves the meta-data record returned by connect for a particular database
121
+ * @param {String } [dbid] optional database id - if omitted current connection config db will be used
122
+ * @param {String } [srvr] optional server URL - if omitted current connection config server will be used
123
+ * @returns {terminus:Database } terminus:Database JSON document as returned by WOQLClient.connect
124
+ */
120
125
ConnectionCapabilities . prototype . getDBRecord = function ( dbid , srvr ) {
121
126
const url = ( srvr || this . connectionConfig . server ) ;
122
127
dbid = ( dbid || this . connectionConfig . dbid ) ;
@@ -132,6 +137,11 @@ ConnectionCapabilities.prototype.getDBRecord = function (dbid, srvr) {
132
137
return undefined ;
133
138
} ;
134
139
140
+ /**
141
+ * @summary retrieves all the db meta-data records returned by connect for a particular server
142
+ * @param {String } [srvr] optional server URL - if omitted current connection config server will be used
143
+ * @returns {[terminus:Database] } array of terminus:Database JSON documents as returned by WOQLClient.connect
144
+ */
135
145
ConnectionCapabilities . prototype . getServerDBRecords = function ( srvr ) {
136
146
const url = ( srvr || this . connectionConfig . server ) ;
137
147
const dbrecs = { } ;
@@ -145,8 +155,11 @@ ConnectionCapabilities.prototype.getServerDBRecords = function (srvr) {
145
155
return dbrecs ;
146
156
} ;
147
157
148
- /*
149
- * removes a database record from the connection registry (after deletion, for example)
158
+ /**
159
+ * @summary removes a database record from the connection registry (after deletion, for example)
160
+ * @param {String } [dbid] optional DB ID - if omitted current connection config db will be used
161
+ * @param {String } [srvr] optional server URL - if omitted current connection config server will be used
162
+ * @returns {[terminus:Database] } array of terminus:Database JSON documents as returned by WOQLClient.connect
150
163
*/
151
164
ConnectionCapabilities . prototype . removeDB = function ( dbid , srvr ) {
152
165
dbid = dbid || this . connectionConfig . dbid ;
@@ -162,24 +175,41 @@ ConnectionCapabilities.prototype.removeDB = function (dbid, srvr) {
162
175
}
163
176
} ;
164
177
165
-
178
+ /**
179
+ * @param {String } dbid local id of database
180
+ * @returns {String } the id of the terminus:Document describing the DB
181
+ * @summary Generates the ID of the terminus:Database document from the database ID
182
+ */
166
183
ConnectionCapabilities . prototype . dbCapabilityID = function ( dbid ) {
167
184
return `doc:${ dbid } ` ;
168
185
} ;
169
186
170
- ConnectionCapabilities . prototype . getDBRecord = function ( dbid , srvr ) {
171
- const url = srvr || this . connectionConfig . server ;
187
+ /**
188
+ * @param {String } action - the action that will be carried out
189
+ * @param {String } [dbid] optional DB ID - if omitted current connection config db will be used
190
+ * @param {String } [srvr] optional server URL - if omitted current connection config server will be used
191
+ * @returns {Boolean } true if the client's capabilities allow the action on the given server / db
192
+ * @summary supports client side access control checks (in addition to server side)
193
+ */
194
+ ConnectionCapabilities . prototype . capabilitiesPermit = function ( action , dbid , server ) {
195
+ server = server || this . connectionConfig . server ;
172
196
dbid = dbid || this . connectionConfig . dbid ;
173
- if ( typeof this . connection [ url ] !== 'object' ) {
174
- return false ;
175
- }
176
- if ( typeof this . connection [ url ] [ dbid ] !== 'undefined' ) { return this . connection [ url ] [ dbid ] ; }
177
-
178
- dbid = this . dbCapabilityID ( dbid ) ;
179
197
180
- if ( typeof this . connection [ url ] [ dbid ] !== 'undefined' ) { return this . connection [ url ] [ dbid ] ; }
198
+ let rec ;
181
199
182
- return undefined ;
200
+ if ( action === CONST . CREATE_DATABASE ) {
201
+ rec = this . getServerRecord ( server ) ;
202
+ } else if ( dbid ) rec = this . getDBRecord ( dbid ) ;
203
+ else console . log ( 'no dbid provided in capabilities check ' , server , dbid ) ;
204
+ if ( rec ) {
205
+ const auths = rec [ 'terminus:authority' ] ;
206
+ if ( auths && auths . indexOf ( `terminus:${ action } ` ) !== - 1 ) return true ;
207
+ } else {
208
+ console . log ( 'No record found for connection: ' , this . connection , action , server , dbid ) ;
209
+ }
210
+ this . error = ErrorMessage . accessDenied ( action , dbid , server ) ;
211
+ return false ;
183
212
} ;
184
213
214
+
185
215
module . exports = ConnectionCapabilities ;
0 commit comments