Search in sources :

Example 6 with IdmScriptAuthorityDto

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;
    }
}
Also used : IdmScript(eu.bcvsolutions.idm.core.model.entity.IdmScript) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ArrayList(java.util.ArrayList) IdmSecurityException(eu.bcvsolutions.idm.core.security.exception.IdmSecurityException) IdmSecurityException(eu.bcvsolutions.idm.core.security.exception.IdmSecurityException) IdmSecurityException(eu.bcvsolutions.idm.core.security.exception.IdmSecurityException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 7 with IdmScriptAuthorityDto

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());
}
Also used : IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) IdmScriptDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptDto) IdmScriptAuthorityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 8 with IdmScriptAuthorityDto

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;
}
Also used : IdmScriptType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType) IdmScriptServicesType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServicesType) IdmScriptAllowClassType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassType) IdmScriptServiceType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServiceType) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) ArrayList(java.util.ArrayList) IdmScriptAllowClassesType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassesType)

Example 9 with IdmScriptAuthorityDto

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);
}
Also used : IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto)

Example 10 with IdmScriptAuthorityDto

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());
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) HashMap(java.util.HashMap) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) IdmIdentityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter) PageRequest(org.springframework.data.domain.PageRequest) IdmScriptDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptDto) Sort(org.springframework.data.domain.Sort) ExecutionException(java.util.concurrent.ExecutionException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

IdmScriptAuthorityDto (eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto)10 IdmScriptDto (eu.bcvsolutions.idm.core.api.dto.IdmScriptDto)5 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)2 IdmIdentityFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter)2 IdmScriptAuthorityFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter)2 IdmScriptAllowClassType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassType)2 IdmScriptServiceType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServiceType)2 SysSyncIdentityConfigDto (eu.bcvsolutions.idm.acc.dto.SysSyncIdentityConfigDto)1 SysSyncLogDto (eu.bcvsolutions.idm.acc.dto.SysSyncLogDto)1 SysSystemAttributeMappingDto (eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto)1 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)1 SysSystemMappingDto (eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto)1 SysSystemAttributeMappingFilter (eu.bcvsolutions.idm.acc.dto.filter.SysSystemAttributeMappingFilter)1 SysSystemMappingFilter (eu.bcvsolutions.idm.acc.dto.filter.SysSystemMappingFilter)1 DefaultSynchronizationServiceTest (eu.bcvsolutions.idm.acc.service.impl.DefaultSynchronizationServiceTest)1 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1