Search in sources :

Example 1 with AttributeCond

use of org.apache.syncope.core.persistence.api.dao.search.AttributeCond in project syncope by apache.

the class AuthDataAccessor method authenticate.

/**
 * Attempts to authenticate the given credentials against internal storage and pass-through resources (if
 * configured): the first succeeding causes global success.
 *
 * @param authentication given credentials
 * @return {@code null} if no matching user was found, authentication result otherwise
 */
@Transactional(noRollbackFor = DisabledException.class)
public Pair<User, Boolean> authenticate(final Authentication authentication) {
    User user = null;
    Optional<? extends CPlainAttr> authAttrs = confDAO.find("authentication.attributes");
    List<String> authAttrValues = authAttrs.isPresent() ? authAttrs.get().getValuesAsStrings() : Collections.singletonList("username");
    for (int i = 0; user == null && i < authAttrValues.size(); i++) {
        if ("username".equals(authAttrValues.get(i))) {
            user = userDAO.findByUsername(authentication.getName());
        } else {
            AttributeCond attrCond = new AttributeCond(AttributeCond.Type.EQ);
            attrCond.setSchema(authAttrValues.get(i));
            attrCond.setExpression(authentication.getName());
            List<User> users = searchDAO.search(SearchCond.getLeafCond(attrCond), AnyTypeKind.USER);
            if (users.size() == 1) {
                user = users.get(0);
            } else {
                LOG.warn("Value {} provided for {} does not uniquely identify a user", authentication.getName(), authAttrValues.get(i));
            }
        }
    }
    Boolean authenticated = null;
    if (user != null) {
        authenticated = false;
        if (user.isSuspended() != null && user.isSuspended()) {
            throw new DisabledException("User " + user.getUsername() + " is suspended");
        }
        Optional<? extends CPlainAttr> authStatuses = confDAO.find("authentication.statuses");
        if (authStatuses.isPresent() && !authStatuses.get().getValuesAsStrings().contains(user.getStatus())) {
            throw new DisabledException("User " + user.getUsername() + " not allowed to authenticate");
        }
        boolean userModified = false;
        authenticated = AuthDataAccessor.this.authenticate(user, authentication.getCredentials().toString());
        if (authenticated) {
            if (confDAO.find("log.lastlogindate", true)) {
                user.setLastLoginDate(new Date());
                userModified = true;
            }
            if (user.getFailedLogins() != 0) {
                user.setFailedLogins(0);
                userModified = true;
            }
        } else {
            user.setFailedLogins(user.getFailedLogins() + 1);
            userModified = true;
        }
        if (userModified) {
            userDAO.save(user);
        }
    }
    return ImmutablePair.of(user, authenticated);
}
Also used : User(org.apache.syncope.core.persistence.api.entity.user.User) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) DisabledException(org.springframework.security.authentication.DisabledException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with AttributeCond

use of org.apache.syncope.core.persistence.api.dao.search.AttributeCond in project syncope by apache.

the class DefaultPullCorrelationRule method getSearchCond.

@Override
public SearchCond getSearchCond(final ConnectorObject connObj, final Provision provision) {
    Map<String, Item> mappingItems = provision.getMapping().getItems().stream().collect(Collectors.toMap(Item::getIntAttrName, Function.identity()));
    // search for anys by attribute(s) specified in the policy
    SearchCond searchCond = null;
    for (String schema : conf.getSchemas()) {
        Item mappingItem = mappingItems.get(schema);
        Attribute attr = mappingItem == null ? null : connObj.getAttributeByName(mappingItem.getExtAttrName());
        if (attr == null) {
            throw new IllegalArgumentException("Connector object does not contains the attributes to perform the search: " + schema);
        }
        AttributeCond.Type type;
        String expression = null;
        if (attr.getValue() == null || attr.getValue().isEmpty() || (attr.getValue().size() == 1 && attr.getValue().get(0) == null)) {
            type = AttributeCond.Type.ISNULL;
        } else {
            type = AttributeCond.Type.EQ;
            expression = attr.getValue().size() > 1 ? attr.getValue().toString() : attr.getValue().get(0).toString();
        }
        SearchCond nodeCond;
        // any objects: just key or name can be selected
        if ("key".equalsIgnoreCase(schema) || "username".equalsIgnoreCase(schema) || "name".equalsIgnoreCase(schema)) {
            AnyCond cond = new AnyCond();
            cond.setSchema(schema);
            cond.setType(type);
            cond.setExpression(expression);
            nodeCond = SearchCond.getLeafCond(cond);
        } else {
            AttributeCond cond = new AttributeCond();
            cond.setSchema(schema);
            cond.setType(type);
            cond.setExpression(expression);
            nodeCond = SearchCond.getLeafCond(cond);
        }
        searchCond = searchCond == null ? nodeCond : SearchCond.getAndCond(searchCond, nodeCond);
    }
    return searchCond;
}
Also used : Item(org.apache.syncope.core.persistence.api.entity.resource.Item) Attribute(org.identityconnectors.framework.common.objects.Attribute) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond)

