Search in sources :

Example 1 with RoleNotFoundException

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

the class RoleServiceImpl method delete.

@Override
public void delete(final RoleScope scope, final String name) {
    if (isReserved(name)) {
        throw new RoleReservedNameException(SystemRole.ADMIN.name());
    }
    try {
        Optional<Role> optRole = roleRepository.findById(scope, name);
        if (!optRole.isPresent()) {
            throw new RoleNotFoundException(scope, name);
        }
        Role role = optRole.get();
        roleRepository.delete(scope, name);
        auditService.createPortalAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_DELETED, role.getUpdatedAt(), role, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete role {}/{}", scope, name, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete role " + scope + "/" + name, ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleReservedNameException(io.gravitee.management.service.exceptions.RoleReservedNameException) RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 2 with RoleNotFoundException

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

the class RoleServiceImpl method update.

@Override
public RoleEntity update(final UpdateRoleEntity roleEntity) {
    if (isReserved(roleEntity.getName())) {
        throw new RoleReservedNameException(SystemRole.ADMIN.name());
    }
    RoleScope scope = convert(roleEntity.getScope());
    try {
        Optional<Role> optRole = roleRepository.findById(scope, roleEntity.getName());
        if (!optRole.isPresent()) {
            throw new RoleNotFoundException(scope, roleEntity.getName());
        }
        Role role = optRole.get();
        Role updatedRole = convert(roleEntity);
        updatedRole.setCreatedAt(role.getCreatedAt());
        RoleEntity entity = convert(roleRepository.update(updatedRole));
        auditService.createPortalAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_UPDATED, updatedRole.getUpdatedAt(), role, updatedRole);
        if (entity.isDefaultRole()) {
            toggleDefaultRole(scope, entity.getName());
        }
        return entity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update role {}", roleEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to update 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) RoleScope(io.gravitee.repository.management.model.RoleScope) RoleReservedNameException(io.gravitee.management.service.exceptions.RoleReservedNameException) RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 3 with RoleNotFoundException

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

the class MembershipService_AddOrUpdateMemberTest method shouldDisallowAddUnknownRoleOnApplicationGroup.

@Test(expected = RoleNotFoundException.class)
public void shouldDisallowAddUnknownRoleOnApplicationGroup() throws Exception {
    when(roleService.findById(any(), any())).thenThrow(new RoleNotFoundException(RoleScope.PORTAL, "name"));
    membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser("xxxxx", null), new MembershipService.MembershipRole(RoleScope.PORTAL, "name"));
}
Also used : RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) Test(org.junit.Test)

Example 4 with RoleNotFoundException

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

the class RoleServiceImpl method findById.

@Override
public RoleEntity findById(final RoleScope scope, final String name) {
    try {
        LOGGER.debug("Find Role by id");
        Optional<Role> role = roleRepository.findById(scope, name);
        if (!role.isPresent()) {
            throw new RoleNotFoundException(scope, name);
        }
        return convert(role.get());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find a role : {} {}", scope, name, ex);
        throw new TechnicalManagementException("An error occurs while trying to find a role : " + scope + " " + name, ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 5 with RoleNotFoundException

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

the class MembershipService_AddOrUpdateMemberTest method shouldDisallowAddUnknownRoleOnApiGroup.

@Test(expected = RoleNotFoundException.class)
public void shouldDisallowAddUnknownRoleOnApiGroup() throws Exception {
    when(roleService.findById(any(), any())).thenThrow(new RoleNotFoundException(RoleScope.PORTAL, "name"));
    membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser("xxxxx", null), new MembershipService.MembershipRole(RoleScope.PORTAL, "name"));
}
Also used : RoleNotFoundException(io.gravitee.management.service.exceptions.RoleNotFoundException) Test(org.junit.Test)

Aggregations

RoleNotFoundException (io.gravitee.management.service.exceptions.RoleNotFoundException)5 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 Role (io.gravitee.repository.management.model.Role)3 RoleReservedNameException (io.gravitee.management.service.exceptions.RoleReservedNameException)2 Test (org.junit.Test)2 NewRoleEntity (io.gravitee.management.model.NewRoleEntity)1 RoleEntity (io.gravitee.management.model.RoleEntity)1 UpdateRoleEntity (io.gravitee.management.model.UpdateRoleEntity)1 RoleScope (io.gravitee.repository.management.model.RoleScope)1