Search in sources :

Example 6 with Page

use of io.gravitee.am.model.common.Page in project gravitee-access-management by gravitee-io.

the class JdbcRoleRepository method search.

@Override
public Single<Page<Role>> search(ReferenceType referenceType, String referenceId, String query, int page, int size) {
    LOGGER.debug("search({}, {}, {}, {}, {})", referenceType, referenceId, query, page, size);
    boolean wildcardSearch = query.contains("*");
    String wildcardValue = query.replaceAll("\\*+", "%");
    String search = this.databaseDialectHelper.buildSearchRoleQuery(wildcardSearch, page, size);
    String count = this.databaseDialectHelper.buildCountRoleQuery(wildcardSearch);
    return fluxToFlowable(template.getDatabaseClient().sql(search).bind("value", wildcardSearch ? wildcardValue : query).bind("refId", referenceId).bind("refType", referenceType.name()).map(row -> rowMapper.read(JdbcRole.class, row)).all()).map(this::toEntity).flatMap(role -> completeWithScopes(Maybe.just(role), role.getId()).toFlowable()).toList().flatMap(data -> monoToSingle(template.getDatabaseClient().sql(count).bind("value", wildcardSearch ? wildcardValue : query).bind("refId", referenceId).bind("refType", referenceType.name()).map(row -> row.get(0, Long.class)).first()).map(total -> new Page<Role>(data, page, total)));
}
Also used : SpringRoleRepository(io.gravitee.am.repository.jdbc.management.api.spring.role.SpringRoleRepository) Completable(io.reactivex.Completable) Role(io.gravitee.am.model.Role) Maybe(io.reactivex.Maybe) Query(org.springframework.data.relational.core.query.Query) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) Criteria.where(org.springframework.data.relational.core.query.Criteria.where) Single(io.reactivex.Single) Function(java.util.function.Function) InitializingBean(org.springframework.beans.factory.InitializingBean) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) AbstractJdbcRepository(io.gravitee.am.repository.jdbc.management.AbstractJdbcRepository) Flowable(io.reactivex.Flowable) SpringRoleOauthScopeRepository(io.gravitee.am.repository.jdbc.management.api.spring.role.SpringRoleOauthScopeRepository) ReferenceType(io.gravitee.am.model.ReferenceType) Sort(org.springframework.data.domain.Sort) Repository(org.springframework.stereotype.Repository) Page(io.gravitee.am.model.common.Page) JdbcRole(io.gravitee.am.repository.jdbc.management.api.model.JdbcRole) Publisher(org.reactivestreams.Publisher) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) PageRequest(org.springframework.data.domain.PageRequest) Mono(reactor.core.publisher.Mono) RandomString(io.gravitee.am.common.utils.RandomString) RoleRepository(io.gravitee.am.repository.management.api.RoleRepository) Flux(reactor.core.publisher.Flux) List(java.util.List) RxJava2Adapter(reactor.adapter.rxjava.RxJava2Adapter) JdbcRole(io.gravitee.am.repository.jdbc.management.api.model.JdbcRole) Page(io.gravitee.am.model.common.Page) RandomString(io.gravitee.am.common.utils.RandomString)

Example 7 with Page

use of io.gravitee.am.model.common.Page in project gravitee-access-management by gravitee-io.

the class GroupServiceTest method shouldFindByDomainPagination.

@Test
public void shouldFindByDomainPagination() {
    Page pagedGroups = new Page(Collections.singleton(new Group()), 1, 1);
    when(groupRepository.findAll(ReferenceType.DOMAIN, DOMAIN, 1, 1)).thenReturn(Single.just(pagedGroups));
    TestObserver<Page<Group>> testObserver = groupService.findByDomain(DOMAIN, 1, 1).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    testObserver.assertValue(extensionGrants -> extensionGrants.getData().size() == 1);
}
Also used : Group(io.gravitee.am.model.Group) UpdateGroup(io.gravitee.am.service.model.UpdateGroup) NewGroup(io.gravitee.am.service.model.NewGroup) Page(io.gravitee.am.model.common.Page) Test(org.junit.Test)

