Search in sources :

Example 16 with PlainSchema

use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.

the class ConfigurationLogic method delete.

@PreAuthorize("hasRole('" + StandardEntitlement.CONFIGURATION_DELETE + "')")
public void delete(final String schema) {
    Optional<? extends CPlainAttr> conf = confDAO.find(schema);
    if (!conf.isPresent()) {
        PlainSchema plainSchema = plainSchemaDAO.find(schema);
        if (plainSchema == null) {
            throw new NotFoundException("Configuration schema " + schema);
        }
    }
    confDAO.delete(schema);
}
Also used : NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 17 with PlainSchema

use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.

the class AbstractAnySearchDAO method check.

protected Triple<PlainSchema, PlainAttrValue, AnyCond> check(final AnyCond cond, final AnyTypeKind kind) {
    AnyCond condClone = SerializationUtils.clone(cond);
    AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
    // Keeps track of difference between entity's getKey() and JPA @Id fields
    if ("key".equals(condClone.getSchema())) {
        condClone.setSchema("id");
    }
    Field anyField = ReflectionUtils.findField(attrUtils.anyClass(), condClone.getSchema());
    if (anyField == null) {
        LOG.warn("Ignoring invalid schema '{}'", condClone.getSchema());
        throw new IllegalArgumentException();
    }
    PlainSchema schema = new JPAPlainSchema();
    schema.setKey(anyField.getName());
    for (AttrSchemaType attrSchemaType : AttrSchemaType.values()) {
        if (anyField.getType().isAssignableFrom(attrSchemaType.getType())) {
            schema.setType(attrSchemaType);
        }
    }
    // Deal with any Integer fields logically mapping to boolean values
    boolean foundBooleanMin = false;
    boolean foundBooleanMax = false;
    if (Integer.class.equals(anyField.getType())) {
        for (Annotation annotation : anyField.getAnnotations()) {
            if (Min.class.equals(annotation.annotationType())) {
                foundBooleanMin = ((Min) annotation).value() == 0;
            } else if (Max.class.equals(annotation.annotationType())) {
                foundBooleanMax = ((Max) annotation).value() == 1;
            }
        }
    }
    if (foundBooleanMin && foundBooleanMax) {
        schema.setType(AttrSchemaType.Boolean);
    }
    // Deal with any fields representing relationships to other entities
    if (anyField.getType().getAnnotation(Entity.class) != null) {
        Method relMethod = null;
        try {
            relMethod = ClassUtils.getPublicMethod(anyField.getType(), "getKey", new Class<?>[0]);
        } catch (Exception e) {
            LOG.error("Could not find {}#getKey", anyField.getType(), e);
        }
        if (relMethod != null && String.class.isAssignableFrom(relMethod.getReturnType())) {
            condClone.setSchema(condClone.getSchema() + "_id");
            schema.setType(AttrSchemaType.String);
        }
    }
    PlainAttrValue attrValue = attrUtils.newPlainAttrValue();
    if (condClone.getType() != AttributeCond.Type.LIKE && condClone.getType() != AttributeCond.Type.ILIKE && condClone.getType() != AttributeCond.Type.ISNULL && condClone.getType() != AttributeCond.Type.ISNOTNULL) {
        try {
            ((JPAPlainSchema) schema).validator().validate(condClone.getExpression(), attrValue);
        } catch (ValidationException e) {
            LOG.error("Could not validate expression '" + condClone.getExpression() + "'", e);
            throw new IllegalArgumentException();
        }
    }
    return Triple.of(schema, attrValue, condClone);
}
Also used : Entity(javax.persistence.Entity) ValidationException(javax.validation.ValidationException) Max(javax.validation.constraints.Max) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ValidationException(javax.validation.ValidationException) Field(java.lang.reflect.Field) Min(javax.validation.constraints.Min) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 18 with PlainSchema

use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.

the class AbstractAnySearchDAO method check.

protected Pair<PlainSchema, PlainAttrValue> check(final AttributeCond cond, final AnyTypeKind kind) {
    AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
    PlainSchema schema = schemaDAO.find(cond.getSchema());
    if (schema == null) {
        LOG.warn("Ignoring invalid schema '{}'", cond.getSchema());
        throw new IllegalArgumentException();
    }
    PlainAttrValue attrValue = attrUtils.newPlainAttrValue();
    try {
        if (cond.getType() != AttributeCond.Type.LIKE && cond.getType() != AttributeCond.Type.ILIKE && cond.getType() != AttributeCond.Type.ISNULL && cond.getType() != AttributeCond.Type.ISNOTNULL) {
            ((JPAPlainSchema) schema).validator().validate(cond.getExpression(), attrValue);
        }
    } catch (ValidationException e) {
        LOG.error("Could not validate expression '" + cond.getExpression() + "'", e);
        throw new IllegalArgumentException();
    }
    return Pair.of(schema, attrValue);
}
Also used : ValidationException(javax.validation.ValidationException) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 19 with PlainSchema

use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.

the class AbstractAnyDAO method getWhereClause.

/**
 * Generate one where clause for each different attribute schema into the derived schema expression provided.
 *
 * @param expression derived schema expression
 * @param value derived attribute value
 * @param attrUtils USER / GROUP
 * @return where clauses to use to build the query
 */
