Search in sources :

Example 21 with AnyObject

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

the class AnyObjectTest method delete.

@Test
public void delete() {
    AnyObject anyObject = anyObjectDAO.find("8559d14d-58c2-46eb-a2d4-a7d35161e8f8");
    anyObjectDAO.delete(anyObject.getKey());
    AnyObject actual = anyObjectDAO.find("8559d14d-58c2-46eb-a2d4-a7d35161e8f8");
    assertNull(actual);
}
Also used : AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 22 with AnyObject

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

the class GroupTest method adynMembership.

@Test
public void adynMembership() {
    // 0. create any object matching the condition below
    AnyObject anyObject = entityFactory.newEntity(AnyObject.class);
    anyObject.setName("name");
    anyObject.setType(anyTypeDAO.find("PRINTER"));
    anyObject.setRealm(realmDAO.findByFullPath("/even/two"));
    APlainAttr attr = entityFactory.newEntity(APlainAttr.class);
    attr.setOwner(anyObject);
    attr.setSchema(plainSchemaDAO.find("model"));
    attr.add("Canon MFC8030", anyUtilsFactory.getInstance(AnyTypeKind.ANY_OBJECT));
    anyObject.add(attr);
    anyObject = anyObjectDAO.save(anyObject);
    String newAnyObjectKey = anyObject.getKey();
    assertNotNull(newAnyObjectKey);
    // 1. create group with dynamic membership
    Group group = entityFactory.newEntity(Group.class);
    group.setRealm(realmDAO.getRoot());
    group.setName("new");
    ADynGroupMembership dynMembership = entityFactory.newEntity(ADynGroupMembership.class);
    dynMembership.setAnyType(anyTypeDAO.find("PRINTER"));
    dynMembership.setFIQLCond("model==Canon MFC8030");
    dynMembership.setGroup(group);
    group.add(dynMembership);
    Group actual = groupDAO.save(group);
    assertNotNull(actual);
    groupDAO.flush();
    // 2. verify that dynamic membership is there
    actual = groupDAO.find(actual.getKey());
    assertNotNull(actual);
    assertNotNull(actual.getADynMembership(anyTypeDAO.find("PRINTER")).get());
    assertNotNull(actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getKey());
    assertEquals(actual, actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getGroup());
    // 3. verify that expected any objects have the created group dynamically assigned
    List<String> members = groupDAO.findADynMembers(actual).stream().filter(object -> "PRINTER".equals(anyObjectDAO.find(object).getType().getKey())).collect(Collectors.toList());
    assertEquals(2, members.size());
    assertEquals(new HashSet<>(Arrays.asList("fc6dbc3a-6c07-4965-8781-921e7401a4a5", newAnyObjectKey)), new HashSet<>(members));
    anyObject = anyObjectDAO.find("fc6dbc3a-6c07-4965-8781-921e7401a4a5");
    assertNotNull(anyObject);
    Collection<Group> dynGroupMemberships = findDynGroups(anyObject);
    assertEquals(1, dynGroupMemberships.size());
    assertTrue(dynGroupMemberships.contains(actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getGroup()));
    // 4. delete the new any object and verify that dynamic membership was updated
    anyObjectDAO.delete(newAnyObjectKey);
    anyObjectDAO.flush();
    actual = groupDAO.find(actual.getKey());
    members = groupDAO.findADynMembers(actual).stream().filter(object -> "PRINTER".equals(anyObjectDAO.find(object).getType().getKey())).collect(Collectors.toList());
    assertEquals(1, members.size());
    assertEquals("fc6dbc3a-6c07-4965-8781-921e7401a4a5", members.get(0));
    // 5. delete group and verify that dynamic membership was also removed
    String dynMembershipKey = actual.getADynMembership(anyTypeDAO.find("PRINTER")).get().getKey();
    groupDAO.delete(actual);
    groupDAO.flush();
    assertNull(entityManager().find(JPAADynGroupMembership.class, dynMembershipKey));
    dynGroupMemberships = findDynGroups(anyObject);
    assertTrue(dynGroupMemberships.isEmpty());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) UDynGroupMembership(org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) GPlainAttrValue(org.apache.syncope.core.persistence.api.entity.group.GPlainAttrValue) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) JPAADynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAADynGroupMembership) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) TypeExtension(org.apache.syncope.core.persistence.api.entity.group.TypeExtension) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) Collection(java.util.Collection) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) ADynGroupMembership(org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership) User(org.apache.syncope.core.persistence.api.entity.user.User) GPlainAttr(org.apache.syncope.core.persistence.api.entity.group.GPlainAttr) JPAUDynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.user.JPAUDynGroupMembership) JPAGroupDAO(org.apache.syncope.core.persistence.jpa.dao.JPAGroupDAO) Collectors(java.util.stream.Collectors) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) PlainAttrValueDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Test(org.junit.jupiter.api.Test) List(java.util.List) Query(javax.persistence.Query) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest) AnyTypeClassDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO) PlainAttrDAO(org.apache.syncope.core.persistence.api.dao.PlainAttrDAO) APlainAttr(org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) Transactional(org.springframework.transaction.annotation.Transactional) JPAADynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAADynGroupMembership) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) APlainAttr(org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr) JPAADynGroupMembership(org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAADynGroupMembership) ADynGroupMembership(org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 23 with AnyObject

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

the class ElasticsearchUtils method builder.

/**
 * Returns the builder specialized with content from the provided any.
 *
 * @param any user, group or any object to index
 * @return builder specialized with content from the provided any
 * @throws IOException in case of errors
 */
@Transactional
public XContentBuilder builder(final Any<?> any) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("id", any.getKey()).field("realm", any.getRealm().getFullPath()).field("anyType", any.getType().getKey()).field("creationDate", any.getCreationDate()).field("creator", any.getCreator()).field("lastChangeDate", any.getLastChangeDate()).field("lastModified", any.getLastModifier()).field("status", any.getStatus()).field("resources", any instanceof User ? userDAO.findAllResourceKeys(any.getKey()) : any instanceof AnyObject ? anyObjectDAO.findAllResourceKeys(any.getKey()) : groupDAO.findAllResourceKeys(any.getKey())).field("dynRealms", any instanceof User ? userDAO.findDynRealms(any.getKey()) : any instanceof AnyObject ? anyObjectDAO.findDynRealms(any.getKey()) : groupDAO.findDynRealms(any.getKey()));
    if (any instanceof AnyObject) {
        AnyObject anyObject = ((AnyObject) any);
        builder = builder.field("name", anyObject.getName());
        List<Object> memberships = new ArrayList<>(anyObjectDAO.findAllGroupKeys(anyObject));
        builder = builder.field("memberships", memberships);
        List<Object> relationships = new ArrayList<>();
        List<Object> relationshipTypes = new ArrayList<>();
        anyObjectDAO.findAllRelationships(anyObject).forEach(relationship -> {
            relationships.add(relationship.getRightEnd().getKey());
            relationshipTypes.add(relationship.getType().getKey());
        });
        builder = builder.field("relationships", relationships);
        builder = builder.field("relationshipTypes", relationshipTypes);
    } else if (any instanceof Group) {
        Group group = ((Group) any);
        builder = builder.field("name", group.getName());
        if (group.getUserOwner() != null) {
            builder = builder.field("userOwner", group.getUserOwner().getKey());
        }
        if (group.getGroupOwner() != null) {
            builder = builder.field("groupOwner", group.getGroupOwner().getKey());
        }
        List<Object> members = groupDAO.findUMemberships(group).stream().map(membership -> membership.getLeftEnd().getKey()).collect(Collectors.toList());
        members.add(groupDAO.findUDynMembers(group));
        members.addAll(groupDAO.findAMemberships(group).stream().map(membership -> membership.getLeftEnd().getKey()).collect(Collectors.toList()));
        members.add(groupDAO.findADynMembers(group));
        builder = builder.field("members", members);
    } else if (any instanceof User) {
        User user = ((User) any);
        builder = builder.field("username", user.getUsername()).field("lastLoginDate", user.getLastLoginDate()).field("lastRecertification", user.getLastRecertification()).field("lastRecertificator", user.getLastRecertificator());
        List<Object> roles = userDAO.findAllRoles(user).stream().map(r -> r.getKey()).collect(Collectors.toList());
        builder = builder.field("roles", roles);
        Set<Object> privileges = userDAO.findAllRoles(user).stream().flatMap(role -> role.getPrivileges().stream()).map(Entity::getKey).collect(Collectors.toSet());
        builder = builder.field("privileges", privileges);
        List<Object> memberships = new ArrayList<>(userDAO.findAllGroupKeys(user));
        builder = builder.field("memberships", memberships);
        List<Object> relationships = new ArrayList<>();
        Set<Object> relationshipTypes = new HashSet<>();
        user.getRelationships().stream().map(relationship -> {
            relationships.add(relationship.getRightEnd().getKey());
            return relationship;
        }).forEachOrdered(relationship -> {
            relationshipTypes.add(relationship.getType().getKey());
        });
        builder = builder.field("relationships", relationships);
        builder = builder.field("relationshipTypes", relationshipTypes);
    }
    if (any.getPlainAttrs() != null) {
        for (PlainAttr<?> plainAttr : any.getPlainAttrs()) {
            List<Object> values = plainAttr.getValues().stream().map(value -> value.getValue()).collect(Collectors.toList());
            if (plainAttr.getUniqueValue() != null) {
                values.add(plainAttr.getUniqueValue().getValue());
            }
            builder = builder.field(plainAttr.getSchema().getKey(), values);
        }
    }
    builder = builder.endObject();
    return builder;
}
Also used : XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) PlainAttr(org.apache.syncope.core.persistence.api.entity.PlainAttr) Set(java.util.Set) User(org.apache.syncope.core.persistence.api.entity.user.User) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Entity(org.apache.syncope.core.persistence.api.entity.Entity) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Any(org.apache.syncope.core.persistence.api.entity.Any) Transactional(org.springframework.transaction.annotation.Transactional) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) User(org.apache.syncope.core.persistence.api.entity.user.User) ArrayList(java.util.ArrayList) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) ArrayList(java.util.ArrayList) List(java.util.List) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with AnyObject

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

