Skip to content

Commit

Permalink
✨ Added webservice for checking token mismatch and vauthorized user
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydeeptrivedi13 committed Jan 6, 2025
1 parent d560628 commit 2ac0886
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
40 changes: 38 additions & 2 deletions edwiser-bridge/admin/assets/js/eb-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,14 @@
response = JSON.parse(response);
}
if ( response.data.correct ) {
if ( 'server_blocking_check' == check ) {
if (response.data.validate_access.token_mismatch) {
resolve(false);
}
if ( ! response.data.validate_access.is_authorized) {
resolve(false);
}
}
resolve(true);
} else {
resolve(false);
Expand Down Expand Up @@ -914,8 +922,36 @@
});

$(document).on('click', '.auto_fix_issue.eb_server_blocking_check_fix', function(){
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.contact_hosting);
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
var url = $('#eb_url').val();
var token = $('#eb_access_token').val();
$.ajax({
method: "post",
url: eb_admin_js_object.ajaxurl,
data: {
'action': 'eb_server_blocking_check',
'url': url.trim(),
'token': token,
'_wpnonce_field': eb_admin_js_object.nonce,
},
success: function (response) {
if ( ! response.data.correct ) {
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.contact_hosting);
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
}
if ( response.data.validate_access.token_mismatch ) {
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.token_mismatch);
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
}
if ( ! response.data.validate_access.is_authorized ) {
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').text(eb_admin_js_object.not_authorized);
jQuery('.eb_server_blocking_check_fix + .autofix_custom_message').slideDown();
}
return;
},
error: function(jqXHR, textStatus, errorThrown) {
}
});

return;
});

Expand Down
4 changes: 3 additions & 1 deletion edwiser-bridge/admin/class-eb-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ public function admin_enqueue_scripts() {
'server_blocking_check' => esc_html__( 'Is the moodle site webservice accessible?', 'edwiser-bridge' ),
'contact_support' => esc_html__( 'Invalid response from server. Please contact plugin support', 'edwiser-bridge' ),
'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' ),
'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' ),
'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' ),
'token_mismatch' => esc_html__( 'Token added does not match the token configured on the moodle site.', 'edwiser-bridge' ),
'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' ),
'please_refresh' => esc_html__( 'Please refresh the page and check again. If the issue is still not resolved please contact support.', 'edwiser-bridge' ),
'wp_version_issue' => esc_html__( 'Your WordPress version is not supported. Please upgrade to the latest version.', 'edwiser-bridge' ),
'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' ),
Expand Down
16 changes: 11 additions & 5 deletions edwiser-bridge/admin/class-eb-settings-ajax-initiater.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,23 @@ public function check_moodle_webservice_accessible() {

$connection_helper = new Eb_Connection_Helper( $this->plugin_name, $this->version );
$response = $connection_helper->connection_test_status( $url, $token );

echo wp_send_json_success( array( 'correct' => $response ) );
$validate_access = $connection_helper->connectMoodleWithArgsHelper( 'eb_validate_token', array( 'wp_url' => $url, 'wp_token' => $token ) );
echo wp_send_json_success( array( 'correct' => $response, 'validate_access' => $validate_access['response_data'] ) );
die();
}
public function check_valid_json_response() {
// verifying generated nonce we created earlier.
if ( ! isset( $_POST['_wpnonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce_field'] ) ), 'check_sync_action' ) ) {
wp_send_json_error();
}

return wp_send_json_success( array( 'data' => array( 'x','y','z' ) ) );
// start working on request.
$url = isset( $_POST['url'] ) ? sanitize_text_field( wp_unslash( $_POST['url'] ) ) : '';
$token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';

$connection_helper = new Eb_Connection_Helper( $this->plugin_name, $this->version );
$response = $connection_helper->connection_test_status( $url, $token );

return wp_send_json_success( array( 'data' => $response ) );
}
public function fix_valid_json_response() {
error_reporting(0);
Expand Down Expand Up @@ -197,7 +203,7 @@ public function check_permalink_setting_valid() {
if ( ! isset( $_POST['_wpnonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce_field'] ) ), 'check_sync_action' ) ) {
die( 'Busted!' );
}

if (function_exists('rest_url')) {
$response = wp_safe_remote_get(rest_url());
if (is_wp_error($response)) {
Expand Down

0 comments on commit 2ac0886

Please sign in to comment.