Search in sources :

Example 1 with UpdateGroupEntity

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

the class GroupResource method updateGroup.

@PUT
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Update an existing group")
@ApiResponses({ @ApiResponse(code = 200, message = "Group successfully updated", response = GroupEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_GROUP, acls = RolePermissionAction.UPDATE), @Permission(value = RolePermission.GROUP_MEMBER, acls = RolePermissionAction.UPDATE) })
public GroupEntity updateGroup(@ApiParam(name = "group", required = true) @Valid @NotNull final UpdateGroupEntity updateGroupEntity) {
    final GroupEntity groupEntity = checkRights();
    // check if user is a 'simple group admin' or a platform admin
    if (!permissionService.hasPermission(RolePermission.ENVIRONMENT_GROUP, GraviteeContext.getCurrentEnvironment(), CREATE, UPDATE, DELETE)) {
        updateGroupEntity.setMaxInvitation(groupEntity.getMaxInvitation());
        updateGroupEntity.setLockApiRole(groupEntity.isLockApiRole());
        updateGroupEntity.setLockApplicationRole(groupEntity.isLockApplicationRole());
        updateGroupEntity.setSystemInvitation(groupEntity.isSystemInvitation());
        updateGroupEntity.setEmailInvitation(groupEntity.isEmailInvitation());
        if (groupEntity.isLockApiRole()) {
            updateGroupEntity.getRoles().put(RoleScope.API, groupEntity.getRoles().get(RoleScope.API));
        }
        if (groupEntity.isLockApplicationRole()) {
            updateGroupEntity.getRoles().put(RoleScope.APPLICATION, groupEntity.getRoles().get(RoleScope.APPLICATION));
        }
    }
    return groupService.update(group, updateGroupEntity);
}
Also used : GroupEntity(io.gravitee.rest.api.model.GroupEntity) UpdateGroupEntity(io.gravitee.rest.api.model.UpdateGroupEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 2 with UpdateGroupEntity

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

the class GroupService_UpdateTest method shouldUpdateGroup.

@Test
public void shouldUpdateGroup() throws Exception {
    UpdateGroupEntity updatedGroupEntity = new UpdateGroupEntity();
    updatedGroupEntity.setDisableMembershipNotifications(true);
    updatedGroupEntity.setEmailInvitation(true);
    updatedGroupEntity.setEventRules(null);
    updatedGroupEntity.setLockApiRole(true);
    updatedGroupEntity.setLockApplicationRole(true);
    updatedGroupEntity.setMaxInvitation(100);
    updatedGroupEntity.setName("my-group-name");
    updatedGroupEntity.setRoles(Maps.<RoleScope, String>builder().put(RoleScope.API, "OWNER").build());
    updatedGroupEntity.setSystemInvitation(false);
    when(groupRepository.findById(GROUP_ID)).thenReturn(Optional.of(Mockito.mock(Group.class)));
    when(permissionService.hasPermission(RolePermission.ENVIRONMENT_GROUP, "DEFAULT", CREATE, UPDATE, DELETE)).thenReturn(true);
    when(membershipService.getRoles(any(), any(), any(), any())).thenReturn(Collections.emptySet());
    groupService.update(GROUP_ID, updatedGroupEntity);
    verify(groupRepository).update(argThat(group -> group.isDisableMembershipNotifications() && group.isEmailInvitation() && group.getEventRules() == null && group.isLockApiRole() && group.isLockApplicationRole() && group.getMaxInvitation() == 100 && group.getName().equals("my-group-name") && !group.isSystemInvitation()));
    verify(membershipService).addRoleToMemberOnReference(argThat(membershipReference -> membershipReference.getType() == MembershipReferenceType.API && membershipReference.getId() == null), argThat(membershipMember -> membershipMember.getMemberId().equals(GROUP_ID) && membershipMember.getReference() == null && membershipMember.getMemberType() == MembershipMemberType.GROUP), argThat(membershipRole -> membershipRole.getScope() == RoleScope.API && membershipRole.getName().equals("OWNER")));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) GroupRepository(io.gravitee.repository.management.api.GroupRepository) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Group(io.gravitee.repository.management.model.Group) Test(org.junit.Test) RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) Maps(io.gravitee.common.util.Maps) Mockito(org.mockito.Mockito) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType) Optional(java.util.Optional) GroupServiceImpl(io.gravitee.rest.api.service.impl.GroupServiceImpl) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) MembershipMemberType(io.gravitee.rest.api.model.MembershipMemberType) UpdateGroupEntity(io.gravitee.rest.api.model.UpdateGroupEntity) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) UpdateGroupEntity(io.gravitee.rest.api.model.UpdateGroupEntity) Test(org.junit.Test)

Example 3 with UpdateGroupEntity

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

the class GroupService_UpdateTest method shouldNotUpdateDefaultRoleBecausePrimaryOwner.

@Test
public void shouldNotUpdateDefaultRoleBecausePrimaryOwner() throws Exception {
    UpdateGroupEntity updatedGroupEntity = new UpdateGroupEntity();
    updatedGroupEntity.setRoles(Maps.<RoleScope, String>builder().put(RoleScope.API, "PRIMARY_OWNER").put(RoleScope.APPLICATION, "PRIMARY_OWNER").build());
    when(groupRepository.findById(GROUP_ID)).thenReturn(Optional.of(Mockito.mock(Group.class)));
    when(permissionService.hasPermission(RolePermission.ENVIRONMENT_GROUP, "DEFAULT", CREATE, UPDATE, DELETE)).thenReturn(true);
    when(membershipService.getRoles(any(), any(), any(), any())).thenReturn(Collections.emptySet());
    groupService.update(GROUP_ID, updatedGroupEntity);
    verify(membershipService, never()).deleteReferenceMember(any(), any(), any(), any());
    verify(membershipService, never()).addRoleToMemberOnReference(any(), any(), any());
}
Also used : RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) UpdateGroupEntity(io.gravitee.rest.api.model.UpdateGroupEntity) Test(org.junit.Test)

Aggregations

UpdateGroupEntity (io.gravitee.rest.api.model.UpdateGroupEntity)3 RoleScope (io.gravitee.rest.api.model.permissions.RoleScope)2 Test (org.junit.Test)2 Maps (io.gravitee.common.util.Maps)1 GroupRepository (io.gravitee.repository.management.api.GroupRepository)1 Group (io.gravitee.repository.management.model.Group)1 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)1 GroupEntity (io.gravitee.rest.api.model.GroupEntity)1 MembershipMemberType (io.gravitee.rest.api.model.MembershipMemberType)1 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)1 RolePermission (io.gravitee.rest.api.model.permissions.RolePermission)1 RolePermissionAction (io.gravitee.rest.api.model.permissions.RolePermissionAction)1 GroupServiceImpl (io.gravitee.rest.api.service.impl.GroupServiceImpl)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)1 InjectMocks (org.mockito.InjectMocks)1 Mock (org.mockito.Mock)1