@Override public boolean contains(final CharSequence label) { return entries.containsKey(Label.of(label)); }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { final JsonObject jsonObject = StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder() .set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, FieldType.regularOrSpecial())) .build()) .collect(JsonCollectors.objectsToObject()); return Optional.ofNullable(jsonObject); }
@Override public Optional<PolicyEntry> getEntryFor(final CharSequence label) { return Optional.ofNullable(entries.get(Label.of(label))); }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { final JsonObject jsonObject = StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder() .set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, FieldType.regularOrSpecial())) .build()) .collect(JsonCollectors.objectsToObject()); return Optional.ofNullable(jsonObject); }
private static Map<ResourceKey, Permissions> getPermissions(final CharSequence l, final Map<Label, Map<ResourceKey, Permissions>> permissionsMap) { return permissionsMap.computeIfAbsent(Label.of(l), k -> new LinkedHashMap<>()); }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { final JsonObject jsonObject = StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder() .set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, FieldType.regularOrSpecial())) .build()) .collect(JsonCollectors.objectsToObject()); return Optional.ofNullable(jsonObject); }
private Map<SubjectId, Subject> retrieveExistingSubjects(final CharSequence label) { return subjects.computeIfAbsent(Label.of(label), l -> new LinkedHashMap<>()); }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { final JsonObject jsonObject = StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder() .set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, FieldType.regularOrSpecial())) .build()) .collect(JsonCollectors.objectsToObject()); return Optional.ofNullable(jsonObject); }
/** * Returns a {@code label} scoped PolicyBuilder where the {@link Label} can be omitted in the builder methods. * * @param label the Label to scope to. * @return the label scoped PolicyBuilder. */ default LabelScoped forLabel(final CharSequence label) { return forLabel(Label.of(label)); }
@Override protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); jsonObjectBuilder.set(JSON_POLICY_ENTRIES, StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder().set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, thePredicate), predicate).build()) .collect(JsonCollectors.objectsToObject()), predicate); }
@Override public Policy removeEntry(final CharSequence label) { final Label lbl = Label.of(label); if (!entries.containsKey(lbl)) { return this; } final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.remove(lbl); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
@Override protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); jsonObjectBuilder.set(JSON_POLICY_ENTRIES, StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder().set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, thePredicate), predicate).build()) .collect(JsonCollectors.objectsToObject()), predicate); }
@Override public ImmutablePolicyBuilder remove(final CharSequence label) { checkNotNull(label, "label of the entry to be removed"); removePolicyEntryFor(Label.of(label)); return this; }
@Override protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); jsonObjectBuilder.set(PolicyModifyCommand.JsonFields.JSON_POLICY_ID, policyId, predicate); jsonObjectBuilder.set(JSON_POLICY_ENTRIES, StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder() .set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, thePredicate), predicate) .build()) .collect(JsonCollectors.objectsToObject()), predicate); }
@Override public Optional<EffectedPermissions> getEffectedPermissionsFor(final CharSequence label, final SubjectId subjectId, final ResourceKey resourceKey) { final Label lbl = Label.of(label); Optional<EffectedPermissions> result = Optional.empty(); final PolicyEntry policyEntry = entries.get(lbl); if (null != policyEntry) { final Subjects subjects = policyEntry.getSubjects(); final Optional<Subject> subjectOptional = subjects.getSubject(subjectId); if (subjectOptional.isPresent()) { final Resources resources = policyEntry.getResources(); result = resources.getResource(resourceKey).map(Resource::getEffectedPermissions); } } return result; }
@Override protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); jsonObjectBuilder.set(PolicyModifyCommand.JsonFields.JSON_POLICY_ID, policyId, predicate); jsonObjectBuilder.set(JSON_POLICY_ENTRIES, StreamSupport.stream(policyEntries.spliterator(), false) .map(entry -> JsonFactory.newObjectBuilder() .set(entry.getLabel().getJsonFieldDefinition(), entry.toJson(schemaVersion, thePredicate), predicate) .build()) .collect(JsonCollectors.objectsToObject()), predicate); }
/** * Returns a new immutable {@link PolicyEntry} with the given authorization subject and permissions. * * @param label the Label of the PolicyEntry to create. * @param subjects the Subjects contained in the PolicyEntry to create. * @param resources the Resources of the PolicyEntry to create. * @return the new Policy entry. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code label} is empty. */ public static PolicyEntry newPolicyEntry(final CharSequence label, final Iterable<Subject> subjects, final Iterable<Resource> resources) { return ImmutablePolicyEntry.of(Label.of(label), newSubjects(subjects), newResources(resources)); }
@Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate) { checkNotNull(schemaVersion, "schema version"); checkNotNull(thePredicate, "predicate"); final Predicate<JsonField> predicate = schemaVersion.and(thePredicate); final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder(); jsonObjectBuilder.set(JsonFields.SCHEMA_VERSION, schemaVersion.toInt(), predicate); if (null != lifecycle) { jsonObjectBuilder.set(JsonFields.LIFECYCLE, lifecycle.name(), predicate); } if (null != revision) { jsonObjectBuilder.set(JsonFields.REVISION, revision.toLong(), predicate); } if (null != modified) { jsonObjectBuilder.set(JsonFields.MODIFIED, modified.toString(), predicate); } if (null != policyId) { jsonObjectBuilder.set(JsonFields.NAMESPACE, namespace, predicate); jsonObjectBuilder.set(JsonFields.ID, policyId, predicate); } jsonObjectBuilder.set(JsonFields.ENTRIES, stream() .map(policyEntry -> JsonFactory.newObjectBuilder() .set(policyEntry.getLabel().getJsonFieldDefinition(), policyEntry.toJson(schemaVersion, thePredicate.and(FieldType.notHidden())), predicate) // notice: only "not HIDDEN" sub-fields of PolicyEntry are included .build()) .collect(JsonCollectors.objectsToObject()), predicate); return jsonObjectBuilder.build(); }
@Override public Policy removeSubjectFor(final CharSequence label, final SubjectId subjectId) { final Label lbl = Label.of(label); Policy result = this; final PolicyEntry existingPolicyEntry = entries.get(lbl); if (null != existingPolicyEntry) { final Subjects existingSubjects = existingPolicyEntry.getSubjects(); final Subjects newSubjects = existingSubjects.removeSubject(subjectId); if (!Objects.equals(existingSubjects, newSubjects)) { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(lbl, newPolicyEntry(lbl, newSubjects, existingPolicyEntry.getResources())); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } } return result; }
@Override public Policy removeResourceFor(final CharSequence label, final ResourceKey resourceKey) { final Label lbl = Label.of(label); Policy result = this; final PolicyEntry existingEntry = entries.get(lbl); if (null != existingEntry) { final Resources existingResources = existingEntry.getResources(); final Resources newResources = existingResources.removeResource(resourceKey); if (!Objects.equals(existingResources, newResources)) { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(lbl, newPolicyEntry(lbl, existingEntry.getSubjects(), newResources)); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } } return result; }