Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 authored Jul 11, 2023
1 parent f4d9baa commit d33e8f4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 26 deletions.
37 changes: 30 additions & 7 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down
43 changes: 31 additions & 12 deletions confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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');
Expand Down
10 changes: 10 additions & 0 deletions lang/en/auth_wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '<h6>Welcome {$a->name}</h5>
<p>In order to complete your signup you must have a balance of {$a->required} {$a->currency} in your wallet.</p>
<p><strong>Your current balance is {$a->balance} {$a->currency}</strong></p>
<p>You need to recharge you wallet by {$a->rest} {$a->currency}</p>';
$string['fee_required'] = '<h6>Welcome {$a->name}</h5>
<p>In order to complete your signup you must pay confirmation fee {$a->required} {$a->currency} by your wallet.</p>
<p><strong>Your current balance is {$a->balance} {$a->currency}</strong></p>
<p>You need to recharge you wallet by {$a->rest} {$a->currency} to complete payment</p>';
$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.';
6 changes: 3 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down
26 changes: 23 additions & 3 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit d33e8f4

Please sign in to comment.