@Override
    public Mono<AuthorizingVisitor> visitRules(Authentication authentication,
        RequestInfo requestInfo) {
        var roleNames = AuthorityUtils.authoritiesToRoles(authentication.getAuthorities());
        var record = new AttributesRecord(authentication, requestInfo);
        var visitor = new AuthorizingVisitor(record);

        // If the request is an userspace scoped request,
        // then we should check whether the user is the owner of the userspace.
        if (StringUtils.isNotBlank(requestInfo.getUserspace())) {
            if (!authentication.getName().equals(requestInfo.getUserspace())) {
                return Mono.fromSupplier(() -> {
                    visitor.visit(null, null, null);
                    return visitor;
                });
            }
        }

        var stopVisiting = new AtomicBoolean(false);
        return roleService.listDependenciesFlux(roleNames)
            .filter(role -> !CollectionUtils.isEmpty(role.getRules()))
            .doOnNext(role -> {
                if (stopVisiting.get()) {
                    return;
                }
                String roleName = role.getMetadata().getName();
                var rules = role.getRules();
                var source = roleBindingDescriber(roleName, authentication.getName());
                for (var rule : rules) {
                    if (!visitor.visit(source, rule, null)) {
                        stopVisiting.set(true);
                        return;
                    }
                }
            })
            .takeUntil(item -> stopVisiting.get())
            .onErrorResume(t -> visitor.visit(null, null, t), t -> {
                log.error("Error occurred when visiting rules", t);
                //Do nothing here
                return Mono.empty();
            })
            .then(Mono.just(visitor));
    }
@Test
    void visitRules() {
        when(roleService.listDependenciesFlux(Set.of("ruleReadPost")))
            .thenReturn(Flux.just(mockRole()));
        var fakeUser = new User("admin", "123456", createAuthorityList("ruleReadPost"));
        var authentication = authenticated(fakeUser, fakeUser.getPassword(),
            fakeUser.getAuthorities());

        var cases = getRequestResolveCases();
        cases.forEach(requestResolveCase -> {
            var httpMethod = HttpMethod.valueOf(requestResolveCase.method);
            var request = method(httpMethod, requestResolveCase.url).build();
            var requestInfo = RequestInfoFactory.INSTANCE.newRequestInfo(request);
            StepVerifier.create(ruleResolver.visitRules(authentication, requestInfo))
                .assertNext(
                    visitor -> assertEquals(requestResolveCase.expected, visitor.isAllowed()))
                .verifyComplete();
        });

        verify(roleService, times(cases.size())).listDependenciesFlux(Set.of("ruleReadPost"));
    }