use of eu.bcvsolutions.idm.core.api.dto.IdmRoleFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleFormAttributeServiceIntegrationTest method testSubDefinitionOverrideValidationReqex.
@Test
public void testSubDefinitionOverrideValidationReqex() {
String regex = "regex";
// Create role with attribute (include the sub-definition)
IdmRoleDto role = createRoleWithAttributes();
IdmRoleFormAttributeFilter filter = new IdmRoleFormAttributeFilter();
filter.setRole(role.getId());
List<IdmRoleFormAttributeDto> list = roleFormAttributeService.find(filter, null).getContent();
Assert.assertEquals(2, list.size());
IdmFormDefinitionDto formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(2, formAttributeSubdefinition.getFormAttributes().size());
// Set regex validation on false to IP attribute in the sub-definition
list.stream().filter(roleFormAttributeDto -> {
IdmFormAttributeDto formAttributeDto = DtoUtils.getEmbedded(roleFormAttributeDto, IdmRoleFormAttribute_.formAttribute.getName(), IdmFormAttributeDto.class);
return formAttributeDto.getCode().equals(IP);
}).forEach(roleFormAttributeDto -> {
Assert.assertNull(roleFormAttributeDto.getRegex());
roleFormAttributeDto.setRegex(regex);
roleFormAttributeService.save(roleFormAttributeDto);
});
// Load sub-definition by role
formAttributeSubdefinition = roleService.getFormAttributeSubdefinition(role);
Assert.assertEquals(2, formAttributeSubdefinition.getFormAttributes().size());
IdmFormAttributeDto ipFormAttribute = formAttributeSubdefinition.getFormAttributes().stream().filter(attributeDto -> {
return attributeDto.getCode().equals(IP);
}).findFirst().orElse(null);
Assert.assertNotNull(ipFormAttribute);
Assert.assertEquals(regex, ipFormAttribute.getRegex());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRoleFormAttributeDto in project CzechIdMng by bcvsolutions.
the class AbstractRoleParametrizationFormDefaultValueGenerator method getDefaultValue.
/**
* Return default value for given form-attribute. First try to find override
* role-form-attribute. If exists, then return it's default value. If not, then
* default value from given form-attribute will be returned.
*
* @param attribute
* @param role
* @return
*/
private String getDefaultValue(IdmFormAttributeDto attribute, IdmRoleDto role) {
Assert.notNull(attribute, "Attribute is required.");
Assert.notNull(role, "Role is required.");
IdmRoleFormAttributeFilter roleFormAttributeFilter = new IdmRoleFormAttributeFilter();
roleFormAttributeFilter.setRole(role.getId());
roleFormAttributeFilter.setFormAttribute(attribute.getId());
List<IdmRoleFormAttributeDto> roleFormAttributes = roleFormAttributeService.find(roleFormAttributeFilter, null).getContent();
if (roleFormAttributes.size() > 0) {
return roleFormAttributes.get(0).getDefaultValue();
}
return attribute.getDefaultValue();
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRoleFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleService method getFormAttributeSubdefinition.
@Override
public IdmFormDefinitionDto getFormAttributeSubdefinition(IdmRoleDto role) {
Assert.notNull(role, "Role is required.");
UUID identityRoleAttributeDefinition = role.getIdentityRoleAttributeDefinition();
if (identityRoleAttributeDefinition == null) {
return null;
}
IdmFormDefinitionDto definition = this.getFormService().getDefinition(identityRoleAttributeDefinition);
List<IdmFormAttributeDto> allAttributes = definition.getFormAttributes();
// Find sub-definition for given role
IdmRoleFormAttributeFilter attributeFilter = new IdmRoleFormAttributeFilter();
attributeFilter.setRole(role.getId());
List<IdmRoleFormAttributeDto> roleFormAttributes = roleFormAttributeService.find(attributeFilter, null).getContent();
// Find allowed attributes by sub-definition (and set default value from sub-definition attribute)
List<IdmFormAttributeDto> allowedAttributes = //
allAttributes.stream().filter(attribute -> {
IdmRoleFormAttributeDto result = //
roleFormAttributes.stream().filter(roleFormAttribute -> attribute.getId().equals(roleFormAttribute.getFormAttribute())).findFirst().orElse(//
null);
if (result != null) {
// Set default value from sub-definition attribute
attribute.setDefaultValue(result.getDefaultValue());
// Set validations
attribute.setRequired(result.isRequired());
attribute.setUnique(result.isUnique());
attribute.setMin(result.getMin());
attribute.setMax(result.getMax());
attribute.setRegex(result.getRegex());
attribute.setValidationMessage(result.getValidationMessage());
return true;
}
return false;
}).collect(Collectors.toList());
definition.setFormAttributes(allowedAttributes);
return definition;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRoleFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmRoleFormAttributeService method addAttributeToSubdefintion.
@Override
public IdmRoleFormAttributeDto addAttributeToSubdefintion(IdmRoleDto role, IdmFormAttributeDto attribute, BasePermission... permission) {
Assert.notNull(role, "Role is required.");
Assert.notNull(attribute, "Attribute is required.");
IdmRoleFormAttributeDto roleFormAttributeDto = new IdmRoleFormAttributeDto();
roleFormAttributeDto.setRole(role.getId());
roleFormAttributeDto.setFormAttribute(attribute.getId());
roleFormAttributeDto.setDefaultValue(attribute.getDefaultValue());
roleFormAttributeDto.setRequired(attribute.isRequired());
roleFormAttributeDto.setMin(attribute.getMin());
roleFormAttributeDto.setMax(attribute.getMax());
roleFormAttributeDto.setUnique(attribute.isUnique());
roleFormAttributeDto.setRegex(attribute.getRegex());
return this.save(roleFormAttributeDto, permission);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmRoleFormAttributeDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmFormAttributeService method deleteInternal.
@Override
@Transactional
@SuppressWarnings({ "rawtypes", "unchecked" })
public void deleteInternal(IdmFormAttributeDto dto) {
Assert.notNull(dto, "DTO is required.");
// attribute with filled values cannot be deleted
IdmFormValueFilter filter = new IdmFormValueFilter();
filter.setAttributeId(dto.getId());
formValueServices.getPlugins().forEach(formValueService -> {
if (formValueService.find(filter, PageRequest.of(0, 1)).getTotalElements() > 0) {
throw new ResultCodeException(CoreResultCode.FORM_ATTRIBUTE_DELETE_FAILED_HAS_VALUES, ImmutableMap.of("formAttribute", dto.getCode()));
}
});
//
// check rules for automatic role attributes
IdmAutomaticRoleAttributeRuleFilter automaticRoleRuleFilter = new IdmAutomaticRoleAttributeRuleFilter();
automaticRoleRuleFilter.setFormAttributeId(dto.getId());
long totalElements = automaticRoleAttributeService.find(automaticRoleRuleFilter, PageRequest.of(0, 1)).getTotalElements();
if (totalElements > 0) {
// some automatic roles use this attribute
throw new ResultCodeException(CoreResultCode.FORM_ATTRIBUTE_DELETE_FAILED_AUTOMATIC_ROLE_RULE_ASSIGNED, ImmutableMap.of("formAttribute", dto.getId()));
}
// Check on using this attribute on role (sub-definition)
if (dto.getId() != null) {
IdmRoleFormAttributeFilter roleFormAttributeFilter = new IdmRoleFormAttributeFilter();
roleFormAttributeFilter.setFormAttribute(dto.getId());
List<IdmRoleFormAttributeDto> attributes = roleFormAttributeService.find(roleFormAttributeFilter, PageRequest.of(0, 1)).getContent();
if (attributes.size() > 0) {
IdmRoleDto roleDto = DtoUtils.getEmbedded(attributes.get(0), IdmRoleFormAttribute_.role.getName(), IdmRoleDto.class);
throw new ResultCodeException(CoreResultCode.FORM_ATTRIBUTE_DELETE_FAILED_ROLE_ATTRIBUTE, ImmutableMap.of("definition", dto.getCode(), "role", roleDto.getCode()));
}
}
//
// Check rules requests for automatic role attributes. Deletes relation on this form attribute.
IdmAutomaticRoleAttributeRuleRequestFilter automaticRoleRuleRequestFilter = new IdmAutomaticRoleAttributeRuleRequestFilter();
automaticRoleRuleRequestFilter.setFormAttributeId(dto.getId());
List<IdmAutomaticRoleAttributeRuleRequestDto> ruleRequests = automaticRoleAttributeRequestService.find(automaticRoleRuleRequestFilter, null).getContent();
ruleRequests.forEach(rule -> {
rule.setFormAttribute(null);
automaticRoleAttributeRequestService.save(rule);
});
//
super.deleteInternal(dto);
}
Aggregations