@@ -8,6 +8,9 @@ var currentQueue;
8
8
var queueIndex = - 1 ;
9
9
10
10
function cleanUpNextTick ( ) {
11
+ if ( ! draining || ! currentQueue ) {
12
+ return ;
13
+ }
11
14
draining = false ;
12
15
if ( currentQueue . length ) {
13
16
queue = currentQueue . concat ( queue ) ;
@@ -961,6 +964,29 @@ Kuzzle.prototype.whoAmI = function (callback) {
961
964
return self ;
962
965
} ;
963
966
967
+ /**
968
+ * Gets the rights array of the currently logged user.
969
+ *
970
+ * @param {function } cb The callback containing the normalized array of rights.
971
+ */
972
+ Kuzzle . prototype . getMyRights = function ( options , cb ) {
973
+ var self = this ;
974
+
975
+ if ( ! cb && typeof options === 'function' ) {
976
+ cb = options ;
977
+ options = null ;
978
+ }
979
+
980
+ self . callbackRequired ( 'Kuzzle.getMyRights' , cb ) ;
981
+
982
+ self . query ( { controller : 'auth' , action :'getMyRights' } , { } , null , function ( err , res ) {
983
+ if ( err ) {
984
+ return cb ( err ) ;
985
+ }
986
+
987
+ cb ( null , res . result . hits ) ;
988
+ } ) ;
989
+ } ;
964
990
965
991
/**
966
992
* Update current user in Kuzzle.
@@ -1242,25 +1268,27 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
1242
1268
* Create a new instance of a KuzzleDataCollection object.
1243
1269
* If no index is specified, takes the default index.
1244
1270
*
1245
- * @param {string } [index] - The name of the data index containing the data collection
1246
1271
* @param {string } collection - The name of the data collection you want to manipulate
1272
+ * @param {string } [index] - The name of the data index containing the data collection
1247
1273
* @returns {object } A KuzzleDataCollection instance
1248
1274
*/
1249
- Kuzzle . prototype . dataCollectionFactory = function ( index , collection ) {
1275
+ Kuzzle . prototype . dataCollectionFactory = function ( collection , index ) {
1250
1276
this . isValid ( ) ;
1251
1277
1252
- if ( arguments . length === 1 ) {
1253
- collection = arguments [ 0 ] ;
1278
+ if ( ! index ) {
1279
+ if ( ! this . defaultIndex ) {
1280
+ throw new Error ( 'Unable to create a new data collection object: no index specified' ) ;
1281
+ }
1282
+
1254
1283
index = this . defaultIndex ;
1255
1284
}
1256
- else if ( arguments . length === 2 && typeof collection === 'object' ) {
1257
- headers = collection ;
1258
- collection = index ;
1259
- index = this . defaultIndex ;
1285
+
1286
+ if ( typeof index !== 'string' ) {
1287
+ throw new Error ( 'Invalid "index" argument: string expected, got ' + typeof index ) ;
1260
1288
}
1261
1289
1262
- if ( ! index ) {
1263
- throw new Error ( 'Unable to create a new data collection object: no index specified' ) ;
1290
+ if ( typeof collection !== 'string' ) {
1291
+ throw new Error ( 'Invalid "collection" argument: string expected, got ' + typeof collection ) ;
1264
1292
}
1265
1293
1266
1294
if ( ! this . collections [ index ] ) {
@@ -2134,14 +2162,27 @@ KuzzleDataCollection.prototype.fetchDocument = function (documentId, options, cb
2134
2162
* @returns {Object } this
2135
2163
*/
2136
2164
KuzzleDataCollection . prototype . fetchAllDocuments = function ( options , cb ) {
2165
+ var filters = { } ;
2166
+
2137
2167
if ( ! cb && typeof options === 'function' ) {
2138
2168
cb = options ;
2139
2169
options = null ;
2140
2170
}
2141
2171
2172
+ // copying pagination options to the search filter
2173
+ if ( options ) {
2174
+ if ( options . from ) {
2175
+ filters . from = options . from ;
2176
+ }
2177
+
2178
+ if ( options . size ) {
2179
+ filters . size = options . size ;
2180
+ }
2181
+ }
2182
+
2142
2183
this . kuzzle . callbackRequired ( 'KuzzleDataCollection.fetchAll' , cb ) ;
2143
2184
2144
- this . advancedSearch ( { } , options , cb ) ;
2185
+ this . advancedSearch ( filters , options , cb ) ;
2145
2186
2146
2187
return this ;
2147
2188
} ;
@@ -3769,7 +3810,7 @@ function KuzzleSecurity(kuzzle) {
3769
3810
return this . kuzzle . bluebird . promisifyAll ( this , {
3770
3811
suffix : 'Promise' ,
3771
3812
filter : function ( name , func , target , passes ) {
3772
- var blacklist = [ 'roleFactory' , 'profileFactory' , 'userFactory' ] ;
3813
+ var blacklist = [ 'roleFactory' , 'profileFactory' , 'userFactory' , 'isActionAllowed' ] ;
3773
3814
3774
3815
return passes && blacklist . indexOf ( name ) === - 1 ;
3775
3816
}
@@ -4461,8 +4502,95 @@ KuzzleSecurity.prototype.userFactory = function(id, content) {
4461
4502
return new KuzzleUser ( this , id , content ) ;
4462
4503
} ;
4463
4504
4505
+ /**
4506
+ * Tells whether an action is allowed, denied or conditional based on the rights
4507
+ * rights provided as the first argument. An action is defined as a couple of
4508
+ * action and controller (mandatory), plus an index and a collection(optional).
4509
+ *
4510
+ * @param {object } rights - The rights rights associated to a user
4511
+ * (see getMyrights and getUserrights).
4512
+ * @param {string } controller - The controller to check the action onto.
4513
+ * @param {string } action - The action to perform.
4514
+ * @param {string } index - (optional) The name of index to perform the action onto.
4515
+ * @param {string } collection - (optional) The name of the collection to perform the action onto.
4516
+ *
4517
+ * @returns {string } ['allowed', 'denied', 'conditional'] where conditional cases
4518
+ * correspond to rights containing closures.
4519
+ * See also http://kuzzle.io/guide/#roles-definition
4520
+ */
4521
+ KuzzleSecurity . prototype . isActionAllowed = function ( rights , controller , action , index , collection ) {
4522
+ var filteredRights ;
4523
+
4524
+ if ( ! rights || typeof rights !== 'object' ) {
4525
+ throw new Error ( 'rights parameter is mandatory for isActionAllowed function' ) ;
4526
+ }
4527
+ if ( ! controller || typeof controller !== 'string' ) {
4528
+ throw new Error ( 'controller parameter is mandatory for isActionAllowed function' ) ;
4529
+ }
4530
+ if ( ! action || typeof action !== 'string' ) {
4531
+ throw new Error ( 'action parameter is mandatory for isActionAllowed function' ) ;
4532
+ }
4533
+
4534
+ // We filter in all the rights that match the request (including wildcards).
4535
+ filteredRights = rights . filter ( function ( right ) {
4536
+ return right . controller === controller || right . controller === '*' ;
4537
+ } )
4538
+ . filter ( function ( right ) {
4539
+ return right . action === action || right . action === '*' ;
4540
+ } )
4541
+ . filter ( function ( right ) {
4542
+ return right . index === index || right . index === '*' ;
4543
+ } )
4544
+ . filter ( function ( right ) {
4545
+ return right . collection === collection || right . collection === '*' ;
4546
+ } ) ;
4547
+
4548
+ // Then, if at least one right allows the action, we return 'allowed'
4549
+ if ( filteredRights . some ( function ( item ) { return item . value === 'allowed' ; } ) ) {
4550
+ return 'allowed' ;
4551
+ }
4552
+ // If no right allows the action, we check for conditionals.
4553
+ if ( filteredRights . some ( function ( item ) { return item . value === 'conditional' ; } ) ) {
4554
+ return 'conditional' ;
4555
+ }
4556
+ // Otherwise we return 'denied'.
4557
+ return 'denied' ;
4558
+ } ;
4559
+
4560
+
4561
+ /**
4562
+ * Gets the rights array of a given user.
4563
+ *
4564
+ * @param {string } userId The id of the user.
4565
+ * @param {function } cb The callback containing the normalized array of rights.
4566
+ */
4567
+ KuzzleSecurity . prototype . getUserRights = function ( userId , options , cb ) {
4568
+ var
4569
+ data = { _id : userId } ,
4570
+ self = this ;
4571
+
4572
+ if ( ! userId || typeof userId !== 'string' ) {
4573
+ throw new Error ( 'userId parameter is mandatory for isActionAllowed function' ) ;
4574
+ }
4575
+
4576
+ if ( ! cb && typeof options === 'function' ) {
4577
+ cb = options ;
4578
+ options = null ;
4579
+ }
4580
+
4581
+ self . kuzzle . callbackRequired ( 'Kuzzle.getUserRights' , cb ) ;
4582
+
4583
+ this . kuzzle . query ( this . buildQueryArgs ( 'getUserRights' ) , data , options , function ( err , res ) {
4584
+ if ( err ) {
4585
+ return cb ( err ) ;
4586
+ }
4587
+
4588
+ cb ( null , res . result . hits ) ;
4589
+ } ) ;
4590
+ } ;
4464
4591
4465
4592
module . exports = KuzzleSecurity ;
4593
+
4466
4594
} , { "./kuzzleProfile" :9 , "./kuzzleRole" :10 , "./kuzzleUser" :13 } ] , 12 :[ function ( require , module , exports ) {
4467
4595
function KuzzleSecurityDocument ( kuzzleSecurity , id , content ) {
4468
4596
0 commit comments