private Set<String> getWhereClause(final String expression, final String value) {
    Parser parser = new Parser(new StringReader(expression));
    // Schema names
    List<String> identifiers = new ArrayList<>();
    // Literals
    List<String> literals = new ArrayList<>();
    // Get schema names and literals
    for (Token token = parser.getNextToken(); token != null && StringUtils.isNotBlank(token.toString()); token = parser.getNextToken()) {
        if (token.kind == ParserConstants.STRING_LITERAL) {
            literals.add(token.toString().substring(1, token.toString().length() - 1));
        }
        if (token.kind == ParserConstants.IDENTIFIER) {
            identifiers.add(token.toString());
        }
    }
    // Sort literals in order to process later literals included into others
    Collections.sort(literals, (final String t, final String t1) -> {
        if (t == null && t1 == null) {
            return 0;
        } else if (t != null && t1 == null) {
            return -1;
        } else if (t == null && t1 != null) {
            return 1;
        } else if (t.length() == t1.length()) {
            return 0;
        } else if (t.length() > t1.length()) {
            return -1;
        } else {
            return 1;
        }
    });
    // Split value on provided literals
    List<String> attrValues = split(value, literals);
    if (attrValues.size() != identifiers.size()) {
        LOG.error("Ambiguous JEXL expression resolution: literals and values have different size");
        return Collections.emptySet();
    }
    // clauses to be used with INTERSECTed queries
    Set<String> clauses = new HashSet<>();
    // builder to build the clauses
    StringBuilder bld = new StringBuilder();
    // Contains used identifiers in order to avoid replications
    Set<String> used = new HashSet<>();
    // Create several clauses: one for eanch identifiers
    for (int i = 0; i < identifiers.size(); i++) {
        if (!used.contains(identifiers.get(i))) {
            // verify schema existence and get schema type
            PlainSchema schema = plainSchemaDAO().find(identifiers.get(i));
            if (schema == null) {
                LOG.error("Invalid schema '{}', ignoring", identifiers.get(i));
            } else {
                // clear builder
                bld.delete(0, bld.length());
                bld.append("(");
                // set schema name
                bld.append("s.id = '").append(identifiers.get(i)).append("'");
                bld.append(" AND ");
                bld.append("s.id = a.schema_id").append(" AND ");
                bld.append("a.id = v.attribute_id");
                bld.append(" AND ");
                // use a value clause different for eanch different schema type
                switch(schema.getType()) {
                    case Boolean:
                        bld.append("v.booleanValue = '").append(attrValues.get(i)).append("'");
                        break;
                    case Long:
                        bld.append("v.longValue = ").append(attrValues.get(i));
                        break;
                    case Double:
                        bld.append("v.doubleValue = ").append(attrValues.get(i));
                        break;
                    case Date:
                        bld.append("v.dateValue = '").append(attrValues.get(i)).append("'");
                        break;
                    default:
                        bld.append("v.stringValue = '").append(attrValues.get(i)).append("'");
                }
                bld.append(")");
                used.add(identifiers.get(i));
                clauses.add(bld.toString());
            }
        }
    }
    LOG.debug("Generated where clauses {}", clauses);
    return clauses;
}
Also used : StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Token(org.apache.commons.jexl3.parser.Token) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Parser(org.apache.commons.jexl3.parser.Parser) HashSet(java.util.HashSet)

Example 20 with PlainSchema

use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.

the class AnyTypeClassTest method delete.

@Test
public void delete() {
    AnyTypeClass minimalUser = anyTypeClassDAO.find("minimal user");
    assertNotNull(minimalUser);
    PlainSchema surname = plainSchemaDAO.find("surname");
    assertNotNull(surname);
    assertTrue(minimalUser.getPlainSchemas().contains(surname));
    int before = minimalUser.getPlainSchemas().size();
    plainSchemaDAO.delete("surname");
    anyTypeClassDAO.flush();
    minimalUser = anyTypeClassDAO.find("minimal user");
    assertNotNull(minimalUser);
    assertEquals(before, minimalUser.getPlainSchemas().size() + 1);
}
Also used : PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)55 Test (org.junit.jupiter.api.Test)22 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)21 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)15 VirSchema (org.apache.syncope.core.persistence.api.entity.VirSchema)14 User (org.apache.syncope.core.persistence.api.entity.user.User)13 DerSchema (org.apache.syncope.core.persistence.api.entity.DerSchema)12 UPlainAttr (org.apache.syncope.core.persistence.api.entity.user.UPlainAttr)11 ArrayList (java.util.ArrayList)10 PlainAttrValue (org.apache.syncope.core.persistence.api.entity.PlainAttrValue)10 Transactional (org.springframework.transaction.annotation.Transactional)10 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)9 AnyTypeClass (org.apache.syncope.core.persistence.api.entity.AnyTypeClass)9 MappingItem (org.apache.syncope.core.persistence.api.entity.resource.MappingItem)9 Collections (java.util.Collections)8 HashSet (java.util.HashSet)8 List (java.util.List)8 Optional (java.util.Optional)8 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)8 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)8