Search in sources :

Example 1 with RoleAlreadyExistsException

use of io.gravitee.management.service.exceptions.RoleAlreadyExistsException in project gravitee-management-rest-api by gravitee-io.

the class RoleServiceImpl method create.

@Override
public RoleEntity create(final NewRoleEntity roleEntity) {
    try {
        Role role = convert(roleEntity);
        if (roleRepository.findById(role.getScope(), role.getName()).isPresent()) {
            throw new RoleAlreadyExistsException(role.getScope(), role.getName());
        }
        role.setCreatedAt(new Date());
        role.setUpdatedAt(role.getCreatedAt());
        RoleEntity entity = convert(roleRepository.create(role));
        auditService.createPortalAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_CREATED, role.getCreatedAt(), null, role);
        if (entity.isDefaultRole()) {
            toggleDefaultRole(convert(roleEntity.getScope()), entity.getName());
        }
        return entity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create role {}", roleEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to create role " + roleEntity.getName(), ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) UpdateRoleEntity(io.gravitee.management.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.management.model.NewRoleEntity) RoleEntity(io.gravitee.management.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleAlreadyExistsException(io.gravitee.management.service.exceptions.RoleAlreadyExistsException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

NewRoleEntity (io.gravitee.management.model.NewRoleEntity)1 RoleEntity (io.gravitee.management.model.RoleEntity)1 UpdateRoleEntity (io.gravitee.management.model.UpdateRoleEntity)1 RoleAlreadyExistsException (io.gravitee.management.service.exceptions.RoleAlreadyExistsException)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 Role (io.gravitee.repository.management.model.Role)1