From 9bf843e4fa38f66f4a8e60ce644088551e4fd9df Mon Sep 17 00:00:00 2001 From: anhefti Date: Tue, 11 Jun 2024 16:13:21 +0200 Subject: [PATCH] SEBSERV-417 fixed SEB Restriction --- .../impl/moodle/MoodleAPIRestTemplate.java | 8 ++++++ .../moodle/MoodleRestTemplateFactoryImpl.java | 28 +++++++++++++++++++ .../plugin/MoodlePluginCourseRestriction.java | 11 +++++--- .../plugin/MoodlePluginFullIntegration.java | 2 +- .../MoodleMockupRestTemplateFactory.java | 5 ++++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java index 96329d68..e37fb957 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleAPIRestTemplate.java @@ -18,6 +18,12 @@ import com.fasterxml.jackson.annotation.JsonProperty; public interface MoodleAPIRestTemplate { + public enum MoodlePluginVersion { + NONE, + V1_0, + V2_0, + } + String URI_VAR_USER_NAME = "username"; String URI_VAR_PASSWORD = "pwd"; String URI_VAR_SERVICE = "service"; @@ -33,6 +39,8 @@ public interface MoodleAPIRestTemplate { String getService(); + MoodlePluginVersion getMoodlePluginVersion(); + CharSequence getAccessToken(); void testAPIConnection(String... functions); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java index 8064fc08..98ad514a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactoryImpl.java @@ -24,6 +24,8 @@ import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.MoodlePluginCourseRestriction; +import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.MoodlePluginFullIntegration; import org.apache.commons.lang3.StringUtils; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; @@ -237,6 +239,8 @@ public class MoodleRestTemplateFactoryImpl implements MoodleRestTemplateFactory private final Map tokenReqURIVars; private final HttpEntity tokenReqEntity = new HttpEntity<>(new LinkedMultiValueMap<>()); + private MoodlePluginVersion moodlePluginVersion = null; + protected MoodleAPIRestTemplateImpl( final JSONMapper jsonMapper, final APITemplateDataSupplier apiTemplateDataSupplier, @@ -266,6 +270,30 @@ public class MoodleRestTemplateFactoryImpl implements MoodleRestTemplateFactory return this.tokenReqURIVars.get(URI_VAR_SERVICE); } + @Override + public MoodlePluginVersion getMoodlePluginVersion() { + if (moodlePluginVersion == null) { + try { + final String apiInfo = this.callMoodleAPIFunction(REST_API_TEST_FUNCTION); + final WebserviceInfo webserviceInfo = this.jsonMapper.readValue( + apiInfo, + WebserviceInfo.class); + if (webserviceInfo.functions.containsKey(MoodlePluginCourseRestriction.RESTRICTION_SET_FUNCTION_NAME)) { + if (webserviceInfo.functions.containsKey(MoodlePluginFullIntegration.FUNCTION_NAME_SEBSERVER_CONNECTION)) { + this.moodlePluginVersion = MoodlePluginVersion.V2_0; + } else { + this.moodlePluginVersion = MoodlePluginVersion.V1_0; + } + } else { + this.moodlePluginVersion = MoodlePluginVersion.NONE; + } + } catch (final Exception e) { + log.warn("Failed to verify MoodlePluginVersion. Error: {}", e.getMessage()); + } + } + return moodlePluginVersion; + } + @Override public CharSequence getAccessToken() { if (this.accessToken == null) { diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginCourseRestriction.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginCourseRestriction.java index 5448e4bf..4f71f7f8 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginCourseRestriction.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginCourseRestriction.java @@ -132,8 +132,6 @@ public class MoodlePluginCourseRestriction implements SEBRestrictionAPI { final ArrayList beks = new ArrayList<>(sebRestrictionData.browserExamKeys); final ArrayList configKeys = new ArrayList<>(sebRestrictionData.configKeys); - final String quitLink = this.examConfigurationValueService.getQuitLink(exam.id); - final String quitSecret = this.examConfigurationValueService.getQuitPassword(exam.id); String additionalBEK = sebRestrictionData.additionalProperties.get(SEBRestrictionService.ADDITIONAL_ATTR_ALTERNATIVE_SEB_BEK); if (additionalBEK == null) { additionalBEK = exam.getAdditionalAttribute( @@ -147,8 +145,13 @@ public class MoodlePluginCourseRestriction implements SEBRestrictionAPI { final LinkedMultiValueMap queryAttributes = new LinkedMultiValueMap<>(); queryAttributes.put(ATTRIBUTE_CONFIG_KEYS, configKeys); queryAttributes.put(ATTRIBUTE_BROWSER_EXAM_KEYS, beks); - queryAttributes.add(ATTRIBUTE_QUIT_URL, quitLink); - queryAttributes.add(ATTRIBUTE_QUIT_SECRET, quitSecret); + + if (restTemplate.getMoodlePluginVersion() == MoodleAPIRestTemplate.MoodlePluginVersion.V1_0) { + final String quitLink = this.examConfigurationValueService.getQuitLink(exam.id); + final String quitSecret = this.examConfigurationValueService.getQuitPassword(exam.id); + queryAttributes.add(ATTRIBUTE_QUIT_URL, quitLink); + queryAttributes.add(ATTRIBUTE_QUIT_SECRET, quitSecret); + } final String srJSON = restTemplate.callMoodleAPIFunction( RESTRICTION_SET_FUNCTION_NAME, diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java index 1b3f47e1..842a057b 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/plugin/MoodlePluginFullIntegration.java @@ -38,7 +38,7 @@ public class MoodlePluginFullIntegration implements FullLmsIntegrationAPI { private static final Logger log = LoggerFactory.getLogger(MoodlePluginFullIntegration.class); - private static final String FUNCTION_NAME_SEBSERVER_CONNECTION = "quizaccess_sebserver_connection"; + public static final String FUNCTION_NAME_SEBSERVER_CONNECTION = "quizaccess_sebserver_connection"; private static final String FUNCTION_NAME_SEBSERVER_CONNECTION_DELETE = "quizaccess_sebserver_connection_delete"; private static final String FUNCTION_NAME_SET_EXAM_DATA = "quizaccess_sebserver_set_exam_data"; private static final String ATTRIBUTE_CONNECTION = "connection"; diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleMockupRestTemplateFactory.java b/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleMockupRestTemplateFactory.java index 6ab50e9b..895f3b59 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleMockupRestTemplateFactory.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleMockupRestTemplateFactory.java @@ -114,6 +114,11 @@ public class MoodleMockupRestTemplateFactory implements MoodleRestTemplateFactor return "mockup-service"; } + @Override + public MoodlePluginVersion getMoodlePluginVersion() { + return MoodlePluginVersion.V1_0; + } + @Override public CharSequence getAccessToken() { return this.accessToken;