use of eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmScriptService method duplicate.
@Override
@Transactional
public IdmScriptDto duplicate(UUID id) {
IdmScriptDto originalScript = get(id);
Assert.notNull(originalScript, "Script is required.");
// find script authorities
IdmScriptAuthorityFilter authorityFilt = new IdmScriptAuthorityFilter();
authorityFilt.setScriptId(originalScript.getId());
List<IdmScriptAuthorityDto> authorityDtos = scriptAuthorityService.find(authorityFilt, null).getContent();
// script attributes duplication
String newCode = getUniqueCode(originalScript.getCode(), 0);
IdmScriptDto newScript = clone(originalScript.getId());
newScript.setCode(newCode);
newScript = save(newScript);
// script authority
for (IdmScriptAuthorityDto authorityDto : authorityDtos) {
authorityDto.setId(null);
EntityUtils.clearAuditFields(authorityDto);
authorityDto.setScript(newScript.getId());
}
scriptAuthorityService.saveAll(authorityDtos);
return newScript;
}
use of eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmScriptService method toType.
/**
* Transform dto to type.
*
* @param dto
* @return
*/
@Override
protected IdmScriptType toType(IdmScriptDto dto) {
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 (dto.getId() == null) {
return type;
}
IdmScriptAuthorityFilter filter = new IdmScriptAuthorityFilter();
filter.setScriptId(dto.getId());
List<IdmScriptAuthorityDto> authorities = scriptAuthorityService.find(filter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmScriptAuthority_.type.getName(), IdmScriptAuthority_.service.getName(), IdmScriptAuthority_.className.getName()))).getContent();
if (authorities.isEmpty()) {
return type;
}
//
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.filter.IdmScriptAuthorityFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmScriptServiceIntegrationTest method testDeployScriptFromAttachement.
@Test
public void testDeployScriptFromAttachement() throws IOException {
String scriptCode = getHelper().createName();
String scriptOne = "<script> <code>" + scriptCode + "</code> <name>Test script 1</name> <body> <![CDATA[ String s; ]]> </body> <type>groovy</type> <category>DEFAULT</category> <parameters>test1</parameters> <services> <service> <name>treeNodeService</name> <className>eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmTreeNodeService</className> </service> <service> <name>identityService</name> <className>eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmIdentityService</className> </service> </services><allowClasses> <allowClass> <className>eu.bcvsolutions.idm.core.model.entity.IdmTreeNode</className> </allowClass> <allowClass> <className>eu.bcvsolutions.idm.core.model.entity.IdmIdentity</className> </allowClass> </allowClasses> </script>";
// create attachment
IdmAttachmentDto attachment = new IdmAttachmentDto();
attachment.setOwnerType(AttachmentManager.TEMPORARY_ATTACHMENT_OWNER_TYPE);
attachment.setName("scriptOne.xml");
attachment.setMimetype(MediaType.TEXT_XML_VALUE);
attachment.setInputData(IOUtils.toInputStream(scriptOne));
// owner and version is resolved after attachment is saved
attachment = attachmentManager.saveAttachment(null, attachment);
// deploy
List<IdmScriptDto> results = scriptService.deploy(attachment);
//
Assert.assertEquals(1, results.size());
Assert.assertEquals(scriptCode, results.get(0).getCode());
//
// test authorities
IdmScriptAuthorityFilter authorityFilter = new IdmScriptAuthorityFilter();
authorityFilter.setScriptId(results.get(0).getId());
List<IdmScriptAuthorityDto> authorities = scriptAuthorityService.find(authorityFilter, null).getContent();
Assert.assertEquals(4, authorities.size());
Assert.assertTrue(authorities.stream().allMatch(a -> StringUtils.isNotEmpty(a.getClassName())));
Assert.assertTrue(authorities.stream().anyMatch(a -> "treeNodeService".equals(a.getService())));
Assert.assertTrue(authorities.stream().anyMatch(a -> "identityService".equals(a.getService())));
Assert.assertTrue(authorities.stream().anyMatch(a -> a.getClassName().equals("eu.bcvsolutions.idm.core.model.entity.IdmTreeNode")));
Assert.assertTrue(authorities.stream().anyMatch(a -> a.getClassName().equals("eu.bcvsolutions.idm.core.model.entity.IdmIdentity")));
//
// deploy from archive
IdmScriptDto scriptOneDto = scriptService.getByCode(scriptCode);
String scriptCodeTwo = getHelper().createName();
String scriptOneUpdateName = getHelper().createName();
scriptOne = "<script> <code>" + scriptCode + "</code> <name>" + scriptOneUpdateName + "</name> <body> <![CDATA[ String s; ]]> </body> <type>groovy</type> <category>DEFAULT</category> <parameters>test1</parameters> <services> <service> <name>treeNodeService</name> <className>eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmTreeNodeService</className> </service> </services><allowClasses> <allowClass> <className>eu.bcvsolutions.idm.core.model.entity.IdmTreeNode</className> </allowClass> <allowClass> <className>eu.bcvsolutions.idm.core.model.entity.IdmIdentity</className> </allowClass> </allowClasses> </script>";
// create attachment
String scriptTwo = "<script> <code>" + scriptCodeTwo + "</code> <name>Test script 2</name> <body> <![CDATA[ String s; ]]> </body> <type>groovy</type> <category>DEFAULT</category> <parameters>test1</parameters> <services> <service> <name>treeNodeService</name> <className>eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmTreeNodeService</className> </service> <service> <name>identityService</name> <className>eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmIdentityService</className> </service> </services><allowClasses> <allowClass> <className>eu.bcvsolutions.idm.core.model.entity.IdmTreeNode</className> </allowClass> <allowClass> <className>eu.bcvsolutions.idm.core.model.entity.IdmIdentity</className> </allowClass> </allowClasses> </script>";
IdmAttachmentDto attachmentOne = new IdmAttachmentDto();
attachmentOne.setOwnerType(AttachmentManager.TEMPORARY_ATTACHMENT_OWNER_TYPE);
attachmentOne.setName("scriptOne.xml");
attachmentOne.setMimetype(MediaType.TEXT_XML_VALUE);
attachmentOne.setInputData(IOUtils.toInputStream(scriptOne));
attachmentOne = attachmentManager.saveAttachment(null, attachmentOne);
IdmAttachmentDto attachmentTwo = new IdmAttachmentDto();
attachmentTwo.setOwnerType(AttachmentManager.TEMPORARY_ATTACHMENT_OWNER_TYPE);
attachmentTwo.setName("scriptTwo.xml");
attachmentTwo.setMimetype(MediaType.TEXT_XML_VALUE);
attachmentTwo.setInputData(IOUtils.toInputStream(scriptTwo));
attachmentTwo = attachmentManager.saveAttachment(null, attachmentTwo);
// zip
File zipFolder = attachmentManager.createTempDirectory(null).toFile();
File targetFileOne = new File(zipFolder.toString(), String.format("%s.xml", attachmentOne.getName()));
Files.copy(attachmentManager.getAttachmentData(attachmentOne.getId()), targetFileOne.toPath(), StandardCopyOption.REPLACE_EXISTING);
File targetFileTwo = new File(zipFolder.toString(), String.format("%s.xml", attachmentTwo.getName()));
Files.copy(attachmentManager.getAttachmentData(attachmentTwo.getId()), targetFileTwo.toPath(), StandardCopyOption.REPLACE_EXISTING);
// compress
File zipFile = attachmentManager.createTempFile();
ZipUtils.compress(zipFolder, zipFile.getPath());
IdmAttachmentDto attachmentZip = new IdmAttachmentDto();
attachmentZip.setOwnerType(AttachmentManager.TEMPORARY_ATTACHMENT_OWNER_TYPE);
attachmentZip.setInputData(new FileInputStream(zipFile));
attachmentZip.setEncoding(AttachableEntity.DEFAULT_ENCODING);
// zip ~ octet stream
attachmentZip.setMimetype(AttachableEntity.DEFAULT_MIMETYPE);
attachmentZip.setName("backup.zip");
attachmentZip = attachmentManager.saveAttachment(null, attachmentZip);
// deploy
results = scriptService.deploy(attachmentZip);
//
Assert.assertEquals(2, results.size());
Assert.assertTrue(results.stream().anyMatch(s -> s.getId().equals(scriptOneDto.getId())));
Assert.assertTrue(results.stream().anyMatch(s -> s.getCode().equals(scriptCode) && s.getName().equals(scriptOneUpdateName)));
Assert.assertTrue(results.stream().anyMatch(s -> s.getCode().equals(scriptCodeTwo)));
//
// test authorities
authorityFilter = new IdmScriptAuthorityFilter();
authorityFilter.setScriptId(scriptOneDto.getId());
authorities = scriptAuthorityService.find(authorityFilter, null).getContent();
Assert.assertEquals(3, authorities.size());
Assert.assertTrue(authorities.stream().allMatch(a -> StringUtils.isNotEmpty(a.getClassName())));
Assert.assertTrue(authorities.stream().anyMatch(a -> "treeNodeService".equals(a.getService())));
Assert.assertTrue(authorities.stream().anyMatch(a -> a.getClassName().equals("eu.bcvsolutions.idm.core.model.entity.IdmTreeNode")));
Assert.assertTrue(authorities.stream().anyMatch(a -> a.getClassName().equals("eu.bcvsolutions.idm.core.model.entity.IdmIdentity")));
//
authorityFilter = new IdmScriptAuthorityFilter();
authorityFilter.setScriptId(scriptService.getByCode(scriptCodeTwo).getId());
authorities = scriptAuthorityService.find(authorityFilter, null).getContent();
Assert.assertEquals(4, authorities.size());
Assert.assertTrue(authorities.stream().allMatch(a -> StringUtils.isNotEmpty(a.getClassName())));
Assert.assertTrue(authorities.stream().anyMatch(a -> "treeNodeService".equals(a.getService())));
Assert.assertTrue(authorities.stream().anyMatch(a -> "identityService".equals(a.getService())));
Assert.assertTrue(authorities.stream().anyMatch(a -> a.getClassName().equals("eu.bcvsolutions.idm.core.model.entity.IdmTreeNode")));
Assert.assertTrue(authorities.stream().anyMatch(a -> a.getClassName().equals("eu.bcvsolutions.idm.core.model.entity.IdmIdentity")));
}
use of eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmScriptServiceIntegrationTest method removeAuthRedeploy.
@Test
public void removeAuthRedeploy() {
configurationService.setValue(Recoverable.BACKUP_FOLDER_CONFIG, TEST_BACKUP_FOLDER);
IdmScriptDto script1 = scriptService.getByCode(TEST_SCRIPT_CODE_1);
assertNotNull(script1);
IdmScriptAuthorityFilter filter = new IdmScriptAuthorityFilter();
filter.setScriptId(script1.getId());
List<IdmScriptAuthorityDto> authorities = scriptAuthorityService.find(filter, null).getContent();
assertEquals(4, authorities.size());
scriptAuthorityService.deleteAllByScript(script1.getId());
authorities = scriptAuthorityService.find(filter, null).getContent();
assertEquals(0, authorities.size());
scriptService.redeploy(script1);
authorities = scriptAuthorityService.find(filter, null).getContent();
assertEquals(4, authorities.size());
}
use of eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter in project CzechIdMng by bcvsolutions.
the class DefaultIdmScriptAuthorityService method deleteAllByScript.
@Override
public void deleteAllByScript(UUID scriptId) {
Assert.notNull(scriptId, "Script identifier is required.");
//
IdmScriptAuthorityFilter filter = new IdmScriptAuthorityFilter();
filter.setScriptId(scriptId);
// remove internal by id each script authority
find(filter, null).getContent().forEach(scriptAuthority -> this.deleteInternalById(scriptAuthority.getId()));
}
Aggregations