Skip to content

Commit

Permalink
✨ add @user predefined argument support in Field-to-Aggregation mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Nov 21, 2024
1 parent fc59bd2 commit 8249ee9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static void injectAvars(MongoRequest request, BsonDocument avars) {
// add @mongoPermissions to avars
var mongoPermissions = MongoPermissions.of(request);
if (mongoPermissions != null) {
avars.put("@mongoPermissions" ,mongoPermissions.asBson());
avars.put("@mongoPermissions", mongoPermissions.asBson());

avars.put("@mongoPermissions.projectResponse", mongoPermissions.getProjectResponse() == null
? BsonNull.VALUE
Expand Down
11 changes: 11 additions & 0 deletions graphql/src/main/java/org/restheart/graphql/GraphQLService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.restheart.plugins.OnInit;
import org.restheart.plugins.RegisterPlugin;
import org.restheart.plugins.Service;
import org.restheart.security.MongoRealmAccount;
import org.restheart.security.WithProperties;
import org.restheart.utils.BsonUtils;
import static org.restheart.utils.BsonUtils.document;
import org.restheart.utils.HttpStatus;
Expand Down Expand Up @@ -179,6 +181,15 @@ public void handle(GraphQLRequest req, GraphQLResponse res) throws Exception {

var localContext = document();

// add authenticated account properties to local context
// used by AggregationMapping to interpolate args as @user and @user._id
switch (req.getAuthenticatedAccount()) {
case null -> localContext.put("@user", document());
case MongoRealmAccount ma -> localContext.put("@user", ma.properties());
case WithProperties<?> awp -> localContext.put("@user", BsonUtils.toBsonDocument(awp.propertiesAsMap()));
default -> localContext.put("@user", document());
}

// add the query-time-limit to the local context;
if (this.queryTimeLimit > 0) {
localContext.put("query-time-limit", this.queryTimeLimit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonNull;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.dataloader.DataLoader;
Expand Down Expand Up @@ -96,6 +97,11 @@ public List<BsonDocument> interpolateArgs(DataFetchingEnvironment env) throws Qu
values.put("rootDoc", rootDoc);
}

// add the @user args
var user = locaLContext.getDocument("@user");
values.put("@user", user.isEmpty() ? BsonNull.VALUE : user);
user.entrySet().stream().forEach(e -> values.put("@user.".concat(e.getKey()), e.getValue()));

try {
var argInterpolated = StagesInterpolator.interpolate(VAR_OPERATOR.$arg, STAGE_OPERATOR.$ifarg, stages, values);
var argAndFkInterpolated = new ArrayList<BsonDocument>();
Expand Down

0 comments on commit 8249ee9

Please sign in to comment.