the class ElasticsearchReindex method doExecute.

@Override
protected String doExecute(final boolean dryRun) throws JobExecutionException {
    if (!dryRun) {
        try {
            LOG.debug("Start rebuild index {}", AuthContextUtils.getDomain().toLowerCase());
            IndicesExistsResponse existsIndexResponse = client.admin().indices().exists(new IndicesExistsRequest(AuthContextUtils.getDomain().toLowerCase())).get();
            if (existsIndexResponse.isExists()) {
                DeleteIndexResponse deleteIndexResponse = client.admin().indices().delete(new DeleteIndexRequest(AuthContextUtils.getDomain().toLowerCase())).get();
                LOG.debug("Successfully removed {}: {}", AuthContextUtils.getDomain().toLowerCase(), deleteIndexResponse);
            }
            XContentBuilder settings = XContentFactory.jsonBuilder().startObject().startObject("analysis").startObject("analyzer").startObject("string_lowercase").field("type", "custom").field("tokenizer", "standard").field("filter").startArray().value("lowercase").endArray().endObject().endObject().endObject().endObject();
            XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startArray("dynamic_templates").startObject().startObject("strings").field("match_mapping_type", "string").startObject("mapping").field("type", "keyword").field("analyzer", "string_lowercase").endObject().endObject().endObject().endArray().endObject();
            CreateIndexResponse createIndexResponse = client.admin().indices().create(new CreateIndexRequest(AuthContextUtils.getDomain().toLowerCase()).settings(settings).mapping(AnyTypeKind.USER.name(), mapping).mapping(AnyTypeKind.GROUP.name(), mapping).mapping(AnyTypeKind.ANY_OBJECT.name(), mapping)).get();
            LOG.debug("Successfully created {}: {}", AuthContextUtils.getDomain().toLowerCase(), createIndexResponse);
            LOG.debug("Indexing users...");
            for (int page = 1; page <= (userDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (User user : userDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.USER.name(), user.getKey()).setSource(elasticsearchUtils.builder(user)).get();
                    LOG.debug("Index successfully created for {}: {}", user, response);
                }
            }
            LOG.debug("Indexing groups...");
            for (int page = 1; page <= (groupDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (Group group : groupDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.GROUP.name(), group.getKey()).setSource(elasticsearchUtils.builder(group)).get();
                    LOG.debug("Index successfully created for {}: {}", group, response);
                }
            }
            LOG.debug("Indexing any objects...");
            for (int page = 1; page <= (anyObjectDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (AnyObject anyObject : anyObjectDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.ANY_OBJECT.name(), anyObject.getKey()).setSource(elasticsearchUtils.builder(anyObject)).get();
                    LOG.debug("Index successfully created for {}: {}", anyObject, response);
                }
            }
            LOG.debug("Rebuild index {} successfully completed", AuthContextUtils.getDomain().toLowerCase());
        } catch (Exception e) {
            throw new JobExecutionException("While rebuilding index " + AuthContextUtils.getDomain().toLowerCase(), e);
        }
    }
    return "SUCCESS";
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) JobExecutionException(org.quartz.JobExecutionException) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) JobExecutionException(org.quartz.JobExecutionException) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 25 with AnyObject

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

