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);
}
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);
}
}
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);
}
}
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);
}
}
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());
}
Aggregations