Example 8 with Page

use of io.gravitee.am.model.common.Page in project gravitee-access-management by gravitee-io.

the class DomainServiceTest method shouldDeleteWithoutRelatedData.

@Test
public void shouldDeleteWithoutRelatedData() {
    when(domainRepository.findById(DOMAIN_ID)).thenReturn(Maybe.just(domain));
    when(domainRepository.delete(DOMAIN_ID)).thenReturn(Completable.complete());
    when(applicationService.findByDomain(DOMAIN_ID)).thenReturn(Single.just(Collections.emptySet()));
    when(certificateService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(identityProviderService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(extensionGrantService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(roleService.findByDomain(DOMAIN_ID)).thenReturn(Single.just(Collections.emptySet()));
    when(scopeService.findByDomain(DOMAIN_ID, 0, Integer.MAX_VALUE)).thenReturn(Single.just(new Page<>(Collections.emptySet(), 0, 1)));
    when(userService.deleteByDomain(DOMAIN_ID)).thenReturn(Completable.complete());
    when(groupService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(formService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(emailTemplateService.findAll(ReferenceType.DOMAIN, DOMAIN_ID)).thenReturn(Flowable.empty());
    when(reporterService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(flowService.findAll(ReferenceType.DOMAIN, DOMAIN_ID)).thenReturn(Flowable.empty());
    when(membershipService.findByReference(DOMAIN_ID, ReferenceType.DOMAIN)).thenReturn(Flowable.empty());
    when(factorService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(resourceService.findByDomain(DOMAIN_ID)).thenReturn(Single.just(Collections.emptySet()));
    when(alertTriggerService.findByDomainAndCriteria(DOMAIN_ID, new AlertTriggerCriteria())).thenReturn(Flowable.empty());
    when(alertNotifierService.findByDomainAndCriteria(DOMAIN_ID, new AlertNotifierCriteria())).thenReturn(Flowable.empty());
    when(authenticationDeviceNotifierService.findByDomain(DOMAIN_ID)).thenReturn(Flowable.empty());
    when(eventService.create(any())).thenReturn(Single.just(new Event()));
    TestObserver testObserver = domainService.delete(DOMAIN_ID).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(applicationService, never()).delete(anyString());
    verify(certificateService, never()).delete(anyString());
    verify(identityProviderService, never()).delete(anyString(), anyString());
    verify(extensionGrantService, never()).delete(anyString(), anyString());
    verify(roleService, never()).delete(eq(ReferenceType.DOMAIN), eq(DOMAIN_ID), anyString());
    verify(scopeService, never()).delete(anyString(), anyBoolean());
    verify(formService, never()).delete(anyString(), anyString());
    verify(emailTemplateService, never()).delete(anyString());
    verify(reporterService, never()).delete(anyString());
    verify(flowService, never()).delete(anyString());
    verify(membershipService, never()).delete(anyString());
    verify(factorService, never()).delete(anyString(), anyString());
    verify(alertTriggerService, never()).delete(any(ReferenceType.class), anyString(), anyString(), any(io.gravitee.am.identityprovider.api.User.class));
    verify(alertNotifierService, never()).delete(any(ReferenceType.class), anyString(), anyString(), any(io.gravitee.am.identityprovider.api.User.class));
    verify(eventService, times(1)).create(any());
}
Also used : AlertTriggerCriteria(io.gravitee.am.repository.management.api.search.AlertTriggerCriteria) AlertNotifierCriteria(io.gravitee.am.repository.management.api.search.AlertNotifierCriteria) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Event(io.gravitee.am.model.common.event.Event) Page(io.gravitee.am.model.common.Page) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 9 with Page

use of io.gravitee.am.model.common.Page in project gravitee-access-management by gravitee-io.

the class ApplicationsResource method list.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List registered applications for a security domain", notes = "User must have the APPLICATION[LIST] permission on the specified domain, environment or organization " + "AND either APPLICATION[READ] permission on each domain's application " + "or APPLICATION[READ] permission on the specified domain " + "or APPLICATION[READ] permission on the specified environment " + "or APPLICATION[READ] permission on the specified organization. " + "Each returned application is filtered and contains only basic information such as id, name, description and isEnabled.")
@ApiResponses({ @ApiResponse(code = 200, message = "List registered applications for a security domain", response = Application.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
public void list(@PathParam("organizationId") String organizationId, @PathParam("environmentId") String environmentId, @PathParam("domain") String domain, @QueryParam("page") @DefaultValue("0") int page, @QueryParam("size") @DefaultValue(MAX_APPLICATIONS_SIZE_PER_PAGE_STRING) int size, @QueryParam("q") String query, @Suspended final AsyncResponse response) {
    final User authenticatedUser = getAuthenticatedUser();
    checkAnyPermission(organizationId, environmentId, domain, Permission.APPLICATION, Acl.LIST).andThen(domainService.findById(domain).switchIfEmpty(Maybe.error(new DomainNotFoundException(domain))).flatMapSingle(__ -> {
        if (query != null) {
            return applicationService.search(domain, query, 0, Integer.MAX_VALUE);
        } else {
            return applicationService.findByDomain(domain, 0, Integer.MAX_VALUE);
        }
    }).flatMap(pagedApplications -> Maybe.concat(pagedApplications.getData().stream().map(application -> hasAnyPermission(authenticatedUser, organizationId, environmentId, domain, application.getId(), Permission.APPLICATION, Acl.READ).filter(Boolean::booleanValue).map(__ -> filterApplicationInfos(application))).collect(Collectors.toList())).sorted((a1, a2) -> a2.getUpdatedAt().compareTo(a1.getUpdatedAt())).toList().map(applications -> new Page<>(applications.stream().skip(page * size).limit(size).collect(Collectors.toList()), page, applications.size())))).subscribe(response::resume, response::resume);
}
Also used : ApplicationService(io.gravitee.am.service.ApplicationService) Permission(io.gravitee.am.model.permissions.Permission) Maybe(io.reactivex.Maybe) DomainService(io.gravitee.am.service.DomainService) Autowired(org.springframework.beans.factory.annotation.Autowired) Application(io.gravitee.am.model.Application) AbstractResource(io.gravitee.am.management.handlers.management.api.resources.AbstractResource) Valid(javax.validation.Valid) Acl(io.gravitee.am.model.Acl) Flowable(io.reactivex.Flowable) User(io.gravitee.am.identityprovider.api.User) ReferenceType(io.gravitee.am.model.ReferenceType) io.swagger.annotations(io.swagger.annotations) URI(java.net.URI) Page(io.gravitee.am.model.common.Page) Context(javax.ws.rs.core.Context) Permissions.of(io.gravitee.am.management.service.permissions.Permissions.of) NewApplication(io.gravitee.am.service.model.NewApplication) AsyncResponse(javax.ws.rs.container.AsyncResponse) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) NotNull(javax.validation.constraints.NotNull) Suspended(javax.ws.rs.container.Suspended) Collectors(java.util.stream.Collectors) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ResourceContext(javax.ws.rs.container.ResourceContext) Permissions.or(io.gravitee.am.management.service.permissions.Permissions.or) User(io.gravitee.am.identityprovider.api.User) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) Page(io.gravitee.am.model.common.Page)

Example 10 with Page

use of io.gravitee.am.model.common.Page in project gravitee-access-management by gravitee-io.

the class AuditsResource method list.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List audit logs for the organization", notes = "User must have the ORGANIZATION_AUDIT[LIST] permission on the specified organization. " + "Except if user has ORGANIZATION_AUDIT[READ] permission on the organization, each returned audit is filtered and contains only basic information such as id, date, event, actor, target and status.")
@ApiResponses({ @ApiResponse(code = 200, message = "List audit logs for the platform", response = Audit.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
public void list(@PathParam("organizationId") String organizationId, @BeanParam AuditParam param, @Suspended final AsyncResponse response) {
    AuditReportableCriteria.Builder queryBuilder = new AuditReportableCriteria.Builder().from(param.getFrom()).to(param.getTo()).status(param.getStatus()).user(param.getUser());
    if (param.getType() != null) {
        queryBuilder.types(Collections.singletonList(param.getType()));
    }
    User authenticatedUser = getAuthenticatedUser();
    checkPermission(ReferenceType.ORGANIZATION, organizationId, Permission.ORGANIZATION_AUDIT, Acl.LIST).andThen(auditService.search(ReferenceType.ORGANIZATION, organizationId, queryBuilder.build(), param.getPage(), param.getSize()).flatMap(auditPage -> hasPermission(authenticatedUser, ReferenceType.ORGANIZATION, organizationId, Permission.ORGANIZATION_AUDIT, Acl.READ).map(hasPermission -> {
        if (hasPermission) {
            return auditPage;
        } else {
            return new Page<>(auditPage.getData().stream().map(FilterUtils::filterAuditInfos).collect(Collectors.toList()), auditPage.getCurrentPage(), auditPage.getTotalCount());
        }
    }))).subscribe(response::resume, response::resume);
}
Also used : Permission(io.gravitee.am.model.permissions.Permission) DomainService(io.gravitee.am.service.DomainService) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractResource(io.gravitee.am.management.handlers.management.api.resources.AbstractResource) ApiResponses(io.swagger.annotations.ApiResponses) Single(io.reactivex.Single) Acl(io.gravitee.am.model.Acl) ApiOperation(io.swagger.annotations.ApiOperation) User(io.gravitee.am.identityprovider.api.User) ReferenceType(io.gravitee.am.model.ReferenceType) Api(io.swagger.annotations.Api) AuditService(io.gravitee.am.management.service.AuditService) Page(io.gravitee.am.model.common.Page) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) EventType(io.gravitee.am.common.audit.EventType) AuditOutcome(io.gravitee.am.reporter.api.audit.model.AuditOutcome) Suspended(javax.ws.rs.container.Suspended) Collectors(java.util.stream.Collectors) AuditReportableCriteria(io.gravitee.am.reporter.api.audit.AuditReportableCriteria) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) AuditParam(io.gravitee.am.management.handlers.management.api.model.AuditParam) ApiResponse(io.swagger.annotations.ApiResponse) ResourceContext(javax.ws.rs.container.ResourceContext) FilterUtils(io.gravitee.am.management.handlers.management.api.resources.utils.FilterUtils) AuditEntity(io.gravitee.am.reporter.api.audit.model.AuditEntity) Collections(java.util.Collections) Audit(io.gravitee.am.reporter.api.audit.model.Audit) User(io.gravitee.am.identityprovider.api.User) AuditReportableCriteria(io.gravitee.am.reporter.api.audit.AuditReportableCriteria) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Page (io.gravitee.am.model.common.Page)68 Test (org.junit.Test)51 User (io.gravitee.am.model.User)29 AbstractManagementTest (io.gravitee.am.repository.management.AbstractManagementTest)26 FilterCriteria (io.gravitee.am.repository.management.api.search.FilterCriteria)16 Autowired (org.springframework.beans.factory.annotation.Autowired)14 Maybe (io.reactivex.Maybe)12 Single (io.reactivex.Single)12 ReferenceType (io.gravitee.am.model.ReferenceType)11 Flowable (io.reactivex.Flowable)11 Response (javax.ws.rs.core.Response)11 Domain (io.gravitee.am.model.Domain)10 List (java.util.List)10 Application (io.gravitee.am.model.Application)9 Scope (io.gravitee.am.model.oauth2.Scope)8 Completable (io.reactivex.Completable)8 LocalDateTime (java.time.LocalDateTime)8 InitializingBean (org.springframework.beans.factory.InitializingBean)8 Criteria.where (org.springframework.data.relational.core.query.Criteria.where)8 Query (org.springframework.data.relational.core.query.Query)8