the class PushJobDelegate method doHandle.

protected void doHandle(final List<? extends Any<?>> anys, final SyncopePushResultHandler handler, final ExternalResource resource) throws JobExecutionException {
    for (int i = 0; i < anys.size() && !interrupt; i++) {
        try {
            handler.handle(anys.get(i).getKey());
            reportHandled(anys.get(i).getType().getKey(), (anys.get(i) instanceof User ? ((User) anys.get(i)).getUsername() : anys.get(i) instanceof Group ? ((Group) anys.get(i)).getName() : ((AnyObject) anys.get(i)).getName()));
        } catch (Exception e) {
            LOG.warn("Failure pushing '{}' on '{}'", anys.get(i), resource, e);
            throw new JobExecutionException("While pushing " + anys.get(i) + " on " + resource, e);
        }
    }
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) User(org.apache.syncope.core.persistence.api.entity.user.User) JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException)

Aggregations

AnyObject (org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)29 Group (org.apache.syncope.core.persistence.api.entity.group.Group)18 User (org.apache.syncope.core.persistence.api.entity.user.User)15 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)11 Test (org.junit.jupiter.api.Test)11 ArrayList (java.util.ArrayList)10 List (java.util.List)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 Transactional (org.springframework.transaction.annotation.Transactional)9 HashSet (java.util.HashSet)8 Set (java.util.Set)8 Collectors (java.util.stream.Collectors)8 AnyTypeKind (org.apache.syncope.common.lib.types.AnyTypeKind)7 AnyObjectDAO (org.apache.syncope.core.persistence.api.dao.AnyObjectDAO)7 AnyTypeDAO (org.apache.syncope.core.persistence.api.dao.AnyTypeDAO)7 GroupDAO (org.apache.syncope.core.persistence.api.dao.GroupDAO)7 UserDAO (org.apache.syncope.core.persistence.api.dao.UserDAO)7 AnyType (org.apache.syncope.core.persistence.api.entity.AnyType)7 Collections (java.util.Collections)6 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)6