Search in sources :

Example 1 with IdmScriptAuthorityFilter

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;
}
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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with IdmScriptAuthorityFilter

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;
}
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) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) IdmScriptServiceType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptServiceType) ArrayList(java.util.ArrayList) IdmScriptAllowClassesType(eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassesType) IdmScriptAuthorityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter)

Example 3 with IdmScriptAuthorityFilter

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")));
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) IdmScriptCategory(eu.bcvsolutions.idm.core.api.domain.IdmScriptCategory) IdmScriptAuthorityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter) IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) AttachableEntity(eu.bcvsolutions.idm.core.ecm.api.entity.AttachableEntity) ZonedDateTime(java.time.ZonedDateTime) Recoverable(eu.bcvsolutions.idm.core.api.service.Recoverable) Autowired(org.springframework.beans.factory.annotation.Autowired) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) StringUtils(org.apache.commons.lang3.StringUtils) StandardCopyOption(java.nio.file.StandardCopyOption) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) After(org.junit.After) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) AttachmentManager(eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager) MediaType(org.springframework.http.MediaType) DecimalFormat(java.text.DecimalFormat) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) IdmScriptDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptDto) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) ZipUtils(eu.bcvsolutions.idm.core.api.utils.ZipUtils) Assert(org.junit.Assert) IdmScriptAuthorityService(eu.bcvsolutions.idm.core.api.service.IdmScriptAuthorityService) Assert.assertEquals(org.junit.Assert.assertEquals) Transactional(org.springframework.transaction.annotation.Transactional) 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) File(java.io.File) FileInputStream(java.io.FileInputStream) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 4 with IdmScriptAuthorityFilter

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());
}
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 5 with IdmScriptAuthorityFilter

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

Aggregations

IdmScriptAuthorityFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmScriptAuthorityFilter)9 IdmScriptAuthorityDto (eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto)6 IdmScriptDto (eu.bcvsolutions.idm.core.api.dto.IdmScriptDto)4 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)3 Test (org.junit.Test)3 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 IdmScriptType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptType)2 File (java.io.File)2 Transactional (org.springframework.transaction.annotation.Transactional)2 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 IdmScriptCategory (eu.bcvsolutions.idm.core.api.domain.IdmScriptCategory)1 ConfigurationService (eu.bcvsolutions.idm.core.api.service.ConfigurationService)1 IdmScriptAuthorityService (eu.bcvsolutions.idm.core.api.service.IdmScriptAuthorityService)1 Recoverable (eu.bcvsolutions.idm.core.api.service.Recoverable)1 ZipUtils (eu.bcvsolutions.idm.core.api.utils.ZipUtils)1 IdmAttachmentDto (eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto)1 AttachableEntity (eu.bcvsolutions.idm.core.ecm.api.entity.AttachableEntity)1 AttachmentManager (eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager)1 IdmScriptAllowClassType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassType)1 IdmScriptAllowClassesType (eu.bcvsolutions.idm.core.model.jaxb.IdmScriptAllowClassesType)1