@@ -188,7 +188,7 @@ export class DatabaseConnection {
188188 * @returns Promise<T[]> Array of results
189189 */
190190 // tslint:disable-next-line: max-line-length
191- async query < T extends Base > ( query : any , projection : Object , top : number , skip : number , orderby : Object | string , collectionname : string , jwt : string ) : Promise < T [ ] > {
191+ async query < T extends Base > ( query : any , projection : Object , top : number , skip : number , orderby : Object | string , collectionname : string , jwt : string , queryas : string = null ) : Promise < T [ ] > {
192192 var arr : T [ ] = [ ] ;
193193 await this . connect ( ) ;
194194 var mysort : Object = { } ;
@@ -236,13 +236,22 @@ export class DatabaseConnection {
236236 var _query : Object = { } ;
237237 if ( collectionname === "files" ) { collectionname = "fs.files" ; }
238238 if ( collectionname === "fs.files" ) {
239- _query = { $and : [ query , this . getbasequery ( jwt , "metadata._acl" , [ Rights . read ] ) ] } ;
239+ if ( ! Util . IsNullEmpty ( queryas ) ) {
240+ _query = { $and : [ query , this . getbasequery ( jwt , "metadata._acl" , [ Rights . read ] ) , await this . getbasequeryuserid ( queryas , "metadata._acl" , [ Rights . read ] ) ] } ;
241+ } else {
242+ _query = { $and : [ query , this . getbasequery ( jwt , "metadata._acl" , [ Rights . read ] ) ] } ;
243+ }
240244 projection = null ;
241245 } else {
242- if ( ! collectionname . endsWith ( "_hist" ) ) {
243- _query = { $and : [ query , this . getbasequery ( jwt , "_acl" , [ Rights . read ] ) ] } ;
246+ // if (!collectionname.endsWith("_hist")) {
247+ // _query = { $and: [query, this.getbasequery(jwt, "_acl", [Rights.read])] };
248+ // } else {
249+ // // todo: enforcer permissions when fetching _hist ?
250+ // _query = { $and: [query, this.getbasequery(jwt, "_acl", [Rights.read])] };
251+ // }
252+ if ( ! Util . IsNullEmpty ( queryas ) ) {
253+ _query = { $and : [ query , this . getbasequery ( jwt , "_acl" , [ Rights . read ] ) , await this . getbasequeryuserid ( queryas , "_acl" , [ Rights . read ] ) ] } ;
244254 } else {
245- // todo: enforcer permissions when fetching _hist ?
246255 _query = { $and : [ query , this . getbasequery ( jwt , "_acl" , [ Rights . read ] ) ] } ;
247256 }
248257 }
@@ -936,31 +945,13 @@ export class DatabaseConnection {
936945 } ;
937946 finalor . push ( q2 ) ;
938947 }
939- //
940- // if (bits.length > 0 && (bits[0] + 1) == Rights.read) {
941- // this._logger.debug("[" + user.username + "] Include isme in base query");
942- // return { $or: finalor.concat(isme) };
943- // } else if (bits.length > 0) {
944- // this._logger.debug("[" + user.username + "] Skip isme in base query, not read (" + bits[0] + ")");
945- // } else {
946- // this._logger.debug("[" + user.username + "] Skip isme in base query, bits missing!");
947- // }
948- // if(bits.length==1 && (bits[0]+1) == Rights.read)
949- // {
950- // for (var i: number = 0; i < user.roles.length; i++) {
951- // var role = user.roles[i];
952- // if(role._id!=WellknownIds.admins && role._id!=WellknownIds.robots && role._id!=WellknownIds.nodered_users &&
953- // role._id!=WellknownIds.nodered_admins && role._id!=WellknownIds.nodered_api_users && role._id!=WellknownIds.filestore_users &&
954- // role._id!=WellknownIds.filestore_admins && role._id!=WellknownIds.robot_users && role._id!=WellknownIds.robot_admins
955- // && role._id!=WellknownIds.personal_nodered_users)
956- // {
957-
958- // }
959- // }
960-
961- // }
962948 return { $or : finalor . concat ( ) } ;
963949 }
950+ private async getbasequeryuserid ( userid : string , field : string , bits : number [ ] ) : Promise < Object > {
951+ var user = await User . FindByUsernameOrId ( null , userid ) ;
952+ var jwt = Crypt . createToken ( user , "5m" ) ;
953+ return this . getbasequery ( jwt , field , bits ) ;
954+ }
964955 /**
965956 * Ensure _type and _acs on object
966957 * @param {T } item Object to validate
0 commit comments