Skip to content

Commit 2ac0886

Browse files
✨ Added webservice for checking token mismatch and vauthorized user
1 parent d560628 commit 2ac0886

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

edwiser-bridge/admin/assets/js/eb-admin.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,14 @@
837837
response = JSON.parse(response);
838838
}
839839
if ( response.data.correct ) {
840+
if ( 'server_blocking_check' == check ) {
841+
if (response.data.validate_access.token_mismatch) {
842+
resolve(false);
843+
}
844+
if ( ! response.data.validate_access.is_authorized) {
845+
resolve(false);
846+
}
847+
}
840848
resolve(true);
841849
} else {
842850
resolve(false);
@@ -914,8 +922,36 @@
914922
});
915923

916924
$(document).on('click', '.auto_fix_issue.eb_server_blocking_check_fix', function(){
917-
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.contact_hosting);
918-
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
925+
var url = $('#eb_url').val();
926+
var token = $('#eb_access_token').val();
927+
$.ajax({
928+
method: "post",
929+
url: eb_admin_js_object.ajaxurl,
930+
data: {
931+
'action': 'eb_server_blocking_check',
932+
'url': url.trim(),
933+
'token': token,
934+
'_wpnonce_field': eb_admin_js_object.nonce,
935+
},
936+
success: function (response) {
937+
if ( ! response.data.correct ) {
938+
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.contact_hosting);
939+
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
940+
}
941+
if ( response.data.validate_access.token_mismatch ) {
942+
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.token_mismatch);
943+
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
944+
}
945+
if ( ! response.data.validate_access.is_authorized ) {
946+
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.not_authorized);
947+
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
948+
}
949+
return;
950+
},
951+
error: function(jqXHR, textStatus, errorThrown) {
952+
}
953+
});
954+
919955
return;
920956
});
921957

edwiser-bridge/admin/class-eb-admin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ public function admin_enqueue_scripts() {
214214
'server_blocking_check' => esc_html__( 'Is the moodle site webservice accessible?', 'edwiser-bridge' ),
215215
'contact_support' => esc_html__( 'Invalid response from server. Please contact plugin support', 'edwiser-bridge' ),
216216
'contact_hosting' => esc_html__( 'The plugin is receiving an invalid response code from Moodle website or is unable to connect. Please contact your hosting provider.', 'edwiser-bridge' ),
217-
'turn_off_debug_log' => esc_html__( 'Please turn off debug display(WP_DEBUG & WP_DEBUG_DISPLAY) in wp-config.php to fix this issue.', 'edwiser-bridge' ),
217+
'turn_off_debug_log' => esc_html__( 'Please turn off debug display(WP_DEBUG & WP_DEBUG_DISPLAY) in wp-config.php and disable debug mode on Moodle website as well to fix this issue.', 'edwiser-bridge' ),
218+
'token_mismatch' => esc_html__( 'Token added does not match the token configured on the moodle site.', 'edwiser-bridge' ),
219+
'not_authorized' => esc_html__( 'The user used to create token on Moodle is not an site administrator or manager and therefore has limited access.', 'edwiser-bridge' ),
218220
'please_refresh' => esc_html__( 'Please refresh the page and check again. If the issue is still not resolved please contact support.', 'edwiser-bridge' ),
219221
'wp_version_issue' => esc_html__( 'Your WordPress version is not supported. Please upgrade to the latest version.', 'edwiser-bridge' ),
220222
'rest_disable_issue' => esc_html__( 'The REST API is disabled by either a Security plugin or some other plugin using hooks. It might also have been disabled in your server configuration. Please disable any security plugins and search for conflicts. If the issue doesnt get resolved contact the hosting provider to confirm that server configuration is not causing any issues.', 'edwiser-bridge' ),

edwiser-bridge/admin/class-eb-settings-ajax-initiater.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,23 @@ public function check_moodle_webservice_accessible() {
151151

152152
$connection_helper = new Eb_Connection_Helper( $this->plugin_name, $this->version );
153153
$response = $connection_helper->connection_test_status( $url, $token );
154-
155-
echo wp_send_json_success( array( 'correct' => $response ) );
154+
$validate_access = $connection_helper->connectMoodleWithArgsHelper( 'eb_validate_token', array( 'wp_url' => $url, 'wp_token' => $token ) );
155+
echo wp_send_json_success( array( 'correct' => $response, 'validate_access' => $validate_access['response_data'] ) );
156156
die();
157157
}
158158
public function check_valid_json_response() {
159159
// verifying generated nonce we created earlier.
160160
if ( ! isset( $_POST['_wpnonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce_field'] ) ), 'check_sync_action' ) ) {
161161
wp_send_json_error();
162162
}
163-
164-
return wp_send_json_success( array( 'data' => array( 'x','y','z' ) ) );
163+
// start working on request.
164+
$url = isset( $_POST['url'] ) ? sanitize_text_field( wp_unslash( $_POST['url'] ) ) : '';
165+
$token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
166+
167+
$connection_helper = new Eb_Connection_Helper( $this->plugin_name, $this->version );
168+
$response = $connection_helper->connection_test_status( $url, $token );
169+
170+
return wp_send_json_success( array( 'data' => $response ) );
165171
}
166172
public function fix_valid_json_response() {
167173
error_reporting(0);
@@ -197,7 +203,7 @@ public function check_permalink_setting_valid() {
197203
if ( ! isset( $_POST['_wpnonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce_field'] ) ), 'check_sync_action' ) ) {
198204
die( 'Busted!' );
199205
}
200-
206+
201207
if (function_exists('rest_url')) {
202208
$response = wp_safe_remote_get(rest_url());
203209
if (is_wp_error($response)) {

0 commit comments

Comments
 (0)