use of eu.bcvsolutions.idm.core.model.entity.IdmScript 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;
}
}
Aggregations