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
55 * Creates an entry in the connection registry for the server
66 * and all the databases that the client has access to
77 * maps the input authorties to a per-db array for internal storage and easy
88 * access control checks
9- * {doc:dbid => {terminus:authority =>
10- * [terminus:woql_select, terminus:create_document, auth3, ...]}}
11- *
129 */
10+ const CONST = require ( './const.js' ) ;
11+ const ErrorMessage = require ( './errorMessage' ) ;
1312
13+ /**
14+ * @constructor
15+ * @param {connectionConfig } connectionConfig connectionConfig object containing the connection parameters
16+ * @param {String } key API key
17+ */
1418function ConnectionCapabilities ( connectionConfig , key ) {
1519 this . connection = { } ;
1620 this . connectionConfig = connectionConfig ;
@@ -19,6 +23,10 @@ function ConnectionCapabilities(connectionConfig, key) {
1923 }
2024}
2125
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+ */
2230ConnectionCapabilities . prototype . getClientKey = function ( serverURL ) {
2331 if ( ! serverURL ) serverURL = this . connectionConfig . serverURL ( ) ;
2432 if ( serverURL && this . connection [ serverURL ] ) return this . connection [ serverURL ] . key ;
@@ -29,6 +37,12 @@ ConnectionCapabilities.prototype.getClientKey = function (serverURL) {
2937/*
3038 * Utility functions for changing the state of connections with Terminus servers
3139 */
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+ */
3246ConnectionCapabilities . prototype . setClientKey = function ( curl , key ) {
3347 if ( typeof key === 'string' && key . trim ( ) ) {
3448 if ( typeof this . connection [ curl ] === 'undefined' ) {
@@ -40,8 +54,8 @@ ConnectionCapabilities.prototype.setClientKey = function (curl, key) {
4054
4155
4256/**
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
4559 */
4660ConnectionCapabilities . prototype . addConnection = function ( curl , capabilities ) {
4761 if ( typeof this . connection [ curl ] === 'undefined' ) {
@@ -59,9 +73,8 @@ ConnectionCapabilities.prototype.addConnection = function (curl, capabilities) {
5973 let scope = auths [ i ] [ 'terminus:authority_scope' ] ;
6074 const actions = auths [ i ] [ 'terminus:action' ] ;
6175
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' ] ) : [ ] ) ;
6578 for ( let j = 0 ; j < scope . length ; j += 1 ) {
6679 const nrec = scope [ j ] ;
6780 if ( typeof this . connection [ curl ] [ nrec [ '@id' ] ] === 'undefined' ) {
@@ -76,34 +89,20 @@ ConnectionCapabilities.prototype.addConnection = function (curl, capabilities) {
7689 }
7790} ;
7891
92+ /**
93+ * @summary returns true if the client is currently connected to a server
94+ * @returns {Boolean }
95+ */
7996ConnectionCapabilities . prototype . serverConnected = function ( ) {
8097 return typeof this . connection [ this . connectionConfig . server ] !== 'undefined' ;
8198} ;
8299
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- } ;
106100
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+ */
107106ConnectionCapabilities . prototype . getServerRecord = function ( srvr ) {
108107 const url = ( srvr || this . connectionConfig . server ) ;
109108 const connectionObj = this . connection [ url ] || { } ;
@@ -117,6 +116,12 @@ ConnectionCapabilities.prototype.getServerRecord = function (srvr) {
117116 return false ;
118117} ;
119118
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+ */
120125ConnectionCapabilities . prototype . getDBRecord = function ( dbid , srvr ) {
121126 const url = ( srvr || this . connectionConfig . server ) ;
122127 dbid = ( dbid || this . connectionConfig . dbid ) ;
@@ -132,6 +137,11 @@ ConnectionCapabilities.prototype.getDBRecord = function (dbid, srvr) {
132137 return undefined ;
133138} ;
134139
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+ */
135145ConnectionCapabilities . prototype . getServerDBRecords = function ( srvr ) {
136146 const url = ( srvr || this . connectionConfig . server ) ;
137147 const dbrecs = { } ;
@@ -145,8 +155,11 @@ ConnectionCapabilities.prototype.getServerDBRecords = function (srvr) {
145155 return dbrecs ;
146156} ;
147157
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
150163 */
151164ConnectionCapabilities . prototype . removeDB = function ( dbid , srvr ) {
152165 dbid = dbid || this . connectionConfig . dbid ;
@@ -162,24 +175,41 @@ ConnectionCapabilities.prototype.removeDB = function (dbid, srvr) {
162175 }
163176} ;
164177
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+ */
166183ConnectionCapabilities . prototype . dbCapabilityID = function ( dbid ) {
167184 return `doc:${ dbid } ` ;
168185} ;
169186
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 ;
172196 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 ) ;
179197
180- if ( typeof this . connection [ url ] [ dbid ] !== 'undefined' ) { return this . connection [ url ] [ dbid ] ; }
198+ let rec ;
181199
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 ;
183212} ;
184213
214+
185215module . exports = ConnectionCapabilities ;
0 commit comments