Search in sources :

Example 1 with RuleMultiExpression

use of io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression in project vertigo by KleeGroup.

the class AbstractSecurityRuleTranslator method withRule.

/**
 * Set security pattern.
 * @param securityRule security Pattern (not null, could be empty)
 * @return this builder
 */
public final S withRule(final String securityRule) {
    Assertion.checkNotNull(securityRule);
    // -----
    try {
        final RuleMultiExpression myMultiExpression = DslParserUtil.parseMultiExpression(securityRule);
        myMultiExpressions.add(myMultiExpression);
    } catch (final PegNoMatchFoundException e) {
        final String message = StringUtil.format("Echec de lecture de la securityRule {0}\n{1}", securityRule, e.getFullMessage());
        throw WrappedException.wrap(e, message);
    } catch (final Exception e) {
        final String message = StringUtil.format("Echec de lecture de la securityRule {0}\n{1}", securityRule, e.getMessage());
        throw WrappedException.wrap(e, message);
    }
    return (S) this;
}
Also used : PegNoMatchFoundException(io.vertigo.commons.peg.PegNoMatchFoundException) RuleMultiExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression) WrappedException(io.vertigo.lang.WrappedException) PegNoMatchFoundException(io.vertigo.commons.peg.PegNoMatchFoundException)

Example 2 with RuleMultiExpression

use of io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression in project vertigo by KleeGroup.

the class SearchSecurityRuleTranslator method appendMultiExpression.

private void appendMultiExpression(final StringBuilder query, final RuleMultiExpression multiExpressionDefinition) {
    String sep = "";
    String boolSep;
    if (multiExpressionDefinition.getBoolOperator() == BoolOperator.AND) {
        boolSep = "+";
    } else {
        boolSep = "";
    }
    for (final RuleExpression expression : multiExpressionDefinition.getExpressions()) {
        query.append(sep).append(boolSep);
        appendExpression(query, expression);
        sep = " ";
    }
    for (final RuleMultiExpression multiExpression : multiExpressionDefinition.getMultiExpressions()) {
        query.append(sep).append(boolSep).append('(');
        appendMultiExpression(query, multiExpression);
        query.append(')');
        sep = " ";
    }
}
Also used : RuleExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleExpression) RuleMultiExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression)

Example 3 with RuleMultiExpression

use of io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression in project vertigo by KleeGroup.

the class SqlSecurityRuleTranslator method appendMultiExpression.

private void appendMultiExpression(final StringBuilder query, final RuleMultiExpression multiExpressionDefinition) {
    String sep = "";
    final String boolSep = " " + multiExpressionDefinition.getBoolOperator() + " ";
    if (multiExpressionDefinition.isBlock()) {
        query.append('(');
    }
    for (final RuleExpression expression : multiExpressionDefinition.getExpressions()) {
        query.append(sep);
        appendExpression(query, expression);
        sep = boolSep;
    }
    for (final RuleMultiExpression multiExpression : multiExpressionDefinition.getMultiExpressions()) {
        query.append(sep);
        appendMultiExpression(query, multiExpression);
        sep = boolSep;
    }
    if (multiExpressionDefinition.isBlock()) {
        query.append(')');
    }
}
Also used : RuleExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleExpression) RuleMultiExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression)

Example 4 with RuleMultiExpression

use of io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression in project vertigo by KleeGroup.

the class AuthorizationManagerImpl method getSearchSecurity.

/**
 * {@inheritDoc}
 */
@Override
public <K extends KeyConcept> String getSearchSecurity(final Class<K> keyConceptClass, final OperationName<K> operationName) {
    Assertion.checkNotNull(keyConceptClass);
    Assertion.checkNotNull(operationName);
    // ---
    final Optional<UserAuthorizations> userPermissionsOpt = getUserPermissionsOpt();
    if (!userPermissionsOpt.isPresent()) {
        // Attention : pas de *:*
        return "";
    }
    final UserAuthorizations userPermissions = userPermissionsOpt.get();
    final SearchSecurityRuleTranslator securityRuleTranslator = new SearchSecurityRuleTranslator();
    securityRuleTranslator.withCriteria(userPermissions.getSecurityKeys());
    final DtDefinition dtDefinition = DtObjectUtil.findDtDefinition(keyConceptClass);
    final List<Authorization> permissions = userPermissions.getEntityAuthorizations(dtDefinition).stream().filter(permission -> permission.getOperation().get().equals(operationName.name())).collect(Collectors.toList());
    for (final Authorization permission : permissions) {
        for (final RuleMultiExpression ruleExpression : permission.getRules()) {
            securityRuleTranslator.withRule(ruleExpression);
        }
    }
    return securityRuleTranslator.toSearchQuery();
}
Also used : Authorization(io.vertigo.account.authorization.metamodel.Authorization) RuleMultiExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression) CriteriaSecurityRuleTranslator(io.vertigo.account.impl.authorization.dsl.translator.CriteriaSecurityRuleTranslator) VSecurityManager(io.vertigo.persona.security.VSecurityManager) UserAuthorizations(io.vertigo.account.authorization.UserAuthorizations) AuthorizationName(io.vertigo.account.authorization.metamodel.AuthorizationName) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) DtObjectUtil(io.vertigo.dynamo.domain.util.DtObjectUtil) Criteria(io.vertigo.dynamo.criteria.Criteria) AuthorizationManager(io.vertigo.account.authorization.AuthorizationManager) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) Home(io.vertigo.app.Home) List(java.util.List) DefinitionUtil(io.vertigo.core.definition.DefinitionUtil) Criterions(io.vertigo.dynamo.criteria.Criterions) Assertion(io.vertigo.lang.Assertion) KeyConcept(io.vertigo.dynamo.domain.model.KeyConcept) Optional(java.util.Optional) Authorization(io.vertigo.account.authorization.metamodel.Authorization) OperationName(io.vertigo.account.authorization.metamodel.OperationName) Collections(java.util.Collections) SearchSecurityRuleTranslator(io.vertigo.account.impl.authorization.dsl.translator.SearchSecurityRuleTranslator) UserSession(io.vertigo.persona.security.UserSession) SecuredEntity(io.vertigo.account.authorization.metamodel.SecuredEntity) SearchSecurityRuleTranslator(io.vertigo.account.impl.authorization.dsl.translator.SearchSecurityRuleTranslator) RuleMultiExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) UserAuthorizations(io.vertigo.account.authorization.UserAuthorizations)

