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 Nov 25, 2023
1 parent 2a2c4db commit d34ddf0
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 133 deletions.
55 changes: 17 additions & 38 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir.'/authlib.php');
use enrol_wallet\transactions;

// For further information about authentication plugins please read
// https://docs.moodle.org/dev/Authentication_plugins.
Expand Down Expand Up @@ -95,11 +94,6 @@ public function user_signup($user, $notify = true) {
$user->calendartype = $CFG->calendartype;
}

if (!$DB->record_exists('auth_wallet_confirm', ['userid' => $user->id])) {
$params = ['userid' => $user->id, 'confirmed' => 0, 'timecreated' => time()];
$DB->insert_record('auth_wallet_confirm', $params);
}

// Check if the user already existed.
$exist = get_complete_user_data('username', $user->username);
if (empty($exist->id)) {
Expand All @@ -110,6 +104,13 @@ public function user_signup($user, $notify = true) {

// Save any custom profile field information.
profile_save_data($user);
} else {
$user->id = $exist->id;
}

if (!$DB->record_exists('auth_wallet_confirm', ['userid' => $user->id])) {
$params = ['userid' => $user->id, 'timecreated' => time(), 'timemodified' => time()];
$DB->insert_record('auth_wallet_confirm', $params);
}

// Save wantsurl against user's profile, so we can return them there upon confirmation.
Expand Down Expand Up @@ -159,50 +160,28 @@ public function user_confirm($username, $confirmsecret) {
$user = get_complete_user_data('username', $username);

if (!empty($user)) {
$payconfirm = get_user_preferences('auth_wallet_balanceconfirm', false, $user);
$all = get_config('auth_wallet', 'all');
$payconfirm = auth_wallet_is_confirmed($user);
$all = $this->config->all;

$verified = empty($user->secret) || $user->secret === $confirmsecret;
if (empty($all) && $user->auth != 'wallet') {
return AUTH_CONFIRM_ERROR;
return AUTH_CONFIRM_OK;

} else if ($user->confirmed && !empty($payconfirm)) {
return AUTH_CONFIRM_ALREADY;

} else if ($verified && !empty($payconfirm)) {
$DB->set_field("user", "confirmed", 1, array("id" => $user->id));
return AUTH_CONFIRM_OK;

} else if ($verified && empty($payconfirm)) {

$DB->set_field("user", "confirmed", 1, array("id" => $user->id));

$required = $this->config->required_balance;
$balance = transactions::get_user_balance($user->id);
$method = $this->config->criteria;
$fee = $this->config->required_fee;
$extrafee = $this->config->extra_fee;

// Check if the user balance is sufficient.
if ($method == 'balance' && $balance < $required) {
return AUTH_CONFIRM_FAIL;
} else if ($method == 'balance' && !empty($extrafee) && $balance >= $required) {
$desc = get_string('debitextrafee_desc', 'auth_wallet');
transactions::debit($user->id, $extrafee, '', '', $desc);
} else if ($verified) {
if (!$user->confirmed) {
$DB->set_field("user", "confirmed", 1, ["id" => $user->id]);
$user->confirmed = true;
}

// Check if the method depend on paying a confirm fee and not confirmed yet.
if ($method === 'fee') {
// Check if there already enough balance for paying the fee.
if ($method === 'fee' && $balance >= $fee) {
$desc = get_string('debitfee_desc', 'auth_wallet');
transactions::debit($user->id, $fee, '', '', $desc);
} else {
return AUTH_CONFIRM_FAIL;
}
if (!$payconfirm) {
return AUTH_CONFIRM_FAIL;
}

auth_wallet_set_confirmed($user);

if ($wantsurl = get_user_preferences('auth_wallet_wantsurl', false, $user)) {
// Ensure user gets returned to page they were trying to access before signing up.
$SESSION->wantsurl = $wantsurl;
Expand Down
84 changes: 84 additions & 0 deletions classes/task/nonconfirmed_cleanup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Clean up non-confirmed users..
*
* @package auth_wallet
* @copyright 2023 Mo Farouk <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace auth_wallet\task;

/**
* Clean up non-confirmed users.
*/
class nonconfirmed_cleanup extends \core\task\scheduled_task {

/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('cleanup_nonconfirmed', 'auth_wallet');
}

/**
* Run task for cleaning up users.
*/
public function execute() {
global $DB, $CFG;
require_once("$CFG->dirroot/user/lib.php");
require_once("$CFG->dirroot/auth/wallet/lib.php");

if (empty($CFG->deleteunconfirmed)) {
mtrace('Configuration deleteunconfirmed set to never ...');
return;
}

$intval = $CFG->deleteunconfirmed * 60 * 60;

\core_php_time_limit::raise();
raise_memory_limit(MEMORY_HUGE);

$trace = new \text_progress_trace();
$trace->output('Task started...');

$select = 'confirmed != :confirmed AND timecreated < :timetosearch';
$params = ['confirmed' => 1, 'timetosearch' => time() - $intval];
$records = $DB->get_records_select('auth_wallet_confirm', $select, $params);

$trace->output(count($records) . ' users found to delete.');
foreach ($records as $record) {
// Double check that the user is confirmed before delete.
if ($user = get_complete_user_data('id', $record->userid)) {
if (auth_wallet_is_confirmed($user)) {
$trace->output('User with id ' . $user->id . ' already confirmed and skipped...');
continue;
}
user_delete_user($user);
}
$DB->delete_records('auth_wallet_confirm', ['userid' => $record->userid]);
$trace->output('user with id ' . $record->userid . ' has been deleted...');
}

$trace->output('Finished.');
$trace->finished();
}

}
71 changes: 19 additions & 52 deletions confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
$p = optional_param('p', '', PARAM_ALPHANUM); // Parameter: secret.
$s = optional_param('s', '', PARAM_RAW); // Parameter: username.
$data = optional_param('data', '', PARAM_RAW);
$redirect = optional_param('redirect', '', PARAM_LOCALURL);
$redirect = optional_param('redirect', '', PARAM_URL);
if (empty($redirect)) {
if (isset($SESSION->wantsurl)) {
$redirect = new moodle_url($SESSION->wantsurl);
$redirect = (new moodle_url($SESSION->wantsurl))->out(false);
} else if ($base = get_user_preferences('auth_wallet_wantsurl', false)) {
$redirect = new moodle_url($base);
$redirect = (new moodle_url($base))->out(false);
} else {
$redirect = core_login_get_return_url();
}
Expand Down Expand Up @@ -81,7 +81,9 @@
}

$user = get_complete_user_data('username', $username);

if (!$user || isguestuser($user)) {
throw new \moodle_exception('cannotfinduser', '', '', s($username));
}
$confirmed = $authplugin->user_confirm($username, $usersecret);
if ($confirmed == AUTH_CONFIRM_ALREADY) {

Expand All @@ -97,12 +99,7 @@
exit;

} else if ($confirmed == AUTH_CONFIRM_OK) {

// The user has confirmed successfully, let's log them in.
if (!$user) {
throw new \moodle_exception('cannotfinduser', '', '', s($username));
}

if (empty($user->suspended)) {
complete_user_login($user);

Expand All @@ -129,29 +126,29 @@
echo $OUTPUT->footer();
exit;
} else if ($confirmed == AUTH_CONFIRM_ERROR) {
throw new \moodle_exception('invalidconfirmdata');
debugging('Confirmation Error.', DEBUG_NONE);
redirect(new moodle_url('/login/index.php'), get_string('invalidconfirmdata'), null, 'error');
}
}

if (!empty($s)) {
$user = get_complete_user_data('username', $s);
} else {
global $USER;
$user = get_complete_user_data('id', $USER->id);
$user = fullclone($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)) {
if (!empty($user) && is_object($user) && !isguestuser($user)) {

$payconfirm = auth_wallet_is_confirmed($user);

if (empty($user->suspended)) {

if (!empty($user->confirmed)
|| empty($emailconfirm)
|| empty($user->confirm)
|| $user->auth != 'wallet') {
// Reaching this part of the code means either the user confirmed by email already and wait payment confirmation,
// or confirmation by email is disabled.

// Prepare redirection url.
if (!empty($user->confirmed)) {
Expand All @@ -165,50 +162,20 @@
$DB->set_field('user', 'secret', $user->secret, ['id' => $user->id]);
}
$params['p'] = $user->secret;

$params['redirect'] = $redirect;
$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 = get_complete_user_data('id', $USER->id);
}
$user = complete_user_login($user);

\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);
$confirmmethod = get_config('auth_wallet', 'criteria');
$required = get_config('auth_wallet', 'required_balance');
$fee = get_config('auth_wallet', 'required_fee');
$extrafee = get_config('auth_wallet', 'extra_fee');

if ($confirmmethod === 'balance' && $balance >= $required) {
if (!empty($extrafee)) {
if ($balance >= $extrafee) {
if (empty($payconfirm)) {
$transactions->debit($user->id, $extrafee);
}
} else {
throw new moodle_exception('insufficientbalance');
}
}
auth_wallet_set_confirmed($user);
redirect($url);

} else if ($confirmmethod === 'fee' && $balance >= $fee) {
if (empty($payconfirm)) {
$transactions->debit($user->id, $fee, 'New user fee');
}
auth_wallet_set_confirmed($user);
if (!empty($payconfirm)) {
redirect($url);
} else {
// Display the payment page.
Expand All @@ -225,6 +192,7 @@
'extrafee' => !empty($extrafee) ? get_string('extrafeerequired', 'auth_wallet', $extrafee) : '',
];

$confirmmethod = get_config('auth_wallet', 'criteria');
if ($confirmmethod === 'balance') {
echo get_string('payment_required', 'auth_wallet', $a);
echo enrol_wallet_display_topup_options();
Expand All @@ -243,8 +211,7 @@
}
}
}
redirect($redirect);

} else {
throw new \moodle_exception("errorwhenconfirming");
}

// Not recognized user, suspended user or not confirmed by email yet.
redirect(new moodle_url('/login/index.php'), get_string('errorwhenconfirming'), null, 'error');
2 changes: 1 addition & 1 deletion db/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
[
'classname' => '\auth_wallet\task\nonconfirmed_cleanup',
'blocking' => 0,
'minute' => '*',
'minute' => '0',
'hour' => '*/4',
'day' => '*',
'month' => '*',
Expand Down
1 change: 1 addition & 0 deletions lang/en/auth_wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@
$string['settingerror'] = 'Configuration error, please contact support.';
$string['usersconfirmed'] = '{$a} user(s) has been confirmed.';
$string['wallet:manualconfirm'] = 'Manually confirm wallet signup users';
$string['cleanup_nonconfirmed'] = 'Cleanup non-confirmed users by wallet balance';
Loading

0 comments on commit d34ddf0

Please sign in to comment.