@Override public Collection<? extends P> getPlainAttrs(final String plainSchema) { return internalGetPlainAttrs().stream().filter(plainAttr -> plainAttr != null && plainAttr.getSchema() != null && plainSchema.equals(plainAttr.getSchema().getKey())). collect(Collectors.toList()); }
@Override public Optional<? extends GPlainAttr> getPlainAttr(final String plainSchema) { return getPlainAttrs().stream().filter(plainAttr -> plainAttr != null && plainAttr.getSchema() != null && plainSchema.equals(plainAttr.getSchema().getKey())).findFirst(); }
public static void addPlainAttrsToContext( final Collection<? extends PlainAttr<?>> attrs, final JexlContext jexlContext) { attrs.stream().filter(attr -> attr.getSchema() != null).forEach(attr -> { List<String> attrValues = attr.getValuesAsStrings(); String expressionValue = attrValues.isEmpty() ? StringUtils.EMPTY : attrValues.get(0); LOG.debug("Add attribute {} with value {}", attr.getSchema().getKey(), expressionValue); jexlContext.set(attr.getSchema().getKey(), expressionValue); }); }
@Override public Optional<? extends CPlainAttr> getPlainAttr(final String plainSchema) { return getPlainAttrs().stream().filter(plainAttr -> plainAttr != null && plainAttr.getSchema() != null && plainSchema.equals(plainAttr.getSchema().getKey())).findFirst(); }
private void checkMandatory( final PlainSchema schema, final PlainAttr<?> attr, final Any<?> any, final SyncopeClientException reqValMissing) { if (attr == null && !schema.isReadonly() && JexlUtils.evaluateMandatoryCondition(schema.getMandatoryCondition(), any)) { LOG.error("Mandatory schema " + schema.getKey() + " not provided with values"); reqValMissing.getElements().add(schema.getKey()); } }
protected Map<String, String> findAllSyncopeConfs() { return confDAO.get().getPlainAttrs().stream().collect( Collectors.toMap(attr -> attr.getSchema().getKey(), attr -> attr.getValuesAsStrings().get(0))); } }
private void fillAttr( final List<String> values, final AnyUtils anyUtils, final PlainSchema schema, final PlainAttr<?> attr, final SyncopeClientException invalidValues) { // if schema is multivalue, all values are considered for addition; // otherwise only the fist one - if provided - is considered List<String> valuesProvided = schema.isMultivalue() ? values : (values.isEmpty() ? Collections.<String>emptyList() : Collections.singletonList(values.get(0))); valuesProvided.forEach(value -> { if (StringUtils.isBlank(value)) { LOG.debug("Null value for {}, ignoring", schema.getKey()); } else { try { attr.add(value, anyUtils); } catch (InvalidPlainAttrValueException e) { String valueToPrint = value.length() > 40 ? value.substring(0, 20) + "..." : value; LOG.warn("Invalid value for attribute " + schema.getKey() + ": " + valueToPrint, e); invalidValues.getElements().add(schema.getKey() + ": " + valueToPrint + " - " + e.getMessage()); } } }); }
@Override public Optional<? extends P> getPlainAttr(final String plainSchema) { return internalGetPlainAttrs().stream().filter(plainAttr -> plainAttr != null && plainAttr.getSchema() != null && plainAttr.getMembership() == null && plainSchema.equals(plainAttr.getSchema().getKey())).findFirst(); }
@Override public Optional<? extends P> getPlainAttr(final String plainSchema, final Membership<?> membership) { return internalGetPlainAttrs().stream().filter(plainAttr -> plainAttr != null && plainAttr.getSchema() != null && plainAttr.getMembership() != null && plainAttr.getMembership().equals(membership) && plainSchema.equals(plainAttr.getSchema().getKey())).findFirst(); }
private SyncopeClientException checkMandatory(final Any<?> any, final AnyUtils anyUtils) { SyncopeClientException reqValMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing); // Check if there is some mandatory schema defined for which no value has been provided AllowedSchemas<PlainSchema> allowedPlainSchemas = anyUtils.dao().findAllowedSchemas(any, PlainSchema.class); allowedPlainSchemas.getForSelf().forEach(schema -> { checkMandatory(schema, any.getPlainAttr(schema.getKey()).orElse(null), any, reqValMissing); }); if (any instanceof GroupableRelatable) { allowedPlainSchemas.getForMemberships().forEach((group, schemas) -> { GroupableRelatable<?, ?, ?, ?, ?> groupable = GroupableRelatable.class.cast(any); Membership<?> membership = groupable.getMembership(group.getKey()).orElse(null); schemas.forEach(schema -> { checkMandatory(schema, groupable.getPlainAttr(schema.getKey(), membership).orElse(null), any, reqValMissing); }); }); } return reqValMissing; }
PlainAttr<?> attr = (PlainAttr<?>) any.getPlainAttr(schema.getKey()).orElse(null); if (attr == null) { attr = newPlainAttr(); dao().save(any); } catch (InvalidPlainAttrValueException e) { LOG.error("Invalid value for attribute {} and {}: {}", schema.getKey(), any, value, e); LOG.debug("{} has already {} set: {}", any, schema.getKey(), attr.getValuesAsStrings());
@Override public Conf save(final CPlainAttr attr) { Conf instance = get(); Optional<? extends CPlainAttr> old = instance.getPlainAttr(attr.getSchema().getKey()); if (old.isPresent() && (!attr.getSchema().isUniqueConstraint() || (!attr.getUniqueValue().getStringValue().equals(old.get().getUniqueValue().getStringValue())))) { old.get().setOwner(null); instance.remove(old.get()); } instance.add(attr); attr.setOwner(instance); return entityManager().merge(instance); }
protected void fillTO( final AnyTO anyTO, final String realmFullPath, final Collection<? extends AnyTypeClass> auxClasses, final Collection<? extends PlainAttr<?>> plainAttrs, final Map<DerSchema, String> derAttrs, final Map<VirSchema, List<String>> virAttrs, final Collection<? extends ExternalResource> resources, final boolean details) { anyTO.setRealm(realmFullPath); anyTO.getAuxClasses().addAll(auxClasses.stream().map(Entity::getKey).collect(Collectors.toList())); plainAttrs.forEach(plainAttr -> { anyTO.getPlainAttrs().add(new AttrTO.Builder(). schema(plainAttr.getSchema().getKey()). values(plainAttr.getValuesAsStrings()).build()); }); derAttrs.forEach((schema, value) -> { anyTO.getDerAttrs().add(new AttrTO.Builder().schema(schema.getKey()).value(value).build()); }); virAttrs.forEach((schema, values) -> { anyTO.getVirAttrs().add(new AttrTO.Builder().schema(schema.getKey()).values(values).build()); }); anyTO.getResources().addAll(resources.stream().map(Entity::getKey).collect(Collectors.toSet())); }
@Override public AttrTO getAttrTO(final CPlainAttr attr) { return new AttrTO.Builder(). schema(attr.getSchema().getKey()). values(attr.getValuesAsStrings()). build(); }
if (attr != null && !allowedPlainSchemas.forSelfContains(attr.getSchema().getKey())) { return raiseNotAllowedViolation(context, attr.getSchema().getKey(), null); for (PlainAttr<?> attr : ((GroupableRelatable<?, ?, ?, ?, ?>) any).getPlainAttrs(membership)) { if (attr != null && !allowedPlainSchemas.forMembershipsContains( membership.getRightEnd(), attr.getSchema().getKey())) { context, attr.getSchema().getKey(), membership.getRightEnd());
@Override public PlainSchema update(final PlainSchemaTO schemaTO, final PlainSchema schema) { SyncopeClientCompositeException scce = SyncopeClientException.buildComposite(); boolean hasAttrs = false; for (AnyTypeKind anyTypeKind : AnyTypeKind.values()) { AnyUtils anyUtils = anyUtilsFactory.getInstance(anyTypeKind); hasAttrs |= plainSchemaDAO.findAttrs(schema, anyUtils.plainAttrClass()).isEmpty(); } if (hasAttrs) { if (schema.getType() != schemaTO.getType()) { SyncopeClientException e = SyncopeClientException.build(ClientExceptionType.InvalidPlainSchema); e.getElements().add("Cannot change type since " + schema.getKey() + " has attributes"); scce.addException(e); } if (schema.isUniqueConstraint() != schemaTO.isUniqueConstraint()) { SyncopeClientException e = SyncopeClientException.build(ClientExceptionType.InvalidPlainSchema); e.getElements().add("Cannot alter unique contraint since " + schema.getKey() + " has attributes"); scce.addException(e); } } if (scce.hasExceptions()) { throw scce; } return fill(schema, schemaTO); }
@Override public boolean isValid(final Object object, final ConstraintValidatorContext context) { String key = null; if (object instanceof PlainSchema) { key = ((PlainSchema) object).getKey(); } else if (object instanceof DerSchema) { key = ((DerSchema) object).getKey(); } else if (object instanceof VirSchema) { key = ((VirSchema) object).getKey(); } boolean isValid = KEY_PATTERN.matcher(key).matches(); if (!isValid) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( getTemplate(EntityViolationType.InvalidKey, "Invalid schema key")). addPropertyNode("key").addConstraintViolation(); } else if (JPAAnyUtils.matchesFieldName(key)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( getTemplate(EntityViolationType.InvalidKey, "Schema key not allowed: " + key)). addPropertyNode("key").addConstraintViolation(); return false; } return isValid; } }
@PreAuthorize("hasRole('" + StandardEntitlement.SCHEMA_CREATE + "')") @SuppressWarnings("unchecked") public <T extends SchemaTO> T create(final SchemaType schemaType, final T schemaTO) { if (StringUtils.isBlank(schemaTO.getKey())) { SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing); sce.getElements().add("Schema key"); throw sce; } if (doesSchemaExist(schemaType, schemaTO.getKey())) { throw new DuplicateException(schemaType + "/" + schemaTO.getKey()); } T created; switch (schemaType) { case VIRTUAL: VirSchema virSchema = virSchemaDAO.save(binder.create((VirSchemaTO) schemaTO)); created = (T) binder.getVirSchemaTO(virSchema.getKey()); break; case DERIVED: DerSchema derSchema = derSchemaDAO.save(binder.create((DerSchemaTO) schemaTO)); created = (T) binder.getDerSchemaTO(derSchema.getKey()); break; case PLAIN: default: PlainSchema plainSchema = plainSchemaDAO.save(binder.create((PlainSchemaTO) schemaTO)); created = (T) binder.getPlainSchemaTO(plainSchema.getKey()); } return created; }
@Override public PlainSchemaTO getPlainSchemaTO(final String key) { PlainSchema schema = plainSchemaDAO.find(key); if (schema == null) { throw new NotFoundException("Schema '" + key + "'"); } PlainSchemaTO schemaTO = new PlainSchemaTO(); schemaTO.setKey(schema.getKey()); schemaTO.setType(schema.getType()); schemaTO.setCipherAlgorithm(schema.getCipherAlgorithm()); schemaTO.setConversionPattern(schema.getConversionPattern()); schemaTO.setEnumerationKeys(schema.getEnumerationKeys()); schemaTO.setEnumerationValues(schema.getEnumerationValues()); schemaTO.setMandatoryCondition(schema.getMandatoryCondition()); schemaTO.setMimeType(schema.getMimeType()); schemaTO.setMultivalue(schema.isMultivalue()); schemaTO.setReadonly(schema.isReadonly()); schemaTO.setSecretKey(schema.getSecretKey()); schemaTO.setUniqueConstraint(schema.isUniqueConstraint()); labels(schema, schemaTO); schemaTO.setAnyTypeClass(schema.getAnyTypeClass() == null ? null : schema.getAnyTypeClass().getKey()); if (schema.getValidator() != null) { schemaTO.setValidator(schema.getValidator().getKey()); } return schemaTO; }