Example 5 with RuleMultiExpression

use of io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression in project vertigo by KleeGroup.

the class DslMultiExpressionRule method handle.

/**
 * {@inheritDoc}
 */
@Override
protected RuleMultiExpression handle(final PegChoice parsing) {
    final List<Object> innerBlock;
    switch(parsing.getChoiceIndex()) {
        case 0:
            final List<?> blockExpression = (List<?>) parsing.getValue();
            innerBlock = (List<Object>) blockExpression.get(2);
            break;
        case 1:
            innerBlock = (List<Object>) parsing.getValue();
            break;
        default:
            throw new IllegalArgumentException("case " + parsing.getChoiceIndex() + " not implemented");
    }
    final List<RuleExpression> expressionDefinitions = new ArrayList<>();
    final List<RuleMultiExpression> multiExpressionDefinitions = new ArrayList<>();
    // first (expression1|multiExpression1)
    final PegChoice firstExpressionChoice = (PegChoice) innerBlock.get(1);
    switch(firstExpressionChoice.getChoiceIndex()) {
        case 0:
            expressionDefinitions.add((RuleExpression) firstExpressionChoice.getValue());
            break;
        case 1:
            multiExpressionDefinitions.add((RuleMultiExpression) firstExpressionChoice.getValue());
            break;
        default:
            throw new IllegalArgumentException("case " + parsing.getChoiceIndex() + " not implemented");
    }
    // manyNextExpressionsRule
    final List<List<Object>> many = (List<List<Object>>) innerBlock.get(3);
    // On récupère le produit de la règle many
    BoolOperator operator = null;
    for (final List<Object> item : many) {
        if (operator != null && operator != item.get(1)) {
            throw new IllegalArgumentException("Can't use different operator in same block, attempt to find " + operator);
        }
        operator = (BoolOperator) item.get(1);
        // next (expression2|multiExpression2)
        final PegChoice nextExpressionChoice = (PegChoice) item.get(3);
        switch(nextExpressionChoice.getChoiceIndex()) {
            case 0:
                expressionDefinitions.add((RuleExpression) nextExpressionChoice.getValue());
                break;
            case 1:
                multiExpressionDefinitions.add((RuleMultiExpression) nextExpressionChoice.getValue());
                break;
            default:
                throw new IllegalArgumentException("case " + nextExpressionChoice.getChoiceIndex() + " not implemented");
        }
    }
    final boolean block = parsing.getChoiceIndex() == 0;
    // ---
    return new RuleMultiExpression(block, operator != null ? operator : BoolOperator.AND, expressionDefinitions, multiExpressionDefinitions);
}
Also used : RuleMultiExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression) ArrayList(java.util.ArrayList) PegChoice(io.vertigo.commons.peg.PegChoice) RuleExpression(io.vertigo.account.authorization.metamodel.rulemodel.RuleExpression) List(java.util.List) ArrayList(java.util.ArrayList) BoolOperator(io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression.BoolOperator)

Aggregations

RuleMultiExpression (io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression)9 RuleExpression (io.vertigo.account.authorization.metamodel.rulemodel.RuleExpression)4 List (java.util.List)2 AuthorizationManager (io.vertigo.account.authorization.AuthorizationManager)1 UserAuthorizations (io.vertigo.account.authorization.UserAuthorizations)1 Authorization (io.vertigo.account.authorization.metamodel.Authorization)1 AuthorizationName (io.vertigo.account.authorization.metamodel.AuthorizationName)1 OperationName (io.vertigo.account.authorization.metamodel.OperationName)1 SecuredEntity (io.vertigo.account.authorization.metamodel.SecuredEntity)1 BoolOperator (io.vertigo.account.authorization.metamodel.rulemodel.RuleMultiExpression.BoolOperator)1 CriteriaSecurityRuleTranslator (io.vertigo.account.impl.authorization.dsl.translator.CriteriaSecurityRuleTranslator)1 SearchSecurityRuleTranslator (io.vertigo.account.impl.authorization.dsl.translator.SearchSecurityRuleTranslator)1 Home (io.vertigo.app.Home)1 PegChoice (io.vertigo.commons.peg.PegChoice)1 PegNoMatchFoundException (io.vertigo.commons.peg.PegNoMatchFoundException)1 DefinitionUtil (io.vertigo.core.definition.DefinitionUtil)1 Criteria (io.vertigo.dynamo.criteria.Criteria)1 Criterions (io.vertigo.dynamo.criteria.Criterions)1 DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)1 KeyConcept (io.vertigo.dynamo.domain.model.KeyConcept)1