-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Ensure predefined aggregation variables (e.g., @user, @mongoPermiss…
…ion) are available in change stream aggregations
- Loading branch information
Showing
4 changed files
with
97 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,16 +20,12 @@ | |
*/ | ||
package org.restheart.mongodb.handlers.aggregation; | ||
|
||
import com.mongodb.MongoCommandException; | ||
import com.mongodb.client.AggregateIterable; | ||
import com.mongodb.client.MapReduceIterable; | ||
import io.undertow.server.HttpServerExchange; | ||
import java.util.ArrayList; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.bson.BsonArray; | ||
import org.bson.BsonDocument; | ||
import org.bson.BsonInt32; | ||
import org.bson.BsonNull; | ||
import org.bson.BsonString; | ||
import org.restheart.exchange.IllegalQueryParamenterException; | ||
import org.restheart.exchange.InvalidMetadataException; | ||
|
@@ -39,18 +35,19 @@ | |
import org.restheart.handlers.PipelinedHandler; | ||
import org.restheart.mongodb.MongoServiceConfiguration; | ||
import org.restheart.mongodb.db.Databases; | ||
import org.restheart.security.AclVarsInterpolator; | ||
import org.restheart.security.MongoPermissions; | ||
import org.restheart.security.MongoRealmAccount; | ||
import org.restheart.security.WithProperties; | ||
import org.restheart.utils.BsonUtils; | ||
import org.restheart.utils.HttpStatus; | ||
import org.restheart.mongodb.utils.StagesInterpolator; | ||
import static org.restheart.mongodb.utils.StagesInterpolator.STAGE_OPERATOR; | ||
import static org.restheart.mongodb.utils.VarsInterpolator.VAR_OPERATOR; | ||
import org.restheart.mongodb.utils.StagesInterpolator.STAGE_OPERATOR; | ||
import org.restheart.mongodb.utils.VarsInterpolator.VAR_OPERATOR; | ||
import org.restheart.utils.HttpStatus; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.mongodb.MongoCommandException; | ||
import com.mongodb.client.AggregateIterable; | ||
import com.mongodb.client.MapReduceIterable; | ||
|
||
import io.undertow.server.HttpServerExchange; | ||
|
||
/** | ||
* | ||
* @author Andrea Di Cesare {@literal <[email protected]>} | ||
|
@@ -117,7 +114,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { | |
: request.getAggregationVars(); | ||
|
||
// add the default variables to the avars document | ||
injectAvars(request, avars); | ||
StagesInterpolator.injectAvars(request, avars); | ||
|
||
switch (query.getType()) { | ||
case MAP_REDUCE -> { | ||
|
@@ -237,69 +234,6 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { | |
} | ||
} | ||
|
||
/** | ||
* adds the default variables to the avars document | ||
* | ||
* Supports accounts handled by MongoRealAuthenticator, | ||
* FileRealmAuthenticator and JwtAuthenticationMechanism | ||
* | ||
* @param request | ||
* @param avars | ||
*/ | ||
private void injectAvars(MongoRequest request, BsonDocument avars) { | ||
// add @page, @pagesize, @limit and @skip to avars to allow handling | ||
// paging in the aggragation via default page and pagesize qparams | ||
avars.put("@page", new BsonInt32(request.getPage())); | ||
avars.put("@pagesize", new BsonInt32(request.getPagesize())); | ||
avars.put("@limit", new BsonInt32(request.getPagesize())); | ||
avars.put("@skip", new BsonInt32(request.getPagesize() * (request.getPage() - 1))); | ||
|
||
// add @mongoPermissions to avars | ||
var mongoPermissions = MongoPermissions.of(request); | ||
if (mongoPermissions != null) { | ||
avars.put("@mongoPermissions" ,mongoPermissions.asBson()); | ||
|
||
avars.put("@mongoPermissions.projectResponse", mongoPermissions.getProjectResponse() == null | ||
? BsonNull.VALUE | ||
: mongoPermissions.getProjectResponse()); | ||
|
||
avars.put("@mongoPermissions.mergeRequest", mongoPermissions.getMergeRequest() == null | ||
? BsonNull.VALUE | ||
: AclVarsInterpolator.interpolateBson(request, mongoPermissions.getMergeRequest())); | ||
|
||
avars.put("@mongoPermissions.readFilter", mongoPermissions.getReadFilter() == null | ||
? BsonNull.VALUE | ||
: AclVarsInterpolator.interpolateBson(request, mongoPermissions.getReadFilter())); | ||
|
||
avars.put("@mongoPermissions.writeFilter", mongoPermissions.getWriteFilter() == null | ||
? BsonNull.VALUE | ||
: AclVarsInterpolator.interpolateBson(request, mongoPermissions.getWriteFilter())); | ||
} else { | ||
avars.put("@mongoPermissions", new MongoPermissions().asBson()); | ||
avars.put("@mongoPermissions.projectResponse", BsonNull.VALUE); | ||
avars.put("@mongoPermissions.mergeRequest", BsonNull.VALUE); | ||
avars.put("@mongoPermissions.readFilter", BsonNull.VALUE); | ||
avars.put("@mongoPermissions.writeFilter", BsonNull.VALUE); | ||
} | ||
|
||
// add @user to avars | ||
var account = request.getAuthenticatedAccount(); | ||
|
||
if (account == null) { | ||
avars.put("@user", BsonNull.VALUE); | ||
} else if (account instanceof MongoRealmAccount maccount) { | ||
var ba = maccount.properties(); | ||
avars.put("@user", ba); | ||
ba.keySet().forEach(k -> avars.put("@user.".concat(k), ba.get(k))); | ||
} else if (account instanceof WithProperties<?> accountWithProperties) { | ||
var ba = BsonUtils.toBsonDocument(accountWithProperties.propertiesAsMap()); | ||
avars.put("@user", ba); | ||
ba.keySet().forEach(k -> avars.put("@user.".concat(k), ba.get(k))); | ||
} else { | ||
avars.put("@user", BsonNull.VALUE); | ||
} | ||
} | ||
|
||
public static String mongoCommandExceptionError(MongoCommandException mce) { | ||
var mongoErrorResponse = mce.getResponse(); | ||
var errorCode = mongoErrorResponse.getNumber("code", new BsonInt32(-1)).intValue(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters