This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch CAUSEWAY-3676
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/CAUSEWAY-3676 by this push:
     new 41f3bf6f05 CAUSEWAY-3676: factors out UserMementoProvider
41f3bf6f05 is described below

commit 41f3bf6f05585e256cde951f15257fce6fae567d
Author: danhaywood <[email protected]>
AuthorDate: Fri Jan 26 18:02:24 2024 +0000

    CAUSEWAY-3676: factors out UserMementoProvider
    
    as a way to grab the current user/session
---
 .../viewer/graphql/model/types/TypeMapper.java     | 35 ++++++--------
 .../model/types/TypeMapperConfiguration.java       | 17 -------
 .../src/test/resources/application-test.properties |  1 +
 .../graphql/viewer/auth/UserMementoProvider.java   | 42 +++++++++++++++++
 .../viewer/auth/UserMementoProviderDefault.java    | 54 ++++++++++++++++++++++
 ...xecutionStrategyResolvingWithinInteraction.java | 28 +++--------
 6 files changed, 118 insertions(+), 59 deletions(-)

diff --git 
a/incubator/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/types/TypeMapper.java
 
b/incubator/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/types/TypeMapper.java
index 5f40e8a010..cd702bfc46 100644
--- 
a/incubator/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/types/TypeMapper.java
+++ 
b/incubator/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/types/TypeMapper.java
@@ -18,37 +18,31 @@
  */
 package org.apache.causeway.viewer.graphql.model.types;
 
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.time.LocalDate;
-import java.time.format.DateTimeFormatter;
-import java.util.Map;
-
-import graphql.Scalars;
 import graphql.schema.*;
 
-import lombok.experimental.UtilityClass;
-import lombok.val;
-
-import javax.annotation.Priority;
-import javax.ws.rs.NotSupportedException;
-
-import org.apache.causeway.applib.annotation.PriorityPrecedence;
-import org.apache.causeway.commons.internal.collections._Maps;
+import org.apache.causeway.core.config.CausewayConfiguration;
 import org.apache.causeway.core.metamodel.spec.ObjectSpecification;
 import 
org.apache.causeway.core.metamodel.spec.feature.OneToManyActionParameter;
 import org.apache.causeway.core.metamodel.spec.feature.OneToManyAssociation;
 import org.apache.causeway.core.metamodel.spec.feature.OneToOneFeature;
-import org.apache.causeway.viewer.graphql.model.domain.TypeNames;
 
+import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
 import org.springframework.lang.Nullable;
-import org.springframework.stereotype.Component;
-
-import static graphql.schema.GraphQLNonNull.nonNull;
-import static graphql.schema.GraphQLTypeReference.typeRef;
 
 public interface TypeMapper {
 
+    @Configuration
+    class TypeMapperConfiguration {
+
+        @Bean
+        @ConditionalOnMissingBean(TypeMapper.class)
+        public TypeMapper defaultTypeMapper(final CausewayConfiguration 
causewayConfiguration) {
+            return new TypeMapperDefault(causewayConfiguration);
+        }
+    }
+
     GraphQLScalarType scalarTypeFor(final Class<?> c);
 
     GraphQLOutputType outputTypeFor(final OneToOneFeature oneToOneFeature);
@@ -86,4 +80,5 @@ public interface TypeMapper {
             return !(this == INVOKE || this == SET);
         }
     }
+
 }
diff --git 
a/incubator/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/types/TypeMapperConfiguration.java
 
b/incubator/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/types/TypeMapperConfiguration.java
deleted file mode 100644
index 5eb9ea11d3..0000000000
--- 
a/incubator/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/types/TypeMapperConfiguration.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package org.apache.causeway.viewer.graphql.model.types;
-
-import org.apache.causeway.core.config.CausewayConfiguration;
-
-import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-public class TypeMapperConfiguration {
-
-    @Bean
-    @ConditionalOnMissingBean(TypeMapper.class)
-    public TypeMapper defaultTypeMapper(final CausewayConfiguration 
causewayConfiguration) {
-        return new TypeMapperDefault(causewayConfiguration);
-    }
-}
diff --git 
a/incubator/viewers/graphql/test/src/test/resources/application-test.properties 
b/incubator/viewers/graphql/test/src/test/resources/application-test.properties
index 54fc4d657b..e78ec972d3 100644
--- 
a/incubator/viewers/graphql/test/src/test/resources/application-test.properties
+++ 
b/incubator/viewers/graphql/test/src/test/resources/application-test.properties
@@ -1,3 +1,4 @@
 #logging.level.org.springframework=DEBUG
 #logging.level.graphql=DEBUG
 
