use of eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto in project CzechIdMng by bcvsolutions.
the class AbstractScriptEvaluator method evaluate.
/**
* Evaluated given script with parameters. Check if this we have permission for evaluated this script.
* @param scriptCode
* @param parameters
* @return
* @throws ClassNotFoundException
*/
protected Object evaluate(String scriptCode, Map<String, Object> parameters) {
IdmScript script = scriptRepository.findOneByCode(scriptCode);
//
if (script == null) {
throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("script", scriptCode));
}
//
if (!canExecuteScript(script)) {
throw new ResultCodeException(CoreResultCode.GROOVY_SCRIPT_INVALID_CATEGORY, ImmutableMap.of("scriptCategory", script.getCategory()));
}
//
List<IdmScriptAuthorityDto> scriptAuthorities = getScriptAuthorityForScript(script.getId());
//
List<Class<?>> extraAllowedClasses = new ArrayList<>();
//
// Add builder
extraAllowedClasses.add(Builder.class);
//
for (IdmScriptAuthorityDto scriptAuthority : scriptAuthorities) {
if (scriptAuthority.getType() == ScriptAuthorityType.CLASS_NAME) {
try {
extraAllowedClasses.add(Class.forName(scriptAuthority.getClassName()));
} catch (ClassNotFoundException e) {
LOG.error(e.getLocalizedMessage());
throw new ResultCodeException(CoreResultCode.BAD_VALUE, ImmutableMap.of("class", scriptAuthority.getClassName()), e);
}
} else {
parameters.put(scriptAuthority.getService(), applicationContext.getBean(scriptAuthority.getService()));
}
}
//
try {
return groovyScriptService.evaluate(script.getScript(), parameters, extraAllowedClasses);
} catch (SecurityException | IdmSecurityException ex) {
LOG.error("SecurityException [{}]. Script code: [{}], name: [{}], category: [{}]", ex.getLocalizedMessage(), script.getCode(), script.getName(), script.getCategory().name());
throw ex;
} catch (Exception e) {
LOG.error("Exception [{}]. Script code: [{}], name: [{}], category: [{}]", e.getLocalizedMessage(), script.getCode(), script.getName(), script.getCategory().name());
throw e;
}
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmScriptServiceIntegrationTest method initTest.
@Test
public void initTest() {
IdmScriptDto script1 = scriptService.getByCode(TEST_SCRIPT_CODE_1);
IdmScriptDto script2 = scriptService.getByCode(TEST_SCRIPT_CODE_2);
assertNotNull(script1);
assertNotNull(script2);
assertEquals(TEST_SCRIPT_CODE_1, script1.getCode());
assertEquals(TEST_SCRIPT_CODE_2, script2.getCode());
IdmScriptAuthorityFilter filter = new IdmScriptAuthorityFilter();
filter.setScriptId(script1.getId());
List<IdmScriptAuthorityDto> authorities = scriptAuthorityService.find(filter, null).getContent();
assertEquals(4, authorities.size());
filter.setScriptId(script2.getId());
authorities = scriptAuthorityService.find(filter, null).getContent();
assertEquals(0, authorities.size());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmScriptService method dtoToType.
/**
* Transform dto to type.
*
* @param dto
* @return
*/
private IdmScriptType dtoToType(IdmScriptDto dto, List<IdmScriptAuthorityDto> authorities) {
IdmScriptType type = new IdmScriptType();
if (dto == null) {
return type;
}
// transform DTO to type
type.setCode(dto.getCode());
type.setName(dto.getName());
// parameter isn't implemented yet
// type.setParameters(dto.getParameter());
type.setBody(dto.getScript());
type.setCategory(dto.getCategory());
type.setDescription(dto.getDescription());
type.setType(SCRIPT_DEFAULT_TYPE);
//
if (authorities != null && !authorities.isEmpty()) {
List<IdmScriptAllowClassType> classes = new ArrayList<>();
List<IdmScriptServiceType> services = new ArrayList<>();
for (IdmScriptAuthorityDto auth : authorities) {
if (auth.getType() == ScriptAuthorityType.CLASS_NAME) {
IdmScriptAllowClassType classType = new IdmScriptAllowClassType();
classType.setClassName(auth.getClassName());
classes.add(classType);
} else {
IdmScriptServiceType service = new IdmScriptServiceType();
service.setClassName(auth.getClassName());
service.setName(auth.getService());
services.add(service);
}
}
if (!classes.isEmpty()) {
type.setAllowClasses(new IdmScriptAllowClassesType());
type.getAllowClasses().setAllowClasses(classes);
}
if (!services.isEmpty()) {
type.setServices(new IdmScriptServicesType());
type.getServices().setServices(services);
}
}
return type;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto in project CzechIdMng by bcvsolutions.
the class ExecuteScriptTaskExecutorTest method prepareAuthForTestScript.
/**
* Method prepare test authorities for script.
*
* @param scriptDto
*/
private void prepareAuthForTestScript(IdmScriptDto scriptDto) {
IdmScriptAuthorityDto authDto = new IdmScriptAuthorityDto();
authDto.setType(ScriptAuthorityType.CLASS_NAME);
authDto.setClassName("eu.bcvsolutions.idm.core.scheduler.task.impl.ExecuteScriptTaskExecutor");
authDto.setScript(scriptDto.getId());
scriptAuthorityService.save(authDto);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto in project CzechIdMng by bcvsolutions.
the class ExecuteScriptTaskExecutorTest method testExeciteScriptWithResult.
@Test
public void testExeciteScriptWithResult() {
// remove previous long running task
List<IdmLongRunningTaskDto> tasks = longRunningTaskService.find(null).getContent();
for (IdmLongRunningTaskDto task : tasks) {
longRunningTaskService.delete(task);
}
IdmScriptDto scriptDto = new IdmScriptDto();
scriptDto.setCode(TEST_SCRIPT_CODE + "_3");
scriptDto.setName(TEST_SCRIPT_CODE + "_3");
scriptDto.setCategory(IdmScriptCategory.SYSTEM);
StringBuilder builder = new StringBuilder();
builder.append("import eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto;\n");
builder.append("task.setCounter(0l);\n");
builder.append("task.setCount(5l);\n");
builder.append("for (int index = 0; index < 5; index++) {\n");
builder.append(" IdmIdentityDto dto = new IdmIdentityDto();\n");
builder.append(" dto.setUsername('test-execute-' + index);\n");
builder.append(" dto.setLastName('test-execute-' + index);\n");
builder.append(" dto.setFirstName('' + index);\n");
builder.append(" identityService.save(dto);\n");
builder.append(" task.increaseCounter();\n");
builder.append(" task.updateState();\n");
builder.append("}\n");
scriptDto.setScript(builder.toString());
scriptDto = scriptService.save(scriptDto);
prepareAuthForTestScript(scriptDto);
IdmScriptAuthorityDto authDto = new IdmScriptAuthorityDto();
authDto.setType(ScriptAuthorityType.SERVICE);
authDto.setClassName("eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmIdentityService");
authDto.setScript(scriptDto.getId());
authDto.setService("identityService");
scriptAuthorityService.save(authDto);
authDto = new IdmScriptAuthorityDto();
authDto.setType(ScriptAuthorityType.CLASS_NAME);
authDto.setClassName("eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto");
authDto.setScript(scriptDto.getId());
scriptAuthorityService.save(authDto);
Map<String, Object> parameters = new HashMap<>();
parameters.put("scriptCode", TEST_SCRIPT_CODE + "_3");
taskExecutor.init(parameters);
LongRunningFutureTask<Boolean> futureTask = manager.execute(taskExecutor);
try {
assertEquals(Boolean.TRUE, futureTask.getFutureTask().get());
} catch (InterruptedException | ExecutionException e) {
fail(e.getMessage());
}
IdmLongRunningTaskDto longRunningTask = longRunningTaskService.get(taskExecutor.getLongRunningTaskId());
assertEquals(OperationState.EXECUTED, longRunningTask.getResult().getState());
assertEquals(5, longRunningTask.getCount().longValue());
assertEquals(5, longRunningTask.getCounter().longValue());
IdmIdentityFilter identityFilter = new IdmIdentityFilter();
identityFilter.setText("test-execute-");
List<IdmIdentityDto> identities = identityService.find(identityFilter, new PageRequest(0, 20, new Sort(Direction.ASC, IdmIdentity_.firstName.getName()))).getContent();
assertEquals(5, identities.size());
for (int index = 0; index < 5; index++) {
assertEquals(String.valueOf(index), identities.get(index).getFirstName());
}
}
Aggregations