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);
}
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());
}
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;
}
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";
}
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);
}
}
}
Aggregations