-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractFieldBsonCreator.java
137 lines (111 loc) · 5.3 KB
/
AbstractFieldBsonCreator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.thingsearch.service.persistence.read.expression.visitors;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.DOT;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_GRANTED;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_POLICY;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_REVOKED;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.bson.conversions.Bson;
import org.eclipse.ditto.internal.utils.persistence.mongo.KeyNameReviser;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants;
import com.mongodb.client.model.Filters;
/**
* Base class for visitors that create filter Bson from expressions.
*/
public abstract class AbstractFieldBsonCreator {
static final CharSequence FEATURE_ID_WILDCARD = "*";
private static final KeyNameReviser KEY_NAME_REVISER = KeyNameReviser.escapeProblematicPlainChars();
@Nullable
private final List<String> authorizationSubjectIds;
AbstractFieldBsonCreator(@Nullable final List<String> authorizationSubjectIds) {
this.authorizationSubjectIds = authorizationSubjectIds;
}
/**
* Create filter BSON for global readability.
*
* @param authorizationSubjectIds authorization subject IDs for visibility restriction.
* @return the BSON filter.
*/
public static Bson getGlobalReadBson(final Iterable<String> authorizationSubjectIds) {
return Filters.in(PersistenceConstants.FIELD_GLOBAL_READ, authorizationSubjectIds);
}
abstract Bson visitPointer(final String pointer);
abstract Bson visitRootLevelField(final String fieldName);
// simple fields, attributes and direct (no wildcard) feature
Optional<Bson> getAuthorizationBson(final JsonPointer pointer) {
return Optional.ofNullable(authorizationSubjectIds)
.map(subjectIds -> getAuthFilter(authorizationSubjectIds, pointer));
}
Optional<Bson> getFeatureWildcardAuthorizationBson(final JsonPointer pointer) {
final Stream<JsonPointer> fixedPaths = Stream.of(
JsonPointer.empty(), // root grants
JsonPointer.of("/features"), // features grants
JsonPointer.of("/id")); // feature grants
return Optional.ofNullable(authorizationSubjectIds).map(nonNullSubjectsIds ->
Stream.concat(fixedPaths, collectPaths(pointer)).reduce(null,
(child, p) -> getAuthFilter(nonNullSubjectsIds, p, child),
(b1, b2) -> b1) // TODO
);
}
public final Bson visitSimple(final String fieldName) {
return fieldName.startsWith(PersistenceConstants.SLASH)
? visitPointer(fieldName)
: visitRootLevelField(fieldName);
}
private Bson getAuthFilter(final List<String> authSubjectIds, final JsonPointer pointer) {
if (pointer.isEmpty()) {
return getAuthFilter(authSubjectIds, pointer, null);
} else {
return getAuthFilter(authSubjectIds, pointer, getAuthFilter(authSubjectIds, pointer.cutLeaf()));
}
}
private Bson getAuthFilter(final Iterable<String> authSubjectIds, final CharSequence pointer,
@Nullable final Bson child) {
// p.<pointer>.g
final var grant = String.join(DOT, FIELD_POLICY, pointer, FIELD_GRANTED);
// p.<pointer>.r
final var revoke = String.join(DOT, FIELD_POLICY, pointer, FIELD_REVOKED);
final Bson or = child != null
? Filters.or(Filters.in(grant, authSubjectIds), child)
: Filters.or(Filters.in(grant, authSubjectIds));
return Filters.and(Filters.nin(revoke, authSubjectIds), or);
}
/**
* Collects all paths contained in pointer, excluding the root pointer.
*
* @param pointer the pointer for which to collect all contained paths
* @return stream of paths contained in pointer, without root pointer.
*/
private Stream<CharSequence> collectPaths(final JsonPointer pointer) {
if (pointer.isEmpty()) {
return Stream.empty();
}
return Stream.concat(collectPaths(pointer.cutLeaf()), Stream.of(pointer));
}
static String toDottedPath(final CharSequence prefix, final Iterable<JsonKey> jsonPointer) {
return String.join(DOT, prefix, toDottedPath(jsonPointer));
}
static String toDottedPath(final Iterable<JsonKey> jsonPointer) {
return StreamSupport.stream(jsonPointer.spliterator(), false)
.map(k -> KEY_NAME_REVISER.apply(k.toString()))
.collect(Collectors.joining("."));
}
}