Search in sources :

Example 21 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipService_GetMemberPermissionsTest method shouldGetPermissionsIfMemberOfApiGroup.

@Test
public void shouldGetPermissionsIfMemberOfApiGroup() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    doReturn(Collections.singleton(GROUP_ID1)).when(api).getGroups();
    doReturn(api).when(apiService).findById(API_ID);
    doReturn(Collections.emptySet()).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    Membership membership = mock(Membership.class);
    doReturn("API_" + ROLENAME).when(membership).getRoleId();
    doReturn(new HashSet<>(asList(membership))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
    UserEntity userEntity = mock(UserEntity.class);
    doReturn(userEntity).when(userService).findById(USERNAME);
    RoleEntity roleEntity = mock(RoleEntity.class);
    Map<String, char[]> rolePerms = new HashMap<>();
    rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
    doReturn(rolePerms).when(roleEntity).getPermissions();
    doReturn(RoleScope.API).when(roleEntity).getScope();
    doReturn(roleEntity).when(roleService).findById("API_" + ROLENAME);
    Map<String, char[]> permissions = membershipService.getUserMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    assertPermissions(rolePerms, permissions);
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
    verify(apiService, times(1)).findById(API_ID);
    verify(userService, times(1)).findById(USERNAME);
    verify(roleService, times(1)).findById("API_" + ROLENAME);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 22 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity 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(roleEntity.getName());
    }
    RoleScope scope = roleEntity.getScope();
    try {
        Optional<Role> optRole = roleRepository.findById(roleEntity.getId());
        if (!optRole.isPresent()) {
            throw new RoleNotFoundException(roleEntity.getId());
        }
        Role role = optRole.get();
        Role updatedRole = convert(roleEntity);
        updatedRole.setCreatedAt(role.getCreatedAt());
        updatedRole.setReferenceId(role.getReferenceId());
        updatedRole.setReferenceType(role.getReferenceType());
        RoleEntity entity = convert(roleRepository.update(updatedRole));
        auditService.createOrganizationAuditLog(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.rest.api.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException)

Example 23 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.

the class RoleServiceImpl method create.

private RoleEntity create(final NewRoleEntity roleEntity, String organizationId) {
    try {
        Role role = convert(roleEntity);
        if (roleRepository.findByScopeAndNameAndReferenceIdAndReferenceType(role.getScope(), role.getName(), organizationId, RoleReferenceType.ORGANIZATION).isPresent()) {
            throw new RoleAlreadyExistsException(role.getScope(), role.getName());
        }
        role.setId(UuidString.generateRandom());
        role.setCreatedAt(new Date());
        role.setUpdatedAt(role.getCreatedAt());
        role.setReferenceId(organizationId);
        role.setReferenceType(RoleReferenceType.ORGANIZATION);
        RoleEntity entity = convert(roleRepository.create(role));
        auditService.createOrganizationAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_CREATED, role.getCreatedAt(), null, role);
        if (entity.isDefaultRole()) {
            toggleDefaultRole(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.rest.api.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException)

Example 24 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.

the class RoleServiceImpl method delete.

@Override
public void delete(final String roleId) {
    try {
        Optional<Role> optRole = roleRepository.findById(roleId);
        if (!optRole.isPresent()) {
            throw new RoleNotFoundException(roleId);
        }
        Role role = optRole.get();
        RoleScope scope = convert(role.getScope());
        if (role.isDefaultRole() || role.isSystem()) {
            throw new RoleDeletionForbiddenException(scope, role.getName());
        }
        List<RoleEntity> defaultRoleByScopes = findDefaultRoleByScopes(scope);
        if (defaultRoleByScopes.isEmpty()) {
            throw new DefaultRoleNotFoundException();
        }
        membershipService.removeRoleUsage(roleId, defaultRoleByScopes.get(0).getId());
        roleRepository.delete(roleId);
        auditService.createOrganizationAuditLog(Collections.singletonMap(ROLE, scope + ":" + role.getName()), ROLE_DELETED, role.getUpdatedAt(), role, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete role {}", roleId, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete role " + roleId, ex);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException)

Example 25 with RoleEntity

use of io.gravitee.rest.api.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.

the class TaskServiceTest method shouldFindAll.

@Test
public void shouldFindAll() throws TechnicalException {
    MembershipEntity m1 = new MembershipEntity();
    m1.setId("1");
    m1.setReferenceId("api1");
    m1.setReferenceType(MembershipReferenceType.API);
    m1.setRoleId("API_PO");
    MembershipEntity m2 = new MembershipEntity();
    m2.setId("2");
    m2.setReferenceId("api2");
    m2.setReferenceType(MembershipReferenceType.API);
    m2.setRoleId("API_USER");
    Map<String, char[]> withPerm = new HashMap<>();
    withPerm.put("SUBSCRIPTION", new char[] { 'C', 'R', 'U', 'D' });
    Map<String, char[]> withoutPerm = new HashMap<>();
    withoutPerm.put("SUBSCRIPTION", new char[] { 'C', 'R', 'D' });
    RoleEntity roleEntityWithPerm = new RoleEntity();
    roleEntityWithPerm.setName("PO");
    roleEntityWithPerm.setPermissions(withPerm);
    roleEntityWithPerm.setScope(io.gravitee.rest.api.model.permissions.RoleScope.API);
    RoleEntity roleEntityWithoutPerm = new RoleEntity();
    roleEntityWithoutPerm.setName("USER");
    roleEntityWithoutPerm.setPermissions(withoutPerm);
    roleEntityWithoutPerm.setScope(io.gravitee.rest.api.model.permissions.RoleScope.API);
    when(roleService.findById("API_PO")).thenReturn(roleEntityWithPerm);
    when(roleService.findById("API_USER")).thenReturn(roleEntityWithoutPerm);
    when(promotionTasksService.getPromotionTasks(any())).thenReturn(emptyList());
    Set<MembershipEntity> memberships = new HashSet<>();
    memberships.add(m1);
    memberships.add(m2);
    when(membershipService.getMembershipsByMemberAndReference(any(), any(), any())).thenReturn(memberships);
    when(userService.search(any(UserCriteria.class), any())).thenReturn(new Page<>(emptyList(), 1, 0, 0));
    taskService.findAll("user");
    verify(subscriptionService, times(1)).search(any());
    verify(promotionTasksService, times(1)).getPromotionTasks(any());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) HashMap(java.util.HashMap) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

RoleEntity (io.gravitee.rest.api.model.RoleEntity)29 Test (org.junit.Test)20 UserEntity (io.gravitee.rest.api.model.UserEntity)13 Membership (io.gravitee.repository.management.model.Membership)8 MembershipService (io.gravitee.rest.api.service.MembershipService)7 Role (io.gravitee.repository.management.model.Role)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5 NewRoleEntity (io.gravitee.rest.api.model.NewRoleEntity)5 UpdateRoleEntity (io.gravitee.rest.api.model.UpdateRoleEntity)5 Response (javax.ws.rs.core.Response)5 MembershipCommand (io.gravitee.cockpit.api.command.membership.MembershipCommand)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)4 Instant (java.time.Instant)4 List (java.util.List)4 JWT (com.auth0.jwt.JWT)3 Algorithm (com.auth0.jwt.algorithms.Algorithm)3 Maps (io.gravitee.common.util.Maps)3 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)3