From d33e8f4216d9490c8f9bd796fe9828becb5abf7c Mon Sep 17 00:00:00 2001 From: Mo Farouk <122031508+fmido88@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:49:07 +0300 Subject: [PATCH] Add files via upload --- auth.php | 37 ++++++++++++++++++++++++++++------- confirm.php | 43 +++++++++++++++++++++++++++++------------ lang/en/auth_wallet.php | 10 ++++++++++ lib.php | 6 +++--- settings.php | 26 ++++++++++++++++++++++--- version.php | 2 +- 6 files changed, 98 insertions(+), 26 deletions(-) diff --git a/auth.php b/auth.php index a593cea..117a3f2 100644 --- a/auth.php +++ b/auth.php @@ -119,11 +119,20 @@ public function user_signup($user, $notify = true) { if (!send_confirmation_email($user, $confirmationurl)) { throw new \moodle_exception('auth_walletnoemail', 'auth_wallet'); } + + if ($notify) { + global $CFG, $PAGE, $OUTPUT; + $emailconfirm = get_string('emailconfirm'); + $PAGE->navbar->add($emailconfirm); + $PAGE->set_title($emailconfirm); + $PAGE->set_heading($PAGE->course->fullname); + echo $OUTPUT->header(); + notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php"); + } else { + return true; + } + } else { // Redirect to confirm. - $params = [ - 's' => $user->username, - ]; - $confirmationurl = new \moodle_url('/auth/wallet/confirm.php', $params); redirect($confirmationurl); } @@ -152,7 +161,7 @@ public function user_confirm($username, $confirmsecret) { $user = get_complete_user_data('username', $username); if (!empty($user)) { - $paycofirm = get_user_preferences('auth_wallet_balanceconfirm'); + $paycofirm = get_user_preferences('auth_wallet_balanceconfirm', false, $user); if ($user->auth != $this->authtype) { return AUTH_CONFIRM_ERROR; @@ -164,14 +173,28 @@ public function user_confirm($username, $confirmsecret) { $DB->set_field("user", "confirmed", 1, array("id" => $user->id)); - // Check if the user balance is sufficient. $required = $this->config->required_balance; $balance = transactions::get_user_balance($user->id); - if ($balance < $required) { + $method = $this->config->criteria; + $fee = $this->config->required_fee; + + // Check if the user balance is sufficient. + if ($method == 'balance' && $balance < $required) { set_user_preference('auth_wallet_balanceconfirm', false, $user); return AUTH_CONFIRM_FAIL; } + // Check if the method depend on paying a confirm fee and not confirmed yet. + if ($method == 'fee' && empty($paycofirm)) { + // Check if there already enough balance for paying the fee. + if ($method == 'fee' && $balance >= $fee) { + transactions::debit($user->id, $fee); + } else { + set_user_preference('auth_wallet_balanceconfirm', false, $user); + return AUTH_CONFIRM_FAIL; + } + } + set_user_preference('auth_wallet_balanceconfirm', true, $user); if ($wantsurl = get_user_preferences('auth_wallet_wantsurl', false, $user)) { diff --git a/confirm.php b/confirm.php index 889f6c3..0864bd3 100644 --- a/confirm.php +++ b/confirm.php @@ -36,6 +36,8 @@ redirect($CFG->wwwroot.'/'); exit; } +$emailconfirm = get_config('auth_wallet', 'emailconfirm'); + $PAGE->set_url('/auth/wallet/confirm.php'); $PAGE->set_context(context_system::instance()); $PAGE->set_pagelayout('login'); @@ -67,7 +69,6 @@ } else if ($confirmed == AUTH_CONFIRM_OK) { // The user has confirmed successfully, let's log them in. - if (!$user = get_complete_user_data('username', $username)) { throw new \moodle_exception('cannotfinduser', '', '', s($username)); } @@ -109,28 +110,46 @@ $user = $USER; } +// Reaching this part of the code means either the user confirmed by email already and wait payment confirmation, +// or confirmation by email is disabled. if (!empty($user) && is_object($user)) { - $s = (empty($s)) ? $user->username : $s; - if (empty($user->suspended)) { + if (empty($user->suspended) && (!empty($user->confirmed) || empty($emailconfirm))) { + + // Prepare redirection url. + $params = [ + 's' => (empty($s)) ? $user->username : $s, + 'p' => $user->secret, + ]; + $url = new \moodle_url('/auth/wallet/confirm.php', $params); + // Login the user to enable payment. if (!isloggedin() || empty($user->id)) { complete_user_login($user); + if (empty($user->id)) { + global $USER; + $user->id = $USER->id; + } \core\session\manager::apply_concurrent_login_limit($user->id, session_id()); } + require_login(); + $transactions = new enrol_wallet\transactions; - $balance = $transactions->get_user_balance($user->id); - $required = get_config('auth_wallet', 'required_balance'); - if ($balance >= $required) { + + $balance = $transactions->get_user_balance($user->id); + $confirmmethod = get_config('auth_wallet', 'criteria'); + $required = get_config('auth_wallet', 'required_balance'); + $fee = get_config('auth_wallet', 'required_fee'); + + if ($confirmmethod === 'balance' && $balance >= $required) { + set_user_preference('auth_wallet_balanceconfirm', true, $user); + redirect($url); + } else if ($confirmmethod === 'fee' && $balance >= $fee) { + $transactions->debit($user->id, $fee, 'New user fee'); set_user_preference('auth_wallet_balanceconfirm', true, $user); - $params = [ - 's' => $s, - 'p' => $user->secret, - ]; - $url = new \moodle_url('/auth/wallet/confirm.php', $params); redirect($url); } else { - $PAGE->set_title(''); + $PAGE->set_title($COURSE->fullname); $PAGE->set_heading($COURSE->fullname); echo $OUTPUT->header(); echo $OUTPUT->box_start('generalbox centerpara boxwidthnormal boxaligncenter'); diff --git a/lang/en/auth_wallet.php b/lang/en/auth_wallet.php index 29d941a..2891d6f 100644 --- a/lang/en/auth_wallet.php +++ b/lang/en/auth_wallet.php @@ -30,13 +30,23 @@ $string['auth_walletrecaptcha'] = 'Adds a visual/audio confirmation form element to the sign-up page for email self-registering users. This protects your site against spammers and contributes to a worthwhile cause. See https://www.google.com/recaptcha for more details.'; $string['auth_walletrecaptcha_key'] = 'Enable reCAPTCHA element'; $string['auth_walletsettings'] = 'Settings'; +$string['balance_required'] = 'Balance Required'; +$string['confirmcriteria'] = 'Confirmation Requirement'; +$string['confirmcriteria_desc'] = 'Choose if the confirmation required a payment fee or some amount in the wallet.'; $string['emailconfirm'] = 'Send confirmation email'; $string['emailconfirm_desc'] = 'Sending a confirmation email first then redirect the user to the topping up page to charge the wallet with the minimum required amount.'; +$string['feerequired'] = 'Fee Required'; $string['payment_required'] = '
In order to complete your signup you must have a balance of {$a->required} {$a->currency} in your wallet.
Your current balance is {$a->balance} {$a->currency}
You need to recharge you wallet by {$a->rest} {$a->currency}
'; +$string['fee_required'] = 'In order to complete your signup you must pay confirmation fee {$a->required} {$a->currency} by your wallet.
+Your current balance is {$a->balance} {$a->currency}
+You need to recharge you wallet by {$a->rest} {$a->currency} to complete payment
'; $string['pluginname'] = 'Signup with Wallet Balance Confirm'; $string['privacy:metadata'] = 'The Signup with Wallet Balance Confirm authentication plugin does not store any personal data.'; $string['required_balance'] = 'Min required balance.'; $string['required_balance_desc'] = 'The minimum required balance the user need to charge wallet to confirm registration'; +$string['required_fee'] = 'Required fee'; +$string['required_fee_desc'] = 'Minimum required payment fee to confirm the account.'; diff --git a/lib.php b/lib.php index 73c9c0c..e3c3ac2 100644 --- a/lib.php +++ b/lib.php @@ -34,12 +34,12 @@ function auth_wallet_after_require_login() { global $USER, $CFG; require_once($CFG->dirroot . '/enrol/wallet/locallib.php'); // Disable redirection in case of another auth plugin. - if ($USER->auth !== 'wallet' || !empty($wallet)) { + if ($USER->auth !== 'wallet') { return; } // Check if first required payment already done. - $payconfirm = get_user_preferences('auth_wallet_balanceconfirm'); + $payconfirm = get_user_preferences('auth_wallet_balanceconfirm', false, $USER); if (!empty($payconfirm) && !empty($USER->confirmed)) { return; } @@ -52,7 +52,7 @@ function auth_wallet_after_require_login() { $s = optional_param('s', '', PARAM_TEXT); $l = optional_param('logout', '', PARAM_TEXT); - // Disable redirection in case of payment process. + // Disable redirection in case of payment process or confirm page. if (!empty($itemid) || !empty($paymentarea) || !empty($component) diff --git a/settings.php b/settings.php index 5782ec7..f8daa65 100644 --- a/settings.php +++ b/settings.php @@ -33,17 +33,37 @@ get_string('emailconfirm', 'auth_wallet'), get_string('emailconfirm_desc', 'auth_wallet'), 0)); - $settings->add(new admin_setting_configtext('auth_wallet/required_balance', + $options = [ + 'balance' => get_string('balance_required', 'auth_wallet'), + 'fee' => get_string('feerequired', 'auth_wallet') + ]; + $settings->add(new admin_setting_configselect('auth_wallet/criteria', + get_string('confirmcriteria', 'auth_wallet'), + get_string('confirmcriteria', 'auth_wallet'), 1, $options)); + + $requiredbalance = new admin_setting_configtext('auth_wallet/required_balance', get_string('required_balance', 'auth_wallet'), get_string('required_balance_desc', 'auth_wallet'), 0, PARAM_FLOAT, - null)); + null); + $settings->add($requiredbalance); + + $requiredfee = new admin_setting_configtext('auth_wallet/required_fee', + get_string('required_fee', 'auth_wallet'), + get_string('required_fee_desc', 'auth_wallet'), + 0, + PARAM_FLOAT, + null); + $settings->add($requiredfee); + + $settings->hide_if('auth_wallet/required_fee', 'auth_wallet/criteria', 'eq', 'balance'); + $settings->hide_if('auth_wallet/required_balance', 'auth_wallet/criteria', 'eq', 'fee'); + $options = [ 0 => get_string('no'), 1 => get_string('yes'), ]; - $settings->add(new admin_setting_configselect('auth_wallet/recaptcha', get_string('auth_walletrecaptcha_key', 'auth_wallet'), get_string('auth_walletrecaptcha', 'auth_wallet'), 0, $options)); diff --git a/version.php b/version.php index 818c9ea..4fea5f9 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $plugin->component = 'auth_wallet'; $plugin->release = '1.0.0'; -$plugin->version = 2023070800; +$plugin->version = 2023071114; $plugin->requires = 2022112800; $plugin->maturity = MATURITY_BETA; $plugin->dependencies = [