use of io.gravitee.am.service.exception.RoleNotFoundException in project gravitee-access-management by gravitee-io.
the class OrganizationUserServiceImpl method setRoles.
public Completable setRoles(io.gravitee.am.identityprovider.api.User principal, io.gravitee.am.model.User user) {
final Maybe<Role> defaultRoleObs = roleService.findDefaultRole(user.getReferenceId(), DefaultRole.ORGANIZATION_USER, ReferenceType.ORGANIZATION);
Maybe<Role> roleObs = defaultRoleObs;
if (principal != null && principal.getRoles() != null && !principal.getRoles().isEmpty()) {
// We allow only one role in AM portal. Get the first (should not append).
String roleId = principal.getRoles().get(0);
roleObs = roleService.findById(user.getReferenceType(), user.getReferenceId(), roleId).toMaybe().onErrorResumeNext(throwable -> {
if (throwable instanceof RoleNotFoundException) {
return roleService.findById(ReferenceType.PLATFORM, Platform.DEFAULT, roleId).toMaybe().switchIfEmpty(defaultRoleObs).onErrorResumeNext(defaultRoleObs);
} else {
return defaultRoleObs;
}
});
}
Membership membership = new Membership();
membership.setMemberType(MemberType.USER);
membership.setMemberId(user.getId());
membership.setReferenceType(user.getReferenceType());
membership.setReferenceId(user.getReferenceId());
return roleObs.switchIfEmpty(Maybe.error(new TechnicalManagementException(String.format("Cannot add user membership to organization %s. Unable to find ORGANIZATION_USER role", user.getReferenceId())))).flatMapCompletable(role -> {
membership.setRoleId(role.getId());
return membershipService.addOrUpdate(user.getReferenceId(), membership).ignoreElement();
});
}
use of io.gravitee.am.service.exception.RoleNotFoundException in project gravitee-access-management by gravitee-io.
the class SystemRoleResourceTest method shouldGetRole_notFound.
@Test
public void shouldGetRole_notFound() {
final String roleId = "role-id";
doReturn(Single.error(new RoleNotFoundException(roleId))).when(roleService).findById(ReferenceType.PLATFORM, Platform.DEFAULT, roleId);
final Response response = target("platform").path("roles").path(roleId).request().get();
assertEquals(HttpStatusCode.NOT_FOUND_404, response.getStatus());
}
Aggregations