+
diff --git 
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/auth/UserMementoProvider.java
 
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/auth/UserMementoProvider.java
new file mode 100644
index 0000000000..e54ff131f0
--- /dev/null
+++ 
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/auth/UserMementoProvider.java
@@ -0,0 +1,42 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.causeway.viewer.graphql.viewer.auth;
+
+import org.apache.causeway.applib.services.user.UserMemento;
+import org.apache.causeway.core.config.CausewayConfiguration;
+
+import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+public interface UserMementoProvider {
+
+    UserMemento userMemento();
+
+    @Configuration
+    class AutoConfiguration {
+
+        @Bean
+        @ConditionalOnMissingBean(UserMementoProvider.class)
+        public UserMementoProvider defaultIdentityProvider(final 
CausewayConfiguration causewayConfiguration) {
+            return new UserMementoProviderDefault(causewayConfiguration);
+        }
+    }
+
+}
diff --git 
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/auth/UserMementoProviderDefault.java
 
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/auth/UserMementoProviderDefault.java
new file mode 100644
index 0000000000..fb1000a94b
--- /dev/null
+++ 
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/auth/UserMementoProviderDefault.java
@@ -0,0 +1,54 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.causeway.viewer.graphql.viewer.auth;
+
+import javax.inject.Inject;
+
+import org.apache.causeway.applib.services.user.RoleMemento;
+import org.apache.causeway.applib.services.user.UserMemento;
+import org.apache.causeway.commons.collections.Can;
+import org.apache.causeway.core.config.CausewayConfiguration;
+
+import lombok.RequiredArgsConstructor;
+import lombok.val;
+
+@RequiredArgsConstructor(onConstructor_ = {@Inject})
+public class UserMementoProviderDefault implements UserMementoProvider {
+
+    @SuppressWarnings("CdiInjectInspection")
+    @Inject private final CausewayConfiguration causewayConfiguration;
+
+
+    @Override
+    public UserMemento userMemento() {
+
+        val fallbackUsername = 
causewayConfiguration.getViewer().getGqlv().getAuthentication().getFallback().getUsername();
+        if (fallbackUsername == null) {
+            return null;
+        }
+
+        val fallbackRoles = 
causewayConfiguration.getViewer().getGqlv().getAuthentication().getFallback().getRoles();
+        val roles = Can.ofStream(fallbackRoles.stream().map(roleName -> 
RoleMemento.builder().name(roleName).build()));
+        return UserMemento.builder()
+                .name(fallbackUsername)
+                .roles(roles)
+                .build();
+
+    }
+}
diff --git 
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/AsyncExecutionStrategyResolvingWithinInteraction.java
 
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/AsyncExecutionStrategyResolvingWithinInteraction.java
index 816086b4d6..882fd1b610 100644
--- 
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/AsyncExecutionStrategyResolvingWithinInteraction.java
+++ 
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/AsyncExecutionStrategyResolvingWithinInteraction.java
@@ -18,17 +18,16 @@
  */
 package org.apache.causeway.viewer.graphql.viewer.integration;
 
-import java.util.List;
 import java.util.concurrent.CompletableFuture;
 
-import javax.inject.Inject;
-
 import org.apache.causeway.applib.services.iactnlayer.InteractionContext;
 import org.apache.causeway.applib.services.user.RoleMemento;
 import org.apache.causeway.applib.services.user.UserMemento;
 import org.apache.causeway.commons.collections.Can;
 import org.apache.causeway.core.config.CausewayConfiguration;
 
+import org.apache.causeway.viewer.graphql.viewer.auth.UserMementoProvider;
+
 import org.springframework.stereotype.Service;
 
 import org.apache.causeway.applib.services.iactnlayer.InteractionService;
@@ -37,7 +36,8 @@ import graphql.execution.AsyncExecutionStrategy;
 import graphql.execution.ExecutionContext;
 import graphql.execution.ExecutionStrategyParameters;
 import graphql.execution.FieldValueInfo;
-import lombok.RequiredArgsConstructor;
+
+import lombok.val;
 
 @Service
 public class AsyncExecutionStrategyResolvingWithinInteraction extends 
AsyncExecutionStrategy {
@@ -48,23 +48,10 @@ public class 
AsyncExecutionStrategyResolvingWithinInteraction extends AsyncExecu
 
     public AsyncExecutionStrategyResolvingWithinInteraction(
             final InteractionService interactionService,
-            final CausewayConfiguration causewayConfiguration) {
+            final UserMementoProvider userMementoProvider) {
         this.interactionService = interactionService;
 
-        String fallbackUsername = 
causewayConfiguration.getViewer().getGqlv().getAuthentication().getFallback().getUsername();
-        List<String> fallbackRoles = 
causewayConfiguration.getViewer().getGqlv().getAuthentication().getFallback().getRoles();
-        userMemento = fallbackUsername != null
-                        ? UserMemento.builder()
-                            .name(fallbackUsername)
-                            .roles(Can.ofStream(
-                                    fallbackRoles.stream()
-                                                .map(roleName -> 
RoleMemento.builder()
-                                                                    
.name(roleName)
-                                                                    .build()
-                                                )
-                                    )
-                            ).build()
-                        : null;
+        this.userMemento = userMementoProvider.userMemento();
     }
 
 
@@ -73,9 +60,6 @@ public class AsyncExecutionStrategyResolvingWithinInteraction 
extends AsyncExecu
             final ExecutionContext executionContext,
             final ExecutionStrategyParameters parameters) {
 
-        // TODO: propagate identity from executionContext
-        // 
interactionService.openInteraction(InteractionContext.builder().user(UserMemento.builder().build()).build());
-
         if (userMemento != null) {
             return interactionService.call(
                     InteractionContext.builder().user(userMemento).build(),

Reply via email to