use of org.apache.syncope.common.rest.api.service.SchemaService in project syncope by apache.
the class VirSchemaITCase method anonymous.
@Test
public void anonymous() {
SchemaService unauthenticated = clientFactory.create().getService(SchemaService.class);
try {
unauthenticated.search(new SchemaQuery.Builder().type(SchemaType.VIRTUAL).build());
fail("This should not happen");
} catch (AccessControlException e) {
assertNotNull(e);
}
SchemaService anonymous = clientFactory.create(new AnonymousAuthenticationHandler(ANONYMOUS_UNAME, ANONYMOUS_KEY)).getService(SchemaService.class);
assertFalse(anonymous.search(new SchemaQuery.Builder().type(SchemaType.VIRTUAL).build()).isEmpty());
}
use of org.apache.syncope.common.rest.api.service.SchemaService in project syncope by apache.
the class AuthenticationITCase method userSchemaAuthorization.
@Test
public void userSchemaAuthorization() {
String schemaName = "authTestSchema" + getUUIDString();
// 1. create a schema (as admin)
PlainSchemaTO schemaTO = new PlainSchemaTO();
schemaTO.setKey(schemaName);
schemaTO.setMandatoryCondition("false");
schemaTO.setType(AttrSchemaType.String);
PlainSchemaTO newPlainSchemaTO = createSchema(SchemaType.PLAIN, schemaTO);
assertEquals(schemaTO, newPlainSchemaTO);
// 2. create an user with the role created above (as admin)
UserTO userTO = UserITCase.getUniqueSampleTO("auth@test.org");
userTO = createUser(userTO).getEntity();
assertNotNull(userTO);
// 3. read the schema created above (as admin) - success
schemaTO = schemaService.read(SchemaType.PLAIN, schemaName);
assertNotNull(schemaTO);
// 4. read the schema created above (as user) - success
SchemaService schemaService2 = clientFactory.create(userTO.getUsername(), "password123").getService(SchemaService.class);
schemaTO = schemaService2.read(SchemaType.PLAIN, schemaName);
assertNotNull(schemaTO);
// 5. update the schema create above (as user) - failure
try {
schemaService2.update(SchemaType.PLAIN, schemaTO);
fail("Schema update as user should not work");
} catch (ForbiddenException e) {
assertNotNull(e);
}
assertEquals(0, getFailedLogins(userService, userTO.getKey()));
}
use of org.apache.syncope.common.rest.api.service.SchemaService in project syncope by apache.
the class SchemaResource method newResourceResponse.
@Override
protected AbstractResource.ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
LOG.debug("Search all {} any type kind related schemas", AnyTypeKind.USER.name());
ResourceResponse response = new AbstractResource.ResourceResponse();
response.setContentType(MediaType.APPLICATION_JSON);
try {
HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
if (!xsrfCheck(request)) {
LOG.error("XSRF TOKEN does not match");
response.setError(Response.Status.BAD_REQUEST.getStatusCode(), "XSRF TOKEN does not match");
return response;
}
List<String> classes = Collections.emptyList();
String group = attributes.getParameters().get("group").toString();
if (group != null) {
try {
TypeExtensionTO typeExt = SyncopeEnduserSession.get().getService(SyncopeService.class).readUserTypeExtension(group);
classes = typeExt.getAuxClasses();
} catch (Exception e) {
LOG.error("Could not read User type extension for Group {}", group);
}
} else {
String anyTypeClass = attributes.getParameters().get("anyTypeClass").toString();
if (anyTypeClass != null) {
classes = Collections.singletonList(anyTypeClass);
} else {
classes = SyncopeEnduserSession.get().getService(SyncopeService.class).platform().getUserClasses();
}
}
// USER from customization, if empty or null ignore it, use it to filter attributes otherwise
Map<String, CustomAttributesInfo> customForm = SyncopeEnduserApplication.get().getCustomForm();
SchemaService schemaService = SyncopeEnduserSession.get().getService(SchemaService.class);
final List<SchemaTO> plainSchemas = classes.isEmpty() ? Collections.<SchemaTO>emptyList() : customForm == null || customForm.isEmpty() || customForm.get(SchemaType.PLAIN.name()) == null ? schemaService.search(new SchemaQuery.Builder().type(SchemaType.PLAIN).anyTypeClasses(classes).build()) : customForm.get(SchemaType.PLAIN.name()).isShow() ? customizeSchemas(schemaService.search(new SchemaQuery.Builder().type(SchemaType.PLAIN).anyTypeClasses(classes).build()), group, customForm.get(SchemaType.PLAIN.name()).getAttributes()) : Collections.<SchemaTO>emptyList();
final List<SchemaTO> derSchemas = classes.isEmpty() ? Collections.<SchemaTO>emptyList() : customForm == null || customForm.isEmpty() || customForm.get(SchemaType.DERIVED.name()) == null ? schemaService.search(new SchemaQuery.Builder().type(SchemaType.DERIVED).anyTypeClasses(classes).build()) : customForm.get(SchemaType.DERIVED.name()).isShow() ? customizeSchemas(schemaService.search(new SchemaQuery.Builder().type(SchemaType.DERIVED).anyTypeClasses(classes).build()), group, customForm.get(SchemaType.DERIVED.name()).getAttributes()) : Collections.<SchemaTO>emptyList();
final List<SchemaTO> virSchemas = classes.isEmpty() ? Collections.<SchemaTO>emptyList() : customForm == null || customForm.isEmpty() || customForm.get(SchemaType.VIRTUAL.name()) == null ? schemaService.search(new SchemaQuery.Builder().type(SchemaType.VIRTUAL).anyTypeClasses(classes).build()) : customForm.get(SchemaType.VIRTUAL.name()).isShow() ? customizeSchemas(schemaService.search(new SchemaQuery.Builder().type(SchemaType.VIRTUAL).anyTypeClasses(classes).build()), group, customForm.get(SchemaType.VIRTUAL.name()).getAttributes()) : Collections.<SchemaTO>emptyList();
if (group != null) {
plainSchemas.forEach(schema -> {
schema.setKey(compositeSchemaKey(group, schema.getKey()));
});
derSchemas.forEach(schema -> {
schema.setKey(compositeSchemaKey(group, schema.getKey()));
});
virSchemas.forEach(schema -> {
schema.setKey(compositeSchemaKey(group, schema.getKey()));
});
}
response.setTextEncoding(StandardCharsets.UTF_8.name());
response.setWriteCallback(new AbstractResource.WriteCallback() {
@Override
public void writeData(final IResource.Attributes attributes) throws IOException {
attributes.getResponse().write(MAPPER.writeValueAsString(new SchemaResponse().plainSchemas(plainSchemas).derSchemas(derSchemas).virSchemas(virSchemas)));
}
});
response.setStatusCode(Response.Status.OK.getStatusCode());
} catch (Exception e) {
LOG.error("Error retrieving {} any type kind related schemas", AnyTypeKind.USER.name(), e);
response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
}
return response;
}
Aggregations