Example 3 with AttributeCond

use of org.apache.syncope.core.persistence.api.dao.search.AttributeCond in project syncope by apache.

the class SearchCondConverterTest method ilike.

@Test
public void ilike() {
    String fiql = new UserFiqlSearchConditionBuilder().is("username").equalToIgnoreCase("ros*").query();
    assertEquals("username=~ros*", fiql);
    AttributeCond attrCond = new AnyCond(AttributeCond.Type.ILIKE);
    attrCond.setSchema("username");
    attrCond.setExpression("ros%");
    SearchCond simpleCond = SearchCond.getLeafCond(attrCond);
    assertEquals(simpleCond, SearchCondConverter.convert(fiql));
}
Also used : AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) UserFiqlSearchConditionBuilder(org.apache.syncope.common.lib.search.UserFiqlSearchConditionBuilder) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) Test(org.junit.jupiter.api.Test)

Example 4 with AttributeCond

use of org.apache.syncope.core.persistence.api.dao.search.AttributeCond in project syncope by apache.

the class SearchCondConverterTest method like.

@Test
public void like() {
    String fiql = new UserFiqlSearchConditionBuilder().is("username").equalTo("ros*").query();
    assertEquals("username==ros*", fiql);
    AttributeCond attrCond = new AnyCond(AttributeCond.Type.LIKE);
    attrCond.setSchema("username");
    attrCond.setExpression("ros%");
    SearchCond simpleCond = SearchCond.getLeafCond(attrCond);
    assertEquals(simpleCond, SearchCondConverter.convert(fiql));
}
Also used : AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) UserFiqlSearchConditionBuilder(org.apache.syncope.common.lib.search.UserFiqlSearchConditionBuilder) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) Test(org.junit.jupiter.api.Test)

Example 5 with AttributeCond

use of org.apache.syncope.core.persistence.api.dao.search.AttributeCond in project syncope by apache.

the class SearchCondConverterTest method isNotNull.

@Test
public void isNotNull() {
    String fiql = new UserFiqlSearchConditionBuilder().is("loginDate").notNullValue().query();
    assertEquals("loginDate!=" + SpecialAttr.NULL, fiql);
    AttributeCond attrCond = new AttributeCond(AttributeCond.Type.ISNOTNULL);
    attrCond.setSchema("loginDate");
    SearchCond simpleCond = SearchCond.getLeafCond(attrCond);
    assertEquals(simpleCond, SearchCondConverter.convert(fiql));
}
Also used : AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) UserFiqlSearchConditionBuilder(org.apache.syncope.common.lib.search.UserFiqlSearchConditionBuilder) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Test(org.junit.jupiter.api.Test)

Aggregations

AttributeCond (org.apache.syncope.core.persistence.api.dao.search.AttributeCond)35 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)25 Test (org.junit.jupiter.api.Test)25 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)16 User (org.apache.syncope.core.persistence.api.entity.user.User)15 AnyCond (org.apache.syncope.core.persistence.api.dao.search.AnyCond)12 UserFiqlSearchConditionBuilder (org.apache.syncope.common.lib.search.UserFiqlSearchConditionBuilder)8 ArrayList (java.util.ArrayList)4 MembershipCond (org.apache.syncope.core.persistence.api.dao.search.MembershipCond)4 Map (java.util.Map)3 SCIMUserAddressConf (org.apache.syncope.common.lib.scim.SCIMUserAddressConf)3 SCIMUserConf (org.apache.syncope.common.lib.scim.SCIMUserConf)3 OrderByClause (org.apache.syncope.core.persistence.api.dao.search.OrderByClause)3 Group (org.apache.syncope.core.persistence.api.entity.group.Group)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 StringUtils (org.apache.commons.lang3.StringUtils)2 SCIMComplexConf (org.apache.syncope.common.lib.scim.SCIMComplexConf)2 SCIMConf (org.apache.syncope.common.lib.scim.SCIMConf)2