Search in sources :

Example 1 with RoleNotFoundException

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();
    });
}
Also used : DefaultRole(io.gravitee.am.model.permissions.DefaultRole) RoleService(io.gravitee.am.service.RoleService) Action(io.gravitee.am.common.event.Action) MemberType(io.gravitee.am.model.membership.MemberType) Date(java.util.Date) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) Autowired(org.springframework.beans.factory.annotation.Autowired) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) RoleNotFoundException(io.gravitee.am.service.exception.RoleNotFoundException) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Single(io.reactivex.Single) io.gravitee.am.model(io.gravitee.am.model) Event(io.gravitee.am.model.common.event.Event) Type(io.gravitee.am.common.event.Type) OrganizationUserRepository(io.gravitee.am.repository.management.api.OrganizationUserRepository) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Component(org.springframework.stereotype.Component) Payload(io.gravitee.am.model.common.event.Payload) OrganizationUserService(io.gravitee.am.service.OrganizationUserService) Lazy(org.springframework.context.annotation.Lazy) GroupService(io.gravitee.am.service.GroupService) MembershipService(io.gravitee.am.service.MembershipService) RoleNotFoundException(io.gravitee.am.service.exception.RoleNotFoundException) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Example 2 with RoleNotFoundException

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());
}
Also used : Response(javax.ws.rs.core.Response) RoleNotFoundException(io.gravitee.am.service.exception.RoleNotFoundException) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Aggregations

RoleNotFoundException (io.gravitee.am.service.exception.RoleNotFoundException)2 Action (io.gravitee.am.common.event.Action)1 Type (io.gravitee.am.common.event.Type)1 JerseySpringTest (io.gravitee.am.management.handlers.management.api.JerseySpringTest)1 io.gravitee.am.model (io.gravitee.am.model)1 Event (io.gravitee.am.model.common.event.Event)1 Payload (io.gravitee.am.model.common.event.Payload)1 MemberType (io.gravitee.am.model.membership.MemberType)1 DefaultRole (io.gravitee.am.model.permissions.DefaultRole)1 OrganizationUserRepository (io.gravitee.am.repository.management.api.OrganizationUserRepository)1 GroupService (io.gravitee.am.service.GroupService)1 MembershipService (io.gravitee.am.service.MembershipService)1 OrganizationUserService (io.gravitee.am.service.OrganizationUserService)1 RoleService (io.gravitee.am.service.RoleService)1 AbstractManagementException (io.gravitee.am.service.exception.AbstractManagementException)1 TechnicalManagementException (io.gravitee.am.service.exception.TechnicalManagementException)1 Completable (io.reactivex.Completable)1 Maybe (io.reactivex.Maybe)1 Single (io.reactivex.Single)1 Date (java.util.Date)1