Skip to content

Commit 9af6dd0

Browse files
committed
Added User activation confirmation email - Refs BT#13479
1 parent 6d80ec6 commit 9af6dd0

File tree

8 files changed

+152
-0
lines changed

8 files changed

+152
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/* For licensing terms, see /license.txt */
3+
4+
namespace Application\Migrations\Schema\V111;
5+
6+
use Application\Migrations\AbstractMigrationChamilo;
7+
use Doctrine\DBAL\Schema\Schema;
8+
9+
/**
10+
* Class Version20171002154600
11+
*
12+
* Added a new option in registration settings called "confirmation"
13+
* This option prevents the new user to login in the platform if your account is not
14+
* confirmed via email.
15+
* @package Application\Migrations\Schema\V111
16+
*/
17+
class Version20171002154600 extends AbstractMigrationChamilo
18+
{
19+
/**
20+
* @param Schema $schema
21+
*/
22+
public function up(Schema $schema)
23+
{
24+
$this->addSql("INSERT INTO settings_options (variable, value, display_text) VALUES ('allow_registration', 'confirmation', 'MailConfirmation')");
25+
}
26+
27+
/**
28+
* @param Schema $schema
29+
*/
30+
public function down(Schema $schema)
31+
{
32+
$this->addSql("DELETE settings_options WHERE variable='allow_registration' AND value='confirmation' AND display_text='MailConfirmation'");
33+
}
34+
}

main/auth/inscription.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,26 @@
785785
echo $content;
786786
Display::display_footer();
787787
exit;
788+
} else if (api_get_setting('allow_registration') === 'confirmation') {
789+
$TABLE_USER = Database::get_main_table(TABLE_MAIN_USER);
790+
// 1. set account inactive
791+
$sql = "UPDATE $TABLE_USER SET active='0' WHERE user_id = ".$user_id;
792+
Database::query($sql);
793+
794+
// 2. Send mail to the user
795+
/** @var \Chamilo\UserBundle\Entity\User $thisUser */
796+
$thisUser = Database::getManager()->getRepository('ChamiloUserBundle:User')->find($user_id);
797+
798+
UserManager::sendUserConfirmationMail($thisUser);
799+
800+
// 3. exit the page
801+
unset($user_id);
802+
803+
Display::display_header(get_lang('ConfirmationForNewAccount', null, $values['language']));
804+
echo Display::page_header(get_lang('YouNeedToConfirmYourAccountViaMailToAccessPlatform', null, $values['language']));
805+
echo $content;
806+
Display::display_footer();
807+
exit;
788808
}
789809
}
790810
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/* For license terms, see /license.txt */
3+
4+
require_once __DIR__.'/../inc/global.inc.php';
5+
6+
// Build the form
7+
$form = new FormValidator('resend');
8+
$form->addElement('header', get_lang('ReSendConfirmationMail'));
9+
$form->addText('user', get_lang('UserName'), true);
10+
$form->addButtonSend(get_lang('Send'));
11+
12+
if ($form->validate()) {
13+
$values = $form->exportValues();
14+
15+
/** @var \Chamilo\UserBundle\Entity\User $thisUser */
16+
$thisUser = Database::getManager()->getRepository('ChamiloUserBundle:User')->findBy(['username' => $values['user']]);
17+
18+
UserManager::sendUserConfirmationMail($thisUser);
19+
Display::addFlash(Display::return_message(get_lang('EmailSend')));
20+
header('Location: '.api_get_path(WEB_PATH));
21+
exit;
22+
}
23+
24+
$tpl = new Template(null);
25+
$tpl->assign('form', $form->toHtml());
26+
$content = $tpl->get_template('auth/resend_confirmation_mail.tpl');
27+
$tpl->assign('content', $tpl->fetch($content));
28+
$tpl->display_one_col_template();
29+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/* For license terms, see /license.txt */
3+
4+
require_once __DIR__.'/../inc/global.inc.php';
5+
6+
$token = isset($_GET['token']) ? $_GET['token'] : '';
7+
8+
if (!ctype_alnum($token)) {
9+
$token = '';
10+
}
11+
12+
/** @var \Chamilo\UserBundle\Entity\User $user */
13+
$user = UserManager::getManager()->findUserByConfirmationToken($token);
14+
15+
if ($user) {
16+
17+
$user->setActive(1); // Setted 1 to active the user
18+
$user->setConfirmationToken(null);
19+
20+
Database::getManager()->persist($user);
21+
Database::getManager()->flush();
22+
23+
Display::addFlash(Display::return_message(get_lang('UserConfirmedNowYouCanLogInThePlatform'), 'success'));
24+
header('Location: '.api_get_path(WEB_PATH));
25+
exit;
26+
} else {
27+
Display::addFlash(
28+
Display::return_message(get_lang('LinkExpired'))
29+
);
30+
header('Location: '.api_get_path(WEB_PATH));
31+
exit;
32+
}

main/inc/lib/template.lib.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,12 @@ public function handleLoginFailed()
13891389
break;
13901390
case 'account_inactive':
13911391
$message = get_lang('AccountInactive');
1392+
1393+
if (api_get_setting('allow_registration') === 'confirmation') {
1394+
$message = sprintf(
1395+
get_lang('YourAccountIsInactiveBecauseYouDoesntConfirmItCheckYourMailAndFollowTheInstructionsOrClickHereToReSendEmail'),
1396+
Display::url(get_lang('ReSendConfirmationMail'), api_get_path(WEB_PATH) . 'main/auth/resend_confirmation_mail.php'));
1397+
}
13921398
break;
13931399
case 'user_password_incorrect':
13941400
$message = get_lang('InvalidId');

main/inc/lib/usermanager.lib.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5681,4 +5681,33 @@ public static function isTeacherOfStudent($teacherId, $studentId)
56815681

56825682
return false;
56835683
}
5684+
5685+
/**
5686+
* Send user confirmation mail
5687+
*
5688+
* @param User $user
5689+
*/
5690+
public static function sendUserConfirmationMail(User $user)
5691+
{
5692+
$uniqueId = api_get_unique_id();
5693+
$user->setConfirmationToken($uniqueId);
5694+
5695+
Database::getManager()->persist($user);
5696+
Database::getManager()->flush();
5697+
5698+
$url = api_get_path(WEB_CODE_PATH).'auth/user_mail_confirmation.php?token='.$uniqueId;
5699+
$mailSubject = get_lang('InscriptionConfirmation');
5700+
$mailBody = sprintf(
5701+
get_lang('ToCompleteYourPlatformInscriptionYouNeedToConfirmYourAccountClickingTheFollowingLink'),
5702+
$url
5703+
);
5704+
5705+
api_mail_html(
5706+
$user->getCompleteName(),
5707+
$user->getEmail(),
5708+
$mailSubject,
5709+
$mailBody
5710+
);
5711+
Display::addFlash(Display::return_message(get_lang('CheckYourEmailAndFollowInstructions')));
5712+
}
56845713
}

main/install/data.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ VALUES
336336
('allow_registration','true','Yes'),
337337
('allow_registration','false','No'),
338338
('allow_registration','approval','AfterApproval'),
339+
('allow_registration','confirmation','MailConfirmation'),
339340
('allow_registration_as_teacher','true','Yes'),
340341
('allow_registration_as_teacher','false','No'),
341342
('allow_lostpassword','true','Yes'),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{form}}

0 commit comments

Comments
 (0)